class TestDashboard(GoDjangoTestCase): def setUp(self): self.vumi_helper = self.add_helper(DjangoVumiApiHelper()) self.diamondash_api = FakeDiamondashApiClient() layout = ToyDashboardLayout([{ 'type': 'lvalue', 'time_range': '1d', 'title': 'Spam (24h)', 'target': { 'metric_type': 'foo', 'name': 'spam', }, }, { 'type': 'lvalue', 'time_range': '1d', 'title': 'Ham (24h)', 'target': { 'metric_type': 'foo', 'name': 'ham', }, }]) self.monkey_patch(client, 'get_diamondash_api', lambda: self.diamondash_api) self.dashboard = Dashboard('ackbar-the-dashboard', layout) def test_sync(self): self.diamondash_api.set_response({'happy': 'config'}) self.dashboard.sync() [request] = self.diamondash_api.get_requests() self.assertEqual( request['data'], { 'name': 'ackbar-the-dashboard', 'widgets': [{ 'type': 'lvalue', 'time_range': '1d', 'title': 'Spam (24h)', 'target': 'foo.spam', }, { 'type': 'lvalue', 'time_range': '1d', 'title': 'Ham (24h)', 'target': 'foo.ham', }] }) self.assertEqual(self.dashboard.get_config(), {'happy': 'config'}) def test_sync_for_error_responses(self): self.diamondash_api.set_error_response(404, ':(') self.assertRaises(DashboardSyncError, self.dashboard.sync) def get_config_before_sync(self): self.assertRaises(self.dashboard.get_config, DashboardError)
class TestConversationReportsView(BaseConversationViewTestCase): def setUp(self): super(TestConversationReportsView, self).setUp() self.diamondash_api = FakeDiamondashApiClient() self.error_log = [] logger = logging.getLogger('go.conversation.view_definition') def log_error(e, exc_info): exc_type, exc_value, exc_traceback = exc_info self.assertEqual(e, exc_value) self.error_log.append(unicode(e)) self.monkey_patch(logger, 'error', log_error) self.monkey_patch(dashboard_client, 'get_diamondash_api', lambda: self.diamondash_api) def test_get_dashboard(self): self.diamondash_api.set_response({'happy': 'dashboard'}) conv = self.user_helper.create_conversation(u'dummy') response = self.client.get(self.get_view_url(conv, 'reports')) [dd_request] = self.diamondash_api.get_requests() raw_dashboard = dd_request['data'] self.assertEqual(raw_dashboard['name'], "go.conversations.%s" % conv.key) self.assertTrue('widgets' in raw_dashboard) self.assertEqual(json.loads(response.context['dashboard_config']), {'happy': 'dashboard'}) def test_get_dashboard_for_sync_error_handling(self): self.diamondash_api.set_error_response(400, ':(') conv = self.user_helper.create_conversation(u'dummy') response = self.client.get(self.get_view_url(conv, 'reports')) self.assertEqual(self.error_log, [ 'Dashboard sync failed: ' '400: {"message": ":(", "success": false}' ]) self.assertEqual(response.context['dashboard_config'], None) def test_get_dashboard_for_parse_error_handling(self): conv = self.user_helper.create_conversation(u'dummy') def bad_add_entity(*a, **kw): raise DashboardParseError(':(') self.monkey_patch(DashboardLayout, 'add_entity', bad_add_entity) response = self.client.get(self.get_view_url(conv, 'reports')) self.assertEqual(self.error_log, [':(']) self.assertEqual(response.context['dashboard_config'], None)
class TestDashboard(GoDjangoTestCase): def setUp(self): self.vumi_helper = self.add_helper(DjangoVumiApiHelper()) self.diamondash_api = FakeDiamondashApiClient() layout = ToyDashboardLayout([{ 'type': 'lvalue', 'time_range': '1d', 'title': 'Spam (24h)', 'target': { 'metric_type': 'foo', 'name': 'spam', }, }, { 'type': 'lvalue', 'time_range': '1d', 'title': 'Ham (24h)', 'target': { 'metric_type': 'foo', 'name': 'ham', }, }]) self.monkey_patch( client, 'get_diamondash_api', lambda: self.diamondash_api) self.dashboard = Dashboard('ackbar-the-dashboard', layout) def test_sync(self): self.diamondash_api.set_response({'happy': 'config'}) self.dashboard.sync() [request] = self.diamondash_api.get_requests() self.assertEqual(request['data'], { 'name': 'ackbar-the-dashboard', 'widgets': [{ 'type': 'lvalue', 'time_range': '1d', 'title': 'Spam (24h)', 'target': 'foo.spam', }, { 'type': 'lvalue', 'time_range': '1d', 'title': 'Ham (24h)', 'target': 'foo.ham', }] }) self.assertEqual(self.dashboard.get_config(), {'happy': 'config'}) def test_sync_for_error_responses(self): self.diamondash_api.set_error_response(404, ':(') self.assertRaises(DashboardSyncError, self.dashboard.sync) def get_config_before_sync(self): self.assertRaises(self.dashboard.get_config, DashboardError)
class TestConversationReportsView(BaseConversationViewTestCase): def setUp(self): super(TestConversationReportsView, self).setUp() self.diamondash_api = FakeDiamondashApiClient() self.error_log = [] logger = logging.getLogger('go.conversation.view_definition') def log_error(e, exc_info): exc_type, exc_value, exc_traceback = exc_info self.assertEqual(e, exc_value) self.error_log.append(unicode(e)) self.monkey_patch(logger, 'error', log_error) self.monkey_patch( dashboard_client, 'get_diamondash_api', lambda: self.diamondash_api) def test_get_dashboard(self): self.diamondash_api.set_response({'happy': 'dashboard'}) conv = self.user_helper.create_conversation(u'dummy') response = self.client.get(self.get_view_url(conv, 'reports')) [dd_request] = self.diamondash_api.get_requests() raw_dashboard = dd_request['data'] self.assertEqual( raw_dashboard['name'], "go.conversations.%s" % conv.key) self.assertTrue('widgets' in raw_dashboard) self.assertEqual( json.loads(response.context['dashboard_config']), {'happy': 'dashboard'}) def test_get_dashboard_for_sync_error_handling(self): self.diamondash_api.set_error_response(400, ':(') conv = self.user_helper.create_conversation(u'dummy') response = self.client.get(self.get_view_url(conv, 'reports')) self.assertEqual( self.error_log, ['Dashboard sync failed: ' '400: {"message": ":(", "success": false}']) self.assertEqual(response.context['dashboard_config'], None) def test_get_dashboard_for_parse_error_handling(self): conv = self.user_helper.create_conversation(u'dummy') def bad_add_entity(*a, **kw): raise DashboardParseError(':(') self.monkey_patch(DashboardLayout, 'add_entity', bad_add_entity) response = self.client.get(self.get_view_url(conv, 'reports')) self.assertEqual(self.error_log, [':(']) self.assertEqual(response.context['dashboard_config'], None)