Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/project_test.go
2497 views
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
"strings"
10
"testing"
11
12
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
13
14
"github.com/gitpod-io/gitpod/components/gitpod-db/go/dbtest"
15
"github.com/google/uuid"
16
"github.com/stretchr/testify/require"
17
"gorm.io/gorm"
18
)
19
20
var projectJSON = map[string]interface{}{
21
"id": uuid.New().String(),
22
"cloneUrl": "https://github.com/gptest1/gptest1-repo1-private.git",
23
"teamId": "0e433063-1358-4892-9ed2-68e273d17d07",
24
"appInstallationId": "20446411",
25
"creationTime": "2021-11-01T19:36:07.532Z",
26
"_lastModified": "2021-11-02 10:49:12.473658",
27
"name": "gptest1-repo1-private",
28
"markedDeleted": 1,
29
"userId": nil,
30
"slug": "gptest1-repo1-private",
31
"settings": nil,
32
}
33
34
func TestProject_ReadExistingRecords(t *testing.T) {
35
conn := dbtest.ConnectForTests(t)
36
37
id := insertRawProject(t, conn, projectJSON)
38
39
project := db.Project{ID: id}
40
tx := conn.First(&project)
41
require.NoError(t, tx.Error)
42
43
require.Equal(t, id, project.ID)
44
require.Equal(t, projectJSON["teamId"], project.TeamID.String)
45
require.Equal(t, stringToVarchar(t, "2021-11-01T19:36:07.532Z"), project.CreationTime)
46
require.NoError(t, conn.Where("id = ?", project.ID).Delete(&db.Project{}).Error)
47
}
48
49
func insertRawProject(t *testing.T, conn *gorm.DB, obj map[string]interface{}) uuid.UUID {
50
columns := []string{
51
"id", "cloneUrl", "teamId", "appInstallationId", "creationTime", "_lastModified", "name", "markedDeleted", "userId", "slug", "settings",
52
}
53
statement := fmt.Sprintf(`INSERT INTO d_b_project (%s) VALUES ?;`, strings.Join(columns, ", "))
54
id := uuid.MustParse(obj["id"].(string))
55
56
var values []interface{}
57
for _, col := range columns {
58
val, ok := obj[col]
59
if !ok {
60
values = append(values, "null")
61
} else {
62
values = append(values, val)
63
}
64
}
65
66
tx := conn.Exec(statement, values)
67
require.NoError(t, tx.Error)
68
69
return id
70
}
71
72