Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating most services integration tests that use DEFAULT syntax. #51

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions src/golang/item-service/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
},
})
Expand Down
21 changes: 10 additions & 11 deletions src/golang/matchmaking-service/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/golang/profile-service/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ";")
Expand Down
26 changes: 7 additions & 19 deletions src/golang/tradepost-service/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"}
Expand All @@ -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}),
}
Expand Down