This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_database.py
92 lines (70 loc) · 2.86 KB
/
test_database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from plume import Database, Model
from utils import DB_NAME, Pokemon, Trainer
from contextlib import closing
import os
import pytest
class TestDatabaseAPI:
def setup_method(self):
self.db = Database(DB_NAME)
def table_exists(self, model):
with closing(self.db._connection.cursor()) as cursor:
query = cursor.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='{}'".format(model.__name__.lower())
)
return bool(len(query.fetchall()))
def count_tables(self):
with closing(self.db._connection.cursor()) as cursor:
query = cursor.execute("SELECT count(*) as number FROM sqlite_master WHERE type='table'").fetchall()
return query[0][0]
def test_register_a_custom_model(self):
assert self.table_exists(Trainer) == False
self.db.register(Trainer)
assert self.table_exists(Trainer) == True
def test_register_a_non_custom_model(self):
# A error in table creation must raise an exception an must not make any change in the database.
assert self.count_tables() == 0
with pytest.raises(TypeError):
self.db.register(object)
assert self.count_tables() == 0
def test_register_several_custom_models(self):
assert self.count_tables() == 0
self.db.register(Trainer, Pokemon)
assert self.table_exists(Trainer) == True
assert self.table_exists(Pokemon) == True
def test_store_a_database_reference_into_model_when_registered(self):
self.db.register(Trainer)
try:
Trainer._db
except AttributeError:
pytest.fail()
def test_no_database_reference_in_unregistered_model(self):
class CustomModel(Model):
pass
with pytest.raises(AttributeError):
CustomModel._db
def test_register_an_existing_table(self):
self.db.register(Trainer)
try:
self.db.register(Trainer)
except:
pytest.fail()
def test_all_model_fields_match_table_fields(self):
self.db.register(Trainer)
# Collect field names of the pokemon table
db_fields = [ row[1] for row in
self.db._connection.execute('PRAGMA table_info(trainer)').fetchall()
]
assert len(db_fields) == 3
for model_field in ['pk', 'name', 'age']:
assert model_field in db_fields
def test_is_slotted(self):
with pytest.raises(AttributeError):
self.db.__dict__
def test_pragma_foreign_key_is_on(self):
result = self.db._connection.execute('PRAGMA foreign_keys').fetchone()[0]
assert result == 1
def teardown_method(self):
try:
os.remove(DB_NAME)
except:
return