forked from pudo/dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_freeze_app.py
67 lines (58 loc) · 2.48 KB
/
test_freeze_app.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
# coding: utf-8
"""
Test CLI following the recipe at http://dustinrcollins.com/testing-python-command-line-apps
"""
import os
import unittest
from tempfile import mkdtemp
from shutil import rmtree
from copy import copy
from six import StringIO
from dataset import connect
from dataset.util import FreezeException
from dataset.freeze.config import Configuration, Export
from dataset.freeze.app import create_parser, freeze_with_config, freeze_export
from .sample_data import TEST_DATA
class FreezeAppTestCase(unittest.TestCase):
"""
Base TestCase class, sets up a CLI parser
"""
def setUp(self):
parser = create_parser()
self.parser = parser
self.d = mkdtemp()
self.db_path = os.path.abspath(os.path.join(self.d, 'db.sqlite'))
self.db = 'sqlite:///' + self.db_path
_db = connect(self.db)
tbl = _db['weather']
for i, row in enumerate(TEST_DATA):
_row = copy(row)
_row['count'] = i
_row['bool'] = True
_row['none'] = None
tbl.insert(_row)
def tearDown(self):
rmtree(self.d, ignore_errors=True)
def test_with_config(self):
cfg = Configuration(os.path.join(os.path.dirname(__file__), 'Freezefile.yaml'))
cfg.data['common']['database'] = self.db
cfg.data['common']['prefix'] = self.d
cfg.data['common']['query'] = 'SELECT * FROM weather'
cfg.data['exports'] = [
{'filename': '{{identity:count}}.json', 'mode': 'item', 'transform': {'bool': 'identity'}},
{'filename': 'weather.json', 'format': 'tabson'},
{'filename': 'weather.csv', 'fileobj': StringIO(), 'format': 'csv'},
{'filename': 'weather.json', 'fileobj': StringIO(), 'format': 'tabson'},
{'filename': 'weather.json', 'format': 'tabson', 'callback': 'read'},
{'skip': True}]
freeze_with_config(cfg, db=self.db)
self.assertRaises(FreezeException, freeze_export, Export(cfg.data['common'], {'query': 'SELECT * FROM notable'}))
def test_unicode_path(self):
cfg = Configuration(os.path.join(os.path.dirname(__file__), 'Freezefile.yaml'))
cfg.data['common']['database'] = self.db
cfg.data['common']['prefix'] = os.path.join(self.d, u'über')
cfg.data['common']['query'] = 'SELECT * FROM weather'
cfg.data['exports'] = [{'filename': 'weather.csv', 'format': 'csv'}]
freeze_with_config(cfg, db=self.db)
if __name__ == '__main__':
unittest.main()