From 3821832b0f83dbe62685b192eb84d36d71eef273 Mon Sep 17 00:00:00 2001 From: Vinayaks439 Date: Sun, 3 Dec 2023 20:32:09 +0530 Subject: [PATCH] Added tests for all funcs in accounts curd --- db/migration/000001_init_schema.down.sql | 6 +- db/sqlc/accounts.sql_test.go | 82 +++++++++++++++++++++++- db/sqlc/{full_test.go => main_test.go} | 0 go.mod | 10 ++- go.sum | 13 ++++ 5 files changed, 104 insertions(+), 7 deletions(-) rename db/sqlc/{full_test.go => main_test.go} (100%) diff --git a/db/migration/000001_init_schema.down.sql b/db/migration/000001_init_schema.down.sql index 0cc6dd2..05ac4ef 100644 --- a/db/migration/000001_init_schema.down.sql +++ b/db/migration/000001_init_schema.down.sql @@ -1,3 +1,3 @@ -DROP TABLE IF EXISTS accounts; -DROP TABLE IF EXISTS entries; -DROP TABLE IF EXISTS transfers; \ No newline at end of file +DROP TABLE IF EXISTS accounts CASCADE; +DROP TABLE IF EXISTS entries CASCADE; +DROP TABLE IF EXISTS transfers CASCADE; \ No newline at end of file diff --git a/db/sqlc/accounts.sql_test.go b/db/sqlc/accounts.sql_test.go index f244fcc..1f3d4dd 100644 --- a/db/sqlc/accounts.sql_test.go +++ b/db/sqlc/accounts.sql_test.go @@ -1,7 +1,83 @@ package db -import "testing" +import ( + "context" + "github.com/stretchr/testify/require" + "testing" +) -func TestCreateAccount(T *testing.T) { - +func TestCreateAccount(t *testing.T) { + args := CreateAccountParams{ + Owner: "Test", + Balance: 100, + Currency: "USD", + } + account, err := testQueries.CreateAccount(context.Background(), args) + require.NoError(t, err) + require.NotEmpty(t, account) + require.Equal(t, args.Owner, account.Owner) + require.Equal(t, args.Balance, account.Balance) + require.Equal(t, args.Currency, account.Currency) + require.NotZero(t, account.ID) + require.NotZero(t, account.CreatedAt) +} + +func TestGetAccount(t *testing.T) { + getAccount, err := testQueries.GetAccount(context.Background(), 1) + require.NoError(t, err) + require.NotEmpty(t, getAccount) + require.Equal(t, getAccount.ID, int64(1)) + require.Equal(t, getAccount.Owner, "Test") + require.Equal(t, getAccount.Balance, int64(100)) + require.Equal(t, getAccount.Currency, "USD") + require.NotZero(t, getAccount.ID) + require.NotZero(t, getAccount.CreatedAt) +} + +func TestListAccounts(t *testing.T) { + arg := ListAccountsParams{ + Limit: 5, + Offset: 0, + } + + accounts, err := testQueries.ListAccounts(context.Background(), arg) + require.NoError(t, err) + require.NotEmpty(t, accounts) + + for _, account := range accounts { + require.NotEmpty(t, account) + require.Equal(t, "Test", account.Owner) + } +} + +func TestUpdateAccount(t *testing.T) { + args := UpdateAccountParams{ + ID: 1, + Balance: 200, + } + account, err := testQueries.UpdateAccount(context.Background(), args) + require.NoError(t, err) + require.NotEmpty(t, account) + require.Equal(t, args.ID, account.ID) + require.Equal(t, args.Balance, account.Balance) + require.NotZero(t, account.ID) + require.NotZero(t, account.CreatedAt) +} + +func TestDeleteAccount(t *testing.T) { + account, err := testQueries.CreateAccount(context.Background(), CreateAccountParams{ + Owner: "Test_DELETE", + Balance: 100, + Currency: "USD", + }) + getAccount, err := testQueries.GetAccount(context.Background(), account.ID) + err = testQueries.DeleteAccount(context.Background(), getAccount.ID) + require.NoError(t, err) + require.NotEmpty(t, account) + require.NotEmpty(t, getAccount) + require.Equal(t, getAccount.ID, account.ID) + require.Equal(t, getAccount.Owner, account.Owner) + require.Equal(t, getAccount.Balance, account.Balance) + require.NotZero(t, account.ID) + require.NotZero(t, account.CreatedAt) } diff --git a/db/sqlc/full_test.go b/db/sqlc/main_test.go similarity index 100% rename from db/sqlc/full_test.go rename to db/sqlc/main_test.go diff --git a/go.mod b/go.mod index 23f7b96..42a525f 100644 --- a/go.mod +++ b/go.mod @@ -2,13 +2,21 @@ module github.com/Vinayaks439/golang-backend go 1.21.1 -require github.com/jackc/pgx/v5 v5.5.0 +require ( + github.com/jackc/pgx/v5 v5.5.0 + github.com/stretchr/testify v1.8.1 +) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect golang.org/x/crypto v0.9.0 // indirect golang.org/x/sync v0.1.0 // indirect golang.org/x/text v0.9.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 1f26880..4078dd8 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -9,11 +10,21 @@ github.com/jackc/pgx/v5 v5.5.0 h1:NxstgwndsTRy7eq9/kqYc/BZh5w2hHJV86wjvO+1xPw= github.com/jackc/pgx/v5 v5.5.0/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= @@ -23,6 +34,8 @@ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=