|
| 1 | +import XCTest |
| 2 | +import Haystack |
| 3 | +import HaystackClient |
| 4 | + |
| 5 | +@available(macOS 13.0, *) |
| 6 | +/// To use these tests, run a [Haxall](https://github.com/haxall/haxall) server and set the username and password |
| 7 | +/// in the `HAYSTACK_USER` and `HAYSTACK_PASSWORD` environment variables |
| 8 | +final class HaystackClientIntegrationTests: XCTestCase { |
| 9 | + var client: Client = try! Client( |
| 10 | + baseUrl: URL(string: "http://localhost:8080/api/")!, |
| 11 | + username: ProcessInfo.processInfo.environment["HAYSTACK_USER"] ?? "su", |
| 12 | + password: ProcessInfo.processInfo.environment["HAYSTACK_PASSWORD"] ?? "su" |
| 13 | + ) |
| 14 | + |
| 15 | + override func setUp() async throws { |
| 16 | + try await client.open() |
| 17 | + } |
| 18 | + |
| 19 | + override func tearDown() async throws { |
| 20 | + try await client.close() |
| 21 | + } |
| 22 | + |
| 23 | + func testCloseAndOpen() async throws { |
| 24 | + print(try await client.close()) |
| 25 | + print(try await client.open()) |
| 26 | + } |
| 27 | + |
| 28 | + func testAbout() async throws { |
| 29 | + print(try await client.about().toZinc()) |
| 30 | + } |
| 31 | + |
| 32 | + func testDefs() async throws { |
| 33 | + print(try await client.defs().toZinc()) |
| 34 | + print(try await client.defs(filter: "lib==^lib:phIoT").toZinc()) |
| 35 | + print(try await client.defs(limit: Number(1)).toZinc()) |
| 36 | + print(try await client.defs(filter: "lib==^lib:phIoT", limit: Number(1)).toZinc()) |
| 37 | + } |
| 38 | + |
| 39 | + func testLibs() async throws { |
| 40 | + print(try await client.libs().toZinc()) |
| 41 | + print(try await client.libs(filter: "lib==^lib:phIoT").toZinc()) |
| 42 | + print(try await client.libs(limit: Number(1)).toZinc()) |
| 43 | + print(try await client.libs(filter: "lib==^lib:phIoT", limit: Number(1)).toZinc()) |
| 44 | + } |
| 45 | + |
| 46 | + func testOps() async throws { |
| 47 | + print(try await client.ops().toZinc()) |
| 48 | + print(try await client.ops(filter: "lib==^lib:phIoT").toZinc()) |
| 49 | + print(try await client.ops(limit: Number(1)).toZinc()) |
| 50 | + print(try await client.ops(filter: "lib==^lib:phIoT", limit: Number(1)).toZinc()) |
| 51 | + } |
| 52 | + |
| 53 | + func testFiletypes() async throws { |
| 54 | + print(try await client.filetypes().toZinc()) |
| 55 | + print(try await client.filetypes(filter: "lib==^lib:phIoT").toZinc()) |
| 56 | + print(try await client.filetypes(limit: Number(1)).toZinc()) |
| 57 | + print(try await client.filetypes(filter: "lib==^lib:phIoT", limit: Number(1)).toZinc()) |
| 58 | + } |
| 59 | + |
| 60 | + func testRead() async throws { |
| 61 | + print(try await client.read(ids: [Ref("28e7fb47-d67ab19a")]).toZinc()) |
| 62 | + } |
| 63 | + |
| 64 | + func testReadAll() async throws { |
| 65 | + print(try await client.read(filter: "site").toZinc()) |
| 66 | + } |
| 67 | + |
| 68 | + func testHisRead() async throws { |
| 69 | + print(try await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .today).toZinc()) |
| 70 | + print(try await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .yesterday).toZinc()) |
| 71 | + print(try await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .date(Date("2022-01-01"))).toZinc()) |
| 72 | + print(try await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .dateRange(from: Date("2022-01-01"), to: Date("2022-02-01"))).toZinc()) |
| 73 | + print(try await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .dateTimeRange(from: DateTime("2022-01-01T00:00:00Z"), to: DateTime("2022-02-01T00:00:00Z"))).toZinc()) |
| 74 | + print(try await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .after(DateTime("2022-01-01T00:00:00Z"))).toZinc()) |
| 75 | + } |
| 76 | + |
| 77 | + func testHisWrite() async throws { |
| 78 | + print(try await client.hisWrite( |
| 79 | + id: Ref("28e7fb7d-e20316e0"), |
| 80 | + items: [ |
| 81 | + HisItem(ts: DateTime("2022-01-01T00:00:00-07:00 Denver"), val: Number(14)) |
| 82 | + ] |
| 83 | + ).toZinc()) |
| 84 | + } |
| 85 | + |
| 86 | + func testEval() async throws { |
| 87 | + print(try await client.eval(expression: "readAll(site)").toZinc()) |
| 88 | + } |
| 89 | + |
| 90 | + func testWatchUnsub() async throws { |
| 91 | + print(try await client.watchUnsub(watchId: "id", ids: [Ref("28e7fb47-d67ab19a")])) |
| 92 | + } |
| 93 | +} |
0 commit comments