Arch/tests/test_documents.py

157 lines
5.8 KiB
Python
Raw Normal View History

2025-03-03 17:50:11 -03:00
import pytest
import os
import io
import json
from werkzeug.datastructures import FileStorage
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
class TestDocuments:
"""Test document management functionality."""
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
def setup_project(self, logged_in_client, app):
"""Helper to create a test project."""
# Create a project
2025-03-04 06:38:19 -03:00
logged_in_client.post(
"/projects/create",
data={
"codigo": "TESTDOC",
"descripcion": "Proyecto para documentos",
"cliente": "Cliente Test",
"esquema": "TEST001",
"destinacion": "Pruebas",
"notas": "Proyecto para pruebas de documentos",
},
follow_redirects=True,
)
2025-03-03 17:50:11 -03:00
# Find the project ID
with app.app_context():
2025-03-04 06:38:19 -03:00
projects_dir = os.path.join(app.config["STORAGE_PATH"], "projects")
project_dirs = [d for d in os.listdir(projects_dir) if "TESTDOC" in d]
2025-03-03 17:50:11 -03:00
assert len(project_dirs) > 0
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
project_dir = project_dirs[0]
2025-03-04 06:38:19 -03:00
return project_dir.split("_")[0].replace("@", "")
2025-03-03 17:50:11 -03:00
def test_upload_document(self, logged_in_client, app):
"""Test uploading a document to a project."""
# Create a project and get its ID
project_id = self.setup_project(logged_in_client, app)
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
# Create a test file
test_file = FileStorage(
2025-03-04 06:38:19 -03:00
stream=io.BytesIO(b"This is a test document content."),
filename="test_document.txt",
content_type="text/plain",
2025-03-03 17:50:11 -03:00
)
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
# Upload the document
response = logged_in_client.post(
2025-03-04 06:38:19 -03:00
f"/projects/{project_id}/documents/upload",
2025-03-03 17:50:11 -03:00
data={
2025-03-04 06:38:19 -03:00
"tipo_doc": "Documento de Prueba", # Match schema type from TEST001
"descripcion": "Documento de prueba para tests",
"file": test_file,
2025-03-03 17:50:11 -03:00
},
2025-03-04 06:38:19 -03:00
content_type="multipart/form-data",
follow_redirects=True,
2025-03-03 17:50:11 -03:00
)
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
assert response.status_code == 200
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
# Verify document was created
with app.app_context():
project_path = None
2025-03-04 06:38:19 -03:00
projects_dir = os.path.join(app.config["STORAGE_PATH"], "projects")
2025-03-03 17:50:11 -03:00
for dir_name in os.listdir(projects_dir):
2025-03-04 06:38:19 -03:00
if dir_name.startswith(f"@{project_id}_"):
2025-03-03 17:50:11 -03:00
project_path = os.path.join(projects_dir, dir_name)
break
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
assert project_path is not None
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
# Check for documents directory with content
2025-03-04 06:38:19 -03:00
docs_path = os.path.join(project_path, "documents")
2025-03-03 17:50:11 -03:00
assert os.path.exists(docs_path)
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
# Should have at least one document folder
assert len(os.listdir(docs_path)) > 0
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
def test_document_versions(self, logged_in_client, app):
"""Test document versioning functionality."""
# Create a project and upload first version
project_id = self.setup_project(logged_in_client, app)
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
# Upload first version
test_file1 = FileStorage(
2025-03-04 06:38:19 -03:00
stream=io.BytesIO(b"Document content version 1"),
filename="test_versioning.txt",
content_type="text/plain",
2025-03-03 17:50:11 -03:00
)
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
logged_in_client.post(
2025-03-04 06:38:19 -03:00
f"/projects/{project_id}/documents/upload",
2025-03-03 17:50:11 -03:00
data={
2025-03-04 06:38:19 -03:00
"tipo_doc": "Documento de Prueba",
"descripcion": "Documento para pruebas de versiones",
"file": test_file1,
2025-03-03 17:50:11 -03:00
},
2025-03-04 06:38:19 -03:00
content_type="multipart/form-data",
follow_redirects=True,
2025-03-03 17:50:11 -03:00
)
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
# Find the document ID
doc_id = None
with app.app_context():
2025-03-04 06:38:19 -03:00
projects_dir = os.path.join(app.config["STORAGE_PATH"], "projects")
2025-03-03 17:50:11 -03:00
for dir_name in os.listdir(projects_dir):
2025-03-04 06:38:19 -03:00
if dir_name.startswith(f"@{project_id}_"):
2025-03-03 17:50:11 -03:00
project_path = os.path.join(projects_dir, dir_name)
2025-03-04 06:38:19 -03:00
docs_path = os.path.join(project_path, "documents")
2025-03-03 17:50:11 -03:00
# Get first document directory
doc_dirs = os.listdir(docs_path)
if doc_dirs:
2025-03-04 06:38:19 -03:00
doc_id = doc_dirs[0].split("_")[0].replace("@", "")
2025-03-03 17:50:11 -03:00
break
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
assert doc_id is not None
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
# Upload second version of the same document
test_file2 = FileStorage(
2025-03-04 06:38:19 -03:00
stream=io.BytesIO(b"Document content version 2 - UPDATED"),
filename="test_versioning_v2.txt",
content_type="text/plain",
2025-03-03 17:50:11 -03:00
)
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
response = logged_in_client.post(
2025-03-04 06:38:19 -03:00
f"/projects/{project_id}/documents/{doc_id}/upload",
data={"descripcion": "Segunda versión del documento", "file": test_file2},
content_type="multipart/form-data",
follow_redirects=True,
2025-03-03 17:50:11 -03:00
)
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
assert response.status_code == 200
2025-03-04 06:38:19 -03:00
2025-03-03 17:50:11 -03:00
# Check for multiple versions in metadata
with app.app_context():
2025-03-04 06:38:19 -03:00
projects_dir = os.path.join(app.config["STORAGE_PATH"], "projects")
2025-03-03 17:50:11 -03:00
for dir_name in os.listdir(projects_dir):
2025-03-04 06:38:19 -03:00
if dir_name.startswith(f"@{project_id}_"):
2025-03-03 17:50:11 -03:00
project_path = os.path.join(projects_dir, dir_name)
2025-03-04 06:38:19 -03:00
docs_path = os.path.join(project_path, "documents")
2025-03-03 17:50:11 -03:00
# Find document directory
for doc_dir in os.listdir(docs_path):
2025-03-04 06:38:19 -03:00
if doc_dir.startswith(f"@{doc_id}_"):
2025-03-03 17:50:11 -03:00
doc_path = os.path.join(docs_path, doc_dir)
2025-03-04 06:38:19 -03:00
meta_path = os.path.join(doc_path, "meta.json")
2025-03-03 17:50:11 -03:00
# Check metadata for versions
if os.path.exists(meta_path):
2025-03-04 06:38:19 -03:00
with open(meta_path, "r") as f:
2025-03-03 17:50:11 -03:00
metadata = json.load(f)
2025-03-04 06:38:19 -03:00
assert "versions" in metadata
assert len(metadata["versions"]) >= 2