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

get everything working together kinda

This commit is contained in:
2025-05-23 13:53:40 -06:00
parent 5687871b86
commit a683fceb10
4 changed files with 43 additions and 32 deletions

View File

@@ -8,8 +8,8 @@ from two_chainz import app
client = TestClient(app)
class TestApi:
class TestApi:
def test_api_endpoint(self):
response = client.get("/api/")
assert response.status_code == 200
@@ -24,12 +24,15 @@ class TestApi:
# 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
])
@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):
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

@@ -2,13 +2,22 @@ import time
from datetime import datetime
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
import tomllib
from pathlib import Path
import random
app = FastAPI()
start_time = time.time()
def read_data() -> dict[str, str]:
raw_data = tomllib.loads(Path("data.toml").read_text())
raw_data['aliases'] = [alias['name'] for alias in raw_data['aliases'] for _ in range(alias['weight'])]
return raw_data
data = read_data()
@app.get("/api/")
async def ping():
return {
@@ -18,5 +27,15 @@ async def ping():
}
@app.get("/api/quote")
async def quote():
return {"quote": random.choice(data['quotes'])}
@app.get("/api/alias")
async def alias():
return {"alias": random.choice(data['aliases'])}
# Mount static files
app.mount("/", StaticFiles(directory="website", html=True), name="static")