Skip to content

Commit

Permalink
Add tests for DI and cleanup for seed command test
Browse files Browse the repository at this point in the history
  • Loading branch information
dsimmons87 committed Feb 6, 2023
1 parent cf6b655 commit 9acb347
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
1 change: 1 addition & 0 deletions openra/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def ready(self):
".views",
".handlers",
".tests.test_commands",
".tests.test_container",
".management.commands.test_docker",
".management.commands.test_utility",
".management.commands.import_latest_engines",
Expand Down
18 changes: 11 additions & 7 deletions openra/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ class TestCommandSeedTestData(TestCase):
def run_seeder(self):
call_command('seedtestdata', '[email protected]', 'sampleuser', 'pass123')

def mock_file_system(self):
container.data_fs.override(providers.Singleton(
MemoryFS
))

@inject
def oramap_file_exists_for_map_id(self, map_id, data_fs:FS=Provide[Container.data_fs]):
file_path = path.join('maps', str(map_id))
Expand All @@ -40,7 +35,10 @@ def oramap_file_exists_for_map_id(self, map_id, data_fs:FS=Provide[Container.dat
return False

def test_it_creates_a_super_user_with_the_details_provided(self):
self.mock_file_system()
overrides = container.override_providers(
data_fs = MemoryFS()
)

self.run_seeder()

user = User.objects.first()
Expand Down Expand Up @@ -69,8 +67,12 @@ def test_it_creates_a_super_user_with_the_details_provided(self):
timezone.now()-timezone.timedelta(days=5)
)

overrides.__exit__()

def test_it_imports_the_sample_maps(self):
self.mock_file_system()
overrides = container.override_providers(
data_fs = MemoryFS()
)

self.assertFalse(self.oramap_file_exists_for_map_id(1))
self.run_seeder()
Expand Down Expand Up @@ -110,6 +112,8 @@ def test_it_imports_the_sample_maps(self):

self.assertTrue(self.oramap_file_exists_for_map_id(lua_map.id))

overrides.__exit__()

class TestTestDocker(TestCase):

def test_it_runs_the_test_docker_command_and_prints_the_result_if_it_is_successful(self):
Expand Down
58 changes: 58 additions & 0 deletions openra/tests/test_container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@



from unittest import TestCase

from dependency_injector.wiring import Provide, inject
from fs.osfs import OSFS
from openra.services.engine_file_repository import EngineFileRepository
from openra.services.file_downloader import FileDownloader
from openra.services.github import Github

from openra.services.log import Log
from openra.services.map_file_repository import MapFileRepository
from openra.services.map_search import MapSearch
from openra.services.utility import Utility


class TestContainer(TestCase):

@inject
def test_log_singleton_can_be_injected(self,
log: Log = Provide['log']):
self.assertIsInstance(log, Log)

@inject
def test_data_fs_can_be_injected(self,
data_fs: OSFS = Provide['data_fs']):
self.assertIsInstance(data_fs, OSFS)

@inject
def test_github_can_be_injected(self,
github: Github = Provide['github']):
self.assertIsInstance(github, Github)

@inject
def test_engine_file_repository_can_be_injected(self,
engine_file_repository: EngineFileRepository = Provide['engine_file_repository']):
self.assertIsInstance(engine_file_repository, EngineFileRepository)

@inject
def test_map_file_repository_can_be_injected(self,
map_file_repository: MapFileRepository = Provide['map_file_repository']):
self.assertIsInstance(map_file_repository, MapFileRepository)

@inject
def test_file_downloader_can_be_injected(self,
file_downloader: FileDownloader = Provide['file_downloader']):
self.assertIsInstance(file_downloader, FileDownloader)

@inject
def test_utility_can_be_injected(self,
utility: Utility = Provide['utility']):
self.assertIsInstance(utility, Utility)

@inject
def test_map_search_can_be_injected(self,
map_search: MapSearch = Provide['map_search']):
self.assertIsInstance(map_search, MapSearch)

0 comments on commit 9acb347

Please sign in to comment.