Skip to content

Commit f10e119

Browse files
fix: optional avg and sum fields in facet stats (#786)
1 parent 6aa3e87 commit f10e119

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

Sources/AlgoliaSearchClient/Models/Search/Response/SearchResponse/Auxiliary/FacetStats/FacetStats.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public struct FacetStats: Codable {
1818
public let max: Double
1919

2020
/// The average of all values.
21-
public let avg: Double
21+
public let avg: Double?
2222

2323
/// The sum of all values.
24-
public let sum: Double
24+
public let sum: Double?
2525

2626
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// FacetStats.swift
3+
//
4+
//
5+
// Created by Vladislav Fitc on 16/03/2022.
6+
//
7+
8+
import Foundation
9+
import XCTest
10+
@testable import AlgoliaSearchClient
11+
12+
class FacetStatsTests: XCTestCase {
13+
14+
func testDecoding() throws {
15+
let jsonData = """
16+
{
17+
"min": 10,
18+
"max": 200,
19+
"avg": 105,
20+
"sum": 210,
21+
}
22+
""".data(using: .utf8)!
23+
let facetStats = try JSONDecoder().decode(FacetStats.self, from: jsonData)
24+
XCTAssertEqual(facetStats.min, 10)
25+
XCTAssertEqual(facetStats.max, 200)
26+
XCTAssertEqual(facetStats.avg, 105)
27+
XCTAssertEqual(facetStats.sum, 210)
28+
}
29+
30+
func testMissingSumAvgDecoding() throws {
31+
let jsonData = """
32+
{
33+
"min": 10,
34+
"max": 200,
35+
}
36+
""".data(using: .utf8)!
37+
let facetStats = try JSONDecoder().decode(FacetStats.self, from: jsonData)
38+
XCTAssertEqual(facetStats.min, 10)
39+
XCTAssertEqual(facetStats.max, 200)
40+
XCTAssertNil(facetStats.avg)
41+
XCTAssertNil(facetStats.sum)
42+
}
43+
44+
}

0 commit comments

Comments
 (0)