-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
246 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gorm.io/gorm" | ||
|
||
"jcourse_go/dal" | ||
"jcourse_go/model/model" | ||
"jcourse_go/model/po" | ||
) | ||
|
||
func TestReviewQuery_GetReview(t *testing.T) { | ||
ctx := context.Background() | ||
db := dal.GetDBClient() | ||
query := NewReviewQuery(db) | ||
|
||
reviews, err := query.GetReview(ctx, WithID(1)) | ||
assert.Nil(t, err) | ||
assert.Len(t, reviews, 1) | ||
assert.Equal(t, int64(5), reviews[0].Rating) | ||
} | ||
|
||
func TestReviewQuery_GetReviewCount(t *testing.T) { | ||
ctx := context.Background() | ||
db := dal.GetDBClient() | ||
query := NewReviewQuery(db) | ||
|
||
count, err := query.GetReviewCount(ctx) | ||
assert.Nil(t, err) | ||
assert.Equal(t, int64(2), count) | ||
} | ||
|
||
func TestReviewQuery_CreateReview(t *testing.T) { | ||
ctx := context.Background() | ||
db := dal.GetDBClient() | ||
query := NewReviewQuery(db) | ||
ratingQuery := NewRatingQuery(db) | ||
|
||
t.Run("normal", func(t *testing.T) { | ||
reviewPO := po.ReviewPO{ | ||
CourseID: 2, | ||
UserID: 2, | ||
Comment: "", | ||
Rating: 5, | ||
Semester: "", | ||
IsAnonymous: false, | ||
} | ||
id, err := query.CreateReview(ctx, reviewPO) | ||
assert.Nil(t, err) | ||
assert.NotZero(t, id) | ||
|
||
rating, err := ratingQuery.GetRatingInfo(ctx, model.RelatedTypeCourse, 2) | ||
assert.Nil(t, err) | ||
assert.Equal(t, int64(2), rating.Count) | ||
assert.Equal(t, float64(5), rating.Average) | ||
}) | ||
t.Run("duplicated review", func(t *testing.T) { | ||
reviewPO := po.ReviewPO{ | ||
CourseID: 1, | ||
UserID: 1, | ||
Comment: "", | ||
Rating: 0, | ||
Semester: "", | ||
IsAnonymous: false, | ||
} | ||
id, err := query.CreateReview(ctx, reviewPO) | ||
assert.NotNil(t, err) | ||
assert.Zero(t, id) | ||
|
||
rating, err := ratingQuery.GetRatingInfo(ctx, model.RelatedTypeCourse, 1) | ||
assert.Nil(t, err) | ||
assert.Equal(t, int64(1), rating.Count) | ||
}) | ||
} | ||
|
||
func TestReviewQuery_UpdateReview(t *testing.T) { | ||
ctx := context.Background() | ||
db := dal.GetDBClient() | ||
query := NewReviewQuery(db) | ||
ratingQuery := NewRatingQuery(db) | ||
|
||
t.Run("duplicate", func(t *testing.T) { | ||
reviewPO := po.ReviewPO{ | ||
Model: gorm.Model{ID: 1}, | ||
CourseID: 2, | ||
UserID: 1, | ||
Comment: "", | ||
Rating: 1, | ||
Semester: "", | ||
IsAnonymous: false, | ||
} | ||
|
||
err := query.UpdateReview(ctx, reviewPO) | ||
assert.NotNil(t, err) | ||
|
||
// no change | ||
reviews, err := query.GetReview(ctx, WithID(1)) | ||
assert.Nil(t, err) | ||
assert.Equal(t, int64(5), reviews[0].Rating) | ||
|
||
info, err := ratingQuery.GetRatingInfo(ctx, model.RelatedTypeCourse, 1) | ||
if err != nil { | ||
return | ||
} | ||
assert.Equal(t, int64(5), info.RatingDist[0].Rating) | ||
}) | ||
|
||
t.Run("normal", func(t *testing.T) { | ||
reviewPO := po.ReviewPO{ | ||
Model: gorm.Model{ID: 1}, | ||
CourseID: 1, | ||
UserID: 1, | ||
Comment: "", | ||
Rating: 1, | ||
Semester: "", | ||
IsAnonymous: false, | ||
} | ||
|
||
err := query.UpdateReview(ctx, reviewPO) | ||
assert.Nil(t, err) | ||
reviews, err := query.GetReview(ctx, WithID(1)) | ||
assert.Nil(t, err) | ||
assert.Equal(t, int64(1), reviews[0].Rating) | ||
|
||
info, err := ratingQuery.GetRatingInfo(ctx, model.RelatedTypeCourse, 1) | ||
if err != nil { | ||
return | ||
} | ||
assert.Equal(t, int64(1), info.RatingDist[0].Rating) | ||
}) | ||
|
||
} | ||
|
||
func TestReviewQuery_DeleteReview(t *testing.T) { | ||
ctx := context.Background() | ||
db := dal.GetDBClient() | ||
query := NewReviewQuery(db) | ||
ratingQuery := NewRatingQuery(db) | ||
|
||
t.Run("normal", func(t *testing.T) { | ||
err := query.DeleteReview(ctx, WithID(1)) | ||
assert.Nil(t, err) | ||
reviews, err := query.GetReview(ctx, WithID(1)) | ||
assert.Nil(t, err) | ||
assert.Len(t, reviews, 0) | ||
|
||
info, err := ratingQuery.GetRatingInfo(ctx, model.RelatedTypeCourse, 1) | ||
if err != nil { | ||
return | ||
} | ||
assert.Len(t, info.RatingDist, 0) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
|
||
"gorm.io/gorm" | ||
|
||
"jcourse_go/model/model" | ||
"jcourse_go/model/po" | ||
) | ||
|
||
|
@@ -177,6 +178,68 @@ func createTrainingPlan(db *gorm.DB) error { | |
return nil | ||
} | ||
|
||
func createTestUser(db *gorm.DB) error { | ||
users := []po.UserPO{ | ||
{ | ||
Model: gorm.Model{ID: 1}, | ||
Username: "test1", | ||
Email: "[email protected]", | ||
}, | ||
{ | ||
Model: gorm.Model{ID: 2}, | ||
Username: "test2", | ||
Email: "[email protected]", | ||
}, | ||
{ | ||
Model: gorm.Model{ID: 3}, | ||
Username: "test3", | ||
Email: "[email protected]", | ||
}, | ||
} | ||
err := db.Create(&users).Error | ||
return err | ||
} | ||
|
||
func createTestReview(db *gorm.DB) error { | ||
reviews := []po.ReviewPO{ | ||
{ | ||
Model: gorm.Model{ID: 1}, | ||
CourseID: 1, | ||
UserID: 1, | ||
Comment: "test review", | ||
Rating: 5, | ||
}, | ||
{ | ||
Model: gorm.Model{ID: 2}, | ||
CourseID: 2, | ||
UserID: 1, | ||
Comment: "test review", | ||
Rating: 4, | ||
}, | ||
} | ||
err := db.Create(&reviews).Error | ||
if err != nil { | ||
return err | ||
} | ||
ratings := []po.RatingPO{ | ||
{ | ||
Rating: 5, | ||
RelatedID: 1, | ||
UserID: 1, | ||
RelatedType: model.RelatedTypeCourse, | ||
}, | ||
{ | ||
Rating: 5, | ||
RelatedID: 2, | ||
UserID: 1, | ||
RelatedType: model.RelatedTypeCourse, | ||
}, | ||
} | ||
|
||
err = db.Create(&ratings).Error | ||
return err | ||
} | ||
|
||
func CreateTestEnv(ctx context.Context, db *gorm.DB) error { | ||
db = db.WithContext(ctx) | ||
createFunc := []func(db *gorm.DB) error{ | ||
|
@@ -185,11 +248,13 @@ func CreateTestEnv(ctx context.Context, db *gorm.DB) error { | |
createTestCourse, | ||
createCourseCategories, | ||
createTrainingPlan, | ||
createTestUser, | ||
createTestReview, | ||
} | ||
for _, fn := range createFunc { | ||
err := fn(db) | ||
if err != nil { | ||
return err | ||
panic(err) | ||
} | ||
} | ||
return nil | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters