From e32d6983c9d4edfdc8952e70d77514259bb8a767 Mon Sep 17 00:00:00 2001 From: "Sven A. Schmidt" Date: Mon, 20 Jan 2025 08:06:36 +0100 Subject: [PATCH 1/5] Add CustomCollectionControllerTests.test_query --- .../CustomCollectionControllerTests.swift | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Tests/AppTests/CustomCollectionControllerTests.swift diff --git a/Tests/AppTests/CustomCollectionControllerTests.swift b/Tests/AppTests/CustomCollectionControllerTests.swift new file mode 100644 index 000000000..842536c91 --- /dev/null +++ b/Tests/AppTests/CustomCollectionControllerTests.swift @@ -0,0 +1,48 @@ +// Copyright Dave Verwer, Sven A. Schmidt, and other contributors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import XCTest + +@testable import App + +import Dependencies +import Vapor + + +class CustomCollectionControllerTests: AppTestCase { + + func test_query() async throws { + // setup + let pkg = Package(id: .id0, url: "1".asGithubUrl.url) + try await pkg.save(on: app.db) + try await Repository(package: pkg, name: "1", owner: "owner").save(on: app.db) + try await Version(package: pkg, latest: .defaultBranch).save(on: app.db) + let collection = CustomCollection(id: .id1, .init(key: "list", + name: "List", + url: "https://github.com/foo/bar/list.json")) + try await collection.save(on: app.db) + try await collection.$packages.attach([pkg], on: app.db) + + // MUT + let page = try await CustomCollectionsController.query(on: app.db, + key: "list", + page: 1, + pageSize: 10) + + // validation + XCTAssertEqual(page.results.map(\.repository.name), ["1"]) + XCTAssertEqual(page.hasMoreResults, false) + } + +} From 09feefb1a7b9a1f357951090184ee0beb7696654 Mon Sep 17 00:00:00 2001 From: "Sven A. Schmidt" Date: Mon, 20 Jan 2025 14:03:39 +0100 Subject: [PATCH 2/5] Add test_query_pagination --- .../CustomCollectionControllerTests.swift | 82 +++++++++++++++++-- 1 file changed, 73 insertions(+), 9 deletions(-) diff --git a/Tests/AppTests/CustomCollectionControllerTests.swift b/Tests/AppTests/CustomCollectionControllerTests.swift index 842536c91..745cd1e86 100644 --- a/Tests/AppTests/CustomCollectionControllerTests.swift +++ b/Tests/AppTests/CustomCollectionControllerTests.swift @@ -17,6 +17,7 @@ import XCTest @testable import App import Dependencies +import Fluent import Vapor @@ -24,15 +25,13 @@ class CustomCollectionControllerTests: AppTestCase { func test_query() async throws { // setup - let pkg = Package(id: .id0, url: "1".asGithubUrl.url) - try await pkg.save(on: app.db) - try await Repository(package: pkg, name: "1", owner: "owner").save(on: app.db) - try await Version(package: pkg, latest: .defaultBranch).save(on: app.db) - let collection = CustomCollection(id: .id1, .init(key: "list", - name: "List", - url: "https://github.com/foo/bar/list.json")) - try await collection.save(on: app.db) - try await collection.$packages.attach([pkg], on: app.db) + try await CustomCollection.save( + on: app.db, + key: "list", + name: "List", + url: "https://github.com/foo/bar/list.json", + packages: [( id: .id0, url: "https://github.com/foo/1", owner: "foo", name: "1" )] + ) // MUT let page = try await CustomCollectionsController.query(on: app.db, @@ -45,4 +44,69 @@ class CustomCollectionControllerTests: AppTestCase { XCTAssertEqual(page.hasMoreResults, false) } + func test_query_pagination() async throws { + // setup + let pkgInfo = [UUID.id0, .id1, .id2, .id3, .id4].enumerated().shuffled().map { (idx, id) in + (id, URL(string: "https://github.com/foo/\(idx)")!, "foo", "\(idx)") + } + try await CustomCollection.save( + on: app.db, + key: "list", + name: "List", + url: "https://github.com/foo/bar/list.json", + packages: pkgInfo + ) + + do { // first page + // MUT + let page = try await CustomCollectionsController.query(on: app.db, + key: "list", + page: 1, + pageSize: 2) + // validate + XCTAssertEqual(page.results.map(\.repository.name), ["0", "1"]) + XCTAssertEqual(page.hasMoreResults, true) + } + + do { // second page + // MUT + let page = try await CustomCollectionsController.query(on: app.db, + key: "list", + page: 2, + pageSize: 2) + // validate + XCTAssertEqual(page.results.map(\.repository.name), ["2", "3"]) + XCTAssertEqual(page.hasMoreResults, true) + } + + do { // first page + // MUT + let page = try await CustomCollectionsController.query(on: app.db, + key: "list", + page: 3, + pageSize: 2) + // validate + XCTAssertEqual(page.results.map(\.repository.name), ["4"]) + XCTAssertEqual(page.hasMoreResults, false) + } + } + +} + + +private extension CustomCollection { + @discardableResult + static func save(on database: Database, key: String, name: String, url: URL, packages: [(id: Package.Id, url: URL, owner: String, name: String)]) async throws -> CustomCollection { + let packages = try await packages.mapAsync { + let pkg = Package(id: $0.id, url: $0.url) + try await pkg.save(on: database) + try await Repository(package: pkg, name: $0.name, owner: $0.owner).save(on: database) + try await Version(package: pkg, latest: .defaultBranch).save(on: database) + return pkg + } + let collection = CustomCollection(id: .id1, .init(key: key, name: name, url: url)) + try await collection.save(on: database) + try await collection.$packages.attach(packages, on: database) + return collection + } } From 62076f859901fdabe274f2c1145b6b06c54047ff Mon Sep 17 00:00:00 2001 From: "Sven A. Schmidt" Date: Mon, 20 Jan 2025 14:08:48 +0100 Subject: [PATCH 3/5] Add test_show_collection (fails without fix from #3619) --- .../CustomCollectionControllerTests.swift | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Tests/AppTests/CustomCollectionControllerTests.swift b/Tests/AppTests/CustomCollectionControllerTests.swift index 745cd1e86..77dee0984 100644 --- a/Tests/AppTests/CustomCollectionControllerTests.swift +++ b/Tests/AppTests/CustomCollectionControllerTests.swift @@ -91,6 +91,26 @@ class CustomCollectionControllerTests: AppTestCase { } } + func test_show_collection() async throws { + try await withDependencies { + $0.environment.dbId = { nil } + } operation: { + try await CustomCollection.save( + on: app.db, + key: "list", + name: "List", + url: "https://github.com/foo/bar/list.json", + packages: [( id: .id0, url: "https://github.com/foo/1", owner: "foo", name: "1" )] + ) + + // MUT + try await app.test(.GET, "/collections/list") { req async in + // validate + XCTAssertEqual(req.status, .ok) + } + } + } + } From 551bd2deb21f57e71c052a0c242e1895761b80e7 Mon Sep 17 00:00:00 2001 From: "Sven A. Schmidt" Date: Mon, 20 Jan 2025 14:10:01 +0100 Subject: [PATCH 4/5] Add test_not_found --- Tests/AppTests/CustomCollectionControllerTests.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Tests/AppTests/CustomCollectionControllerTests.swift b/Tests/AppTests/CustomCollectionControllerTests.swift index 77dee0984..dc5c1b02f 100644 --- a/Tests/AppTests/CustomCollectionControllerTests.swift +++ b/Tests/AppTests/CustomCollectionControllerTests.swift @@ -102,7 +102,7 @@ class CustomCollectionControllerTests: AppTestCase { url: "https://github.com/foo/bar/list.json", packages: [( id: .id0, url: "https://github.com/foo/1", owner: "foo", name: "1" )] ) - + // MUT try await app.test(.GET, "/collections/list") { req async in // validate @@ -111,6 +111,16 @@ class CustomCollectionControllerTests: AppTestCase { } } + func test_not_found() throws { + try withDependencies { + $0.environment.dbId = { nil } + } operation: { + try app.test(.GET, "/collections/list") { + XCTAssertEqual($0.status, .notFound) + } + } + } + } From a720caa30b2930e6eba492e4e7971e228e6f3cd7 Mon Sep 17 00:00:00 2001 From: "Sven A. Schmidt" Date: Mon, 20 Jan 2025 14:14:35 +0100 Subject: [PATCH 5/5] Typo --- Tests/AppTests/CustomCollectionControllerTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/AppTests/CustomCollectionControllerTests.swift b/Tests/AppTests/CustomCollectionControllerTests.swift index dc5c1b02f..4d0baed67 100644 --- a/Tests/AppTests/CustomCollectionControllerTests.swift +++ b/Tests/AppTests/CustomCollectionControllerTests.swift @@ -79,7 +79,7 @@ class CustomCollectionControllerTests: AppTestCase { XCTAssertEqual(page.hasMoreResults, true) } - do { // first page + do { // third page // MUT let page = try await CustomCollectionsController.query(on: app.db, key: "list",