|
| 1 | +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License-AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package db_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "github.com/gitpod-io/gitpod/usage/pkg/db" |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "gorm.io/gorm" |
| 13 | + "strings" |
| 14 | + "testing" |
| 15 | +) |
| 16 | + |
| 17 | +var teamJSON = map[string]interface{}{ |
| 18 | + "id": "2db3d0ae-9fa2-41ed-9781-acfc7e17294b", |
| 19 | + "name": "Gitpod", |
| 20 | + "slug": "gitpod", |
| 21 | + "creationTime": "2021-07-22T11:46:43.281Z", |
| 22 | + "deleted": 0, |
| 23 | + "_lastModified": "2021-10-01 10:26:10.718585", |
| 24 | + "markedDeleted": 0, |
| 25 | +} |
| 26 | + |
| 27 | +func TestTeam_ReadFromExistingData(t *testing.T) { |
| 28 | + conn := db.ConnectForTests(t) |
| 29 | + id := insertRawTeam(t, conn, teamJSON) |
| 30 | + |
| 31 | + team := db.Team{ID: id} |
| 32 | + tx := conn.First(&team) |
| 33 | + require.NoError(t, tx.Error) |
| 34 | + |
| 35 | + require.Equal(t, id, team.ID) |
| 36 | + require.Equal(t, teamJSON["name"], team.Name) |
| 37 | + require.Equal(t, teamJSON["slug"], team.Slug) |
| 38 | + require.Equal(t, stringToVarchar(t, teamJSON["creationTime"].(string)), team.CreationTime) |
| 39 | + require.EqualValues(t, teamJSON["markedDeleted"] == 1, team.MarkedDeleted) |
| 40 | +} |
| 41 | + |
| 42 | +func insertRawTeam(t *testing.T, conn *gorm.DB, object map[string]interface{}) uuid.UUID { |
| 43 | + t.Helper() |
| 44 | + |
| 45 | + columns := []string{"id", "name", "slug", "creationTime", "deleted", "_lastModified", "markedDeleted"} |
| 46 | + statement := fmt.Sprintf(`INSERT INTO d_b_team (%s) VALUES ?;`, strings.Join(columns, ", ")) |
| 47 | + |
| 48 | + id := uuid.MustParse(insertRawObject(t, conn, columns, statement, object)) |
| 49 | + |
| 50 | + t.Cleanup(func() { |
| 51 | + tx := conn.Delete(&db.Team{ID: id}) |
| 52 | + require.NoError(t, tx.Error) |
| 53 | + }) |
| 54 | + |
| 55 | + return id |
| 56 | +} |
0 commit comments