Skip to content

Commit 17de771

Browse files
committed
Add tests for the entity and property DB functions
1 parent fbaaf5b commit 17de771

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

internal/db/entities_test.go

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
//
2+
// Copyright 2024 Stacklok, Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package db
17+
18+
import (
19+
"context"
20+
"database/sql"
21+
"testing"
22+
23+
"github.com/google/uuid"
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
func Test_EntityCrud(t *testing.T) {
28+
t.Parallel()
29+
30+
org := createRandomOrganization(t)
31+
proj := createRandomProject(t, org.ID)
32+
prov := createRandomProvider(t, proj.ID)
33+
34+
t.Run("CreateEntity", func(t *testing.T) {
35+
t.Parallel()
36+
37+
const testRepoName = "testorg/testrepo"
38+
39+
ent, err := testQueries.CreateEntity(context.Background(), CreateEntityParams{
40+
EntityType: EntitiesRepository,
41+
Name: testRepoName,
42+
ProjectID: proj.ID,
43+
ProviderID: prov.ID,
44+
OriginatedFrom: uuid.NullUUID{},
45+
})
46+
require.NoError(t, err)
47+
require.NotEmpty(t, ent)
48+
require.NotEqual(t, ent.ID, uuid.Nil)
49+
require.Equal(t, ent.EntityType, EntitiesRepository)
50+
require.Equal(t, ent.Name, testRepoName)
51+
require.Equal(t, ent.ProjectID, proj.ID)
52+
require.Equal(t, ent.ProviderID, prov.ID)
53+
require.Equal(t, ent.OriginatedFrom, uuid.NullUUID{})
54+
55+
entGet, err := testQueries.GetEntityByID(context.Background(), GetEntityByIDParams{
56+
ID: ent.ID,
57+
Projects: []uuid.UUID{proj.ID},
58+
})
59+
require.NoError(t, err)
60+
require.Equal(t, entGet, ent)
61+
62+
err = testQueries.DeleteEntity(context.Background(), DeleteEntityParams{
63+
ID: ent.ID,
64+
ProjectID: proj.ID,
65+
})
66+
require.NoError(t, err)
67+
68+
entGet, err = testQueries.GetEntityByID(context.Background(), GetEntityByIDParams{
69+
ID: ent.ID,
70+
Projects: []uuid.UUID{proj.ID},
71+
})
72+
require.ErrorIs(t, err, sql.ErrNoRows)
73+
require.Empty(t, entGet)
74+
})
75+
76+
t.Run("No such entity", func(t *testing.T) {
77+
t.Parallel()
78+
79+
ent, err := testQueries.GetEntityByName(context.Background(), GetEntityByNameParams{
80+
ProjectID: proj.ID,
81+
Name: "garbage/nosuchentity",
82+
})
83+
require.ErrorIs(t, err, sql.ErrNoRows)
84+
require.Empty(t, ent)
85+
})
86+
}
87+
88+
func Test_PropertyCrud(t *testing.T) {
89+
t.Parallel()
90+
91+
org := createRandomOrganization(t)
92+
proj := createRandomProject(t, org.ID)
93+
prov := createRandomProvider(t, proj.ID)
94+
95+
t.Run("UpsertProperty", func(t *testing.T) {
96+
t.Parallel()
97+
98+
const testRepoName = "testorg/testrepo_props"
99+
100+
ent, err := testQueries.CreateEntity(context.Background(), CreateEntityParams{
101+
EntityType: EntitiesRepository,
102+
Name: testRepoName,
103+
ProjectID: proj.ID,
104+
ProviderID: prov.ID,
105+
OriginatedFrom: uuid.NullUUID{},
106+
})
107+
require.NoError(t, err)
108+
require.NotEmpty(t, ent)
109+
110+
dbProp, err := testQueries.GetAllPropertyValuesV1(context.Background(), ent.ID)
111+
require.NoError(t, err)
112+
require.Empty(t, dbProp)
113+
114+
prop, err := testQueries.UpsertPropertyValueV1(context.Background(), UpsertPropertyValueV1Params{
115+
EntityID: ent.ID,
116+
Key: "testkey",
117+
Value: "testvalue",
118+
})
119+
require.NoError(t, err)
120+
require.NotEmpty(t, prop)
121+
122+
prop, err = testQueries.UpsertPropertyValueV1(context.Background(), UpsertPropertyValueV1Params{
123+
EntityID: ent.ID,
124+
Key: "anotherkey",
125+
Value: "anothervalue",
126+
})
127+
require.NoError(t, err)
128+
require.NotEmpty(t, prop)
129+
130+
dbProp, err = testQueries.GetAllPropertyValuesV1(context.Background(), ent.ID)
131+
require.NoError(t, err)
132+
require.Len(t, dbProp, 2)
133+
134+
propTestKey := propertyByKey(t, dbProp, "testkey")
135+
require.Equal(t, propTestKey.Value, "testvalue")
136+
propAnotherKey := propertyByKey(t, dbProp, "anotherkey")
137+
require.Equal(t, propAnotherKey.Value, "anothervalue")
138+
139+
keyVal, err := testQueries.GetPropertyValueV1(context.Background(), ent.ID, "testkey")
140+
require.NoError(t, err)
141+
require.Equal(t, keyVal.Value, "testvalue")
142+
143+
anotherKeyVal, err := testQueries.GetPropertyValueV1(context.Background(), ent.ID, "anotherkey")
144+
require.NoError(t, err)
145+
require.Equal(t, anotherKeyVal.Value, "anothervalue")
146+
})
147+
}
148+
149+
func propertyByKey(t *testing.T, props []PropertyValueV1, key string) PropertyValueV1 {
150+
t.Helper()
151+
152+
for _, prop := range props {
153+
if prop.Key == key {
154+
return prop
155+
}
156+
}
157+
return PropertyValueV1{}
158+
}
159+
160+
func Test_PropertyHelpers(t *testing.T) {
161+
t.Parallel()
162+
163+
t.Run("TestFromTo", func(t *testing.T) {
164+
t.Parallel()
165+
166+
// TODO: we run into the issue with the large integers here again..
167+
strValue := "hello world"
168+
169+
jsonValue, err := PropValueToDbV1(strValue)
170+
require.NoError(t, err)
171+
172+
propValue, err := PropValueFromDbV1(jsonValue)
173+
require.NoError(t, err)
174+
require.Equal(t, strValue, propValue)
175+
})
176+
177+
t.Run("Bad Version", func(t *testing.T) {
178+
t.Parallel()
179+
180+
_, err := PropValueFromDbV1([]byte(`{"version": "2", "value": "hello world"}`))
181+
require.Error(t, err)
182+
require.ErrorIs(t, err, ErrBadPropVersion)
183+
})
184+
}

0 commit comments

Comments
 (0)