Skip to content

Commit

Permalink
Start implementing AutofillMockResponse for integration tests with Au…
Browse files Browse the repository at this point in the history
…tofill

- Add static MockResponse.apiType and associated type Mock in MockServerIntegrationTestCase so that subclasses can query Mock.apiType when instantiating SearchEngines based on the generic.
	- Also provides the MockResponse parameter generic type
- Add extension on CoreSearchEngine.ApiType to bridge to MapboxSearch.ApiType enum (does not contain Autofill or search-box)
- Replace hard-coded apiType: .geocoding parameters with XCTUnwrap(apiType: Mock.coreApiType.toSDKType())
- Add AutofillMockResponse with only endpoints used by the Autofill engine and tests!
  • Loading branch information
aokj4ck committed Feb 14, 2024
1 parent b508e71 commit e992fa1
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 12 deletions.
8 changes: 8 additions & 0 deletions MapboxSearch.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
042477C62B72CCB000D870D5 /* geocoding-reverse-geocoding.json in Resources */ = {isa = PBXBuildFile; fileRef = 042477C42B72CCB000D870D5 /* geocoding-reverse-geocoding.json */; };
042477C72B72CCB000D870D5 /* geocoding-reverse-geocoding.json in Resources */ = {isa = PBXBuildFile; fileRef = 042477C42B72CCB000D870D5 /* geocoding-reverse-geocoding.json */; };
043A3D4D2B30F38300DB681B /* CoreAddress+AddressComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 043A3D4C2B30F38300DB681B /* CoreAddress+AddressComponents.swift */; };
045514C22B7D4B58000D88B9 /* CoreApiType+ToSDKType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 045514C12B7D4B58000D88B9 /* CoreApiType+ToSDKType.swift */; };
045514C32B7D4B58000D88B9 /* CoreApiType+ToSDKType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 045514C12B7D4B58000D88B9 /* CoreApiType+ToSDKType.swift */; };
045514C42B7D4B58000D88B9 /* CoreApiType+ToSDKType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 045514C12B7D4B58000D88B9 /* CoreApiType+ToSDKType.swift */; };
04970F8D2B7A97C900213763 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 04970F8C2B7A97C900213763 /* PrivacyInfo.xcprivacy */; };
04AB0B4B2B6AADB700FDE7D5 /* mapbox.places.san.francisco.json in Resources */ = {isa = PBXBuildFile; fileRef = 04AB0B4A2B6AADB700FDE7D5 /* mapbox.places.san.francisco.json */; };
04AB0B4C2B6AADB700FDE7D5 /* mapbox.places.san.francisco.json in Resources */ = {isa = PBXBuildFile; fileRef = 04AB0B4A2B6AADB700FDE7D5 /* mapbox.places.san.francisco.json */; };
Expand Down Expand Up @@ -494,6 +497,7 @@
042477C12B7290E700D870D5 /* SearchEngineGeocodingIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchEngineGeocodingIntegrationTests.swift; sourceTree = "<group>"; };
042477C42B72CCB000D870D5 /* geocoding-reverse-geocoding.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "geocoding-reverse-geocoding.json"; sourceTree = "<group>"; };
043A3D4C2B30F38300DB681B /* CoreAddress+AddressComponents.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoreAddress+AddressComponents.swift"; sourceTree = "<group>"; };
045514C12B7D4B58000D88B9 /* CoreApiType+ToSDKType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoreApiType+ToSDKType.swift"; sourceTree = "<group>"; };
04970F8C2B7A97C900213763 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
04AB0B4A2B6AADB700FDE7D5 /* mapbox.places.san.francisco.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = mapbox.places.san.francisco.json; sourceTree = "<group>"; };
04BBC6332B61898F00E24E99 /* LocalhostMockServiceProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalhostMockServiceProvider.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1451,6 +1455,7 @@
children = (
FEEDD3882508E24900DC0A98 /* LotLan+Extensions.swift */,
F970FAD7252B14950000806B /* SearchResultType+Extensions.swift */,
045514C12B7D4B58000D88B9 /* CoreApiType+ToSDKType.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -2604,6 +2609,7 @@
F953A49325ECEB60003A3681 /* SearchCategorySuggestion+Samples.swift in Sources */,
F9F882962583779A00062A82 /* CoreImageInfoStub.swift in Sources */,
FEA92E712564149100427965 /* CoreSuggestAction+Samples.swift in Sources */,
045514C32B7D4B58000D88B9 /* CoreApiType+ToSDKType.swift in Sources */,
2CA1E22129F09CD200A533CF /* PlaceAutocomplete.Suggestion+Tests.swift in Sources */,
FED7E1A825517C5100DA34D9 /* CoreBoundingBoxTests.swift in Sources */,
F9D70E5C252740DD0019105F /* IndexableDataProviderTests.swift in Sources */,
Expand Down Expand Up @@ -2652,6 +2658,7 @@
F9201ECA253EECE0002D141B /* CategorySuggestionsIntegrationTestCase.swift in Sources */,
F907443B261B13F40091899C /* MockServerUITestCase.swift in Sources */,
F9ACA6132642B66600F50CD4 /* MockWebServer.swift in Sources */,
045514C42B7D4B58000D88B9 /* CoreApiType+ToSDKType.swift in Sources */,
FEEDD3D22508E45B00DC0A98 /* VisibilityTestCase.swift in Sources */,
F9AB03C3261DBD7F00EDF1E7 /* MockResponse.swift in Sources */,
FEEDD3CE2508E45B00DC0A98 /* BaseTestCase.swift in Sources */,
Expand All @@ -2667,6 +2674,7 @@
files = (
F9C557B62670CC4500BE8B94 /* SearchEngineDelegateStub.swift in Sources */,
F9C9461B2743CB1700763F2C /* TestTileStore.swift in Sources */,
045514C22B7D4B58000D88B9 /* CoreApiType+ToSDKType.swift in Sources */,
F9C557C12670CD8C00BE8B94 /* CoreSearchOptions+Samples.swift in Sources */,
1420F31C29A2800300D4A511 /* CoreSearchResultStub+Samples.swift in Sources */,
F9C557B72670CC5600BE8B94 /* CoreSearchResultStub.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import CoreLocation
@testable import MapboxSearch
import XCTest

final class AddressAutofillIntegrationTests: MockServerIntegrationTestCase<LegacyResponse> {
final class AddressAutofillIntegrationTests: MockServerIntegrationTestCase<AutofillMockResponse> {
private var addressAutofill: AddressAutofill!
private let locationProvider = WrapperLocationProvider(wrapping: DefaultLocationProvider())

Expand All @@ -18,7 +18,7 @@ final class AddressAutofillIntegrationTests: MockServerIntegrationTestCase<Legac
)

let engine = LocalhostMockServiceProvider.shared.createEngine(
apiType: CoreSearchEngine.ApiType.autofill,
apiType: Mock.coreApiType,
accessToken: "access-token",
locationProvider: locationProvider
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import XCTest
final class CategorySearchEngineIntegrationTests: MockServerIntegrationTestCase<LegacyResponse> {
private var searchEngine: CategorySearchEngine!

override func setUp() {
super.setUp()
override func setUpWithError() throws {
try super.setUpWithError()

let apiType = try XCTUnwrap(Mock.coreApiType.toSDKType())
searchEngine = CategorySearchEngine(
accessToken: "access-token",
serviceProvider: LocalhostMockServiceProvider.shared,
apiType: .SBS
apiType: apiType
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import CoreLocation
import XCTest

class MockServerIntegrationTestCase<Mock: MockResponse>: XCTestCase {
typealias Mock = Mock
let server = MockWebServer<Mock>()

func setServerResponse(_ response: Mock, query: String? = nil) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class PlaceAutocompleteIntegrationTests: MockServerIntegrationTestCase<Leg
)

let engine = LocalhostMockServiceProvider.shared.createEngine(
apiType: CoreSearchEngine.ApiType.SBS,
apiType: Mock.coreApiType,
accessToken: "access-token",
locationProvider: WrapperLocationProvider(wrapping: DefaultLocationProvider())
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ class SearchEngineGeocodingIntegrationTests: MockServerIntegrationTestCase<Legac
let delegate = SearchEngineDelegateStub()
var searchEngine: SearchEngine!

override func setUp() {
super.setUp()
override func setUpWithError() throws {
try super.setUpWithError()

let apiType = try XCTUnwrap(Mock.coreApiType.toSDKType())
searchEngine = SearchEngine(
accessToken: "access-token",
serviceProvider: LocalhostMockServiceProvider.shared,
locationProvider: DefaultLocationProvider(),
apiType: .geocoding
apiType: apiType
)

searchEngine.delegate = delegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ class SearchEngineIntegrationTests: MockServerIntegrationTestCase<LegacyResponse
let delegate = SearchEngineDelegateStub()
var searchEngine: SearchEngine!

override func setUp() {
super.setUp()
override func setUpWithError() throws {
try super.setUpWithError()

let apiType = try XCTUnwrap(Mock.coreApiType.toSDKType())
searchEngine = SearchEngine(
accessToken: "access-token",
serviceProvider: LocalhostMockServiceProvider.shared,
locationProvider: DefaultLocationProvider(),
apiType: .SBS
apiType: apiType
)

searchEngine.delegate = delegate
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@testable import MapboxSearch

extension CoreSearchEngine.ApiType {
func toSDKType() -> ApiType? {
switch self {
case .geocoding:
return .geocoding
case .SBS:
return .SBS
case .autofill,
.searchBox:
return nil
@unknown default:
fatalError()
}
}
}
52 changes: 52 additions & 0 deletions Tests/MapboxSearchUITests/MockWebServer/MockResponse.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Foundation
@_implementationOnly import MapboxCoreSearch
@testable import MapboxSearch
import Swifter
import XCTest

protocol MockResponse {
/// Resource from test bundle containing response JSON.
Expand All @@ -10,6 +13,10 @@ protocol MockResponse {

/// URL path for this request.
var path: String { get }

/// The API type for this mock.
/// Uses Core types to support address autofill.
static var coreApiType: CoreSearchEngine.ApiType { get }
}

enum LegacyResponse: MockResponse {
Expand Down Expand Up @@ -154,4 +161,49 @@ enum LegacyResponse: MockResponse {
return .post
}
}

static var coreApiType: CoreSearchEngine.ApiType {
.geocoding
}
}

enum AutofillMockResponse: MockResponse {
case suggestAddressSanFrancisco
case retrieveAddressSanFrancisco

var filepath: String {
let bundle = Bundle(for: MockWebServer<Self>.self)
switch self {
case .retrieveAddressSanFrancisco:
return bundle.path(forResource: "address-retrieve-san-francisco", ofType: "json")!
case .suggestAddressSanFrancisco:
return bundle.path(forResource: "address-suggestions-san-francisco", ofType: "json")!
}
}

var path: String {
var path = "/search/v1"

switch self {
case .suggestAddressSanFrancisco:
path = "/autofill/v1/suggest/:query"

case .retrieveAddressSanFrancisco:
path = "/autofill/v1/retrieve/:action.id"
}

return path
}

var httpMethod: HttpServer.HTTPMethod {
switch self {
case .suggestAddressSanFrancisco,
.retrieveAddressSanFrancisco:
return .get
}
}

static var coreApiType: CoreSearchEngine.ApiType {
.autofill
}
}

0 comments on commit e992fa1

Please sign in to comment.