def test_execute_query_with_operational_error(self, mock_connect): mock_cursor = MagicMock() mock_cursor.execute = MagicMock(side_effect=OperationalError()) mock_connect.return_value = mock_cursor query = 'SELECT 1' with self.assertRaises(OktaCredentialError): DBService.execute_query(query, MagicMock())
def test_create_connection(self, mock_execute_query, mock_connect): DBService.create_connection('username', 'password', MagicMock()) result_execute = mock_execute_query.call_count result_connect = mock_connect.call_count self.assertEqual(result_execute, 1) self.assertEqual(result_connect, 1)
def test_execute_query_with_exception(self, mock_connect): mock_cursor = MagicMock() mock_cursor.execute = MagicMock(side_effect=Exception()) mock_connect.return_value = mock_cursor query = 'SELECT 1' with self.assertRaises(PrestoError): DBService.execute_query(query, MagicMock())
def test_format_query_without_filters(self): expected = FindFeatureQueryParameters.columns_select + FindFeatureQueryParameters.default_tables + FindFeatureQueryParameters.constraints expected += FindFeatureQueryParameters.group_by + FindFeatureQueryParameters.order_by + FindFeatureQueryParameters.limit result = DBService.format_query([], FindFeatureQueryParameters) self.assertEqual(result, expected)
def get_queryset(self): if self.is_from_login(): return [] FilterManager.apply_filters(self.request) if FilterManager.filter_has_changed( ) or not StateManager.get_last_searched_events( ) or self.request.path != StateManager.url: db_service_filters = FilterManager.get_list_dto_db_service_filter() query_parameters = QueryParameterFactory.get_query_parameters( self.request) events = DBService.get_events(db_service_filters, query_parameters, self.request.session) StateManager.set_events(events) self.has_eb_studio = len( [event for event in events if event.eb_studio_url]) > 0 self.has_facebook = len( [event for event in events if event.facebook]) > 0 StateManager.change_url(self.request) return events events = StateManager.get_last_searched_events() self.has_eb_studio = len( [event for event in events if event.eb_studio_url]) > 0 self.has_facebook = len([event for event in events if event.facebook]) > 0 return events
def test_format_query_with_filters(self): where_query = FindFeatureQueryParameters.constraints + " " + "AND dw_event.event_language LIKE '%en%'" expected = FindFeatureQueryParameters.columns_select + FindFeatureQueryParameters.default_tables + where_query + " " + FindFeatureQueryParameters.group_by expected += FindFeatureQueryParameters.order_by + FindFeatureQueryParameters.limit result = DBService.format_query(self.mock_dto_filter, FindFeatureQueryParameters) self.assertEqual(result.split(), expected.split())
def login(request): session = request.session if request.method == 'GET': if DBService.is_connected(session): return redirect('find_feature') return render(request, 'login.html') else: username = request.POST.get('username') password = request.POST.get('password') session.create() try: DBService.create_connection(username, password, session) session['username'] = username except (OktaCredentialError, PrestoError) as e: DBService.disconnect(session) return render(request, 'login.html', {'error': e.message}) return redirect('find_feature')
def get(self, request): self.template_name = TemplateFactory.get_template(request) if not DBService.is_connected(request.session): return redirect('login') try: return super().get(request) except PrestoError as e: StateManager.reset_events() return render(request, 'find_feature.html', {'error': e.message}) except Exception as e: return redirect('login')
def test_execute_query(self, mock_connect): mock_cursor = MagicMock() mock_cursor.execute = MagicMock() mock_cursor.fetchall = MagicMock(return_value=self.mock_db_response) mock_connect.return_value = mock_cursor query = 'This is query' session = MagicMock() session.session_key = MagicMock() result = DBService.execute_query(query, session) self.assertEqual(len(result), 2) self.assertEqual(type(result), list)
def test_get_events(self, mock_process_events, mock_format_query, mock_execute_query): mock_process_events.return_value = self.mock_response_processed mock_cursor = MagicMock() mock_cursor.execute = MagicMock() mock_cursor.fetchall = MagicMock(return_value=self.mock_db_response) result = DBService.get_events( dto_filters_array=self.mock_dto_filter, query_parameters=FindFeatureQueryParameters, session=MagicMock()) self.assertEqual(len(result), 2) self.assertIsInstance(result[0], Event) self.assertIsInstance(result[1], Event)
def test_format_join_query_with_filters(self): mock_dto_filter = [ DTODBServiceFilter(join_query=[ 'INNER JOIN dw.f_ticket_merchandise_purchase f ON f.event_id = dw_event.event_id' ], where_query='') ] join_query = FindFeatureQueryParameters.default_tables + " " + "INNER JOIN dw.f_ticket_merchandise_purchase f ON f.event_id = dw_event.event_id" where_query = FindFeatureQueryParameters.constraints + " " expected = " " + FindFeatureQueryParameters.columns_select + join_query + where_query + " " + FindFeatureQueryParameters.group_by expected += FindFeatureQueryParameters.order_by + FindFeatureQueryParameters.limit result = DBService.format_query(mock_dto_filter, FindFeatureQueryParameters) self.assertEqual(result.split(), expected.split())
def test_disconnect(self, mock_disconnect): DBService.disconnect(MagicMock()) count = mock_disconnect.call_count self.assertEquals(count, 1)
def test_is_connected_false(self, mock_get_connection): mock_get_connection.return_value = None result = DBService.is_connected(MagicMock()) self.assertFalse(result)
def test_is_connected_true(self, mock_get_connection): mock_get_connection.return_value = MagicMock() result = DBService.is_connected(MagicMock()) self.assertTrue(result)
def logout(request): if request.method == 'POST': DBService.disconnect(request.session) return redirect('login') else: raise SuspiciousOperation()