-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathget_test.go
More file actions
90 lines (75 loc) · 2.27 KB
/
get_test.go
File metadata and controls
90 lines (75 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package repo
import (
"reflect"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/go-vela/types/library"
)
func TestRepo_Engine_GetRepo(t *testing.T) {
// setup types
_repo := testRepo()
_repo.SetID(1)
_repo.SetUserID(1)
_repo.SetHash("baz")
_repo.SetOrg("foo")
_repo.SetName("bar")
_repo.SetFullName("foo/bar")
_repo.SetVisibility("public")
_repo.SetPipelineType("yaml")
_repo.SetTopics([]string{})
_postgres, _mock := testPostgres(t)
defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }()
// create expected result in mock
_rows := sqlmock.NewRows(
[]string{"id", "user_id", "hash", "org", "name", "full_name", "link", "clone", "branch", "topics", "build_limit", "timeout", "counter", "visibility", "private", "trusted", "active", "allow_pull", "allow_push", "allow_deploy", "allow_tag", "allow_comment", "pipeline_type", "previous_name"}).
AddRow(1, 1, "baz", "foo", "bar", "foo/bar", "", "", "", "{}", 0, 0, 0, "public", false, false, false, false, false, false, false, false, "yaml", "")
// ensure the mock expects the query
_mock.ExpectQuery(`SELECT * FROM "repos" WHERE id = $1 LIMIT 1`).WithArgs(1).WillReturnRows(_rows)
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()
_, err := _sqlite.CreateRepo(_repo)
if err != nil {
t.Errorf("unable to create test repo for sqlite: %v", err)
}
// setup tests
tests := []struct {
failure bool
name string
database *engine
want *library.Repo
}{
{
failure: false,
name: "postgres",
database: _postgres,
want: _repo,
},
{
failure: false,
name: "sqlite3",
database: _sqlite,
want: _repo,
},
}
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.GetRepo(1)
if test.failure {
if err == nil {
t.Errorf("GetRepo for %s should have returned err", test.name)
}
return
}
if err != nil {
t.Errorf("GetRepo for %s returned err: %v", test.name, err)
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("GetRepo for %s is %v, want %v", test.name, got, test.want)
}
})
}
}