From 0c2314a4135ee18e3b833fedfe03e350ed5f73c9 Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Thu, 19 Dec 2024 22:08:35 +0100 Subject: [PATCH] First attempt of json tests --- .../test_json/data/20241219-test-braiins.json | 20 ++++++ .../data/20241219-test-ocean-api.json | 1 + test/test_json/data/bitaxe_2_3_0.json | 41 +++++++++++ test/test_json/test_bitaxe_parser.hpp | 15 ++++ test/test_json/test_datasources.cpp | 68 +++++++++++++++++++ 5 files changed, 145 insertions(+) create mode 100644 test/test_json/data/20241219-test-braiins.json create mode 100644 test/test_json/data/20241219-test-ocean-api.json create mode 100644 test/test_json/data/bitaxe_2_3_0.json create mode 100644 test/test_json/test_bitaxe_parser.hpp create mode 100644 test/test_json/test_datasources.cpp diff --git a/test/test_json/data/20241219-test-braiins.json b/test/test_json/data/20241219-test-braiins.json new file mode 100644 index 0000000..f670655 --- /dev/null +++ b/test/test_json/data/20241219-test-braiins.json @@ -0,0 +1,20 @@ +{ + "username": "bd123", + "btc": { + "all_time_reward": "0.00012536", + "hash_rate_unit": "Gh/s", + "hash_rate_5m": 2586.8588, + "hash_rate_60m": 2628.4603, + "hash_rate_24h": 120.2364, + "hash_rate_yesterday": 0, + "low_workers": 0, + "off_workers": 5, + "ok_workers": 1, + "dis_workers": 1, + "current_bad": "0.00000006", + "shares_5m": 180690, + "shares_60m": 2203150, + "shares_24h": 2418744, + "shares_yesterday": 0 + } +} \ No newline at end of file diff --git a/test/test_json/data/20241219-test-ocean-api.json b/test/test_json/data/20241219-test-ocean-api.json new file mode 100644 index 0000000..8718365 --- /dev/null +++ b/test/test_json/data/20241219-test-ocean-api.json @@ -0,0 +1 @@ +{"result":{"snap_type":"user","snap_ts":"1734565260","shares_60s":"9988210688","shares_300s":"49864507392","hashrate_60s":"714983970841960960","hashrate_300s":"713888094932634112","lastest_share_ts":"1734565263","shares_in_tides":"159369973182353","estimated_earn_next_block":"0.58907916","estimated_bonus_earn_next_block":"0.00000000","estimated_total_earn_next_block":"0.58907916","estimated_payout_next_block":"0.58907916","unpaid":"0.00000000"}} \ No newline at end of file diff --git a/test/test_json/data/bitaxe_2_3_0.json b/test/test_json/data/bitaxe_2_3_0.json new file mode 100644 index 0000000..194e370 --- /dev/null +++ b/test/test_json/data/bitaxe_2_3_0.json @@ -0,0 +1,41 @@ +{ + "power": 15.710000038146973, + "voltage": 5178.75, + "current": 3028.75, + "temp": 72, + "vrTemp": 0, + "hashRate": 690.13493843712524, + "bestDiff": "6.78G", + "bestSessionDiff": "6.78G", + "isUsingFallbackStratum": 0, + "freeHeap": 145052, + "coreVoltage": 1200, + "coreVoltageActual": 1201, + "frequency": 500, + "ssid": "BTClock", + "macAddr": "DC:DB:0C:13:10:F8", + "hostname": "bitaxe1-white", + "wifiStatus": "Connected!", + "sharesAccepted": 514214, + "sharesRejected": 293, + "uptimeSeconds": 2084435, + "asicCount": 1, + "smallCoreCount": 1276, + "ASICModel": "BM1368", + "stratumURL": "pool.noderunners.network", + "fallbackStratumURL": "pool.noderunners.network", + "stratumPort": 1337, + "fallbackStratumPort": 7331, + "stratumUser": "bc1.BitAxe1", + "fallbackStratumUser": "bc1.BitAxe1", + "version": "v2.3.0", + "boardVersion": "401", + "runningPartition": "ota_1", + "flipscreen": 1, + "overheat_mode": 0, + "invertscreen": 0, + "invertfanpolarity": 1, + "autofanspeed": 0, + "fanspeed": 100, + "fanrpm": 5850 +} \ No newline at end of file diff --git a/test/test_json/test_bitaxe_parser.hpp b/test/test_json/test_bitaxe_parser.hpp new file mode 100644 index 0000000..fa0b7f4 --- /dev/null +++ b/test/test_json/test_bitaxe_parser.hpp @@ -0,0 +1,15 @@ +#pragma once + +// Mock the Arduino String class if needed +#ifndef ARDUINO +class String { +public: + String(const char* str) : data(str) {} + const char* c_str() const { return data; } +private: + const char* data; +}; +#endif + +// Declare only the function you want to test +void bitaxeParseApiResponse(String apiResponse); diff --git a/test/test_json/test_datasources.cpp b/test/test_json/test_datasources.cpp new file mode 100644 index 0000000..c2c48a5 --- /dev/null +++ b/test/test_json/test_datasources.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include "test_bitaxe_parser.hpp" + +void setUp(void) +{ + // Setup code +} + +void tearDown(void) +{ + // Cleanup code +} + +void test_bitaxe_api(void) +{ + JsonDocument doc; + + std::ifstream file("test/test_json/data/bitaxe_2_3_0.json"); + std::string json((std::istreambuf_iterator(file)), + std::istreambuf_iterator()); + + DeserializationError error = deserializeJson(doc, json); + std::string bitaxeHashrate = std::to_string(static_cast(std::round(doc["hashRate"].as()))); + std::string bitaxeBestDiff = doc["bestDiff"].as(); + String testInput = "{\"test\":\"data\"}"; + + bitaxeParseApiResponse(testInput); + + TEST_ASSERT_EQUAL_STRING(bitaxeHashrate.c_str(), std::string("690").c_str()); +} + +void test_braiins_api(void) +{ + JsonDocument doc; + + std::ifstream file("test/test_json/data/20241219-test-braiins.json"); + std::string json((std::istreambuf_iterator(file)), + std::istreambuf_iterator()); + + DeserializationError error = deserializeJson(doc, json); + + TEST_ASSERT_EQUAL(DeserializationError::Ok, error.code()); +} + +void test_ocean_api(void) +{ + JsonDocument doc; + + std::ifstream file("test/test_json/data/20241219-test-ocean-api.json"); + std::string json((std::istreambuf_iterator(file)), + std::istreambuf_iterator()); + + DeserializationError error = deserializeJson(doc, json); + + TEST_ASSERT_EQUAL(DeserializationError::Ok, error.code()); +} + +int main(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_bitaxe_api); + RUN_TEST(test_braiins_api); + RUN_TEST(test_ocean_api); + return UNITY_END(); +}