more stuff yay
This commit is contained in:
154
tests/box_test.py
Normal file
154
tests/box_test.py
Normal file
@@ -0,0 +1,154 @@
|
||||
from fastapi.testclient import TestClient
|
||||
from fastapi import BackgroundTasks
|
||||
import pytest
|
||||
from unittest.mock import Mock, patch
|
||||
import asyncio
|
||||
import os
|
||||
from main import app
|
||||
from services.file import FileService
|
||||
from services.task import TaskService
|
||||
|
||||
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
test_boxes = []
|
||||
|
||||
def test_create_box():
|
||||
# Send as form data, not JSON
|
||||
response = client.post("/api/boxes",
|
||||
data={
|
||||
"type": "play",
|
||||
"set_code": "BLB",
|
||||
"sku": "1234",
|
||||
"num_cards_expected": 504
|
||||
}
|
||||
)
|
||||
test_boxes.append(response.json()["box"][0]["product_id"])
|
||||
|
||||
assert response.status_code == 201
|
||||
assert response.json()["success"] == True
|
||||
assert response.json()["box"][0]["type"] == "play"
|
||||
assert response.json()["box"][0]["set_code"] == "BLB"
|
||||
assert response.json()["box"][0]["sku"] == "1234"
|
||||
assert response.json()["box"][0]["num_cards_expected"] == 504
|
||||
|
||||
def test_update_box():
|
||||
# Create a box first
|
||||
create_response = client.post("/api/boxes",
|
||||
data={
|
||||
"type": "collector",
|
||||
"set_code": "MKM",
|
||||
"sku": "3456",
|
||||
"num_cards_expected": 504
|
||||
}
|
||||
)
|
||||
box_id = create_response.json()["box"][0]["product_id"]
|
||||
test_boxes.append(box_id)
|
||||
|
||||
# Update the box
|
||||
response = client.put(f"/api/boxes/{box_id}",
|
||||
data={
|
||||
"num_cards_expected": 500
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json()["success"] == True
|
||||
assert response.json()["box"][0]["type"] == "collector"
|
||||
assert response.json()["box"][0]["set_code"] == "MKM"
|
||||
assert response.json()["box"][0]["sku"] == "3456"
|
||||
assert response.json()["box"][0]["num_cards_expected"] == 500
|
||||
|
||||
def test_delete_box():
|
||||
# Create a box first
|
||||
create_response = client.post("/api/boxes",
|
||||
data={
|
||||
"type": "set",
|
||||
"set_code": "LCI",
|
||||
"sku": "7890",
|
||||
"num_cards_expected": 504
|
||||
}
|
||||
)
|
||||
box_id = create_response.json()["box"][0]["product_id"]
|
||||
|
||||
# Delete the box
|
||||
response = client.delete(f"/api/boxes/{box_id}")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json()["success"] == True
|
||||
assert response.json()["box"][0]["type"] == "set"
|
||||
assert response.json()["box"][0]["set_code"] == "LCI"
|
||||
assert response.json()["box"][0]["sku"] == "7890"
|
||||
assert response.json()["box"][0]["num_cards_expected"] == 504
|
||||
|
||||
# Constants for reused values
|
||||
TEST_FILE_PATH = os.path.join(os.getcwd(), "tests/test_files", "manabox_test_file.csv")
|
||||
DEFAULT_METADATA = {
|
||||
"source": "manabox",
|
||||
"type": "scan_export_common"
|
||||
}
|
||||
|
||||
def get_file_size_kb(file_path):
|
||||
"""Helper to consistently calculate file size in KB"""
|
||||
with open(file_path, "rb") as f:
|
||||
return round(len(f.read()) / 1024, 2)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_box():
|
||||
"""Test creating a new manabox file"""
|
||||
# Open file within the test scope
|
||||
with open(TEST_FILE_PATH, "rb") as test_file:
|
||||
files = {"file": test_file}
|
||||
|
||||
# Make request
|
||||
response = client.post("/api/files", data=DEFAULT_METADATA, files=files)
|
||||
|
||||
# Check response
|
||||
assert response.status_code == 201
|
||||
assert response.json()["success"] == True
|
||||
|
||||
file_data = response.json()["files"][0]
|
||||
assert file_data["source"] == DEFAULT_METADATA["source"]
|
||||
assert file_data["type"] == DEFAULT_METADATA["type"]
|
||||
assert file_data["status"] == "pending"
|
||||
assert file_data["service"] == None
|
||||
assert file_data["filename"] == "manabox_test_file.csv"
|
||||
assert file_data["filesize_kb"] == get_file_size_kb(TEST_FILE_PATH)
|
||||
assert file_data["id"] is not None
|
||||
|
||||
# Execute background tasks if they were added
|
||||
background_tasks = BackgroundTasks()
|
||||
for task in background_tasks.tasks:
|
||||
await task()
|
||||
|
||||
# Create a box first
|
||||
create_response = client.post("/api/boxes",
|
||||
data={
|
||||
"type": "play",
|
||||
"set_code": "OTJ",
|
||||
"sku": "2314",
|
||||
"num_cards_expected": 504
|
||||
}
|
||||
)
|
||||
box_id = create_response.json()["box"][0]["product_id"]
|
||||
test_boxes.append(box_id)
|
||||
|
||||
# Open the box
|
||||
response = client.post(f"/api/boxes/{box_id}/open",
|
||||
data={
|
||||
"product_id": box_id,
|
||||
"file_ids": [file_data["id"]],
|
||||
"num_cards_actual": 500
|
||||
}
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
assert response.json()["success"] == True
|
||||
|
||||
|
||||
def test_cleanup():
|
||||
# Delete all boxes created during testing
|
||||
for box_id in test_boxes:
|
||||
client.delete(f"/api/boxes/{box_id}")
|
||||
|
@@ -14,7 +14,7 @@ client = TestClient(app)
|
||||
TEST_FILE_PATH = os.path.join(os.getcwd(), "tests/test_files", "manabox_test_file.csv")
|
||||
DEFAULT_METADATA = {
|
||||
"source": "manabox",
|
||||
"type": "scan_export"
|
||||
"type": "scan_export_rare"
|
||||
}
|
||||
|
||||
def get_file_size_kb(file_path):
|
||||
|
Reference in New Issue
Block a user