import XCTest @testable import USDA_FDC func loadEnvironment() -> [String: String] { var dic: [String: String] = [:] if FileManager.default.fileExists(atPath: ".env") { do { let contents = try String(contentsOfFile: ".env", encoding: .utf8) let lines = contents.components(separatedBy: .newlines) for line in lines { let components = line.components(separatedBy: "=") if components.count == 2 { let key = components[0].trimmingCharacters(in: .whitespacesAndNewlines) let value = components[1].trimmingCharacters(in: .whitespacesAndNewlines) dic[key] = value } } } catch { print("Error reading .env file: \(error)") } } return dic } final class USDATests: XCTestCase { var env: [String: String] { loadEnvironment() } func testFoodList() async throws { let fdc = USDA_FDC_Client() fdc.apiKey = env["API_KEY"] ?? "" do { let _ = try await fdc.getFoodList() } catch { throw error } } func testFoodItem() async throws { let fdc = USDA_FDC_Client() fdc.apiKey = env["API_KEY"] ?? "" do { let _ = try await fdc.getFood(.init(fdcId: 167782)) } catch { throw error } } func testGetFoodSearch() async throws { let fdc = USDA_FDC_Client() fdc.apiKey = env["API_KEY"] ?? "" do { let _ = try await fdc.searchFoods(.init(query: "cheddar cheese")) } catch { throw error } } func testPostFoodSearch() async throws { let fdc = USDA_FDC_Client() fdc.apiKey = env["API_KEY"] ?? "" do { let res = try await fdc.searchFoods(.init(query: "037600106214", dataType: [.Branded]), httpMethod: .POST) print(res) } catch { throw error } } }