@@ -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 )
0 commit comments