Files
cleverswarm-python-client/tests/conftest.py
T
2025-09-27 20:55:31 +01:00

145 lines
4.8 KiB
Python

"""
Pytest configuration and shared fixtures for CleverSwarm Python client tests.
Copyright (c) 2016 - present Syncleus, Inc.
Copyright (c) 2016 - present CleverThis, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import tempfile
from pathlib import Path
import pytest
@pytest.fixture
def temp_directory():
"""Create a temporary directory for testing."""
with tempfile.TemporaryDirectory() as temp_dir:
yield Path(temp_dir)
@pytest.fixture
def sample_text_file(temp_directory):
"""Create a sample text file for testing."""
text_file = temp_directory / "sample.txt"
text_file.write_text("This is a sample text file for testing purposes.")
return text_file
@pytest.fixture
def sample_json_file(temp_directory):
"""Create a sample JSON file for testing."""
json_file = temp_directory / "sample.json"
json_file.write_text('{"test": "data", "ontology": "sample"}')
return json_file
@pytest.fixture
def sample_owl_file(temp_directory):
"""Create a sample OWL file for testing."""
owl_file = temp_directory / "sample.owl"
owl_file.write_text('<?xml version="1.0"?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="http://example.org/test"/></rdf:RDF>')
return owl_file
@pytest.fixture
def sample_wildcards_file(temp_directory):
"""Create a sample wildcards file for testing."""
wildcards_file = temp_directory / "sample.wildcards"
wildcards_file.write_text('{"wildcards": "test_data"}')
return wildcards_file
@pytest.fixture
def sample_ground_truth_file(temp_directory):
"""Create a sample ground truth file for testing."""
gt_file = temp_directory / "sample_gt.jsonl"
gt_file.write_text('{"id": "test1", "sent": "Test sentence", "triples": [{"sub": "Test", "rel": "is", "obj": "example"}]}')
return gt_file
@pytest.fixture
def mock_requests_response():
"""Create a mock requests response for testing."""
class MockResponse:
def __init__(self, status_code=200, json_data=None, content=None):
self.status_code = status_code
self._json_data = json_data or {}
self._content = content or b''
def json(self):
return self._json_data
@property
def content(self):
return self._content
return MockResponse
@pytest.fixture
def mock_cleverswarm_client():
"""Create a mock CleverSwarmClient for testing."""
from unittest.mock import Mock
mock_client = Mock()
mock_client.login.return_value = "test_token"
mock_client.logout.return_value = None
mock_client.is_logged_in.return_value = True
mock_client.create_unstructured_to_kg_job.return_value = "test_job_123"
mock_client.create_unstructured_to_kg_wildcards_job.return_value = "test_job_123"
mock_client.create_benchmark_job.return_value = "test_benchmark_123"
mock_client.append_to_benchmark_job.return_value = True
mock_client.get_job_status.return_value = "Completed"
mock_client.get_job_details.return_value = {"id": "test_job_123", "status": "Completed"}
mock_client.get_jobs_list.return_value = []
mock_client.delete_job.return_value = True
mock_client.poll_job_ready_or_failed.return_value = "Completed"
mock_client.retrieve_unstructured_to_kg_files.return_value = True
mock_client.retrieve_benchmark_files.return_value = True
return mock_client
@pytest.fixture
def sample_job_data():
"""Create sample job data for testing."""
return {
"id": "test_job_123",
"created": "2023-01-01T00:00:00Z",
"type": "UnstructuredWithOntology",
"status": "Completed",
"retries_count": 0,
"file_type": "JSONL",
"unstructured_sources": ["test.txt"],
"ontology_sources": ["test.json"],
"ontology_spec_sources": ["test.owl"]
}
@pytest.fixture
def sample_benchmark_job_data():
"""Create sample benchmark job data for testing."""
return {
"id": "test_benchmark_123",
"created": "2023-01-01T00:00:00Z",
"type": "Benchmark",
"status": "Completed",
"retries_count": 0,
"file_type": "JSONL",
"unstructured_sources": ["test.txt"],
"ontology_sources": ["1_university_ontology.json"],
"ground_truth_sources": ["ont_1_university_ground_truth.jsonl"]
}