-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtests.py
80 lines (69 loc) · 2.42 KB
/
tests.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
#!/usr/bin/env python
from pathlib import Path
import subprocess
import sys
import unittest
import click
from datacube import Datacube
DATACUBE_CONFIG = f'{Path(__file__).parent.absolute()}/tests/datacube.conf'
@click.group(invoke_without_command=True)
@click.pass_context
def tests(ctx):
if ctx.invoked_subcommand is None:
run()
@tests.command()
@click.option('--start_dir', '-s', required=False, type=click.STRING, default='tests',
help='The directory to discover tests inside [default: tests]')
@click.option('--top_level_dir', '-t', required=False, type=click.STRING, default='.',
help='The root directory of the project being tested [default: .]')
@click.option('-v', '--verbose', count=True, default=0)
def run(**kwargs):
try:
Datacube(config=DATACUBE_CONFIG)
except:
raise RuntimeError('Could not connect to the test database.\n'\
'Make sure to run: python tests.py initdb')
start_dir = kwargs['start_dir']
top_level_dir = kwargs['top_level_dir']
def suite():
loader = unittest.TestLoader()
suite = unittest.TestSuite()
suite.addTests(loader.discover(start_dir, top_level_dir=top_level_dir))
return suite
runner = unittest.TextTestRunner(verbosity=kwargs['verbose']+1)
result = runner.run(suite())
if result.wasSuccessful():
sys.exit(0)
else:
sys.exit(1)
@tests.command()
def initdb():
try:
cmd1 = ["createdb", "dctest"]
cmd2 = ["datacube", "-C", DATACUBE_CONFIG, "system", "init"]
subprocess.check_output(cmd1)
subprocess.check_output(cmd2)
# TODO: handle this better
except subprocess.CalledProcessError:
datacube = Datacube(config=DATACUBE_CONFIG)
if datacube is not None:
print('Check your setup; database may already be initialized')
except Exception as error:
raise error
@tests.command()
def dropdb():
try:
cmd = ["dropdb", "dctest"]
subprocess.check_output(cmd)
except subprocess.CalledProcessError as error:
try:
datacube = Datacube(config=DATACUBE_CONFIG)
if datacube is not None:
raise error
# TODO: handle this better
except Exception:
print('Check your setup; database may have already been dropped')
except Exception as error:
raise error
if __name__ == '__main__':
tests()