1
0
mirror of https://gitlab.com/2-chainz/2chainz.git synced 2025-08-02 03:31:25 +00:00

Add tests

This commit is contained in:
2025-05-23 14:28:17 -06:00
parent a683fceb10
commit d05cc46660
3 changed files with 107 additions and 60 deletions

View File

@@ -1,38 +0,0 @@
from fastapi.testclient import TestClient
from datetime import datetime
import time
import pytest
from unittest.mock import patch
from two_chainz import app
client = TestClient(app)
class TestApi:
def test_api_endpoint(self):
response = client.get("/api/")
assert response.status_code == 200
data = response.json()
assert "status" in data
assert data["status"] == "ok"
assert "timestamp" in data
assert "uptime_seconds" in data
# Validate timestamp format (ISO format)
assert datetime.fromisoformat(data["timestamp"])
@pytest.mark.parametrize(
"mocked_time,start_time,expected",
[
(100, 50, 50), # 100 - 50 = 50 seconds uptime
(200, 100, 100), # 200 - 100 = 100 seconds uptime
],
)
def test_api_uptime_calculation(self, mocked_time, start_time, expected):
with patch("time.time", return_value=mocked_time):
with patch("two_chainz.start_time", start_time):
response = client.get("/api/")
assert response.json()["uptime_seconds"] == expected

View File

@@ -0,0 +1,85 @@
from fastapi.testclient import TestClient
from datetime import datetime
import pytest
from unittest.mock import patch
from two_chainz import app
import two_chainz
client = TestClient(app)
class TestApi:
def test_api_endpoint(self):
response = client.get("/api/")
assert response.status_code == 200
data = response.json()
assert "status" in data
assert data["status"] == "ok"
assert "timestamp" in data
assert "uptime_seconds" in data
# Validate timestamp format (ISO format)
assert datetime.fromisoformat(data["timestamp"])
@pytest.mark.parametrize(
"mocked_time,start_time,expected",
[
(100, 50, 50), # 100 - 50 = 50 seconds uptime
(200, 100, 100), # 200 - 100 = 100 seconds uptime
],
)
def test_api_uptime_calculation(self, mocked_time, start_time, expected):
with patch("time.time", return_value=mocked_time):
with patch("two_chainz.start_time", start_time):
response = client.get("/api/")
assert response.json()["uptime_seconds"] == expected
@pytest.mark.parametrize("endpoint", ["/api/quote", "/api/alias"])
class TestDataEndpoints:
def test_endpoint_nonempty(self, endpoint):
for _ in range(1000): # Data is random so run the test a ton
# Given we have an endpoint
# When we do a get request
response = client.get(endpoint)
# Then we should get a 200 status code
assert response.status_code == 200
data = response.json()
# Then there should be a single entry in the dict
assert len(data) == 1
# Then the values should not be empty
key, value = next(iter(data.items()))
assert key
assert value
class TestData:
def test_data_exists(self):
assert two_chainz.data
assert two_chainz.data['quotes']
assert two_chainz.data['aliases']
@pytest.mark.parametrize("quote", two_chainz.data['quotes'])
class TestQuotes:
def test_no_empty(self, quote):
assert quote
def test_no_ending_period(self, quote):
assert quote[-1] != "."
def test_no_ending_newline(self, quote):
assert quote[-1] != "\n"
@pytest.mark.parametrize("alias", two_chainz.data['quotes'])
class TestAlias:
def test_no_empty(self, alias):
assert alias
def test_no_ending_newline(self, alias):
assert alias[-1] != "\n"