diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e50a44b..922f92f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,20 +18,22 @@ jobs: python-version: ['3.12'] steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v7 - name: Install uv - uses: astral-sh/setup-uv@v5 + uses: astral-sh/setup-uv@v10.0.1 with: python-version: ${{ matrix.python-version }} enable-cache: true - name: Install dependencies - run: | - uv sync + run: uv sync - - uses: astral-sh/ruff-action@v3 + - name: Check formatting + run: uv run ruff format --check --diff . + + - name: Check linting + run: uv run ruff check . - name: Run tests - run: | - uv run pytest + run: uv run pytest diff --git a/README.md b/README.md index d7d8a7c..f843377 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,9 @@ To upgrade Lycophron: uv tool upgrade lycophron ``` -### Initalize a local project +### Initialize a local project -Let's initalize a local project named `monkeys`: +Let's initialize a local project named `monkeys`: ```bash lycophron init monkeys @@ -173,7 +173,7 @@ This command validates the current project. I.e. it checks whether the applicati **new-template** -This command generates a CSV file from the default template, containing all the required headers. The template can either be generated with all the fields or by explicitely adding custom fields on top of the required ones. +This command generates a CSV file from the default template, containing all the required headers. The template can either be generated with all the fields or by explicitly adding custom fields on top of the required ones. `new-template --file` : creates the output file in the given path and name. `new-template --all` : creates a template using all fields (required and custom fields). diff --git a/src/lycophron/errors.py b/src/lycophron/errors.py index d4842a0..fbde50d 100644 --- a/src/lycophron/errors.py +++ b/src/lycophron/errors.py @@ -54,7 +54,7 @@ class DatabaseNotFound(DatabaseError): class DatabaseResourceNotModified(DatabaseError): - message = "Datbase resource not modified." + message = "Database resource not modified." class RecordError(LycophronError): diff --git a/src/lycophron/references.py b/src/lycophron/references.py index 17173a4..dd2228b 100644 --- a/src/lycophron/references.py +++ b/src/lycophron/references.py @@ -65,7 +65,7 @@ def store_references(self, record_id: str, references: list[dict]) -> None: target_record_id=ref["target_record_id"], source_field=ref["source_field"], target_field=ref["target_field"], - bidirectional=str(ref["bidirectional"]), + bidirectional=ref["bidirectional"], ) self.session.add(reference) diff --git a/src/lycophron/tasks/__init__.py b/src/lycophron/tasks/__init__.py index 38c3233..290d6eb 100644 --- a/src/lycophron/tasks/__init__.py +++ b/src/lycophron/tasks/__init__.py @@ -43,7 +43,7 @@ def init_celery_app(): app.conf.max_memory_per_child = 100000 app.conf.update(imports=["lycophron.tasks.tasks"]) app.conf.beat_schedule = { - "record-dispathcer": { + "record-dispatcher": { "task": "lycophron.tasks.tasks.record_dispatcher", "schedule": 5, "args": (20,), diff --git a/tests/conftest.py b/tests/conftest.py index 01c6f4f..e2e659f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,8 @@ import pytest +from lycophron.cli import lycophron + @pytest.fixture(autouse=True) def reset_singleton(): @@ -23,3 +25,16 @@ def reset_singleton(): # Restore original instances after test SingletonMeta._instances = original_instances + + +def init(runner, token="", project_name=None): + # Skip the token prompt by providing an empty string token by default. + cli_args = ["init", "--token", token] + if project_name: + cli_args.append(project_name) + result = runner.invoke(lycophron, cli_args) + + assert result.exit_code == 0 + assert "Project initialized in directory" in result.output + + return result diff --git a/tests/test_cli.py b/tests/test_cli.py index d11142a..237aced 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -6,12 +6,14 @@ """Test Lycophron CLI commands.""" import csv +import json import os import sqlite3 import tempfile from pathlib import Path from click.testing import CliRunner +from conftest import init from lycophron.cli import lycophron @@ -21,10 +23,7 @@ def test_init_command_with_name(): runner = CliRunner() with tempfile.TemporaryDirectory() as tmpdir: os.chdir(tmpdir) - result = runner.invoke(lycophron, ["init", "testproject"]) - - assert result.exit_code == 0 - assert "Project initialized in directory" in result.output + init(runner, project_name="testproject") # Verify project structure project_dir = Path(tmpdir) / "testproject" @@ -39,11 +38,7 @@ def test_init_command_default_directory(): runner = CliRunner() with tempfile.TemporaryDirectory() as tmpdir: os.chdir(tmpdir) - # Skip the token prompt by providing an empty token - result = runner.invoke(lycophron, ["init", "--token", ""]) - - assert result.exit_code == 0 - assert "Project initialized in directory" in result.output + init(runner) # Verify project structure assert (Path(tmpdir) / "files").exists() @@ -56,10 +51,7 @@ def test_init_command_with_token(): runner = CliRunner() with tempfile.TemporaryDirectory() as tmpdir: os.chdir(tmpdir) - result = runner.invoke(lycophron, ["init", "--token", "test_token123"]) - - assert result.exit_code == 0 - assert "Project initialized in directory" in result.output + init(runner, token="test_token123") # Verify token in config config_path = Path(tmpdir) / "lycophron.cfg" @@ -77,7 +69,7 @@ def test_new_template_default(): os.chdir(tmpdir) # Initialize project first - runner.invoke(lycophron, ["init"]) + init(runner) # Generate template result = runner.invoke(lycophron, ["new-template"]) @@ -105,7 +97,7 @@ def test_new_template_with_options(): os.chdir(tmpdir) # Initialize project first - runner.invoke(lycophron, ["init"]) + init(runner) # Generate template with options custom_path = os.path.join(tmpdir, "custom_template.csv") @@ -146,7 +138,7 @@ def test_new_template_minimal(): os.chdir(tmpdir) # Initialize project first - runner.invoke(lycophron, ["init"]) + init(runner) # Generate minimal template result = runner.invoke(lycophron, ["new-template", "--minimal"]) @@ -178,7 +170,7 @@ def test_load_command(): os.chdir(tmpdir) # Initialize project - runner.invoke(lycophron, ["init"]) + init(runner) # Create a simple CSV file with required fields csv_path = os.path.join(tmpdir, "test_data.csv") @@ -225,7 +217,7 @@ def test_load_db_objects(): os.chdir(tmpdir) # Initialize project - runner.invoke(lycophron, ["init"]) + init(runner) # Create test file files_dir = Path(tmpdir) / "files" @@ -292,7 +284,7 @@ def test_load_db_objects(): assert record["id"] == "record1" # Verify input_metadata - metadata = eval(record["input_metadata"]) + metadata = json.loads(record["input_metadata"]) assert metadata["metadata"]["title"] == "Test Record" assert metadata["metadata"]["publication_date"] == "2023-01-01" assert metadata["metadata"]["resource_type"]["id"] == "image" @@ -335,7 +327,7 @@ def test_load_multiple_records(): os.chdir(tmpdir) # Initialize project - runner.invoke(lycophron, ["init"]) + init(runner) # Create CSV file with multiple records csv_path = os.path.join(tmpdir, "test_multi.csv") @@ -414,13 +406,13 @@ def test_load_multiple_records(): # Verify first record metadata cursor.execute("SELECT * FROM record WHERE id = ?", ("record1",)) record1 = cursor.fetchone() - metadata1 = eval(record1["input_metadata"]) + metadata1 = json.loads(record1["input_metadata"]) assert metadata1["metadata"]["title"] == "First Record" # Verify second record metadata cursor.execute("SELECT * FROM record WHERE id = ?", ("record2",)) record2 = cursor.fetchone() - metadata2 = eval(record2["input_metadata"]) + metadata2 = json.loads(record2["input_metadata"]) assert metadata2["metadata"]["title"] == "Second Record" assert metadata2["metadata"]["resource_type"]["id"] == "publication" assert ( @@ -438,7 +430,7 @@ def test_load_update_record(): os.chdir(tmpdir) # Initialize project - runner.invoke(lycophron, ["init"]) + init(runner) # Create initial CSV file csv_path = os.path.join(tmpdir, "initial.csv") @@ -539,7 +531,7 @@ def test_load_update_record(): cursor.execute("SELECT * FROM record WHERE id = ?", ("record1",)) record = cursor.fetchone() - metadata = eval(record["input_metadata"]) + metadata = json.loads(record["input_metadata"]) # Check updated fields assert metadata["metadata"]["title"] == "Updated Title" diff --git a/tests/test_references.py b/tests/test_references.py index db1d2af..5ad3224 100644 --- a/tests/test_references.py +++ b/tests/test_references.py @@ -10,9 +10,9 @@ from unittest.mock import MagicMock, patch from click.testing import CliRunner +from conftest import init from lycophron.app import LycophronApp -from lycophron.cli import lycophron from lycophron.models import Record, RecordStatus, Reference from lycophron.template import LazyReference, TemplateEngine @@ -79,7 +79,7 @@ def test_bidirectional_references(): os.chdir(tmpdir) # Initialize project - runner.invoke(lycophron, ["init"]) + init(runner) # Create test records with bidirectional references app = LycophronApp() @@ -157,7 +157,7 @@ def test_bidirectional_references(): assert len(specimen_refs) == 1 assert specimen_refs[0].target_record_id == "figure1" assert specimen_refs[0].target_field == "doi" - assert specimen_refs[0].bidirectional == "True" + assert specimen_refs[0].bidirectional def test_reference_parsing_without_schema(): @@ -305,7 +305,7 @@ def test_two_phase_processing(mock_publish, mock_upload, mock_update, mock_creat os.chdir(tmpdir) # Initialize project - runner.invoke(lycophron, ["init"]) + init(runner) app = LycophronApp() # Mock draft creation to set upload_id