mirror of
https://github.com/bellingcat/auto-archiver-api.git
synced 2026-06-12 05:28:34 +03:00
adds missing tests
This commit is contained in:
@@ -1,8 +1,27 @@
|
||||
from app.shared.db import models
|
||||
from app.shared.db import worker_crud, models
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
from app.tests.web.db.test_crud import test_data
|
||||
|
||||
def test_update_sheet_last_url_archived_at(db_session):
|
||||
|
||||
# Create test sheet
|
||||
test_sheet = models.Sheet(id="sheet-123")
|
||||
db_session.add(test_sheet)
|
||||
db_session.commit()
|
||||
|
||||
# Test updating existing sheet
|
||||
assert isinstance(test_sheet.last_url_archived_at, datetime)
|
||||
before = test_sheet.last_url_archived_at
|
||||
assert worker_crud.update_sheet_last_url_archived_at(db_session, "sheet-123") is True
|
||||
db_session.refresh(test_sheet)
|
||||
assert isinstance(test_sheet.last_url_archived_at, datetime)
|
||||
assert test_sheet.last_url_archived_at > before
|
||||
|
||||
# Test non-existent sheet
|
||||
assert worker_crud.update_sheet_last_url_archived_at(db_session, "non-existent-sheet") is False
|
||||
|
||||
def test_get_group(test_data, db_session):
|
||||
from app.shared.db import worker_crud
|
||||
@@ -95,4 +114,4 @@ def test_create_task(db_session):
|
||||
assert nt.group_id == "spaceship"
|
||||
assert len(nt.tags) == 0
|
||||
assert len(nt.urls) == 0
|
||||
assert nt.created_at is not None
|
||||
assert nt.created_at is not None
|
||||
36
app/tests/shared/test_business_logic.py
Normal file
36
app/tests/shared/test_business_logic.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from datetime import datetime, timedelta
|
||||
from unittest.mock import MagicMock, patch
|
||||
import pytest
|
||||
from app.shared.business_logic import get_store_archive_until
|
||||
|
||||
class Test_get_store_archive_until:
|
||||
GROUP_ID = "test-group"
|
||||
|
||||
def test_group_not_found(self, db_session):
|
||||
with pytest.raises(AssertionError) as exc:
|
||||
get_store_archive_until(db_session, self.GROUP_ID)
|
||||
assert str(exc.value) == f"Group {self.GROUP_ID} not found."
|
||||
|
||||
@patch("app.shared.db.worker_crud.get_group")
|
||||
def test_no_max_lifespan(self, mock_get_group, db_session):
|
||||
group = MagicMock()
|
||||
group.permissions = {"max_archive_lifespan_months": -1}
|
||||
mock_get_group.return_value = group
|
||||
|
||||
result = get_store_archive_until(db_session, self.GROUP_ID)
|
||||
assert result is None
|
||||
mock_get_group.assert_called_once_with(db_session, self.GROUP_ID)
|
||||
|
||||
@patch("app.shared.db.worker_crud.get_group")
|
||||
def test_with_max_lifespan(self, mock_get_group, db_session):
|
||||
group = MagicMock()
|
||||
group.permissions = {"max_archive_lifespan_months": 6}
|
||||
mock_get_group.return_value = group
|
||||
|
||||
result = get_store_archive_until(db_session, self.GROUP_ID)
|
||||
expected = datetime.now() + timedelta(days=180) # 6 months
|
||||
|
||||
assert isinstance(result, datetime)
|
||||
# Allow 1 second difference due to execution time
|
||||
assert abs(result - expected) < timedelta(seconds=1)
|
||||
mock_get_group.assert_called_once_with(db_session, self.GROUP_ID)
|
||||
31
app/tests/shared/utils/test_misc.py
Normal file
31
app/tests/shared/utils/test_misc.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from app.shared.utils.misc import fnv1a_hash_mod
|
||||
|
||||
|
||||
def test_fnv1a_hash_mod():
|
||||
# Test basic string hashing
|
||||
assert fnv1a_hash_mod("test", 10) == fnv1a_hash_mod("test", 10)
|
||||
assert 0 <= fnv1a_hash_mod("test", 10) < 10
|
||||
|
||||
# Test different strings give different hashes
|
||||
assert fnv1a_hash_mod("test1", 100) != fnv1a_hash_mod("test2", 100)
|
||||
|
||||
# Test different modulos
|
||||
hash1 = fnv1a_hash_mod("test", 5)
|
||||
hash2 = fnv1a_hash_mod("test", 10)
|
||||
assert 0 <= hash1 < 5
|
||||
assert 0 <= hash2 < 10
|
||||
|
||||
# Test empty string
|
||||
assert isinstance(fnv1a_hash_mod("", 10), int)
|
||||
assert 0 <= fnv1a_hash_mod("", 10) < 10
|
||||
|
||||
# Test long string
|
||||
long_str = "a" * 1000
|
||||
assert 0 <= fnv1a_hash_mod(long_str, 20) < 20
|
||||
|
||||
# Test unicode string
|
||||
assert isinstance(fnv1a_hash_mod("测试", 10), int)
|
||||
assert 0 <= fnv1a_hash_mod("测试", 10) < 10
|
||||
|
||||
# Test modulo = 1 edge case
|
||||
assert fnv1a_hash_mod("test", 1) == 0
|
||||
Reference in New Issue
Block a user