Skip to content

Commit

Permalink
more debugging & testing
Browse files Browse the repository at this point in the history
  • Loading branch information
extreme4all committed May 20, 2024
1 parent 39f9701 commit 0e64859
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/app/repositories/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,19 @@ def _filter_valid_time(self, data: list[Detection]) -> list[Detection]:
current_time = int(time.time())
min_ts = current_time - 25200 # 7 hours ago
max_ts = current_time + 3600 # 1 hour in the future
return [d for d in data if min_ts < d.ts < max_ts]
# [d for d in data if min_ts < d.ts < max_ts]
output = []

for d in data:
if d.ts <= min_ts:
logger.info(f"invalid: {d.ts} <= {min_ts}, {d.reporter}")
continue
if d.ts >= max_ts:
logger.info(f"invalid: {d.ts} >= {max_ts}, {d.reporter}")
continue
output.append(d)

return output

def _check_unique_reporter(self, data: list[Detection]) -> list[Detection] | None:
return None if len(set(d.reporter for d in data)) > 1 else data
Expand Down
4 changes: 2 additions & 2 deletions src/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from . import logging_config
from . import config, logging_config

__all__ = ["logging_config"]
__all__ = ["logging_config", "config"]
85 changes: 85 additions & 0 deletions tests/test_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import os
import sys
import time
from typing import Optional

import pytest
from httpx import AsyncClient
from pydantic import BaseModel
from pydantic.fields import Field

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

example_data = {
"reporter": "player1",
"reported": "player2",
"region_id": 1,
"x_coord": 100,
"y_coord": 200,
"z_coord": 300,
"ts": int(time.time()), # current timestamp
"manual_detect": 0,
"on_members_world": 0,
"on_pvp_world": 0,
"world_number": 350,
"equipment": {
"equip_head_id": 10,
"equip_amulet_id": 20,
"equip_torso_id": 30,
"equip_legs_id": 40,
"equip_boots_id": 50,
"equip_cape_id": 60,
"equip_hands_id": 70,
"equip_weapon_id": 80,
"equip_shield_id": 90,
},
"equip_ge_value": 1000,
}


@pytest.mark.asyncio
async def test_valid_report(custom_client):
global example_data
endpoint = "/v2/report"
_data = example_data.copy()
_data["ts"] = int(time.time())

# Example of a valid detection data
detection_data = [_data]

async with custom_client as client:
client: AsyncClient
response = await client.post(endpoint, json=detection_data)
assert response.status_code == 201


@pytest.mark.asyncio
async def test_invalid_ts_high_report(custom_client):
global example_data
endpoint = "/v2/report"
_data = example_data.copy()
_data["ts"] = int(time.time()) + 3700

# Example of a valid detection data
detection_data = [_data]

async with custom_client as client:
client: AsyncClient
response = await client.post(endpoint, json=detection_data)
assert response.status_code == 400


@pytest.mark.asyncio
async def test_invalid_ts_low_report(custom_client):
global example_data
endpoint = "/v2/report"
_data = example_data.copy()
_data["ts"] = int(time.time()) - 25300

# Example of a valid detection data
detection_data = [_data]

async with custom_client as client:
client: AsyncClient
response = await client.post(endpoint, json=detection_data)
assert response.status_code == 400

0 comments on commit 0e64859

Please sign in to comment.