|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/nix-united/golang-echo-boilerplate/internal/models" |
| 7 | + "github.com/nix-united/golang-echo-boilerplate/internal/repositories" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestPostRepository(t *testing.T) { |
| 14 | + userRepository := repositories.NewUserRepository(gormDB) |
| 15 | + postRepository := repositories.NewPostRepository(gormDB) |
| 16 | + |
| 17 | + userToCreate := &models.User{ |
| 18 | + Email: "example_user_email@email.com", |
| 19 | + Name: "some-user-with-posts", |
| 20 | + Password: "some-user-with-posts-password", |
| 21 | + } |
| 22 | + |
| 23 | + err := gormDB.Create(userToCreate).Error |
| 24 | + require.NoError(t, err) |
| 25 | + |
| 26 | + user := new(models.User) |
| 27 | + userRepository.GetUserByEmail(user, "example_user_email@email.com") |
| 28 | + require.NotNil(t, user) |
| 29 | + |
| 30 | + postToCreate := &models.Post{ |
| 31 | + Title: "Post title", |
| 32 | + Content: "Post content", |
| 33 | + UserID: user.ID, |
| 34 | + } |
| 35 | + |
| 36 | + postRepository.Create(postToCreate) |
| 37 | + |
| 38 | + gotPosts := new([]models.Post) |
| 39 | + postRepository.GetPosts(gotPosts) |
| 40 | + require.NotNil(t, gotPosts) |
| 41 | + require.Len(t, (*gotPosts), 1) |
| 42 | + |
| 43 | + gotPost := (*gotPosts)[0] |
| 44 | + wantPost := postToCreate |
| 45 | + wantPost.User = *user |
| 46 | + wantPost.CreatedAt = gotPost.CreatedAt |
| 47 | + wantPost.UpdatedAt = gotPost.UpdatedAt |
| 48 | + |
| 49 | + assert.Equal(t, *postToCreate, (*gotPosts)[0]) |
| 50 | +} |
0 commit comments