-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_tables.py
executable file
·93 lines (78 loc) · 2.81 KB
/
test_tables.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
92
93
#!/usr/bin/env python
"""Unit test of table definitions
This is a few quick checks and particularly examining tricky stuff; testing
all the tables amounts to a check that SQLalchemy works rather than that
our definitions work.
"""
import os
import os.path
import shutil
import sqlite3
import tempfile
import unittest
import sqlalchemy
import sqlalchemy.inspection
import dbp_testing
import dbprocessing.tables
class TableDefnTests(unittest.TestCase):
"""Test table definitions"""
def setUp(self):
"""Create the test database"""
self.td = tempfile.mkdtemp()
self.engine = sqlalchemy.create_engine(
'sqlite:///{}'.format(os.path.join(self.td, 'test.sqlite')),
echo=False)
self.metadata = sqlalchemy.schema.MetaData()
def tearDown(self):
"""Delete test database"""
self.engine.dispose()
shutil.rmtree(self.td)
def makeTables(self, *tables):
"""Helper functions, makes all tables named in args, in that order
Parameters
----------
tables : list of str
names of tables to make
Returns
-------
dict
Created table objects, keyed by name
"""
created = {
name: sqlalchemy.schema.Table(
name, self.metadata, *dbprocessing.tables.definition(name))
for name in tables}
self.metadata.create_all(bind=self.engine)
actual = sqlalchemy.inspection.inspect(self.engine)\
.get_table_names()
self.assertEqual(sorted(tables), sorted(actual))
return created
def testFile(self):
"""Test file table definition"""
# file requires product requires instrument requires satellite
# requires mission
t = self.makeTables(
'file', 'product', 'instrument', 'satellite', 'mission')['file']
# Check that got desired columns (in order)
self.assertEqual(
['file_id', 'filename',
'utc_file_date', 'utc_start_time', 'utc_stop_time',
'data_level',
'interface_version', 'quality_version', 'revision_version',
'verbose_provenance', 'check_date', 'quality_comment', 'caveats',
'file_create_date',
'met_start_time', 'met_stop_time', 'exists_on_disk',
'quality_checked', 'product_id', 'shasum', 'process_keywords'],
[c.name for c in t.columns])
# Check all indices in place
self.assertEqual(
['ix_file_big',
'ix_file_data_level',
'ix_file_file_id',
'ix_file_filename',
'ix_file_utc_file_date',
'ix_file_utc_start_time',
'ix_file_utc_stop_time'],
sorted([i.name for i in t.indexes]))
if __name__ == "__main__":
unittest.main()