Skip to content

Commit 8ea504e

Browse files
committed
update tests
1 parent de21635 commit 8ea504e

3 files changed

Lines changed: 99 additions & 4 deletions

File tree

logs_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,11 @@ func TestLogsEndpoints(t *testing.T) {
397397
func addLogsForPaginationCapTest(t *testing.T) {
398398
t.Helper()
399399

400+
const (
401+
insertCount = 110
402+
message = "Pagination cap test log"
403+
)
404+
400405
query := fmt.Sprintf(
401406
"INSERT INTO logs (id, message, created_at, updated_at) VALUES (%s, %s, %s, %s)",
402407
placeholder(1),
@@ -405,17 +410,21 @@ func addLogsForPaginationCapTest(t *testing.T) {
405410
placeholder(4),
406411
)
407412

408-
for i := range 100 {
413+
for i := range insertCount {
409414
createdAt := time.Date(2011, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(i) * time.Second)
410415
_, err := db.Exec(
411416
query,
412417
fmt.Sprintf("00000000-0000-0000-0000-%012d", i),
413-
"Pagination cap test log",
418+
message,
414419
createdAt,
415420
createdAt,
416421
)
417422
require.NoError(t, err)
418423
}
424+
425+
t.Cleanup(func() {
426+
_, _ = db.Exec("DELETE FROM logs WHERE message = "+placeholder(1), message)
427+
})
419428
}
420429

421430
func TestLogsDBChecks(t *testing.T) {

publishers_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"fmt"
45
"strings"
56
"testing"
7+
"time"
68

79
"github.com/stretchr/testify/assert"
810
"github.com/stretchr/testify/require"
@@ -114,9 +116,10 @@ func TestPublishersEndpoints(t *testing.T) {
114116

115117
expectedCode: 200,
116118
expectedContentType: "application/json",
119+
setupFunc: addPublishersForPaginationCapTest,
117120
validateFunc: func(t *testing.T, response map[string]interface{}) {
118121
items := assertListResponse(t, response)
119-
assert.LessOrEqual(t, len(items), 100)
122+
assert.Equal(t, 100, len(items))
120123
},
121124
},
122125
{
@@ -1165,3 +1168,40 @@ func TestPublishersDeleteDBChecks(t *testing.T) {
11651168
assert.Equal(t, 0, dbCount(t, "publishers_code_hosting", "publisher_id", publisherID))
11661169
})
11671170
}
1171+
1172+
func addPublishersForPaginationCapTest(t *testing.T) {
1173+
t.Helper()
1174+
1175+
const (
1176+
insertCount = 110
1177+
descriptionPrefix = "Pagination cap test publisher "
1178+
)
1179+
1180+
query := fmt.Sprintf(
1181+
"INSERT INTO publishers (id, description, email, active, created_at, updated_at) VALUES (%s, %s, %s, %s, %s, %s)",
1182+
placeholder(1),
1183+
placeholder(2),
1184+
placeholder(3),
1185+
placeholder(4),
1186+
placeholder(5),
1187+
placeholder(6),
1188+
)
1189+
1190+
for i := range insertCount {
1191+
createdAt := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(i) * time.Second)
1192+
_, err := db.Exec(
1193+
query,
1194+
fmt.Sprintf("11111111-1111-1111-1111-%012d", i),
1195+
fmt.Sprintf("%s%d", descriptionPrefix, i),
1196+
fmt.Sprintf("cap-test-%d@example.org", i),
1197+
true,
1198+
createdAt,
1199+
createdAt,
1200+
)
1201+
require.NoError(t, err)
1202+
}
1203+
1204+
t.Cleanup(func() {
1205+
_, _ = db.Exec("DELETE FROM publishers WHERE description LIKE "+placeholder(1), descriptionPrefix+"%")
1206+
})
1207+
}

software_test.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"strings"
67
"testing"
78
"time"
@@ -200,9 +201,10 @@ func TestSoftwareEndpoints(t *testing.T) {
200201

201202
expectedCode: 200,
202203
expectedContentType: "application/json",
204+
setupFunc: addSoftwareForPaginationCapTest,
203205
validateFunc: func(t *testing.T, response map[string]interface{}) {
204206
items := assertListResponse(t, response)
205-
assert.LessOrEqual(t, len(items), 100)
207+
assert.Equal(t, 100, len(items))
206208
},
207209
},
208210
{
@@ -1669,3 +1671,47 @@ func TestSoftwareDeleteDBChecks(t *testing.T) {
16691671
assert.Equal(t, 0, dbCount(t, "software_urls", "software_id", softwareID))
16701672
})
16711673
}
1674+
1675+
func addSoftwareForPaginationCapTest(t *testing.T) {
1676+
t.Helper()
1677+
1678+
const (
1679+
insertCount = 110
1680+
urlPrefix = "https://cap-test.example.org/repo-"
1681+
)
1682+
1683+
swQuery := fmt.Sprintf(
1684+
"INSERT INTO software (id, software_url_id, publiccode_yml, active, created_at, updated_at) VALUES (%s, %s, %s, %s, %s, %s)",
1685+
placeholder(1),
1686+
placeholder(2),
1687+
placeholder(3),
1688+
placeholder(4),
1689+
placeholder(5),
1690+
placeholder(6),
1691+
)
1692+
urlQuery := fmt.Sprintf(
1693+
"INSERT INTO software_urls (id, url, software_id, created_at, updated_at) VALUES (%s, %s, %s, %s, %s)",
1694+
placeholder(1),
1695+
placeholder(2),
1696+
placeholder(3),
1697+
placeholder(4),
1698+
placeholder(5),
1699+
)
1700+
1701+
for i := range insertCount {
1702+
swID := fmt.Sprintf("11111111-1111-1111-1111-%012d", i)
1703+
urlID := fmt.Sprintf("22222222-2222-2222-2222-%012d", i)
1704+
createdAt := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(i) * time.Second)
1705+
1706+
_, err := db.Exec(swQuery, swID, urlID, "-", true, createdAt, createdAt)
1707+
require.NoError(t, err)
1708+
1709+
_, err = db.Exec(urlQuery, urlID, fmt.Sprintf("%s%d", urlPrefix, i), swID, createdAt, createdAt)
1710+
require.NoError(t, err)
1711+
}
1712+
1713+
t.Cleanup(func() {
1714+
_, _ = db.Exec("DELETE FROM software_urls WHERE url LIKE "+placeholder(1), urlPrefix+"%")
1715+
_, _ = db.Exec("DELETE FROM software WHERE id LIKE "+placeholder(1), "11111111-1111-1111-1111-%")
1716+
})
1717+
}

0 commit comments

Comments
 (0)