From 2f095276775144b7b436d1e90cc91bd2de5a2506 Mon Sep 17 00:00:00 2001 From: William Perron Date: Wed, 31 Aug 2022 21:25:30 +0000 Subject: [PATCH] =?UTF-8?q?add=20=E2=9C=A8=20tests=20=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 8 +++++++ go.sum | 15 +++++++++++++ store.go | 25 +++++++++++++++++---- store_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 store_test.go diff --git a/go.mod b/go.mod index c73c025..672bde7 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,11 @@ module go.wperron.io/themis go 1.19 require github.com/mattn/go-sqlite3 v1.14.15 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.4.0 // indirect + github.com/stretchr/testify v1.8.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index 252cb10..7713f5e 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,17 @@ +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= github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +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= diff --git a/store.go b/store.go index 885de2d..a55ff4a 100644 --- a/store.go +++ b/store.go @@ -39,10 +39,6 @@ var claimTypeToColumn = map[ClaimType]string{ CLAIM_TYPE_TRADE: "trade_node", } -type Store struct { - db *sql.DB -} - type Claim struct { ID int Player string @@ -62,6 +58,10 @@ func (ec ErrConflict) Error() string { return fmt.Sprintf("found conflicting provinces: %s", strings.Join(ec.Conflicts, ", ")) } +type Store struct { + db *sql.DB +} + func NewStore(conn string) (*Store, error) { db, err := sql.Open("sqlite3", conn) if err != nil { @@ -114,6 +114,23 @@ func (s *Store) Claim(ctx context.Context, player, province string, claimType Cl return ErrConflict{Conflicts: conflicts} } + // check that provided name matches the claim type + stmt, err = s.db.PrepareContext(ctx, fmt.Sprintf(`SELECT COUNT(1) FROM provinces WHERE provinces.%s = ?`, claimTypeToColumn[claimType])) + if err != nil { + return fmt.Errorf("failed to prepare count query: %w", err) + } + + row := stmt.QueryRowContext(ctx, province) + var count int + err = row.Scan(&count) + if err != nil { + return fmt.Errorf("failed to scan: %w", err) + } + + if count == 0 { + return fmt.Errorf("found no provinces for %s named %s", claimType, province) + } + stmt, err = s.db.PrepareContext(ctx, "INSERT INTO claims (player, claim_type, val) VALUES (?, ?, ?)") if err != nil { return fmt.Errorf("failed to prepare claim query: %w", err) diff --git a/store_test.go b/store_test.go new file mode 100644 index 0000000..913086e --- /dev/null +++ b/store_test.go @@ -0,0 +1,61 @@ +package themis + +import ( + "context" + _ "embed" + "testing" + + _ "github.com/mattn/go-sqlite3" + "github.com/stretchr/testify/assert" +) + +func TestStore_Claim(t *testing.T) { + store, err := NewStore("file::memory:?cache=shared") + assert.NoError(t, err) + + type args struct { + player string + province string + claimType ClaimType + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "simple", + args: args{ + player: "foo", + province: "Italy", + claimType: CLAIM_TYPE_REGION, + }, + wantErr: false, + }, + { + name: "invalid name", + args: args{ + player: "foo", + province: "Italy", + claimType: CLAIM_TYPE_TRADE, // Italy is a Region you silly goose + }, + wantErr: true, + }, + { + name: "conflicts", + args: args{ + player: "bar", + province: "Genoa", + claimType: CLAIM_TYPE_TRADE, + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := store.Claim(context.TODO(), tt.args.player, tt.args.province, tt.args.claimType); (err != nil) != tt.wantErr { + t.Errorf("Store.Claim() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}