def test_module_type_serialization(self): module_type = ModuleTypeFactory(name='foo', schema={ 'some': 'thing', }) assert_that(module_type.serialize(), has_key('id')) assert_that(module_type.serialize(), has_entry('name', 'foo')) assert_that(module_type.serialize(), has_entry('schema', {'some': 'thing'}))
def test_list_types_filter_by_name_case_insensitive(self): ModuleTypeFactory(name="foo", schema={}) ModuleTypeFactory(name="bar", schema={}) resp = self.client.get('/module-type?name=fOo') resp_json = json.loads(resp.content) assert_that(len(resp_json), 1) assert_that( resp_json, has_item(has_entry('name', 'foo')), )
def test_module_type_serialization(self): module_type = ModuleTypeFactory( name='foo', schema={ 'some': 'thing', } ) assert_that(module_type.serialize(), has_key('id')) assert_that(module_type.serialize(), has_entry('name', 'foo')) assert_that( module_type.serialize(), has_entry('schema', {'some': 'thing'}))
def test_dashboard_with_section_slug_returns_module_and_children(self): dashboard = DashboardFactory(slug='my-first-slug') dashboard.owners.add(self.user) module_type = ModuleTypeFactory() parent = ModuleFactory( type=module_type, slug='section-we-want', order=1, dashboard=dashboard ) ModuleFactory( type=module_type, slug='module-we-want', order=2, dashboard=dashboard, parent=parent) resp = self.client.get( '/public/dashboards', {'slug': 'my-first-slug/section-we-want'}) data = json.loads(resp.content) assert_that(data['modules'], contains(has_entry('slug', 'section-we-want'))) assert_that(len(data['modules']), equal_to(1)) assert_that(data['modules'][0]['modules'], contains(has_entry('slug', 'module-we-want'))) assert_that(data, has_entry('page-type', 'module'))
def test_dashboard_with_tab_slug_only_returns_tab(self): dashboard = DashboardFactory(slug='my-first-slug') dashboard.owners.add(self.user) module_type = ModuleTypeFactory() ModuleFactory( type=module_type, dashboard=dashboard, slug='module-we-want', info=['module-info'], title='module-title', options={ 'tabs': [ { 'slug': 'tab-we-want', 'title': 'tab-title' }, { 'slug': 'tab-we-dont-want', } ] }) ModuleFactory( type=module_type, dashboard=dashboard, slug='module-we-dont-want') resp = self.client.get( '/public/dashboards', {'slug': 'my-first-slug/module-we-want/module-we-want-tab-we-want'} ) data = json.loads(resp.content) assert_that(data['modules'], contains( has_entries({'slug': 'tab-we-want', 'info': contains('module-info'), 'title': 'module-title - tab-title' }))) assert_that(data, has_entry('page-type', 'module'))
def setUpClass(cls): cls.data_group = DataGroupFactory(name='group') cls.data_type = DataTypeFactory(name='type') cls.data_set = DataSetFactory( data_group=cls.data_group, data_type=cls.data_type, ) cls.module_type = ModuleTypeFactory( name='a-type', schema={ 'type': 'object', 'properties': { 'thing': { 'type': 'string', 'required': True } }, '$schema': "http://json-schema.org/draft-03/schema#" } ) cls.dashboard = DashboardFactory( published=True, title='A service', slug='some-slug', )
def test_list_types(self): ModuleTypeFactory(name="foo", schema={}) ModuleTypeFactory(name="bar", schema={}) resp = self.client.get('/module-type') resp_json = json.loads(resp.content) assert_that(len(resp_json), 2) assert_that( resp_json, has_item(has_entry('name', 'foo')), ) assert_that( resp_json, has_item(has_entry('name', 'bar')), )
def test_dashboard_with_nonexistent_tab_slug_returns_nothing(self): dashboard = DashboardFactory(slug='my-first-slug') dashboard.owners.add(self.user) module_type = ModuleTypeFactory() ModuleFactory( type=module_type, dashboard=dashboard, slug='module', info=['module-info'], title='module-title', options={ 'tabs': [ { 'slug': 'tab-we-want', 'title': 'tab-title' }, { 'slug': 'tab-we-dont-want', } ] }) ModuleFactory( type=module_type, dashboard=dashboard, slug='module-we-dont-want') resp = self.client.get( '/public/dashboards', {'slug': 'my-first-slug/module/module-non-existent-tab'} ) data = json.loads(resp.content) assert_that(data, has_entry('status', 404))
def test_create_dashboard_ok_with_modules(self): module_type = ModuleTypeFactory() def make_module(slug, title, order): return { 'slug': slug, 'title': title, 'type_id': module_type.id, 'description': 'a description', 'info': [], 'options': {}, 'order': order, 'modules': [], } data = self._get_dashboard_payload() data['modules'] = [ make_module('foo', 'The Foo', 1), make_module('bar', 'The Bar', 4), make_module('monkey', 'The the', 2), ] data['modules'][2]['modules'] = [make_module('chimp', 'Ooh', 3)] resp = self.client.post( '/dashboard', to_json(data), content_type="application/json", HTTP_AUTHORIZATION='Bearer correct-token') assert_that(resp.status_code, equal_to(200)) dashboard = Dashboard.objects.first() assert_that(dashboard.module_set.count(), equal_to(4)) nested_module = Module.objects.get(slug='chimp') assert_that(nested_module.parent.slug, equal_to('monkey'))
def test_spotlightify_with_a_nested_module(self): section_type = ModuleTypeFactory(name='section') graph_type = ModuleTypeFactory(name='graph') parent = ModuleFactory(type=section_type, slug='a-module', order=1, dashboard=self.dashboard) ModuleFactory(type=graph_type, slug='b-module', order=2, dashboard=self.dashboard, parent=parent) spotlightify = self.dashboard.spotlightify() assert_that(spotlightify, has_entry('modules', contains(parent.spotlightify()))) assert_that(len(spotlightify['modules']), equal_to(1))
def setUpClass(cls): cls.data_group = DataGroupFactory(name='group') cls.data_type = DataTypeFactory(name='type') cls.data_set = DataSetFactory( data_group=cls.data_group, data_type=cls.data_type, ) cls.module_type = ModuleTypeFactory(name='graph', schema={}) cls.dashboard_a = DashboardFactory(slug='a-dashboard', published=False) cls.dashboard_b = DashboardFactory(slug='b-dashboard', published=False)
def test_dashboard_with_non_existing_module_slug_returns_nothing(self): dashboard = DashboardFactory(slug='my-first-slug') module_type = ModuleTypeFactory() ModuleFactory( type=module_type, dashboard=dashboard, slug='module-we-want') resp = self.client.get( '/public/dashboards', {'slug': 'my-first-slug/nonexisting-module'}) data = json.loads(resp.content) assert_that(data, has_entry('status', 404))
def test_spotlightify_with_a_module(self): module_type = ModuleTypeFactory(name='graph', schema={}) ModuleFactory( type=module_type, dashboard=self.dashboard, slug='a-module', options={}, order=1, ) spotlight_dashboard = self.dashboard.spotlightify() assert_that(len(spotlight_dashboard['modules']), equal_to(1)) assert_that(spotlight_dashboard['modules'], has_item(has_entry('slug', 'a-module')))
def test_dashboard_with_module_slug_only_returns_module(self): dashboard = DashboardFactory(slug='my-first-slug') module_type = ModuleTypeFactory() ModuleFactory(type=module_type, dashboard=dashboard, slug='module-we-want') ModuleFactory(type=module_type, dashboard=dashboard, slug='module-we-dont-want') resp = self.client.get('/public/dashboards', {'slug': 'my-first-slug/module-we-want'}) data = json.loads(resp.content) assert_that(data['modules'], contains(has_entry('slug', 'module-we-want'))) assert_that(data, has_entry('page-type', 'module'))
def test_dashboards_by_dataset_some_dashboards(self): data_set = DataSet.objects.get(name='group1_type1') dashboard = DashboardFactory() module_type = ModuleTypeFactory() ModuleFactory(type=module_type, dashboard=dashboard, data_set=data_set) resp = self.client.get( '/data-sets/group1_type1/dashboard', HTTP_AUTHORIZATION='Bearer development-oauth-access-token') assert_equal(resp.status_code, 200) json_resp = json.loads(resp.content.decode('utf-8')) assert_equal(len(json_resp), 1) assert_equal(json_resp[0]['id'], str(dashboard.id))
def test_create_dashboard_fails_with_invalid_module(self): module_type = ModuleTypeFactory() module = { 'slug': 'bad slug', 'title': 'bad slug', 'type_id': module_type.id, 'description': '', 'info': [], 'options': {}, 'order': 1, } data = self._get_dashboard_payload() data['modules'] = [module] resp = self.client.post( '/dashboard', to_json(data), content_type="application/json", HTTP_AUTHORIZATION='Bearer correct-token') assert_that(resp.status_code, equal_to(400))
def test_serialize_contains_nested_modules(self): module_type = ModuleTypeFactory() ModuleFactory(type=module_type, dashboard=self.dashboard, order=3, slug='slug3') parent = ModuleFactory(type=module_type, dashboard=self.dashboard, order=1, slug='slug1') ModuleFactory(parent=parent, type=module_type, order=2, slug='slug2') data = self.dashboard.serialize() assert_that( data['modules'], contains(has_entry('slug', 'slug1'), has_entry('slug', 'slug3'))) assert_that(data['modules'][0]['modules'][0], has_entry('slug', 'slug2')) assert_that(data['modules'], is_not(has_entry('slug', 'slug2')))
def test_modules_are_ordered_correctly(self): dashboard = DashboardFactory(slug='my-first-slug') dashboard.owners.add(self.user) module_type = ModuleTypeFactory() ModuleFactory( type=module_type, dashboard=dashboard, order=2, slug='slug2') ModuleFactory( type=module_type, dashboard=dashboard, order=1, slug='slug1') ModuleFactory( type=module_type, dashboard=dashboard, order=3, slug='slug3') resp = self.client.get( '/public/dashboards', {'slug': 'my-first-slug'}) data = json.loads(resp.content) assert_that(data['modules'], contains( has_entry('slug', 'slug1'), has_entry('slug', 'slug2'), has_entry('slug', 'slug3')))
def test_options_validated_against_type(self): module_type = ModuleTypeFactory(name='some-graph', schema={ "type": "object", "properties": { "title": { "type": "string", "maxLength": 3 } } }) module = Module(slug='a-module', type=module_type, dashboard=self.dashboard_a, options={'title': 'bar'}) assert_that(calling(lambda: module.validate_options()), is_not(raises(ValidationError))) module.options = {'title': 'foobar'} assert_that(calling(lambda: module.validate_options()), raises(ValidationError))
def setUpClass(cls): cls.data_group = DataGroupFactory(name='group') cls.data_type = DataTypeFactory(name='type') cls.data_set = DataSetFactory( data_group=cls.data_group, data_type=cls.data_type, ) cls.module_type = ModuleTypeFactory( name='a-type', schema={ 'type': 'object', 'properties': { 'thing': { 'type': 'string', 'required': True } }, '$schema': "http://json-schema.org/draft-03/schema#" }) cls.dashboard = DashboardFactory( published=True, title='A service', slug='some-slug', ) cls.dashboard_without_owner = DashboardFactory( published=True, title='Another service', slug='some-other-slug', ) cls.user, _ = User.objects.get_or_create( email='*****@*****.**') cls.dashboard.owners.add(cls.user)
def test_truncated_slug_is_replaced_in_modules(): DataSetFactory(data_group__name='transactional-services', data_type__name='summaries') ModuleTypeFactory(name='kpi') ModuleTypeFactory(name='bar_chart_with_number') munger = SpreadsheetMunger({ 'names_transaction_name': 11, 'names_transaction_slug': 12, 'names_service_name': 9, 'names_service_slug': 10, 'names_tx_id': 19, 'names_other_notes': 17, 'names_description': 8, }) mock_account = Mock() mock_account.open_by_key() \ .worksheet().get_all_values.return_value = tx_worksheet tx = munger.load_tx_worksheet(mock_account) mock_account = Mock() mock_account.open_by_key() \ .worksheet().get_all_values.return_value = names_worksheet names = munger.load_names_worksheet(mock_account) record = munger.merge(tx, names)[0] truncated_slug = 'truncated-{}'.format(random.randrange(1e7)) full_tx_id = record['tx_id'] record['tx_id'] = truncated_slug DashboardFactory(slug=truncated_slug) summaries = [{ 'service_id': record['tx_id'], 'type': 'quarterly', 'cost_per_transaction': 0, 'digital_takeup': 0 }, { 'service_id': record['tx_id'], 'type': 'seasonally-adjusted', 'total_cost': 0 }, { 'service_id': full_tx_id, 'type': 'quarterly', 'cost_per_transaction': 0, 'digital_takeup': 0 }, { 'service_id': full_tx_id, 'type': 'seasonally-adjusted', 'total_cost': 0 }] import_dashboard(record, summaries, dry_run=False) initial_modules = Module.objects.all() service_id_filters = set([ module.query_parameters['filter_by'][0] for module in initial_modules ]) assert_that(len(service_id_filters), equal_to(1)) assert_that(service_id_filters.pop(), equal_to('service_id:{}'.format(truncated_slug))) record['tx_id'] = full_tx_id record['tx_truncated'] = truncated_slug import_dashboard(record, summaries, dry_run=False) new_modules = Module.objects.all() assert_that(len(new_modules), equal_to(len(initial_modules))) service_id_filters = set( [module.query_parameters['filter_by'][0] for module in new_modules]) assert_that(len(service_id_filters), equal_to(1)) assert_that(service_id_filters.pop(), equal_to('service_id:{}'.format(full_tx_id)))
def setUpClass(cls): cls.data_group1 = DataGroup.objects.create(name='data_group1') cls.data_type1 = DataType.objects.create(name='data_type1') cls.data_type2 = DataType.objects.create(name='data_type2') cls.module_type = ModuleTypeFactory(name='tab')