|
| 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/stretchr/testify/require" |
| 11 | + "gorm.io/gorm" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | +) |
| 15 | + |
| 16 | +var testObject = map[string]interface{}{ |
| 17 | + "id": "a0089911-5efc-4e2d-8714-0d3f67d82769", |
| 18 | + "creationTime": "2019-05-10T09:54:28.185Z", |
| 19 | + "ownerId": "39051644-7a8f-4ddb-8bd3-fbede229ad4e", |
| 20 | + "contextURL": "https://github.com/gitpod-io/gitpod", |
| 21 | + "description": "gitpod-io/gitpod - master", |
| 22 | + "context": "{\"isFile\":false,\"path\":\"\",\"" + |
| 23 | + "title\":\"gitpod-io/gitpod - master\",\"" + |
| 24 | + "ref\":\"master\",\"" + |
| 25 | + "revision\":\"6a463917e0718d36ee764a898db2801bc4da580f\",\"" + |
| 26 | + "repository\":{\"cloneUrl\":\"https://github.com/gitpod-io/gitpod.git\",\"" + |
| 27 | + "host\":\"github.com\",\"" + |
| 28 | + "name\":\"gitpod\",\"" + |
| 29 | + "owner\":\"gitpod-io\",\"" + |
| 30 | + "private\":false}}", |
| 31 | + "config": "{\"ports\":[{\"port\":3000}],\"tasks\":[],\"image\":\"eu.gcr.io/gitpod-dev/workspace-full:master.1458\"}", |
| 32 | + "archived": "0", |
| 33 | + "shareable": "0", |
| 34 | + "imageSource": "{\"baseImageResolved\":\"eu.gcr.io/gitpod-dev/workspace-full@sha256:9c0892d1901210f3999c9bd64e371038bfc0505f04858e959e1bb3778dcf19c1\"}", |
| 35 | + "imageNameResolved": "eu.gcr.io/gitpod-dev/workspace-full@sha256:9c0892d1901210f3999c9bd64e371038bfc0505f04858e959e1bb3778dcf19c1", |
| 36 | + "_lastModified": "2020-04-14T00:31:26.101Z", |
| 37 | + "deleted": "0", |
| 38 | + "type": "regular", |
| 39 | + "baseImageNameResolved": "eu.gcr.io/gitpod-dev/workspace-full@sha256:9c0892d1901210f3999c9bd64e371038bfc0505f04858e959e1bb3778dcf19c1", |
| 40 | + "softDeleted": "gc", |
| 41 | + "pinned": "0", |
| 42 | + "softDeletedTime": "2020-03-24T00:03:03.777Z", |
| 43 | + "contentDeletedTime": "2020-04-14T00:31:22.979Z", |
| 44 | + //"basedOnPrebuildId": null, |
| 45 | + //"basedOnSnapshotId": null, |
| 46 | + //"projectId": null, |
| 47 | + "cloneURL": "", |
| 48 | +} |
| 49 | + |
| 50 | +func TestRead(t *testing.T) { |
| 51 | + conn := db.ConnectForTests(t) |
| 52 | + |
| 53 | + insertRawWorkspace(t, conn, testObject) |
| 54 | + |
| 55 | + var ws db.Workspace |
| 56 | + tx := conn.First(&ws) |
| 57 | + require.NoError(t, tx.Error) |
| 58 | + |
| 59 | + require.Equal(t, stringToVarchar(t, testObject["creationTime"].(string)), ws.CreationTime) |
| 60 | + require.Equal(t, stringToVarchar(t, testObject["softDeletedTime"].(string)), ws.SoftDeletedTime) |
| 61 | + require.Equal(t, testObject["context"].(string), ws.Context.String()) |
| 62 | +} |
| 63 | + |
| 64 | +func insertRawWorkspace(t *testing.T, conn *gorm.DB, rawWorkspace map[string]interface{}) string { |
| 65 | + columns := []string{ |
| 66 | + "id", "creationTime", |
| 67 | + "ownerId", "contextURL", "description", "context", "config", "imageSource", "imageNameResolved", "archived", "shareable", |
| 68 | + "deleted", "type", "baseImageNameResolved", "softDeleted", "pinned", "softDeletedTime", "contentDeletedTime", "basedOnPrebuildId", "basedOnSnapshotId", "projectId", "cloneURL", |
| 69 | + } |
| 70 | + statement := fmt.Sprintf(` |
| 71 | + INSERT INTO d_b_workspace ( |
| 72 | + %s |
| 73 | + ) VALUES ?;`, strings.Join(columns, ", ")) |
| 74 | + |
| 75 | + id := rawWorkspace["id"].(string) |
| 76 | + |
| 77 | + var values []interface{} |
| 78 | + for _, col := range columns { |
| 79 | + val, ok := rawWorkspace[col] |
| 80 | + if !ok { |
| 81 | + values = append(values, "null") |
| 82 | + } else { |
| 83 | + values = append(values, val) |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + tx := conn.Debug().Exec(statement, values) |
| 89 | + require.NoError(t, tx.Error) |
| 90 | + |
| 91 | + t.Cleanup(func() { |
| 92 | + tx := conn.Delete(&db.Workspace{ID: id}) |
| 93 | + require.NoError(t, tx.Error) |
| 94 | + }) |
| 95 | + |
| 96 | + return id |
| 97 | +} |
| 98 | + |
| 99 | +func stringToVarchar(t *testing.T, s string) db.VarcharTime { |
| 100 | + t.Helper() |
| 101 | + |
| 102 | + converted, err := db.NewVarcharTimeFromStr(s) |
| 103 | + require.NoError(t, err) |
| 104 | + return converted |
| 105 | +} |
0 commit comments