54 lines
1.4 KiB
Swift
54 lines
1.4 KiB
Swift
import XCTest
|
|
|
|
@testable import OpenFoodFactsSDK
|
|
|
|
final class OpenFoodFactsTests: XCTestCase {
|
|
|
|
var client: OpenFoodFactsClient!
|
|
|
|
override func setUp() {
|
|
let config = OpenFoodFactsConfig(
|
|
environment: .staging,
|
|
userAgent: .init(
|
|
appName: "SwiftOpenFoodFactsTests", version: "1.0",
|
|
contactInfo: "(test@test.com)")
|
|
)
|
|
client = OpenFoodFactsClient(config: config)
|
|
}
|
|
|
|
func testProductFetch() async throws {
|
|
// Fetch specific fields only
|
|
let response = try await client.product(
|
|
barcode: "3017620422003", // Nutella
|
|
fields: [.code, .productName, .nutriscoreGrade, .nutriments]
|
|
)
|
|
|
|
let product = response.product
|
|
|
|
XCTAssertEqual(product?.code, "3017620422003")
|
|
XCTAssertNotNil(product?.productName)
|
|
XCTAssertNotNil(product?.nutriscoreGrade)
|
|
// This field was NOT requested, so it should be nil (if decoding handles strict optionals)
|
|
// or the struct just holds nil.
|
|
}
|
|
|
|
func testSearch() async throws {
|
|
let response = try await client.search(
|
|
.query("chocolate"),
|
|
.tag(tag: .brands, value: "milka"),
|
|
.pageSize(5),
|
|
.sort(.popularity),
|
|
)
|
|
|
|
let results = response.products ?? []
|
|
|
|
let jsonResults = try JSONEncoder().encode(results)
|
|
try jsonResults.write(to: .init(filePath: "./jsonResults.json"))
|
|
|
|
XCTAssertFalse(results.isEmpty)
|
|
XCTAssertEqual(results.count, 5)
|
|
// XCTAssertTrue(
|
|
// results.first?.brands?.lowercased().contains("milka") ?? false)
|
|
}
|
|
}
|