Skip to content

Commit 3819ed5

Browse files
committed
Add DataBaseResolver tests
1 parent 2a8286b commit 3819ed5

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

tests/extensions/__init__.py

Whitespace-only changes.
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import os
2+
import pytest
3+
from pathlib import Path
4+
5+
from investing_algorithm_framework.extensions import DatabaseResolver
6+
from investing_algorithm_framework.extensions.database_resolver import \
7+
DatabaseOperationalException
8+
from investing_algorithm_framework.core.context import Context
9+
from tests.resources.utils import random_string
10+
11+
from sqlalchemy import Column, Integer, String
12+
13+
BASE_DIR = str(Path(__file__).parent.parent)
14+
15+
16+
class TestDatabaseResolverConfiguration:
17+
18+
def test_sqlite_configuration_with_context(self) -> None:
19+
os.environ.setdefault(
20+
'INVESTING_ALGORITHM_FRAMEWORK_SETTINGS_MODULE',
21+
'tests.resources.database_settings'
22+
)
23+
context = Context()
24+
context.config.configure()
25+
db = DatabaseResolver(context=context)
26+
db.configure()
27+
28+
assert os.path.isfile(os.path.join(BASE_DIR, 'test_db.sqlite3'))
29+
os.remove(os.path.join(BASE_DIR, 'test_db.sqlite3'))
30+
31+
def test_sqlite_configuration_from_config_object(self) -> None:
32+
db = DatabaseResolver()
33+
db.configure({
34+
'DATABASE_TYPE': 'sqlite3',
35+
'DATABASE_NAME': 'test_db',
36+
'DATABASE_DIRECTORY_PATH': BASE_DIR
37+
})
38+
39+
assert os.path.isfile(os.path.join(BASE_DIR, 'test_db.sqlite3'))
40+
os.remove(os.path.join(BASE_DIR, 'test_db.sqlite3'))
41+
42+
def test_without_database_configuration(self) -> None:
43+
os.environ.setdefault(
44+
'INVESTING_ALGORITHM_FRAMEWORK_SETTINGS_MODULE',
45+
'tests.resources.standard_settings'
46+
)
47+
context = Context()
48+
context.config.configure()
49+
db = DatabaseResolver()
50+
51+
with pytest.raises(DatabaseOperationalException) as exc_inf:
52+
db.configure()
53+
assert exc_inf == 'Context config has no database configuration'
54+
55+
def test_without_database_type_configuration(self) -> None:
56+
os.environ.setdefault(
57+
'INVESTING_ALGORITHM_FRAMEWORK_SETTINGS_MODULE',
58+
'tests.resources.database_settings_without_database_type'
59+
)
60+
context = Context()
61+
context.config.configure()
62+
db = DatabaseResolver(context)
63+
64+
with pytest.raises(DatabaseOperationalException) as exc_inf:
65+
db.configure()
66+
assert exc_inf == 'Context config database configuration has no ' \
67+
'database type defined'
68+
69+
with pytest.raises(DatabaseOperationalException) as exc_inf:
70+
db.configure(database_config={})
71+
assert exc_inf == 'Context config database configuration has no ' \
72+
'database type defined'
73+
74+
75+
class TestDatabaseResolver:
76+
77+
db = DatabaseResolver()
78+
79+
def setup_method(self) -> None:
80+
self.db.configure({
81+
'DATABASE_TYPE': 'sqlite3',
82+
'DATABASE_NAME': 'test_db',
83+
'DATABASE_DIRECTORY_PATH': BASE_DIR
84+
})
85+
86+
class TestModel(db.model):
87+
id = Column(Integer, primary_key=True)
88+
name = Column(String)
89+
90+
def test_creating(self):
91+
self.db.initialize_tables()
92+
assert len(self.TestModel.query.all()) == 0
93+
94+
test_model_one = self.TestModel(name=random_string(10))
95+
96+
assert len(self.TestModel.query.all()) == 0
97+
98+
test_model_one.save()
99+
100+
assert len(self.TestModel.query.all()) == 1
101+
102+
test_model_two = self.TestModel(name=random_string(10))
103+
104+
assert len(self.TestModel.query.all()) == 1
105+
106+
test_model_two.save()
107+
108+
assert len(self.TestModel.query.all()) == 2
109+
self.db.session.commit()
110+
111+
def test_deleting(self) -> None:
112+
self.db.initialize_tables()
113+
assert len(self.TestModel.query.all()) == 0
114+
115+
test_model_one = self.TestModel(name=random_string(10))
116+
test_model_two = self.TestModel(name=random_string(10))
117+
test_model_one.save()
118+
test_model_two.save()
119+
120+
assert len(self.TestModel.query.all()) == 2
121+
122+
test_model_two.delete()
123+
124+
assert len(self.TestModel.query.all()) == 1
125+
126+
test_model_one.delete()
127+
128+
assert len(self.TestModel.query.all()) == 0
129+
self.db.session.commit()
130+
131+
def test_updating(self) -> None:
132+
self.db.initialize_tables()
133+
assert len(self.TestModel.query.all()) == 0
134+
135+
test_model_one = self.TestModel(name=random_string(10))
136+
test_model_two = self.TestModel(name=random_string(10))
137+
test_model_one.save()
138+
test_model_two.save()
139+
140+
model_one_name = test_model_two.name
141+
self.db.session.commit()
142+
143+
assert len(self.TestModel.query.all()) == 2
144+
145+
test_model_one.update(name=random_string(10))
146+
147+
test_model_one.save()
148+
149+
self.db.session.commit()
150+
151+
assert model_one_name != test_model_one.name
152+
153+
def teardown_method(self) -> None:
154+
os.remove(os.path.join(BASE_DIR, 'test_db.sqlite3'))

0 commit comments

Comments
 (0)