added tdd for comment repo, and comment repo

This commit is contained in:
rawleyIfowler
2022-03-04 17:34:43 -06:00
parent 4c3501cc97
commit cd7a34be0c
6 changed files with 159 additions and 0 deletions

1
repos/blog_repo.go Normal file
View File

@@ -0,0 +1 @@
package repos

0
repos/blog_repo_test.go Normal file
View File

80
repos/comment_repo.go Normal file
View File

@@ -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
}

View File

@@ -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.")
}
}