237 lines
7.5 KiB
Python
237 lines
7.5 KiB
Python
"""Tests for the snapshot module."""
|
|
|
|
import json
|
|
import os
|
|
|
|
import pytest
|
|
|
|
from api_snapshot.snapshot.manager import (
|
|
SNAPSHOT_VERSION,
|
|
Snapshot,
|
|
SnapshotManager,
|
|
SnapshotMetadata,
|
|
)
|
|
|
|
|
|
class TestSnapshotMetadata:
|
|
"""Tests for SnapshotMetadata class."""
|
|
|
|
def test_create_metadata(self):
|
|
"""Test creating snapshot metadata."""
|
|
meta = SnapshotMetadata(
|
|
version="1.0",
|
|
description="Test description",
|
|
source_url="https://api.example.com"
|
|
)
|
|
|
|
assert meta.version == "1.0"
|
|
assert meta.description == "Test description"
|
|
assert meta.source_url == "https://api.example.com"
|
|
assert meta.latency_mode == "original"
|
|
assert meta.tags == []
|
|
|
|
def test_metadata_defaults(self):
|
|
"""Test metadata default values."""
|
|
meta = SnapshotMetadata()
|
|
|
|
assert meta.version == SNAPSHOT_VERSION
|
|
assert meta.description == ""
|
|
assert meta.source_url is None
|
|
assert meta.latency_mode == "original"
|
|
assert meta.custom_latency_ms is None
|
|
assert meta.tags == []
|
|
|
|
def test_metadata_to_dict(self):
|
|
"""Test converting metadata to dictionary."""
|
|
meta = SnapshotMetadata(
|
|
version="1.0",
|
|
description="Test",
|
|
tags=["tag1", "tag2"]
|
|
)
|
|
|
|
data = meta.to_dict()
|
|
|
|
assert data["version"] == "1.0"
|
|
assert data["description"] == "Test"
|
|
assert data["tags"] == ["tag1", "tag2"]
|
|
assert "timestamp" in data
|
|
|
|
def test_metadata_from_dict(self):
|
|
"""Test creating metadata from dictionary."""
|
|
data = {
|
|
"version": "1.0",
|
|
"timestamp": "2024-01-01T00:00:00",
|
|
"description": "My snapshot",
|
|
"source_url": "https://api.com",
|
|
"latency_mode": "fixed",
|
|
"custom_latency_ms": 100,
|
|
"tags": ["api", "test"]
|
|
}
|
|
|
|
meta = SnapshotMetadata.from_dict(data)
|
|
|
|
assert meta.version == "1.0"
|
|
assert meta.description == "My snapshot"
|
|
assert meta.source_url == "https://api.com"
|
|
assert meta.latency_mode == "fixed"
|
|
assert meta.custom_latency_ms == 100
|
|
assert meta.tags == ["api", "test"]
|
|
|
|
|
|
class TestSnapshot:
|
|
"""Tests for Snapshot class."""
|
|
|
|
def test_create_snapshot(self, sample_pair):
|
|
"""Test creating a snapshot."""
|
|
meta = SnapshotMetadata(description="Test snapshot")
|
|
snapshot = Snapshot(metadata=meta, requests=[sample_pair])
|
|
|
|
assert snapshot.metadata == meta
|
|
assert len(snapshot.requests) == 1
|
|
assert snapshot.requests[0] == sample_pair
|
|
|
|
def test_snapshot_to_dict(self, sample_snapshot):
|
|
"""Test converting snapshot to dictionary."""
|
|
data = sample_snapshot.to_dict()
|
|
|
|
assert "metadata" in data
|
|
assert "requests" in data
|
|
assert data["metadata"]["description"] == "Test snapshot"
|
|
assert len(data["requests"]) == 1
|
|
|
|
def test_snapshot_from_dict(self, sample_snapshot_dict):
|
|
"""Test creating snapshot from dictionary."""
|
|
snapshot = Snapshot.from_dict(sample_snapshot_dict)
|
|
|
|
assert snapshot.metadata.description == "Test snapshot"
|
|
assert len(snapshot.requests) == 1
|
|
assert snapshot.requests[0].request.method == "GET"
|
|
|
|
|
|
class TestSnapshotManager:
|
|
"""Tests for SnapshotManager class."""
|
|
|
|
def test_init_manager(self, temp_dir):
|
|
"""Test initializing snapshot manager."""
|
|
manager = SnapshotManager(temp_dir)
|
|
|
|
assert manager.snapshot_dir == temp_dir
|
|
assert os.path.exists(temp_dir)
|
|
|
|
def test_save_snapshot(self, snapshot_manager, sample_pair):
|
|
"""Test saving a snapshot."""
|
|
path = snapshot_manager.save_snapshot(
|
|
name="test-save",
|
|
requests=[sample_pair],
|
|
description="Saved snapshot"
|
|
)
|
|
|
|
assert os.path.exists(path)
|
|
assert path.endswith("test-save.json")
|
|
|
|
with open(path) as f:
|
|
data = json.load(f)
|
|
|
|
assert data["metadata"]["description"] == "Saved snapshot"
|
|
assert len(data["requests"]) == 1
|
|
|
|
def test_save_snapshot_with_tags(self, snapshot_manager, sample_pair):
|
|
"""Test saving snapshot with tags."""
|
|
snapshot_manager.save_snapshot(
|
|
name="with-tags",
|
|
requests=[sample_pair],
|
|
tags=["tag1", "tag2"]
|
|
)
|
|
|
|
loaded = snapshot_manager.load_snapshot("with-tags")
|
|
|
|
assert "tag1" in loaded.metadata.tags
|
|
assert "tag2" in loaded.metadata.tags
|
|
|
|
def test_load_snapshot(self, snapshot_manager, snapshot_file):
|
|
"""Test loading a snapshot."""
|
|
snapshot = snapshot_manager.load_snapshot("test_snapshot")
|
|
|
|
assert snapshot.metadata.description == "Test snapshot"
|
|
assert len(snapshot.requests) == 2
|
|
|
|
def test_load_snapshot_not_found(self, snapshot_manager):
|
|
"""Test loading non-existent snapshot."""
|
|
with pytest.raises(FileNotFoundError):
|
|
snapshot_manager.load_snapshot("nonexistent")
|
|
|
|
def test_load_invalid_snapshot(self, temp_dir):
|
|
"""Test loading invalid snapshot format."""
|
|
path = os.path.join(temp_dir, "invalid.json")
|
|
|
|
with open(path, "w") as f:
|
|
json.dump({"invalid": "format"}, f)
|
|
|
|
manager = SnapshotManager(temp_dir)
|
|
|
|
with pytest.raises(ValueError):
|
|
manager.load_snapshot("invalid")
|
|
|
|
def test_delete_snapshot(self, snapshot_manager, sample_pair):
|
|
"""Test deleting a snapshot."""
|
|
snapshot_manager.save_snapshot("to-delete", requests=[sample_pair])
|
|
|
|
assert snapshot_manager.snapshot_exists("to-delete")
|
|
|
|
snapshot_manager.delete_snapshot("to-delete")
|
|
|
|
assert not snapshot_manager.snapshot_exists("to-delete")
|
|
|
|
def test_delete_nonexistent(self, snapshot_manager):
|
|
"""Test deleting non-existent snapshot."""
|
|
with pytest.raises(FileNotFoundError):
|
|
snapshot_manager.delete_snapshot("nonexistent")
|
|
|
|
def test_list_snapshots(self, snapshot_manager, sample_pair):
|
|
"""Test listing snapshots."""
|
|
snapshot_manager.save_snapshot("snap1", requests=[sample_pair])
|
|
snapshot_manager.save_snapshot("snap2", requests=[sample_pair])
|
|
|
|
snapshots = snapshot_manager.list_snapshots()
|
|
|
|
assert len(snapshots) == 2
|
|
names = [s["name"] for s in snapshots]
|
|
assert "snap1" in names
|
|
assert "snap2" in names
|
|
|
|
def test_list_empty(self, temp_dir):
|
|
"""Test listing with no snapshots."""
|
|
manager = SnapshotManager(temp_dir)
|
|
|
|
snapshots = manager.list_snapshots()
|
|
|
|
assert snapshots == []
|
|
|
|
def test_snapshot_exists(self, snapshot_manager, sample_pair):
|
|
"""Test checking if snapshot exists."""
|
|
assert not snapshot_manager.snapshot_exists("test-exists")
|
|
|
|
snapshot_manager.save_snapshot("test-exists", requests=[sample_pair])
|
|
|
|
assert snapshot_manager.snapshot_exists("test-exists")
|
|
|
|
def test_get_path(self, snapshot_manager):
|
|
"""Test getting snapshot path."""
|
|
path = snapshot_manager._get_path("my-snapshot")
|
|
|
|
assert path.endswith("my-snapshot.json")
|
|
|
|
def test_get_path_with_extension(self, snapshot_manager):
|
|
"""Test getting path with extension."""
|
|
path = snapshot_manager._get_path("my-snapshot.json")
|
|
|
|
assert path.endswith("my-snapshot.json")
|
|
|
|
|
|
class TestSnapshotVersion:
|
|
"""Tests for snapshot versioning."""
|
|
|
|
def test_version_constant(self):
|
|
"""Test version constant is defined."""
|
|
assert SNAPSHOT_VERSION == "1.0"
|