Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bonk1t committed Dec 19, 2023
1 parent 1b0e01a commit d6183e3
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/persistence/test_agency_config_file_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import json
from unittest.mock import patch

import pytest

from nalgonda.constants import DEFAULT_CONFIG_FILE
from nalgonda.persistence.agency_config_file_storage import AgencyConfigFileStorage
from nalgonda.persistence.agency_config_lock_manager import AgencyConfigLockManager


@pytest.fixture
def mock_config_path(temp_dir):
"""Fixture to patch the _get_config_path method in AgencyConfigFileStorage."""
agency_id = "test_agency"
config_path = temp_dir / f"config_{agency_id}.json"

with patch.object(AgencyConfigFileStorage, "_get_config_path", return_value=config_path):
yield config_path, agency_id


def test_lock_acquisition_and_release(mock_config_path):
_, agency_id = mock_config_path
storage = AgencyConfigFileStorage(agency_id)
lock = AgencyConfigLockManager.get_lock(agency_id)

with storage:
assert lock.locked(), "Lock was not acquired"

assert not lock.locked(), "Lock was not released"


def test_load_configuration(mock_config_path):
config_path, agency_id = mock_config_path
config_data = {"key": "value"}
config_path.write_text(json.dumps(config_data))

storage = AgencyConfigFileStorage(agency_id)
with storage:
loaded_config = storage.load()

assert loaded_config == config_data, "Loaded configuration does not match expected data"


def test_save_configuration(mock_config_path):
config_path, agency_id = mock_config_path
new_config = {"new_key": "new_value"}

storage = AgencyConfigFileStorage(agency_id)
with storage:
storage.save(new_config)

assert config_path.exists(), "Configuration file was not created"
with open(config_path) as file:
saved_data = json.load(file)
assert saved_data == new_config, "Saved data does not match"


def test_default_configuration_used(mock_config_path):
config_path, agency_id = mock_config_path

with open(DEFAULT_CONFIG_FILE) as file:
default_config = json.load(file)

# Ensure the specific config file does not exist to trigger default config usage
if config_path.exists():
config_path.unlink()

storage = AgencyConfigFileStorage(agency_id)
with storage:
loaded_config = storage.load()

assert loaded_config == default_config, "Default configuration was not used"
43 changes: 43 additions & 0 deletions tests/persistence/test_agency_config_lock_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import threading

from nalgonda.persistence.agency_config_lock_manager import AgencyConfigLockManager


def test_lock_uniqueness():
agency_id1 = "agency1"
agency_id2 = "agency2"

lock1 = AgencyConfigLockManager.get_lock(agency_id1)
lock2 = AgencyConfigLockManager.get_lock(agency_id2)

assert lock1 is not lock2, "Different agencies should have different locks"


def test_lock_for_same_agency():
agency_id = "agency1"

lock1 = AgencyConfigLockManager.get_lock(agency_id)
lock2 = AgencyConfigLockManager.get_lock(agency_id)

assert lock1 is lock2, "The same agency should return the same lock instance"


def test_lock_concurrency_handling():
agency_id = "agency1"
lock = AgencyConfigLockManager.get_lock(agency_id)

# Define a shared resource
shared_resource = []

def task():
with lock:
shared_resource.append(1)

# Run tasks in parallel to simulate concurrent access
threads = [threading.Thread(target=task) for _ in range(10)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()

assert len(shared_resource) == 10, "Concurrency issue: shared resource was accessed simultaneously"

0 comments on commit d6183e3

Please sign in to comment.