forked from tableau/document-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_datasource.py
More file actions
112 lines (83 loc) · 4 KB
/
test_datasource.py
File metadata and controls
112 lines (83 loc) · 4 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import unittest
import os.path
from tableaudocumentapi import Datasource, Workbook
TEST_ASSET_DIR = os.path.join(
os.path.dirname(__file__),
'assets'
)
TEST_TDS_FILE = os.path.join(
TEST_ASSET_DIR,
'datasource_test.tds'
)
TEST_TWB_FILE = os.path.join(
TEST_ASSET_DIR,
'datasource_test.twb'
)
class DataSourceFieldsTDS(unittest.TestCase):
def setUp(self):
self.ds = Datasource.from_file(TEST_TDS_FILE)
def test_datasource_returns_correct_fields(self):
self.assertIsNotNone(self.ds.fields)
self.assertIsNotNone(self.ds.fields.get('[Number of Records]', None))
def test_datasource_returns_calculation_from_fields(self):
self.assertEqual('1', self.ds.fields['[Number of Records]'].calculation)
def test_datasource_uses_metadata_record(self):
self.assertEqual('Sum', self.ds.fields['[x]'].default_aggregation)
def test_datasource_column_name_contains_apostrophy(self):
self.assertIsNotNone(self.ds.fields.get("[Today's Date]", None))
def test_datasource_field_can_get_caption(self):
self.assertEqual(self.ds.fields['[a]'].caption, 'A')
self.assertEqual(getattr(self.ds.fields['[a]'], 'caption', None), 'A')
def test_datasource_field_caption_can_be_used_to_query(self):
self.assertIsNotNone(self.ds.fields.get('A', None))
def test_datasource_field_is_nominal(self):
self.assertTrue(self.ds.fields['[a]'].is_nominal)
def test_datasource_field_is_quantitative(self):
self.assertTrue(self.ds.fields['[y]'].is_quantitative)
def test_datasource_field_is_ordinal(self):
self.assertTrue(self.ds.fields['[x]'].is_ordinal)
def test_datasource_field_datatype(self):
self.assertEqual(self.ds.fields['[x]'].datatype, 'integer')
def test_datasource_field_role(self):
self.assertEqual(self.ds.fields['[x]'].role, 'measure')
def test_datasource_field_description(self):
actual = self.ds.fields['[a]'].description
self.assertIsNotNone(actual)
self.assertTrue(u'muted gray' in actual)
class DataSourceFieldsTWB(unittest.TestCase):
def setUp(self):
self.wb = Workbook(TEST_TWB_FILE)
# Assume the first datasource in the file
self.ds = self.wb.datasources[0]
def test_datasource_fields_loaded_in_workbook(self):
self.assertIsNotNone(self.ds.fields)
self.assertIsNotNone(self.ds.fields.get('[Number of Records]', None))
class DataSourceFieldsFoundIn(unittest.TestCase):
def setUp(self):
self.wb = Workbook(TEST_TWB_FILE)
# Assume the first datasource in the file
self.ds = self.wb.datasources[0]
def test_datasource_fields_found_in_returns_fields(self):
actual_values = self.ds.fields.used_by_sheet('Sheet 1')
self.assertIsNotNone(actual_values)
self.assertEqual(1, len(actual_values))
self.assertIn('A', (x.name for x in actual_values))
def test_datasource_fields_found_in_does_not_return_fields_not_used_in_worksheet(self):
actual_values = self.ds.fields.used_by_sheet('Sheet 1')
self.assertIsNotNone(actual_values)
self.assertEqual(1, len(actual_values))
self.assertNotIn('X', (x.name for x in actual_values))
def test_datasource_fields_found_in_returns_multiple_fields(self):
actual_values = self.ds.fields.used_by_sheet('Sheet 2')
self.assertIsNotNone(actual_values)
self.assertEqual(2, len(actual_values))
self.assertIn('A', (x.name for x in actual_values))
self.assertIn('X', (x.name for x in actual_values))
self.assertNotIn('Y', (x.name for x in actual_values))
def test_datasource_fields_found_in_accepts_lists(self):
actual_values = self.ds.fields.used_by_sheet(['Sheet 1', 'Sheet 2'])
self.assertIsNotNone(actual_values)
self.assertEqual(2, len(actual_values))
self.assertIn('A', (x.name for x in actual_values))
self.assertIn('X', (x.name for x in actual_values))
self.assertNotIn('Y', (x.name for x in actual_values))