From cd7a34be0c524973d32c37415192108ecaa667e9 Mon Sep 17 00:00:00 2001 From: rawleyIfowler Date: Fri, 4 Mar 2022 17:34:43 -0600 Subject: [PATCH] added tdd for comment repo, and comment repo --- controllers/blog_test.go | 1 + controllers/comment_test.go | 1 + repos/blog_repo.go | 1 + repos/blog_repo_test.go | 0 repos/comment_repo.go | 80 +++++++++++++++++++++++++++++++++++++ repos/comment_repo_test.go | 76 +++++++++++++++++++++++++++++++++++ 6 files changed, 159 insertions(+) create mode 100644 controllers/blog_test.go create mode 100644 controllers/comment_test.go create mode 100644 repos/blog_repo.go create mode 100644 repos/blog_repo_test.go create mode 100644 repos/comment_repo.go create mode 100644 repos/comment_repo_test.go diff --git a/controllers/blog_test.go b/controllers/blog_test.go new file mode 100644 index 0000000..2d32936 --- /dev/null +++ b/controllers/blog_test.go @@ -0,0 +1 @@ +package controllers diff --git a/controllers/comment_test.go b/controllers/comment_test.go new file mode 100644 index 0000000..2d32936 --- /dev/null +++ b/controllers/comment_test.go @@ -0,0 +1 @@ +package controllers diff --git a/repos/blog_repo.go b/repos/blog_repo.go new file mode 100644 index 0000000..2b0d3a9 --- /dev/null +++ b/repos/blog_repo.go @@ -0,0 +1 @@ +package repos diff --git a/repos/blog_repo_test.go b/repos/blog_repo_test.go new file mode 100644 index 0000000..e69de29 diff --git a/repos/comment_repo.go b/repos/comment_repo.go new file mode 100644 index 0000000..b493439 --- /dev/null +++ b/repos/comment_repo.go @@ -0,0 +1,80 @@ +package repos + +import ( + "errors" + + "gitlab.com/rawleyifowler/site-rework/models" + "gitlab.com/rawleyifowler/site-rework/utils" + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +type CommentRepo struct { + DB *gorm.DB + Initialized bool +} + +func NewCommentRepo(dsnPath string) (*CommentRepo, error) { + c := CommentRepo{} + err := c.Initialize(dsnPath) + if err != nil { + return &c, err + } + return &c, nil +} + +func (c *CommentRepo) Initialize(path string) error { + dsn := utils.LoadDSN(path) + var err error + c.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) + if err != nil { + return err + } + c.Initialized = true + return nil +} + +func (c *CommentRepo) GetCommentById(id uint) (*models.Comment, error) { + if id == 0 { + return nil, nil + } + var comm models.Comment + c.DB.Where(&models.Comment{Id: id}).First(&comm) + return &comm, nil +} + +func (c *CommentRepo) CreateComment(comm *models.Comment) error { + if comm == nil { + return errors.New("nil comment") + } + err := c.DB.Create(comm).Error + if err != nil { + return err + } + // nil return means no errors, yay! + return nil +} + +func (c *CommentRepo) GetCommentsByAssociatedPost(title string) (*[]models.Comment, error) { + if len(title) < 1 { + return nil, errors.New("empty associated post title") + } + var comms []models.Comment + err := c.DB.Where(&models.Comment{AssociatedPost: title}).Scan(&comms).Error + if err != nil { + return nil, err + } + return &comms, nil +} + +func (c *CommentRepo) GetCommentsByAuthor(auth string) (*[]models.Comment, error) { + if len(auth) < 1 { + return nil, errors.New("empty author name") + } + var comms []models.Comment + err := c.DB.Where(&models.Comment{Author: auth}).Scan(&comms).Error + if err != nil { + return nil, err + } + return &comms, nil +} diff --git a/repos/comment_repo_test.go b/repos/comment_repo_test.go new file mode 100644 index 0000000..07752c1 --- /dev/null +++ b/repos/comment_repo_test.go @@ -0,0 +1,76 @@ +package repos + +import ( + "testing" + + "gitlab.com/rawleyifowler/site-rework/models" +) + +var cr *CommentRepo + +func TestInitialize(t *testing.T) { + var err error + cr, err = NewCommentRepo("dsn") + if err != nil { + + } +} + +func TestGetCommentById(t *testing.T) { + const ID uint = 1 + const AUTHOR string = "Whoops" + const CONTENT string = "Uhhhhhhhh that wasn't supposed to happen..." + // This comment will exist forever on the database, it's part of the test blog post. + x := models.Comment{Id: ID, Author: AUTHOR, Content: CONTENT} + y, err := cr.GetCommentById(ID) + if err != nil { + t.Fatalf("Expected: Comment with id: %d should be found without errors.", ID) + } + if x.Id != y.Id || + x.Author != y.Author || + x.Content != y.Content { + t.Fatalf("Expected(left expected, right result):\nid -> %d == %d\nauthor -> %s == %s\ncontent -> %s == %s", + ID, y.Id, AUTHOR, y.Author, CONTENT, y.Content) + } + _, err := cr.GetCommentById(0) + if err == nil { + t.Fatalf("Expected: Values of less than or equal to 0 are considered illegal values for comment id") + } +} + +func TestGetCommentsByAssociatedPost(t *testing.T) { + _, err := cr.GetCommentsByAssociatedPost("") + if err == nil { + t.Fatalf("Expected: Empty values are considered illegal for associated post") + } + // Wild associated post should return empty array + post, err := cr.GetCommentsByAssociatedPost("kjdoJDOWIJWKIJALSKAJSAKIJDOIAj") + if len(*post) > 0 { + t.Fatalf("Expected: Should return empty array") + } + if err != nil { + t.Fatalf("Expected: Non existant post should not throw error, just empty array") + } +} + +func TestGetCommentsByAuthor(t *testing.T) { + _, err := cr.GetCommentsByAuthor("") + if err == nil { + t.Fatalf("Expected: An empty author value should throw an error") + } +} + +func TestCreateComment(t *testing.T) { + err := cr.CreateComment(nil) + if err == nil { + t.Fatalf("Expected: Nil values should result in errors when trying to create comments") + } + err = cr.CreateComment(&models.Comment{}) + if err == nil { + t.Fatalf("Expected: Empty or incomplete models should result in errors.") + } + err = cr.CreateComment(&models.Comment{Author: "Chuck"}) + if err == nil { + t.Fatalf("Expected: Empty or incomplete models should result in errors.") + } +}