Skip to content

Commit

Permalink
Patch Tests & Rename 'result' to 'document'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sherlouk committed Sep 21, 2023
1 parent 117b3ad commit d0a6a20
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
10 changes: 5 additions & 5 deletions Sources/MeiliSearch/Model/SearchResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public class Searchable<T>: Equatable, Codable where T: Codable, T: Equatable {

@dynamicMemberLookup
public struct SearchHit<T>: Equatable, Codable where T: Codable, T: Equatable {
public let result: T
public let rankingScore: Double?
public let document: T
public internal(set) var rankingScore: Double?

/// Dynamic member lookup is used to allow easy access to instance members of the hit result, maintaining a level of backwards compatibility.
public subscript<V>(dynamicMember keyPath: KeyPath<T, V>) -> V {
result[keyPath: keyPath]
document[keyPath: keyPath]
}

// MARK: Codable
Expand All @@ -52,13 +52,13 @@ public struct SearchHit<T>: Equatable, Codable where T: Codable, T: Equatable {

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.result = try T(from: decoder)
self.document = try T(from: decoder)
self.rankingScore = try container.decodeIfPresent(Double.self, forKey: .rankingScore)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(result)
try container.encode(document)

var containerTwo = encoder.container(keyedBy: CodingKeys.self)
try containerTwo.encodeIfPresent(rankingScore, forKey: .rankingScore)
Expand Down
18 changes: 13 additions & 5 deletions Tests/MeiliSearchIntegrationTests/SearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class SearchTests: XCTestCase {
case .success(let response):
let result = response as! SearchResult<Book>
XCTAssertEqual(result.hits.count, 1)
XCTAssertGreaterThan(result.hits[0].rankingScore ?? 0, 0.9)
XCTAssertGreaterThan(result.hits[0].rankingScore ?? 0, 0.1)
expectation.fulfill()
case .failure(let error):
dump(error)
Expand All @@ -166,7 +166,7 @@ class SearchTests: XCTestCase {
let expectation = XCTestExpectation(description: "Search for Books with query")

let expectedValue = """
{"hits":[{"id":1844,"title":"A Moreninha","comment":"A Book from Joaquim Manuel de Macedo","genres":["Novel"],"_rankingScore":0.90404040404040409}],"query":"Moreninha","processingTimeMs":0}
{"hits":[{"_rankingScore":0.5,"comment":"A Book from Joaquim Manuel de Macedo","genres":["Novel"],"id":1844,"title":"A Moreninha"}],"processingTimeMs":0,"query":"Moreninha"}
"""

typealias MeiliResult = Result<Searchable<Book>, Swift.Error>
Expand All @@ -176,7 +176,12 @@ class SearchTests: XCTestCase {
switch result {
case .success(let response):
do {
let data = try JSONEncoder().encode(response)
// the ranking score and time can change for many reasons, of which is not relevant here. we set it to a constant to test the encoding.
response.processingTimeMs = 0
response.hits[0].rankingScore = 0.5
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
let data = try encoder.encode(response)
XCTAssertEqual(String(decoding: data, as: UTF8.self), expectedValue)
} catch {
XCTFail("Failed to encode search result")
Expand All @@ -196,7 +201,7 @@ class SearchTests: XCTestCase {
let expectation = XCTestExpectation(description: "Search for Books with query")

let expectedValue = """
{"hits":[{"id":1844,"title":"A Moreninha","comment":"A Book from Joaquim Manuel de Macedo","genres":["Novel"]}],"query":"Moreninha","processingTimeMs":0}
{"hits":[{"comment":"A Book from Joaquim Manuel de Macedo","genres":["Novel"],"id":1844,"title":"A Moreninha"}],"processingTimeMs":0,"query":"Moreninha"}
"""

typealias MeiliResult = Result<Searchable<Book>, Swift.Error>
Expand All @@ -206,7 +211,10 @@ class SearchTests: XCTestCase {
switch result {
case .success(let response):
do {
let data = try JSONEncoder().encode(response)
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
response.processingTimeMs = 0
let data = try encoder.encode(response)
XCTAssertEqual(String(decoding: data, as: UTF8.self), expectedValue)
} catch {
XCTFail("Failed to encode search result")
Expand Down

0 comments on commit d0a6a20

Please sign in to comment.