Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to remove reliance on UserDefaults #156

Merged
merged 16 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Guide: https://keepachangelog.com/en/1.0.0/

<!-- Add changes for active work here -->

- [Unit Tests] Update and correct tests for iOS 17 using all mocked data.
- [UI Tests] Update and correct tests for iOS 17 using all mocked data.
- [Search] Rename `SearchEngine.reverseGeocoding` function to `SearchEngine.reverse`.
- [Core] Stop reading "MapboxAPIBaseURL" from UserDefaults in `ServiceProvider.createEngine`. (Providing a value in Info.plist is still supported).
- [Core] Change AbstractSearchEngine.init `supportSBS: Bool = false` parameter to `apiType: ApiType = .SBS`. This changes the default API engine for discover/category and other API requests to SBS. Add ApiType enum to represent non-Autofill and non-PlaceAutocomplete SearchEngine API types.

## 2.0.0-rc.1
Expand Down
58 changes: 42 additions & 16 deletions MapboxSearch.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Sources/Demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
if ProcessInfo.processInfo.arguments.contains("--uitesting") {
ServiceProvider.shared.localFavoritesProvider.deleteAll()
ServiceProvider.shared.localHistoryProvider.deleteAll()
UserDefaults.resetStandardUserDefaults()
}

return true
Expand Down
21 changes: 20 additions & 1 deletion Sources/MapboxSearch/PublicAPI/Engine/SearchEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ extension SearchEngine {
/// - Parameters:
/// - options: Options with coordinates, mode, limits and query types for reverse geocoding.
/// - completion: completion handler with either reverse geocoding Resuts or Error.
public func reverseGeocoding(
public func reverse(
options: ReverseGeocodingOptions,
completion: @escaping (Result<[SearchResult], SearchError>) -> Void
) {
Expand Down Expand Up @@ -490,6 +490,25 @@ extension SearchEngine {
}
}
}

// MARK: - Deprecated functions

/// Reverse geocoding of coordinates to addresses.
/// The default behavior in reverse geocoding is to return at most one feature at each of the multiple levels of the
/// administrative hierarchy (for example, one address, one region, one country).
/// Increasing the limit allows returning multiple features of the same type, but only for one type (for example,
/// multiple address results).
/// Consequently, setting limit to a higher-than-default value requires specifying exactly one types parameter.
/// - Parameters:
/// - options: Options with coordinates, mode, limits and query types for reverse geocoding.
/// - completion: completion handler with either reverse geocoding Resuts or Error.
@available(*, deprecated, renamed: "reverse")
aokj4ck marked this conversation as resolved.
Show resolved Hide resolved
public func reverseGeocoding(
options: ReverseGeocodingOptions,
completion: @escaping (Result<[SearchResult], SearchError>) -> Void
) {
reverse(options: options, completion: completion)
}
}

// MARK: - IndexableDataResolver
Expand Down
21 changes: 13 additions & 8 deletions Sources/MapboxSearch/PublicAPI/ServiceProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ public typealias FavoritesProvider = LocalDataProvider<FavoriteRecord>
/// Built-in local data provider for ``HistoryRecord`` data.
public typealias HistoryProvider = LocalDataProvider<HistoryRecord>

// MARK: - ServiceProvider

/// Services provider for SearchEngine
public class ServiceProvider: ServiceProviderProtocol {
/// Customize API host URL
/// Customize API host URL with a value from the Info.plist
/// Also supports reading a process argument when in non-Release UITest builds
public static var customBaseURL: String? {
Bundle.main.object(forInfoDictionaryKey: baseURLPlistKey) as? String
#if !RELEASE
if ProcessInfo.processInfo.arguments.contains(where: { $0 == "--uitesting" }) {
let testingBaseUrl = ProcessInfo.processInfo.environment["search_endpoint"]
return testingBaseUrl
}
#endif

return Bundle.main.object(forInfoDictionaryKey: baseURLPlistKey) as? String
}

/// LocalDataProvider for favorites records
Expand Down Expand Up @@ -60,15 +70,10 @@ extension ServiceProvider: EngineProviderProtocol {
accessToken: String,
locationProvider: CoreLocationProvider?
) -> CoreSearchEngineProtocol {
// UserDefaults can be used to setup base url in runtime (e.g. UI tests)
// UserDefaults can be used to setup base url in runtime (e.g. UI tests)
let defaultsBaseURL = UserDefaults.standard.value(forKey: baseURLPlistKey) as? String
let bundleBaseURL = Bundle.main.object(forInfoDictionaryKey: baseURLPlistKey) as? String

MapboxOptions.accessToken = accessToken

let engineOptions = CoreSearchEngine.Options(
baseUrl: bundleBaseURL ?? defaultsBaseURL,
baseUrl: Self.customBaseURL,
apiType: NSNumber(value: apiType.rawValue),
sdkInformation: SdkInformation.defaultInfo,
eventsUrl: nil
Expand Down
2 changes: 1 addition & 1 deletion Sources/MapboxSearchUI/MapboxSearchController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ extension MapboxSearchController: SearchCategoriesRootViewDelegate {
case .success(let items):
self.delegate?.categorySearchResultsReceived(category: category, results: items)
case .failure(let searchError):
print("Failed search; error=\(searchError)")
print("Failed search; error=\(searchError)") // swiftlint:disable:this dont_use_print
aokj4ck marked this conversation as resolved.
Show resolved Hide resolved
self.presentSearchError(searchError)
}
self.mapboxPanelController?.setState(.collapsed, animated: true)
Expand Down
3 changes: 0 additions & 3 deletions Tests/CI-dev.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"testTargets" : [
{
"skippedTests" : [
"MockServerTestCase",
"OfflineIntegrationTests"
],
"target" : {
Expand All @@ -43,7 +42,6 @@
},
{
"skippedTests" : [
"MockServerTestCase"
],
"target" : {
"containerPath" : "container:MapboxSearch.xcodeproj",
Expand All @@ -55,7 +53,6 @@
"enabled" : false,
"skippedTests" : [
"BaseTestCase",
"MockServerTestCase"
],
"target" : {
"containerPath" : "container:MapboxSearch.xcodeproj",
Expand Down
7 changes: 2 additions & 5 deletions Tests/Demo.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"testTargets" : [
{
"skippedTests" : [
"MockServerTestCase"
"MockServerIntegrationTestCase"
],
"target" : {
"containerPath" : "container:MapboxSearch.xcodeproj",
Expand All @@ -44,9 +44,6 @@
}
},
{
"skippedTests" : [
"MockServerTestCase"
],
"target" : {
"containerPath" : "container:MapboxSearch.xcodeproj",
"identifier" : "3A0D7E54233522D4006D81BB",
Expand All @@ -56,7 +53,7 @@
{
"skippedTests" : [
"BaseTestCase",
"MockServerTestCase"
"MockServerUITestCase"
],
"target" : {
"containerPath" : "container:MapboxSearch.xcodeproj",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@ import CoreLocation
@testable import MapboxSearch
import XCTest

final class AddressAutofillIntegrationTests: MockServerTestCase {
final class AddressAutofillIntegrationTests: MockServerIntegrationTestCase {
private var addressAutofill: AddressAutofill!
private let locationProvider = WrapperLocationProvider(wrapping: DefaultLocationProvider())

override func setUp() {
super.setUp()

addressAutofill = AddressAutofill(
let reporter = CoreUserActivityReporter.getOrCreate(
for: CoreUserActivityReporterOptions(
sdkInformation:
SdkInformation.defaultInfo,
eventsUrl: nil
)
)

let engine = LocalhostMockServiceProvider.shared.createEngine(
apiType: CoreSearchEngine.ApiType.autofill,
accessToken: "access-token",
locationProvider: DefaultLocationProvider()
locationProvider: locationProvider
)

addressAutofill = AddressAutofill(
searchEngine: engine,
userActivityReporter: reporter
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ import CoreLocation
@testable import MapboxSearch
import XCTest

class CategorySearchEngineIntegrationTests: MockServerTestCase {
lazy var searchEngine = CategorySearchEngine(
accessToken: "access-token",
locationProvider: DefaultLocationProvider(),
apiType: .SBS
)
final class CategorySearchEngineIntegrationTests: MockServerIntegrationTestCase {
private var searchEngine: CategorySearchEngine!

override func setUp() {
super.setUp()

searchEngine = CategorySearchEngine(
accessToken: "access-token",
serviceProvider: LocalhostMockServiceProvider.shared,
apiType: .SBS
)
}

func testCategorySearch() throws {
try server.setResponse(.categoryCafe)

let expectation = XCTestExpectation(description: "Expecting results")
searchEngine.search(categoryName: "ATM") { result in
searchEngine.search(categoryName: "cafe") { result in
switch result {
case .success(let searchResults):
XCTAssertFalse(searchResults.isEmpty)
Expand All @@ -30,7 +36,7 @@ class CategorySearchEngineIntegrationTests: MockServerTestCase {
try server.setResponse(.categoryCafe, statusCode: 500)

let expectation = XCTestExpectation(description: "Expecting failure")
searchEngine.search(categoryName: "ATM") { result in
searchEngine.search(categoryName: "cafe") { result in
switch result {
case .success:
XCTFail("Not expected")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
@testable import MapboxSearch
import XCTest

class LocalhostMockServiceProvider: ServiceProviderProtocol {
public static var customBaseURL: String?

/// LocalDataProvider for favorites records
public let localFavoritesProvider = FavoritesProvider()
/// LocalDataProvider for history records
public let localHistoryProvider = HistoryProvider()
/// MapboxMobileEvents manager for analytics usage
public let eventsManager = EventsManager()

/// Responsible for sending feedback related events.
public private(set) lazy var feedbackManager = FeedbackManager(eventsManager: eventsManager)

/// Shared instance of ServiceProvider
public static let shared = LocalhostMockServiceProvider()

var dataLayerProviders: [IndexableDataProvider] { [localHistoryProvider, localFavoritesProvider] }
}

extension LocalhostMockServiceProvider: EngineProviderProtocol {
func getStoredAccessToken() -> String? {
Bundle.main.object(forInfoDictionaryKey: accessTokenPlistKey) as? String
}

func createEngine(
apiType: CoreSearchEngine.ApiType,
accessToken: String,
locationProvider: CoreLocationProvider?
) -> CoreSearchEngineProtocol {
MapboxOptions.accessToken = accessToken

let engineOptions = CoreSearchEngine.Options(
baseUrl: Self.customBaseURL,
apiType: NSNumber(value: apiType.rawValue),
sdkInformation: SdkInformation.defaultInfo,
eventsUrl: nil
)

return CoreSearchEngine(
options: engineOptions,
location: locationProvider
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import CoreLocation
@testable import MapboxSearch
import XCTest

class MockServerTestCase: XCTestCase {
class MockServerIntegrationTestCase: XCTestCase {
let server = MockWebServer()

func setServerResponse(_ response: MockResponse, query: String? = nil) throws {
Expand All @@ -12,7 +12,7 @@ class MockServerTestCase: XCTestCase {
override func setUpWithError() throws {
try super.setUpWithError()

UserDefaults.standard.setValue(server.endpoint, forKey: "MapboxAPIBaseURL")
LocalhostMockServiceProvider.customBaseURL = server.endpoint

try server.start()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import MapboxCommon
@testable import MapboxSearch
import XCTest

class OfflineIntegrationTests: MockServerTestCase {
class OfflineIntegrationTests: MockServerIntegrationTestCase {
let delegate = SearchEngineDelegateStub()
let searchEngine = SearchEngine()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@ import CoreLocation
@testable import MapboxSearch
import XCTest

final class PlaceAutocompleteIntegrationTests: MockServerTestCase {
final class PlaceAutocompleteIntegrationTests: MockServerIntegrationTestCase {
private var placeAutocomplete: PlaceAutocomplete!

override func setUp() {
super.setUp()

placeAutocomplete = PlaceAutocomplete(
let reporter = CoreUserActivityReporter.getOrCreate(
for: CoreUserActivityReporterOptions(
sdkInformation:
SdkInformation.defaultInfo,
eventsUrl: nil
)
)

let engine = LocalhostMockServiceProvider.shared.createEngine(
apiType: CoreSearchEngine.ApiType.SBS,
accessToken: "access-token",
locationProvider: DefaultLocationProvider()
locationProvider: WrapperLocationProvider(wrapping: DefaultLocationProvider())
)

placeAutocomplete = PlaceAutocomplete(
searchEngine: engine,
userActivityReporter: reporter
)
}

Expand All @@ -21,7 +35,7 @@ final class PlaceAutocompleteIntegrationTests: MockServerTestCase {
try server.setResponse(.retrieveSanFrancisco)

var suggestion: PlaceAutocomplete.Suggestion?
placeAutocomplete.suggestions(for: "query") { result in
placeAutocomplete.suggestions(for: "San Francisco") { result in
switch result {
case .success(let suggestions):
XCTAssertEqual(suggestions.count, 10)
Expand Down
Loading
Loading