commit893b229cc6
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 22:14:08 2025 -0500 j commit06f539aea2
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:55:30 2025 -0500 fk commitd0c2960ec9
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:50:53 2025 -0500 frick commit6b1362c166
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:49:40 2025 -0500 database commit8cadc6df4c
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:38:09 2025 -0500 asdf commit1ca6f98684
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:32:50 2025 -0500 fffff commit8bb337a9c3
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:31:13 2025 -0500 ffff commit65aba280c5
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:26:16 2025 -0500 aa commit59ef03a59e
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:24:21 2025 -0500 asdf commitf44d5740fc
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:23:32 2025 -0500 aaa commit13c96b1643
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:18:54 2025 -0500 sdf commit949c795fd1
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:17:53 2025 -0500 asdf commit8c3cd423fe
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:56:01 2025 -0500 app2 commit78eafc739e
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:54:55 2025 -0500 app commitdc47eced14
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:43:15 2025 -0500 asdfasdfasdf commite24bcae88c
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:39:44 2025 -0500 a commitc894451bfe
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:38:20 2025 -0500 req commit3d09869562
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:33:27 2025 -0500 wrong number = code dont work lol i love computers commit4c93a1271b
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:29:39 2025 -0500 q commit1f5361da88
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 18:27:20 2025 -0500 same as original code now -5 days of my life commit511b070cbb
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 13:52:28 2025 -0500 pricey worky commit964fdd641b
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 11:37:29 2025 -0500 prep for pricing service work commita78c3bcba3
Author: zman <joshua.k.rzemien@gmail.com> Date: Wed Feb 5 21:51:22 2025 -0500 more stuff yay commitbd9cfca7a9
Author: zman <joshua.k.rzemien@gmail.com> Date: Tue Feb 4 22:30:33 2025 -0500 GIGA FIXED EVERYTHING OMG commit85510a4671
Author: zman <joshua.k.rzemien@gmail.com> Date: Tue Feb 4 00:01:34 2025 -0500 data model change and some new services
123 lines
4.5 KiB
Python
123 lines
4.5 KiB
Python
from fastapi.testclient import TestClient
|
|
from fastapi import BackgroundTasks
|
|
import pytest
|
|
from unittest.mock import Mock, patch
|
|
import asyncio
|
|
import os
|
|
from app.main import app
|
|
from app.services.file import FileService
|
|
from app.services.task import TaskService
|
|
|
|
client = TestClient(app)
|
|
|
|
# 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_rare"
|
|
}
|
|
|
|
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_create_manabox_file():
|
|
"""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()
|
|
|
|
def test_get_file():
|
|
"""Test retrieving a specific file"""
|
|
# Create a file first
|
|
with open(TEST_FILE_PATH, "rb") as test_file:
|
|
files = {"file": test_file}
|
|
create_response = client.post("/api/files", data=DEFAULT_METADATA, files=files)
|
|
file_id = create_response.json()["files"][0]["id"]
|
|
|
|
# Get the file
|
|
response = client.get(f"/api/files/{file_id}")
|
|
|
|
# Check response
|
|
assert response.status_code == 200
|
|
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"] == "completed"
|
|
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"] == file_id
|
|
|
|
def test_delete_file():
|
|
"""Test file deletion"""
|
|
# Create a file first
|
|
with open(TEST_FILE_PATH, "rb") as test_file:
|
|
files = {"file": test_file}
|
|
create_response = client.post("/api/files", data=DEFAULT_METADATA, files=files)
|
|
file_id = create_response.json()["files"][0]["id"]
|
|
|
|
# Delete the file
|
|
response = client.delete(f"/api/files/{file_id}")
|
|
|
|
# Check response
|
|
assert response.status_code == 200
|
|
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"] == "deleted"
|
|
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"] == file_id
|
|
|
|
def test_get_prepared_files():
|
|
"""Test retrieving files filtered by status"""
|
|
# Create a test file first
|
|
with open(TEST_FILE_PATH, "rb") as test_file:
|
|
files = {"file": test_file}
|
|
create_response = client.post("/api/files", data=DEFAULT_METADATA, files=files)
|
|
file_id = create_response.json()["files"][0]["id"]
|
|
|
|
# Get prepared files
|
|
response = client.get("/api/files?status=completed")
|
|
|
|
# Check response
|
|
assert response.status_code == 200
|
|
assert response.json()["success"] == True
|
|
|
|
# get file from id
|
|
file_data = [file for file in response.json()["files"] if file["id"] == file_id][0]
|
|
assert file_data["source"] == DEFAULT_METADATA["source"]
|
|
assert file_data["type"] == DEFAULT_METADATA["type"]
|
|
assert file_data["status"] == "completed"
|
|
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) |