Compare commits

...

2 commits

Author SHA1 Message Date
Djuri Baars
7d570b4761 JSON test platformio changes 2024-12-19 22:09:48 +01:00
Djuri Baars
0c2314a413 First attempt of json tests 2024-12-19 22:08:35 +01:00
6 changed files with 156 additions and 1 deletions

View file

@ -159,9 +159,19 @@ build_flags =
[env:native_test_only]
platform = native
test_framework = unity
lib_deps =
ArduinoJson
fabiobatsilva/ArduinoFake@^0.4.0
lib_compat_mode = off
test_build_src = yes
build_src_filter =
-<*> ; exclude all files by default
+<lib/bitaxe_fetch.cpp> ; include only specific files you need
build_flags =
${btclock_base.build_flags}
-D MCP_INT_PIN=8
-D NEOPIXEL_PIN=34
-D NEOPIXEL_COUNT=4
-D NUM_SCREENS=7
-DUNIT_TEST
-std=gnu++17

View file

@ -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
}
}

View file

@ -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"}}

View file

@ -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
}

View file

@ -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);

View file

@ -0,0 +1,68 @@
#include <unity.h>
#include <ArduinoJson.h>
#include <fstream>
#include <string>
#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<char>(file)),
std::istreambuf_iterator<char>());
DeserializationError error = deserializeJson(doc, json);
std::string bitaxeHashrate = std::to_string(static_cast<int>(std::round(doc["hashRate"].as<float>())));
std::string bitaxeBestDiff = doc["bestDiff"].as<std::string>();
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<char>(file)),
std::istreambuf_iterator<char>());
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<char>(file)),
std::istreambuf_iterator<char>());
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();
}