File size: 12,129 Bytes
bf20cb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""Tests for WayyDB REST API endpoints."""

import pytest
import asyncio
import numpy as np
from httpx import AsyncClient, ASGITransport
from fastapi.testclient import TestClient
import tempfile
import shutil
import os

# Set up test environment before importing app
_test_data_path = tempfile.mkdtemp(prefix="wayydb_test_")
os.environ["WAYY_DATA_PATH"] = _test_data_path


@pytest.fixture(scope="module")
def api_client():
    """Create a test client with lifespan managed."""
    from api.main import app
    with TestClient(app) as client:
        yield client


class TestHealthEndpoints:
    """Tests for health check endpoints."""

    def test_root(self, api_client):
        """Test root endpoint."""
        response = api_client.get("/")
        assert response.status_code == 200
        data = response.json()
        assert data["service"] == "WayyDB API"
        assert "version" in data
        assert data["status"] == "healthy"

    def test_health(self, api_client):
        """Test health endpoint."""
        response = api_client.get("/health")
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "healthy"
        assert "tables" in data


class TestTableOperations:
    """Tests for table CRUD operations."""

    def test_list_tables(self, api_client):
        """Test listing tables."""
        response = api_client.get("/tables")
        assert response.status_code == 200
        assert "tables" in response.json()

    def test_create_and_delete_table(self, api_client):
        """Test creating and deleting a table."""
        # Create
        response = api_client.post("/tables", json={"name": "test_table"})
        assert response.status_code == 200
        assert response.json()["created"] == "test_table"

        # Verify exists
        response = api_client.get("/tables/test_table")
        assert response.status_code == 200

        # Delete
        response = api_client.delete("/tables/test_table")
        assert response.status_code == 200

        # Verify deleted
        response = api_client.get("/tables/test_table")
        assert response.status_code == 404

    def test_upload_table(self, api_client):
        """Test uploading a table with data."""
        data = {
            "name": "uploaded_table",
            "columns": [
                {"name": "timestamp", "dtype": "int64", "data": [1000, 2000, 3000]},
                {"name": "price", "dtype": "float64", "data": [100.0, 101.0, 102.0]},
            ],
            "sorted_by": "timestamp",
        }

        response = api_client.post("/tables/upload", json=data)
        assert response.status_code == 200
        result = response.json()
        assert result["created"] == "uploaded_table"
        assert result["rows"] == 3

        # Get data back
        response = api_client.get("/tables/uploaded_table/data")
        assert response.status_code == 200
        result = response.json()
        assert result["total_rows"] == 3
        assert result["data"]["timestamp"] == [1000, 2000, 3000]
        assert result["data"]["price"] == [100.0, 101.0, 102.0]

        # Cleanup
        api_client.delete("/tables/uploaded_table")

    def test_get_table_info(self, api_client):
        """Test getting table metadata."""
        # Upload a table
        data = {
            "name": "info_test",
            "columns": [
                {"name": "ts", "dtype": "int64", "data": [1, 2, 3]},
                {"name": "val", "dtype": "float64", "data": [1.0, 2.0, 3.0]},
            ],
            "sorted_by": "ts",
        }
        api_client.post("/tables/upload", json=data)

        # Get info
        response = api_client.get("/tables/info_test")
        assert response.status_code == 200
        info = response.json()
        assert info["name"] == "info_test"
        assert info["num_rows"] == 3
        assert info["num_columns"] == 2
        assert "ts" in info["columns"]
        assert info["sorted_by"] == "ts"

        # Cleanup
        api_client.delete("/tables/info_test")


class TestAppendAPI:
    """Tests for the append endpoint."""

    def test_append_to_table(self, api_client):
        """Test appending rows to an existing table."""
        # Create initial table
        data = {
            "name": "append_test",
            "columns": [
                {"name": "timestamp", "dtype": "int64", "data": [1000, 2000]},
                {"name": "price", "dtype": "float64", "data": [100.0, 101.0]},
            ],
            "sorted_by": "timestamp",
        }
        api_client.post("/tables/upload", json=data)

        # Append more data
        append_data = {
            "columns": [
                {"name": "timestamp", "dtype": "int64", "data": [3000, 4000]},
                {"name": "price", "dtype": "float64", "data": [102.0, 103.0]},
            ]
        }
        response = api_client.post("/tables/append_test/append", json=append_data)
        assert response.status_code == 200
        result = response.json()
        assert result["new_rows"] == 2
        assert result["total_rows"] == 4

        # Verify data
        response = api_client.get("/tables/append_test/data")
        data = response.json()["data"]
        assert len(data["timestamp"]) == 4
        assert data["timestamp"] == [1000, 2000, 3000, 4000]
        assert data["price"] == [100.0, 101.0, 102.0, 103.0]

        # Cleanup
        api_client.delete("/tables/append_test")

    def test_append_column_mismatch(self, api_client):
        """Test that append fails with mismatched columns."""
        # Create table
        data = {
            "name": "mismatch_test",
            "columns": [
                {"name": "timestamp", "dtype": "int64", "data": [1000]},
                {"name": "price", "dtype": "float64", "data": [100.0]},
            ],
        }
        api_client.post("/tables/upload", json=data)

        # Try to append with wrong columns
        append_data = {
            "columns": [
                {"name": "timestamp", "dtype": "int64", "data": [2000]},
                {"name": "volume", "dtype": "float64", "data": [50.0]},  # Wrong column
            ]
        }
        response = api_client.post("/tables/mismatch_test/append", json=append_data)
        assert response.status_code == 400
        assert "mismatch" in response.json()["detail"].lower()

        # Cleanup
        api_client.delete("/tables/mismatch_test")


class TestIngestAPI:
    """Tests for streaming ingestion REST endpoints."""

    def test_ingest_single_tick(self, api_client):
        """Test ingesting a single tick via REST."""
        tick = {
            "symbol": "BTC-USD",
            "price": 42150.50,
            "volume": 1.5,
        }
        response = api_client.post("/ingest/test_ticks", json=tick)
        assert response.status_code == 200
        assert response.json()["ingested"] == 1

    def test_ingest_batch(self, api_client):
        """Test ingesting a batch of ticks via REST."""
        batch = {
            "ticks": [
                {"symbol": "BTC-USD", "price": 42150.0},
                {"symbol": "ETH-USD", "price": 2250.0},
                {"symbol": "SOL-USD", "price": 100.0},
            ]
        }
        response = api_client.post("/ingest/test_ticks/batch", json=batch)
        assert response.status_code == 200
        assert response.json()["ingested"] == 3


class TestStreamingStats:
    """Tests for streaming statistics endpoints."""

    def test_streaming_stats(self, api_client):
        """Test getting streaming statistics."""
        response = api_client.get("/streaming/stats")
        assert response.status_code == 200
        stats = response.json()
        assert "ticks_received" in stats
        assert "ticks_flushed" in stats
        assert "buffer_sizes" in stats
        assert "running" in stats

    def test_get_quote(self, api_client):
        """Test getting a specific quote."""
        # Ingest a tick first
        api_client.post("/ingest/ticks", json={"symbol": "BTC-USD", "price": 42000.0})

        # Get quote
        response = api_client.get("/streaming/quote/ticks/BTC-USD")
        assert response.status_code == 200
        quote = response.json()
        assert quote["symbol"] == "BTC-USD"
        assert quote["price"] == 42000.0

    def test_get_all_quotes(self, api_client):
        """Test getting all quotes for a table."""
        # Ingest multiple ticks
        for symbol, price in [("BTC-USD", 42000.0), ("ETH-USD", 2200.0)]:
            api_client.post("/ingest/quotes_test", json={"symbol": symbol, "price": price})

        # Get all quotes
        response = api_client.get("/streaming/quotes/quotes_test")
        assert response.status_code == 200
        quotes = response.json()
        assert "BTC-USD" in quotes
        assert "ETH-USD" in quotes


class TestAggregations:
    """Tests for aggregation endpoints."""

    @pytest.fixture
    def setup_table(self, api_client):
        """Set up a table for aggregation tests."""
        data = {
            "name": "agg_test",
            "columns": [
                {"name": "timestamp", "dtype": "int64", "data": [1, 2, 3, 4, 5]},
                {"name": "price", "dtype": "float64", "data": [10.0, 20.0, 30.0, 40.0, 50.0]},
            ],
        }
        api_client.post("/tables/upload", json=data)
        yield
        api_client.delete("/tables/agg_test")

    def test_sum(self, api_client, setup_table):
        """Test sum aggregation."""
        response = api_client.get("/tables/agg_test/agg/price/sum")
        assert response.status_code == 200
        assert response.json()["result"] == pytest.approx(150.0)

    def test_avg(self, api_client, setup_table):
        """Test average aggregation."""
        response = api_client.get("/tables/agg_test/agg/price/avg")
        assert response.status_code == 200
        assert response.json()["result"] == pytest.approx(30.0)

    def test_min_max(self, api_client, setup_table):
        """Test min/max aggregations."""
        response = api_client.get("/tables/agg_test/agg/price/min")
        assert response.json()["result"] == pytest.approx(10.0)

        response = api_client.get("/tables/agg_test/agg/price/max")
        assert response.json()["result"] == pytest.approx(50.0)


class TestWindowFunctions:
    """Tests for window function endpoint."""

    @pytest.fixture
    def setup_table(self, api_client):
        """Set up a table for window tests."""
        data = {
            "name": "window_test",
            "columns": [
                {"name": "timestamp", "dtype": "int64", "data": list(range(10))},
                {"name": "price", "dtype": "float64", "data": [float(i) for i in range(10)]},
            ],
        }
        api_client.post("/tables/upload", json=data)
        yield
        api_client.delete("/tables/window_test")

    def test_mavg(self, api_client, setup_table):
        """Test moving average."""
        response = api_client.post("/window", json={
            "table": "window_test",
            "column": "price",
            "operation": "mavg",
            "window": 3,
        })
        assert response.status_code == 200
        result = response.json()["result"]
        assert len(result) == 10
        # Third element should be avg of 0, 1, 2 = 1.0
        assert result[2] == pytest.approx(1.0)

    def test_ema(self, api_client, setup_table):
        """Test exponential moving average."""
        response = api_client.post("/window", json={
            "table": "window_test",
            "column": "price",
            "operation": "ema",
            "alpha": 0.5,
        })
        assert response.status_code == 200
        result = response.json()["result"]
        assert len(result) == 10
        assert result[0] == pytest.approx(0.0)  # First value unchanged


# Cleanup test directory after all tests
def pytest_sessionfinish(session, exitstatus):
    """Clean up test data directory."""
    if _test_data_path and os.path.exists(_test_data_path):
        shutil.rmtree(_test_data_path, ignore_errors=True)