-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
9d292bd
commit 67adb0f
Showing
32 changed files
with
776 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: Run unit tests | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
|
||
test: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
services: | ||
postgres: | ||
image: postgres | ||
env: | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_DB: moviedb | ||
options: >- | ||
--health-cmd pg_isready | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
ports: | ||
- 5432:5432 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '^1.21' | ||
|
||
- name: Install golang-migrate | ||
run: | | ||
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.16.2/migrate.linux-amd64.tar.gz | tar xvz | ||
sudo mv migrate /usr/bin | ||
which migrate | ||
- name: Run migrations | ||
run: make migrate-up | ||
|
||
- name: Test | ||
run: make run-test |
File renamed without changes.
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,24 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"main/utils" | ||
"os" | ||
"testing" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
) | ||
|
||
var testStore Store | ||
|
||
func TestMain(m *testing.M) { | ||
cfg := utils.LoadConfig("../../.env") | ||
connPool, err := pgxpool.New(context.Background(), cfg.DatabaseURL) | ||
if err != nil { | ||
log.Fatal("cannot connect to db:", err) | ||
} | ||
|
||
testStore = NewStore(connPool) | ||
os.Exit(m.Run()) | ||
} |
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,45 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"main/utils" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func createRandomMovie(t *testing.T) *Movie { | ||
arg := &CreateMovieParams{ | ||
ID: utils.RandomString(8), | ||
Title: utils.RandomString(8), | ||
Description: utils.RandomString(16), | ||
Director: utils.RandomString(8), | ||
} | ||
|
||
movie, err := testStore.CreateMovie(context.Background(), arg) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, movie) | ||
|
||
require.Equal(t, arg.ID, movie.ID) | ||
require.Equal(t, arg.Title, movie.Title) | ||
require.Equal(t, arg.Description, movie.Description) | ||
require.Equal(t, arg.Director, movie.Director) | ||
|
||
return movie | ||
} | ||
|
||
func TestCreateMovie(t *testing.T) { | ||
createRandomMovie(t) | ||
} | ||
|
||
func TestGetMovie(t *testing.T) { | ||
movie1 := createRandomMovie(t) | ||
movie2, err := testStore.GetMovie(context.Background(), movie1.ID) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, movie2) | ||
|
||
require.Equal(t, movie1.ID, movie2.ID) | ||
require.Equal(t, movie1.Title, movie2.Title) | ||
require.Equal(t, movie1.Description, movie2.Description) | ||
require.Equal(t, movie1.Director, movie2.Director) | ||
} |
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,60 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"main/utils" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func createRandomRating(t *testing.T, movieId, recordType string) *Rating { | ||
arg := &CreateRatingParams{ | ||
MovieID: movieId, | ||
RecordType: recordType, | ||
UserID: utils.RandomString(8), | ||
Value: int32(utils.RandomInt(0, 10)), | ||
} | ||
|
||
rating, err := testStore.CreateRating(context.Background(), arg) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, rating) | ||
|
||
require.Equal(t, arg.MovieID, rating.MovieID) | ||
require.Equal(t, arg.RecordType, rating.RecordType) | ||
require.Equal(t, arg.UserID, rating.UserID) | ||
require.Equal(t, arg.Value, rating.Value) | ||
|
||
return rating | ||
} | ||
|
||
func TestCreateRating(t *testing.T) { | ||
movieId := utils.RandomString(8) | ||
recordType := utils.RandomString(8) | ||
createRandomRating(t, movieId, recordType) | ||
} | ||
|
||
func TestListRatings(t *testing.T) { | ||
movieId := utils.RandomString(8) | ||
recordType := utils.RandomString(8) | ||
|
||
var lastAccount *Rating | ||
for i := 0; i < 10; i++ { | ||
lastAccount = createRandomRating(t, movieId, recordType) | ||
} | ||
|
||
arg := &ListRatingsParams{ | ||
MovieID: movieId, | ||
RecordType: recordType, | ||
} | ||
|
||
ratings, err := testStore.ListRatings(context.Background(), arg) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, ratings) | ||
|
||
for _, rating := range ratings { | ||
require.NotEmpty(t, rating) | ||
require.Equal(t, lastAccount.MovieID, rating.MovieID) | ||
require.Equal(t, lastAccount.RecordType, rating.RecordType) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.