From 8b0fbc97102ce70e79e6ae7a5d2245279ef3a138 Mon Sep 17 00:00:00 2001 From: Francesco Paolo Severino <96546612+fpseverino@users.noreply.github.com> Date: Sun, 13 Oct 2024 18:47:24 +0200 Subject: [PATCH] Improve testing and CI (#5) * Add CODEOWNERS file * Add more test coverage --- .github/CODEOWNERS | 1 + .gitignore | 3 + Sources/AppleMapsKit/AppleMapsKitError.swift | 2 +- .../AppleMapsKitTests/AppleMapsKitTests.swift | 249 +++++++++++------- 4 files changed, 155 insertions(+), 100 deletions(-) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..2f6e4e0 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @fpseverino \ No newline at end of file diff --git a/.gitignore b/.gitignore index 47bd4c1..da2e556 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ DerivedData/ .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc Package.resolved +.env +.env.* +!.env.example diff --git a/Sources/AppleMapsKit/AppleMapsKitError.swift b/Sources/AppleMapsKit/AppleMapsKitError.swift index e7a8999..c83f0a4 100644 --- a/Sources/AppleMapsKit/AppleMapsKitError.swift +++ b/Sources/AppleMapsKit/AppleMapsKitError.swift @@ -46,6 +46,6 @@ public struct AppleMapsKitError: Error, Sendable { extension AppleMapsKitError: CustomStringConvertible { public var description: String { - "AppleMapsKitError(errorType: \(self.errorType)" + "AppleMapsKitError(errorType: \(self.errorType))" } } diff --git a/Tests/AppleMapsKitTests/AppleMapsKitTests.swift b/Tests/AppleMapsKitTests/AppleMapsKitTests.swift index a450bf3..c276c11 100644 --- a/Tests/AppleMapsKitTests/AppleMapsKitTests.swift +++ b/Tests/AppleMapsKitTests/AppleMapsKitTests.swift @@ -3,9 +3,11 @@ import AsyncHTTPClient import Foundation import Testing -@Suite("AppleMapsKit Tests", .disabled(if: true, "Needs valid credentials")) +@Suite("AppleMapsKit Tests") struct AppleMapsKitTests { var client: AppleMapsClient + // TODO: Replace with `false` when you have valid credentials. + var credentialsAreInvalid = true init() async throws { // TODO: Replace the following values with valid ones. @@ -37,21 +39,29 @@ struct AppleMapsKitTests { ) ) func geocode(searchLocation: (latitude: Double, longitude: Double)?, searchRegion: MapRegion?) async throws { - let places = try await client.geocode( - address: "1 Apple Park, Cupertino, CA", - limitToCountries: ["US", "CA"], - lang: "en-US", - searchLocation: searchLocation, - searchRegion: searchRegion, - userLocation: (latitude: 37.78, longitude: -122.42) - ) - #expect(!places.isEmpty) + try await withKnownIssue { + let places = try await client.geocode( + address: "1 Apple Park, Cupertino, CA", + limitToCountries: ["US", "CA"], + lang: "en-US", + searchLocation: searchLocation, + searchRegion: searchRegion, + userLocation: (latitude: 37.78, longitude: -122.42) + ) + #expect(!places.isEmpty) + } when: { + credentialsAreInvalid + } } @Test("Reverse geocode") func reverseGeocode() async throws { - let places = try await client.reverseGeocode(latitude: 37.33182, longitude: -122.03118, lang: "en-US") - #expect(!places.isEmpty) + try await withKnownIssue { + let places = try await client.reverseGeocode(latitude: 37.33182, longitude: -122.03118, lang: "en-US") + #expect(!places.isEmpty) + } when: { + credentialsAreInvalid + } } @Test( @@ -68,23 +78,27 @@ struct AppleMapsKitTests { ) ) func search(searchLocation: (latitude: Double, longitude: Double)?, searchRegion: MapRegion?) async throws { - let searchResponse = try await client.search( - for: "eiffel tower", - excludePoiCategories: [.airport], - includePoiCategories: [.landmark], - limitToCountries: ["US", "CA"], - resultTypeFilter: [.pointOfInterest, .physicalFeature, .poi, .address], - lang: "en-US", - searchLocation: searchLocation, - searchRegion: searchRegion, - userLocation: (latitude: 37.78, longitude: -122.42), - searchRegionPriority: .default, - enablePagination: false, - includeAddressCategories: [.postalCode], - excludeAddressCategories: [.administrativeArea] - ) - let results = try #require(searchResponse.results) - #expect(!results.isEmpty) + try await withKnownIssue { + let searchResponse = try await client.search( + for: "eiffel tower", + excludePoiCategories: [.airport], + includePoiCategories: [.landmark], + limitToCountries: ["US", "CA"], + resultTypeFilter: [.pointOfInterest, .physicalFeature, .poi, .address], + lang: "en-US", + searchLocation: searchLocation, + searchRegion: searchRegion, + userLocation: (latitude: 37.78, longitude: -122.42), + searchRegionPriority: .default, + enablePagination: false, + includeAddressCategories: [.postalCode], + excludeAddressCategories: [.administrativeArea] + ) + let results = try #require(searchResponse.results) + #expect(!results.isEmpty) + } when: { + credentialsAreInvalid + } } @Test( @@ -101,21 +115,25 @@ struct AppleMapsKitTests { ) ) func searchAutoComplete(searchLocation: (latitude: Double, longitude: Double)?, searchRegion: MapRegion?) async throws { - let results = try await client.searchAutoComplete( - for: "eiffel", - excludePoiCategories: [.airport], - includePoiCategories: [.landmark], - limitToCountries: ["US", "CA"], - resultTypeFilter: [.pointOfInterest, .physicalFeature, .poi, .address, .query], - lang: "en-US", - searchLocation: searchLocation, - searchRegion: searchRegion, - userLocation: (latitude: 37.78, longitude: -122.42), - searchRegionPriority: .default, - includeAddressCategories: [.postalCode], - excludeAddressCategories: [.administrativeArea] - ) - #expect(!results.isEmpty) + try await withKnownIssue { + let results = try await client.searchAutoComplete( + for: "eiffel", + excludePoiCategories: [.airport], + includePoiCategories: [.landmark], + limitToCountries: ["US", "CA"], + resultTypeFilter: [.pointOfInterest, .physicalFeature, .poi, .address, .query], + lang: "en-US", + searchLocation: searchLocation, + searchRegion: searchRegion, + userLocation: (latitude: 37.78, longitude: -122.42), + searchRegionPriority: .default, + includeAddressCategories: [.postalCode], + excludeAddressCategories: [.administrativeArea] + ) + #expect(!results.isEmpty) + } when: { + credentialsAreInvalid + } } @Test( @@ -132,78 +150,111 @@ struct AppleMapsKitTests { ) ) func directions(searchLocation: (latitude: Double, longitude: Double)?, searchRegion: MapRegion?) async throws { - let arrivalDirections = try await client.directions( - from: "37.7857,-122.4011", - to: "San Francisco City Hall, CA", - arrivalDate: Date(timeIntervalSinceNow: 3600), - avoid: [.tolls], - lang: "en-US", - requestsAlternateRoutes: true, - searchLocation: searchLocation, - searchRegion: searchRegion, - transportType: .walking, - userLocation: (latitude: 37.78, longitude: -122.42) - ) - let arrivalRoutes = try #require(arrivalDirections.routes) - #expect(!arrivalRoutes.isEmpty) + try await withKnownIssue { + let arrivalDirections = try await client.directions( + from: "37.7857,-122.4011", + to: "San Francisco City Hall, CA", + arrivalDate: Date(timeIntervalSinceNow: 3600), + avoid: [.tolls], + lang: "en-US", + requestsAlternateRoutes: true, + searchLocation: searchLocation, + searchRegion: searchRegion, + transportType: .walking, + userLocation: (latitude: 37.78, longitude: -122.42) + ) + let arrivalRoutes = try #require(arrivalDirections.routes) + #expect(!arrivalRoutes.isEmpty) + } when: { + credentialsAreInvalid + } - let departureDirections = try await client.directions( - from: "37.7857,-122.4011", - to: "San Francisco City Hall, CA", - avoid: [.tolls], - departureDate: Date(timeIntervalSinceNow: 3600), - lang: "en-US", - requestsAlternateRoutes: true, - searchLocation: searchLocation, - searchRegion: searchRegion, - transportType: .walking, - userLocation: (latitude: 37.78, longitude: -122.42) - ) - let departureRoutes = try #require(departureDirections.routes) - #expect(!departureRoutes.isEmpty) + try await withKnownIssue { + let departureDirections = try await client.directions( + from: "37.7857,-122.4011", + to: "San Francisco City Hall, CA", + avoid: [.tolls], + departureDate: Date(timeIntervalSinceNow: 3600), + lang: "en-US", + requestsAlternateRoutes: true, + searchLocation: searchLocation, + searchRegion: searchRegion, + transportType: .walking, + userLocation: (latitude: 37.78, longitude: -122.42) + ) + let departureRoutes = try #require(departureDirections.routes) + #expect(!departureRoutes.isEmpty) + } when: { + credentialsAreInvalid + } } @Test("ETA", arguments: zip([Date(timeIntervalSinceNow: 3600), nil], [nil, Date(timeIntervalSinceNow: 3600)])) func eta(arrivalDate: Date?, departureDate: Date?) async throws { - let etas = try await client.eta( - from: (latitude: 37.331423, longitude: -122.030503), - to: [ - (latitude: 37.32556561130194, longitude: -121.94635203581443), - (latitude: 37.44176585512703, longitude: -122.17259315798667), - ], - transportType: .transit, - departureDate: departureDate, - arrivalDate: arrivalDate - ) - #expect(!etas.isEmpty) + try await withKnownIssue { + let etas = try await client.eta( + from: (latitude: 37.331423, longitude: -122.030503), + to: [ + (latitude: 37.32556561130194, longitude: -121.94635203581443), + (latitude: 37.44176585512703, longitude: -122.17259315798667), + ], + transportType: .transit, + departureDate: departureDate, + arrivalDate: arrivalDate + ) + #expect(!etas.isEmpty) + } when: { + credentialsAreInvalid + } } @Test("ETA between addresses", arguments: zip([Date(timeIntervalSinceNow: 3600), nil], [nil, Date(timeIntervalSinceNow: 3600)])) func etaBetweenAddresses(arrivalDate: Date?, departureDate: Date?) async throws { - let etas = try await client.etaBetweenAddresses( - from: "San Francisco City Hall, CA", - to: ["Golden Gate Park, San Francisco"], - transportType: .transit, - departureDate: departureDate, - arrivalDate: arrivalDate - ) - #expect(!etas.isEmpty) + try await withKnownIssue { + let etas = try await client.etaBetweenAddresses( + from: "San Francisco City Hall, CA", + to: ["Golden Gate Park, San Francisco"], + transportType: .transit, + departureDate: departureDate, + arrivalDate: arrivalDate + ) + #expect(!etas.isEmpty) + } when: { + credentialsAreInvalid + } } @Test("Place") func place() async throws { - let place = try await client.place(id: "I7C250D2CDCB364A", lang: "en-US") - #expect(place != nil) + try await withKnownIssue { + let place = try await client.place(id: "I7C250D2CDCB364A", lang: "en-US") + #expect(place != nil) + } when: { + credentialsAreInvalid + } } @Test("Places") func places() async throws { - let placesResponse = try await client.places(ids: ["ICFA2FAE5487B94AF", "IA6FD1E86A544F69D"], lang: "en-US") - let errors = try #require(placesResponse.errors) - #expect(!errors.isEmpty) + try await withKnownIssue { + let placesResponse = try await client.places(ids: ["ICFA2FAE5487B94AF", "IA6FD1E86A544F69D"], lang: "en-US") + let errors = try #require(placesResponse.errors) + #expect(!errors.isEmpty) + } when: { + credentialsAreInvalid + } } @Test("Alternate Place IDs") func alternatePlaceIDs() async throws { - let alternateIDsResponse = try await client.alternatePlaceIDs(ids: ["I7C250D2CDCB364A", "ICFA2FAE5487B94AF"]) - let results = try #require(alternateIDsResponse.results) - #expect(!results.isEmpty) + try await withKnownIssue { + let alternateIDsResponse = try await client.alternatePlaceIDs(ids: ["I7C250D2CDCB364A", "ICFA2FAE5487B94AF"]) + let results = try #require(alternateIDsResponse.results) + #expect(!results.isEmpty) + } when: { + credentialsAreInvalid + } + } + + @Test("AppleMapsKitError") func appleMapsKitError() { + #expect(AppleMapsKitError.noPlacesFound.description == "AppleMapsKitError(errorType: noPlacesFound)") + #expect(AppleMapsKitError.invalidSearchResultType.description == "AppleMapsKitError(errorType: invalidSearchResultType)") } }