Skip to content

Commit 1fecfb0

Browse files
committed
Update tests
1 parent d381ba3 commit 1fecfb0

File tree

5 files changed

+74
-24
lines changed

5 files changed

+74
-24
lines changed

Tests/AppTests/CustomCollectionTests.swift

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,33 @@ class CustomCollectionTests: AppTestCase {
2323

2424
func test_CustomCollection_save() async throws {
2525
// MUT
26-
try await CustomCollection(id: .id0, .init(name: "List", url: "https://github.com/foo/bar/list.json"))
26+
try await CustomCollection(id: .id0, .init(key: "list",
27+
name: "List",
28+
url: "https://github.com/foo/bar/list.json"))
2729
.save(on: app.db)
2830

2931
do { // validate
3032
let collection = try await CustomCollection.find(.id0, on: app.db).unwrap()
33+
XCTAssertEqual(collection.key, "list")
3134
XCTAssertEqual(collection.name, "List")
3235
XCTAssertEqual(collection.url, "https://github.com/foo/bar/list.json")
3336
}
3437

38+
do { // ensure key is unique
39+
try await CustomCollection(.init(key: "list",
40+
name: "List 2",
41+
url: "https://github.com/foo/bar/other-list.json"))
42+
.save(on: app.db)
43+
XCTFail("Expected failure")
44+
} catch {
45+
let msg = String(reflecting: error)
46+
XCTAssert(msg.contains(#"duplicate key value violates unique constraint "uq:custom_collections.key""#),
47+
"was: \(msg)")
48+
}
3549
do { // ensure name is unique
36-
try await CustomCollection(.init(name: "List", url: "https://github.com/foo/bar/other-list.json"))
50+
try await CustomCollection(.init(key: "list-2",
51+
name: "List",
52+
url: "https://github.com/foo/bar/other-list.json"))
3753
.save(on: app.db)
3854
XCTFail("Expected failure")
3955
} catch {
@@ -43,7 +59,9 @@ class CustomCollectionTests: AppTestCase {
4359
}
4460

4561
do { // ensure url is unique
46-
try await CustomCollection(.init(name: "List 2", url: "https://github.com/foo/bar/list.json"))
62+
try await CustomCollection(.init(key: "list-2",
63+
name: "List 2",
64+
url: "https://github.com/foo/bar/list.json"))
4765
.save(on: app.db)
4866
XCTFail("Expected failure")
4967
} catch {
@@ -56,28 +74,36 @@ class CustomCollectionTests: AppTestCase {
5674
func test_CustomCollection_findOrCreate() async throws {
5775
do { // initial call creates collection
5876
// MUT
59-
let res = try await CustomCollection.findOrCreate(on: app.db, .init(name: "List", url: "url"))
77+
let res = try await CustomCollection.findOrCreate(on: app.db, .init(key: "list",
78+
name: "List",
79+
url: "url"))
6080

6181
// validate
82+
XCTAssertEqual(res.key, "list")
6283
XCTAssertEqual(res.name, "List")
6384
XCTAssertEqual(res.url, "url")
6485

6586
let c = try await CustomCollection.query(on: app.db).all()
6687
XCTAssertEqual(c.count, 1)
88+
XCTAssertEqual(c.first?.key, "list")
6789
XCTAssertEqual(c.first?.name, "List")
6890
XCTAssertEqual(c.first?.url, "url")
6991
}
7092

7193
do { // re-running is idempotent
7294
// MUT
73-
let res = try await CustomCollection.findOrCreate(on: app.db, .init(name: "List", url: "url"))
95+
let res = try await CustomCollection.findOrCreate(on: app.db, .init(key: "list",
96+
name: "List",
97+
url: "url"))
7498

7599
// validate
100+
XCTAssertEqual(res.key, "list")
76101
XCTAssertEqual(res.name, "List")
77102
XCTAssertEqual(res.url, "url")
78103

79104
let c = try await CustomCollection.query(on: app.db).all()
80105
XCTAssertEqual(c.count, 1)
106+
XCTAssertEqual(c.first?.key, "list")
81107
XCTAssertEqual(c.first?.name, "List")
82108
XCTAssertEqual(c.first?.url, "url")
83109
}
@@ -87,7 +113,9 @@ class CustomCollectionTests: AppTestCase {
87113
// setup
88114
let pkg = Package(id: .id0, url: "1".asGithubUrl.url)
89115
try await pkg.save(on: app.db)
90-
let collection = CustomCollection(id: .id1, .init(name: "List", url: "https://github.com/foo/bar/list.json"))
116+
let collection = CustomCollection(id: .id1, .init(key: "list",
117+
name: "List",
118+
url: "https://github.com/foo/bar/list.json"))
91119
try await collection.save(on: app.db)
92120

93121
// MUT
@@ -102,7 +130,7 @@ class CustomCollectionTests: AppTestCase {
102130
XCTAssertEqual(pivot.package.url, "1".asGithubUrl)
103131
try await pivot.$customCollection.load(on: app.db)
104132
XCTAssertEqual(pivot.customCollection.id, .id1)
105-
XCTAssertEqual(pivot.customCollection.name, "List")
133+
XCTAssertEqual(pivot.customCollection.key, "list")
106134
}
107135

108136
do { // ensure package is unique per list
@@ -118,7 +146,9 @@ class CustomCollectionTests: AppTestCase {
118146
// setup
119147
let pkg = Package(id: .id0, url: "1".asGithubUrl.url)
120148
try await pkg.save(on: app.db)
121-
let collection = CustomCollection(id: .id1, .init(name: "List", url: "https://github.com/foo/bar/list.json"))
149+
let collection = CustomCollection(id: .id1, .init(key: "list",
150+
name: "List",
151+
url: "https://github.com/foo/bar/list.json"))
122152
try await collection.save(on: app.db)
123153
try await collection.$packages.attach(pkg, on: app.db)
124154

@@ -143,7 +173,9 @@ class CustomCollectionTests: AppTestCase {
143173
try await p1.save(on: app.db)
144174
let p2 = Package(id: .id1, url: "2".asGithubUrl.url)
145175
try await p2.save(on: app.db)
146-
let collection = CustomCollection(id: .id2, .init(name: "List", url: "https://github.com/foo/bar/list.json"))
176+
let collection = CustomCollection(id: .id2, .init(key: "list",
177+
name: "List",
178+
url: "https://github.com/foo/bar/list.json"))
147179
try await collection.save(on: app.db)
148180
try await collection.$packages.attach([p1, p2], on: app.db)
149181

@@ -161,12 +193,16 @@ class CustomCollectionTests: AppTestCase {
161193
let p1 = Package(id: .id0, url: "1".asGithubUrl.url)
162194
try await p1.save(on: app.db)
163195
do {
164-
let collection = CustomCollection(id: .id1, .init(name: "List 1", url: "https://github.com/foo/bar/list-1.json"))
196+
let collection = CustomCollection(id: .id1, .init(key: "list-1",
197+
name: "List 1",
198+
url: "https://github.com/foo/bar/list-1.json"))
165199
try await collection.save(on: app.db)
166200
try await collection.$packages.attach(p1, on: app.db)
167201
}
168202
do {
169-
let collection = CustomCollection(id: .id2, .init(name: "List 2", url: "https://github.com/foo/bar/list-2.json"))
203+
let collection = CustomCollection(id: .id2, .init(key: "list-2",
204+
name: "List 2",
205+
url: "https://github.com/foo/bar/list-2.json"))
170206
try await collection.save(on: app.db)
171207
try await collection.$packages.attach(p1, on: app.db)
172208
}
@@ -182,7 +218,9 @@ class CustomCollectionTests: AppTestCase {
182218
// setup
183219
let pkg = Package(id: .id0, url: "1".asGithubUrl.url)
184220
try await pkg.save(on: app.db)
185-
let collection = CustomCollection(id: .id1, .init(name: "List", url: "https://github.com/foo/bar/list.json"))
221+
let collection = CustomCollection(id: .id1, .init(key: "list",
222+
name: "List",
223+
url: "https://github.com/foo/bar/list.json"))
186224
try await collection.save(on: app.db)
187225
try await collection.$packages.attach(pkg, on: app.db)
188226
do {
@@ -220,7 +258,9 @@ class CustomCollectionTests: AppTestCase {
220258
// setup
221259
let pkg = Package(id: .id0, url: "1".asGithubUrl.url)
222260
try await pkg.save(on: app.db)
223-
let collection = CustomCollection(id: .id1, .init(name: "List", url: "https://github.com/foo/bar/list.json"))
261+
let collection = CustomCollection(id: .id1, .init(key: "list",
262+
name: "List",
263+
url: "https://github.com/foo/bar/list.json"))
224264
try await collection.save(on: app.db)
225265
try await collection.$packages.attach(pkg, on: app.db)
226266
do {
@@ -256,7 +296,9 @@ class CustomCollectionTests: AppTestCase {
256296

257297
func test_CustomCollection_reconcile() async throws {
258298
// Test reconciliation of a custom collection against a list of package URLs
259-
let collection = CustomCollection(id: .id0, .init(name: "List", url: "https://github.com/foo/bar/list.json"))
299+
let collection = CustomCollection(id: .id0, .init(key: "list",
300+
name: "List",
301+
url: "https://github.com/foo/bar/list.json"))
260302
try await collection.save(on: app.db)
261303
try await Package(id: .id1, url: URL("https://github.com/a.git")).save(on: app.db)
262304
try await Package(id: .id2, url: URL("https://github.com/b.git")).save(on: app.db)
@@ -311,7 +353,9 @@ class CustomCollectionTests: AppTestCase {
311353

312354
func test_CustomCollection_reconcile_caseSensitive() async throws {
313355
// Test reconciliation with a case-insensitive matching URL
314-
let collection = CustomCollection(id: .id0, .init(name: "List", url: "https://github.com/foo/bar/list.json"))
356+
let collection = CustomCollection(id: .id0, .init(key: "list",
357+
name: "List",
358+
url: "https://github.com/foo/bar/list.json"))
315359
try await collection.save(on: app.db)
316360
try await Package(id: .id1, url: URL("a")).save(on: app.db)
317361

@@ -321,7 +365,7 @@ class CustomCollectionTests: AppTestCase {
321365
do { // validate
322366
// The package is not added to the custom collection, because it is not an
323367
// exact match for the package URL.
324-
// This is currently a limiting of the Fluent ~~ operator in the query
368+
// This is currently a limitation of the Fluent ~~ operator in the query
325369
// filter(\.$url ~~ urls.map(\.absoluteString))
326370
let count = try await CustomCollectionPackage.query(on: app.db).count()
327371
XCTAssertEqual(count, 0)

Tests/AppTests/PackageCollectionControllerTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ class PackageCollectionControllerTests: AppTestCase {
112112
licenseUrl: "https://foo/mit",
113113
owner: "foo",
114114
summary: "summary 1").create(on: app.db)
115-
let collection = CustomCollection(id: .id2, .init(name: "Custom Collection", url: "https://github.com/foo/bar/list.json"))
115+
let collection = CustomCollection(id: .id2, .init(key: "custom-collection",
116+
name: "Custom Collection",
117+
url: "https://github.com/foo/bar/list.json"))
116118
try await collection.save(on: app.db)
117119
try await collection.$packages.attach(p, on: app.db)
118120

Tests/AppTests/PackageCollectionTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ class PackageCollectionTests: AppTestCase {
195195
.save(on: app.db)
196196
return pkg
197197
}
198-
let collection = CustomCollection(id: .id2, .init(name: "List", url: "https://github.com/foo/bar/list.json"))
198+
let collection = CustomCollection(id: .id2, .init(key: "list",
199+
name: "List",
200+
url: "https://github.com/foo/bar/list.json"))
199201
try await collection.save(on: app.db)
200202
try await collection.$packages.attach([packages[0], packages[1]], on: app.db)
201203

Tests/AppTests/ReconcilerTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class ReconcilerTests: AppTestCase {
143143
try await reconcileCustomCollection(client: app.client,
144144
database: app.db,
145145
fullPackageList: fullPackageList,
146-
.init(name: "List", url: "url"))
146+
.init(key: "list", name: "List", url: "url"))
147147

148148
// validate
149149
let count = try await CustomCollection.query(on: app.db).count()
@@ -161,7 +161,7 @@ class ReconcilerTests: AppTestCase {
161161
try await reconcileCustomCollection(client: app.client,
162162
database: app.db,
163163
fullPackageList: fullPackageList,
164-
.init(name: "List", url: "url"))
164+
.init(key: "list", name: "List", url: "url"))
165165

166166
// validate
167167
let count = try await CustomCollection.query(on: app.db).count()
@@ -181,7 +181,7 @@ class ReconcilerTests: AppTestCase {
181181
try await reconcileCustomCollection(client: app.client,
182182
database: app.db,
183183
fullPackageList: fullPackageList,
184-
.init(name: "List", url: "url"))
184+
.init(key: "list", name: "List", url: "url"))
185185

186186
// validate
187187
let count = try await CustomCollection.query(on: app.db).count()
@@ -207,7 +207,7 @@ class ReconcilerTests: AppTestCase {
207207
try await reconcileCustomCollection(client: app.client,
208208
database: app.db,
209209
fullPackageList: fullPackageList,
210-
.init(name: "List", url: "url"))
210+
.init(key: "list", name: "List", url: "url"))
211211

212212
// validate
213213
let collection = try await CustomCollection.query(on: app.db).first().unwrap()
@@ -233,7 +233,7 @@ class ReconcilerTests: AppTestCase {
233233
}
234234
}
235235
$0.packageListRepository.fetchCustomCollections = { @Sendable _ in
236-
[.init(name: "List", url: "collectionURL")]
236+
[.init(key: "list", name: "List", url: "collectionURL")]
237237
}
238238
} operation: {
239239
// MUT

Tests/AppTests/WebpageSnapshotTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,9 @@ class WebpageSnapshotTests: SnapshotTestCase {
296296
func test_PackageShowView_customCollection() throws {
297297
var model = API.PackageController.GetRoute.Model.mock
298298
model.homepageUrl = "https://swiftpackageindex.com/"
299-
model.customCollections = [.init(name: "Custom Collection", url: "https://github.com/foo/bar/list.json")]
299+
model.customCollections = [.init(key: "custom-collection",
300+
name: "Custom Collection",
301+
url: "https://github.com/foo/bar/list.json")]
300302
let page = { PackageShow.View(path: "", model: model, packageSchema: .mock).document() }
301303

302304
assertSnapshot(of: page, as: .html)

0 commit comments

Comments
 (0)