From 61d189661a5e672b08612598a67f4ef567e69e49 Mon Sep 17 00:00:00 2001 From: Derek Downey Date: Tue, 21 Feb 2023 15:09:24 +0000 Subject: [PATCH] Updating most services integration tests that use DEFAULT syntax. --- src/golang/item-service/main_test.go | 32 +++++++-------------- src/golang/matchmaking-service/main_test.go | 21 +++++++------- src/golang/profile-service/main_test.go | 1 + src/golang/tradepost-service/main_test.go | 26 +++++------------ 4 files changed, 28 insertions(+), 52 deletions(-) diff --git a/src/golang/item-service/main_test.go b/src/golang/item-service/main_test.go index d9c4e43..9587b61 100644 --- a/src/golang/item-service/main_test.go +++ b/src/golang/item-service/main_test.go @@ -28,8 +28,8 @@ import ( instance "cloud.google.com/go/spanner/admin/instance/apiv1" "embed" "fmt" - "github.com/google/uuid" "github.com/cloudspannerecosystem/spanner-gaming-sample/gaming-item-service/models" + "github.com/google/uuid" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" databasepb "google.golang.org/genproto/googleapis/spanner/admin/database/v1" @@ -72,7 +72,7 @@ func teardown(ctx context.Context, emulator *Emulator, service *Service) { func setupSpannerEmulator(ctx context.Context) (*Emulator, error) { req := testcontainers.ContainerRequest{ - Image: "gcr.io/cloud-spanner-emulator/emulator:latest", + Image: "gcr.io/cloud-spanner-emulator/emulator:1.5.0", ExposedPorts: []string{"9010/tcp"}, Networks: []string{ TESTNETWORK, @@ -176,22 +176,10 @@ func setupDatabase(ctx context.Context, ec Emulator) error { // get schema statements from file schema, _ := SCHEMAFILE.ReadFile("test_data/schema.sql") - // Removing NOT NULL constraints for columns we don't care about in item tests - schemaStringFix := strings.Replace(string(schema), "password_hash BYTES(60) NOT NULL,", "password_hash BYTES(60),", 1) - - // TODO: Remove this when the Spanner Emulator supports 'DEFAULT' syntax; NOT NULL removed to avoid errors - // and most of those columns we don't use at the moment - schemaStringFix = strings.Replace(schemaStringFix, "account_balance NUMERIC NOT NULL DEFAULT (0.00),", "account_balance NUMERIC,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "acquire_time TIMESTAMP NOT NULL DEFAULT (CURRENT_TIMESTAMP()),", "acquire_time TIMESTAMP,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "created TIMESTAMP NOT NULL DEFAULT (CURRENT_TIMESTAMP()),", "created TIMESTAMP,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "expires TIMESTAMP NOT NULL DEFAULT (TIMESTAMP_ADD(CURRENT_TIMESTAMP(), interval 24 HOUR)),", "expires TIMESTAMP,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "visible BOOL NOT NULL DEFAULT(true),", "visible BOOL,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "active BOOL NOT NULL DEFAULT (true),", "active BOOL,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "cancelled BOOL NOT NULL DEFAULT (false),", "cancelled BOOL,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "filled BOOL NOT NULL DEFAULT (false),", "filled BOOL,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "expired BOOL NOT NULL DEFAULT (false),", "expired BOOL,", 1) + // Remove trailing semi-colon/newline so Emulator can parse DDL statements + schemaString := strings.TrimSuffix(string(schema), ";\n") - schemaStatements := strings.Split(schemaStringFix, ";") + schemaStatements := strings.Split(schemaString, ";") adminClient, err := database.NewDatabaseAdminClient(ctx) if err != nil { @@ -225,16 +213,16 @@ func loadTestData(ctx context.Context, ec Emulator) error { } defer client.Close() - playerColumns := []string{"playerUUID", "player_name", "email", "account_balance", "current_game"} + playerColumns := []string{"playerUUID", "player_name", "email", "password_hash", "account_balance", "current_game"} gameColumns := []string{"gameUUID", "players", "created"} gameUUID := uuid.NewString() playerUUID := []string{uuid.NewString(), uuid.NewString(), uuid.NewString()} m := []*spanner.Mutation{ spanner.Insert("games", gameColumns, []interface{}{gameUUID, []string{playerUUID[0], playerUUID[1], playerUUID[2]}, time.Now()}), // Adds 3 players to a game - spanner.Insert("players", playerColumns, []interface{}{playerUUID[0], "player1", "player1@email.com", "0.00", gameUUID}), - spanner.Insert("players", playerColumns, []interface{}{playerUUID[1], "player2", "player2@email.com", "0.00", gameUUID}), - spanner.Insert("players", playerColumns, []interface{}{playerUUID[2], "player3", "player3@email.com", "0.00", gameUUID}), + spanner.Insert("players", playerColumns, []interface{}{playerUUID[0], "player1", "player1@email.com", "adsfijapfja3234aipj", "0.00", gameUUID}), + spanner.Insert("players", playerColumns, []interface{}{playerUUID[1], "player2", "player2@email.com", "apoijawernipoav8210", "0.00", gameUUID}), + spanner.Insert("players", playerColumns, []interface{}{playerUUID[2], "player3", "player3@email.com", "9asil23jifa82all3i1", "0.00", gameUUID}), } _, err = client.Apply(ctx, m) if err != nil { @@ -295,7 +283,7 @@ func TestMain(m *testing.M) { net, err := testcontainers.GenericNetwork(ctx, testcontainers.GenericNetworkRequest{ NetworkRequest: testcontainers.NetworkRequest{ Name: TESTNETWORK, - Attachable: true, + Attachable: true, CheckDuplicate: true, }, }) diff --git a/src/golang/matchmaking-service/main_test.go b/src/golang/matchmaking-service/main_test.go index a7c6893..6612390 100644 --- a/src/golang/matchmaking-service/main_test.go +++ b/src/golang/matchmaking-service/main_test.go @@ -68,7 +68,7 @@ func teardown(ctx context.Context, emulator *Emulator, service *Service) { func setupSpannerEmulator(ctx context.Context) (*Emulator, error) { req := testcontainers.ContainerRequest{ - Image: "gcr.io/cloud-spanner-emulator/emulator:latest", + Image: "gcr.io/cloud-spanner-emulator/emulator:1.5.0", ExposedPorts: []string{"9010/tcp"}, Networks: []string{ TESTNETWORK, @@ -172,11 +172,10 @@ func setupDatabase(ctx context.Context, ec Emulator) error { // get schema statements from file schema, _ := SCHEMAFILE.ReadFile("test_data/schema.sql") - // Removing NOT NULL constraints for columns we don't care about in matchmaking tests - schemaStringFix := strings.Replace(string(schema), "password_hash BYTES(60) NOT NULL,", "password_hash BYTES(60),", 1) - schemaStringFix = strings.Replace(schemaStringFix, "account_balance NUMERIC NOT NULL DEFAULT (0.00),", "account_balance NUMERIC,", 1) + // Remove trailing semi-colon/newline so Emulator can parse DDL statements + schemaString := strings.TrimSuffix(string(schema), ";\n") - schemaStatements := strings.Split(schemaStringFix, ";") + schemaStatements := strings.Split(schemaString, ";") adminClient, err := database.NewDatabaseAdminClient(ctx) if err != nil { @@ -210,16 +209,16 @@ func loadTestData(ctx context.Context, ec Emulator) error { } defer client.Close() - playerColumns := []string{"playerUUID", "player_name", "email", "stats"} + playerColumns := []string{"playerUUID", "player_name", "email", "password_hash", "stats"} emptyStatsStruct, _ := json.Marshal(models.PlayerStats{}) emptyStats := string(emptyStatsStruct) m := []*spanner.Mutation{ - spanner.Insert("players", playerColumns, []interface{}{1, "player1", "player1@email.com", emptyStats}), - spanner.Insert("players", playerColumns, []interface{}{2, "player2", "player2@email.com", emptyStats}), - spanner.Insert("players", playerColumns, []interface{}{3, "player3", "player3@email.com", emptyStats}), - spanner.Insert("players", playerColumns, []interface{}{4, "player4", "player4@email.com", emptyStats}), - spanner.Insert("players", playerColumns, []interface{}{5, "player5", "player5@email.com", emptyStats}), + spanner.Insert("players", playerColumns, []interface{}{1, "player1", "player1@email.com", "adsfijapfja3234aipj", emptyStats}), + spanner.Insert("players", playerColumns, []interface{}{2, "player2", "player2@email.com", "apoijawernipoav8210", emptyStats}), + spanner.Insert("players", playerColumns, []interface{}{3, "player3", "player3@email.com", "9asil23jifa82all3i1", emptyStats}), + spanner.Insert("players", playerColumns, []interface{}{4, "player4", "player4@email.com", "zmpoasiqamaf8wl1ioa", emptyStats}), + spanner.Insert("players", playerColumns, []interface{}{5, "player5", "player5@email.com", "adf80awenzlkzjfawif", emptyStats}), } _, err = client.Apply(ctx, m) if err != nil { diff --git a/src/golang/profile-service/main_test.go b/src/golang/profile-service/main_test.go index 8250483..633aa67 100644 --- a/src/golang/profile-service/main_test.go +++ b/src/golang/profile-service/main_test.go @@ -166,6 +166,7 @@ func setupDatabase(ctx context.Context, ec Emulator) error { schema, _ := SCHEMAFILE.ReadFile("test_data/schema.sql") // TODO: remove this when the Spanner Emulator supports 'DEFAULT' syntax + // This is still a problem when using SQL to insert data. see: https://github.com/GoogleCloudPlatform/cloud-spanner-emulator/issues/101 schemaStringFix := strings.Replace(string(schema), "account_balance NUMERIC NOT NULL DEFAULT (0.00),", "account_balance NUMERIC,", 1) schemaStatements := strings.Split(schemaStringFix, ";") diff --git a/src/golang/tradepost-service/main_test.go b/src/golang/tradepost-service/main_test.go index c204cb3..bb208da 100644 --- a/src/golang/tradepost-service/main_test.go +++ b/src/golang/tradepost-service/main_test.go @@ -70,7 +70,7 @@ func teardown(ctx context.Context, emulator *Emulator, service *Service) { func setupSpannerEmulator(ctx context.Context) (*Emulator, error) { req := testcontainers.ContainerRequest{ - Image: "gcr.io/cloud-spanner-emulator/emulator:latest", + Image: "gcr.io/cloud-spanner-emulator/emulator:1.5.0", ExposedPorts: []string{"9010/tcp"}, Networks: []string{ TESTNETWORK, @@ -174,22 +174,10 @@ func setupDatabase(ctx context.Context, ec Emulator) error { // get schema statements from file schema, _ := SCHEMAFILE.ReadFile("test_data/schema.sql") - // Removing NOT NULL constraints for columns we don't care about in item tests - schemaStringFix := strings.Replace(string(schema), "password_hash BYTES(60) NOT NULL,", "password_hash BYTES(60),", 1) + // Remove trailing semi-colon/newline so Emulator can parse DDL statements + schemaString := strings.TrimSuffix(string(schema), ";\n") - // TODO: Remove this when the Spanner Emulator supports 'DEFAULT' syntax; NOT NULL removed to avoid errors - // and most of those columns we don't use at the moment - schemaStringFix = strings.Replace(schemaStringFix, "account_balance NUMERIC NOT NULL DEFAULT (0.00),", "account_balance NUMERIC,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "acquire_time TIMESTAMP NOT NULL DEFAULT (CURRENT_TIMESTAMP()),", "acquire_time TIMESTAMP,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "created TIMESTAMP NOT NULL DEFAULT (CURRENT_TIMESTAMP()),", "created TIMESTAMP,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "expires TIMESTAMP NOT NULL DEFAULT (TIMESTAMP_ADD(CURRENT_TIMESTAMP(), interval 24 HOUR)),", "expires TIMESTAMP NOT NULL,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "visible BOOL NOT NULL DEFAULT(true),", "visible BOOL,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "active BOOL NOT NULL DEFAULT (true),", "active BOOL,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "cancelled BOOL NOT NULL DEFAULT (false),", "cancelled BOOL,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "filled BOOL NOT NULL DEFAULT (false),", "filled BOOL,", 1) - schemaStringFix = strings.Replace(schemaStringFix, "expired BOOL NOT NULL DEFAULT (false),", "expired BOOL,", 1) - - schemaStatements := strings.Split(schemaStringFix, ";") + schemaStatements := strings.Split(schemaString, ";") adminClient, err := database.NewDatabaseAdminClient(ctx) if err != nil { @@ -223,7 +211,7 @@ func loadTestData(ctx context.Context, ec Emulator) error { } defer client.Close() - playerColumns := []string{"playerUUID", "player_name", "email", "account_balance", "current_game"} + playerColumns := []string{"playerUUID", "player_name", "email", "password_hash", "account_balance", "current_game"} gameColumns := []string{"gameUUID", "players", "created"} gameItemColumns := []string{"itemUUID", "item_name", "item_value", "available_time", "duration"} playerItemColumns := []string{"playerItemUUID", "playerUUID", "itemUUID", "price", "source", "game_session", "acquire_time", "visible"} @@ -237,8 +225,8 @@ func loadTestData(ctx context.Context, ec Emulator) error { m := []*spanner.Mutation{ spanner.Insert("games", gameColumns, []interface{}{gameUUID, []string{playerUUID[0], playerUUID[1]}, time.Now()}), // Adds 3 players to a game - spanner.Insert("players", playerColumns, []interface{}{playerUUID[0], "player1", "player1@email.com", "0.00", gameUUID}), - spanner.Insert("players", playerColumns, []interface{}{playerUUID[1], "player2", "player2@email.com", "10.00", gameUUID}), + spanner.Insert("players", playerColumns, []interface{}{playerUUID[0], "player1", "player1@email.com", "adsfijapfja3234aipj", "0.00", gameUUID}), + spanner.Insert("players", playerColumns, []interface{}{playerUUID[1], "player2", "player2@email.com", "apoijawernipoav8210", "10.00", gameUUID}), spanner.Insert("game_items", gameItemColumns, []interface{}{itemUUID, "test_item", testItemPrice, time.Now(), 0}), spanner.Insert("player_items", playerItemColumns, []interface{}{playerItemUUID, playerUUID[0], itemUUID, testItemPrice, "loot", gameUUID, time.Now(), true}), }