This commit is contained in:
cdricms
2025-12-06 16:33:17 +01:00
parent 7be99711e1
commit 043de16a16
31 changed files with 594 additions and 1674 deletions

View File

@@ -1,31 +1,50 @@
@testable import OpenFoodFacts
import XCTest
final class swift_openfoodfacts_sdkTests: XCTestCase {
func testBarcodeProcessing() async throws {
let off = OpenFoodFactsClient()
off.prod = true
@testable import OpenFoodFactsSDK
for barcode in ["0737628064502", "0812133010036", "0849092103196", "22007377", "3033610048398", "3222473161867", "3242272260059", "3245412470929", "3502110000880", "3551100749018", "3560070805259", "3560070976867", "3596710352418", "3800020430781", "4000539770708", "4388858946739", "5010251168577", "5015821151720", "5050854517631", "5054070608074", "5201051001076", "5410228196693", "5449000179661", "5601077161035", "6194002510064", "7311041026670", "7640101710236", "8424259826051", "8585002476821", "8712000031312", "8992696419766", "9300601768226", "9300650658615", "9310155100335"] {
do {
let _ = try await off.getProductByBarcode(barcode)
} catch {
XCTFail("[BARCODE: \(barcode)] \(error)")
}
}
}
final class OpenFoodFactsTests: XCTestCase {
func testPerlSearch() async throws {
let off = OpenFoodFactsClient()
do {
// try await off.search(query: .init(searchTerms: "prince", format: .json))
let _ = try await off.search(query: .init(
searchTerms: "",
searchTags: [.init(tag: .brands, value: "mondelez"), .init(tag: .countries, value: "france")],
format: .json
))
} catch {
XCTFail("\(error)")
}
}
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 product = try await client.product(
barcode: "3017620422003", // Nutella
fields: [.code, .productName, .nutriscoreGrade, .nutriments]
)
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 results = try await client.search(
.query("chocolate"),
.tag(tag: .brands, value: "milka"),
.pageSize(5),
.sort(.popularity),
fields: [.nutrientLevels]
)
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)
}
}