def test_doesnt_enqueue_outdated_queries_for_paused_data_source(self): query = self.factory.create_query(schedule="60") retrieved_at = utcnow() - datetime.timedelta(minutes=10) query_result = self.factory.create_query_result( retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) query.latest_query_data = query_result query.save() query.data_source.pause() with patch('redash.tasks.queries.enqueue_query') as add_job_mock: refresh_queries() add_job_mock.assert_not_called() query.data_source.resume() with patch('redash.tasks.queries.enqueue_query') as add_job_mock: refresh_queries() add_job_mock.assert_called_with(query.query, query.data_source, query.user_id, scheduled=True, metadata=ANY)
def test_enqueues_query_with_correct_data_source(self): query = query_factory.create(schedule="60") query2 = query_factory.create(schedule="60", query=query.query, query_hash=query.query_hash) retrieved_at = utcnow() - datetime.timedelta(minutes=10) query_result = query_result_factory.create(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) query.latest_query_data = query_result query2.latest_query_data = query_result query.save() query2.save() with patch('redash.tasks.QueryTask.add_task') as add_job_mock: refresh_queries() add_job_mock.assert_has_calls([ call(query2.query, query2.data_source, scheduled=True, metadata=ANY), call(query.query, query.data_source, scheduled=True, metadata=ANY) ], any_order=True) self.assertEquals(2, add_job_mock.call_count)
def test_enqueues_query_with_correct_data_source(self): query = self.factory.create_query( schedule="60", data_source=self.factory.create_data_source()) query2 = self.factory.create_query(schedule="60", query_text=query.query_text, query_hash=query.query_hash) retrieved_at = utcnow() - datetime.timedelta(minutes=10) query_result = self.factory.create_query_result( retrieved_at=retrieved_at, query_text=query.query_text, query_hash=query.query_hash) query.latest_query_data = query_result query2.latest_query_data = query_result db.session.add_all([query, query2]) with patch('redash.tasks.queries.enqueue_query') as add_job_mock: refresh_queries() add_job_mock.assert_has_calls([ call(query2.query_text, query2.data_source, query2.user_id, scheduled=True, metadata=ANY), call(query.query_text, query.data_source, query.user_id, scheduled=True, metadata=ANY) ], any_order=True) self.assertEquals(2, add_job_mock.call_count)
def test_skips_queries_with_no_ttl(self): query = query_factory.create(schedule=None) retrieved_at = datetime.datetime.utcnow() - datetime.timedelta(minutes=10) query_result = query_result_factory.create(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) with patch('redash.tasks.QueryTask.add_task') as add_job_mock: refresh_queries() self.assertFalse(add_job_mock.called)
def test_skips_fresh_queries(self): query = self.factory.create_query(schedule="1200") retrieved_at = utcnow() - datetime.timedelta(minutes=10) query_result = self.factory.create_query_result(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) with patch('redash.tasks.QueryTask.add_task') as add_job_mock: refresh_queries() self.assertFalse(add_job_mock.called)
def test_skips_fresh_queries(self): query = self.factory.create_query(schedule="1200") retrieved_at = utcnow() - datetime.timedelta(minutes=10) query_result = self.factory.create_query_result(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) with patch('redash.tasks.queries.enqueue_query') as add_job_mock: refresh_queries() self.assertFalse(add_job_mock.called)
def test_skips_queries_with_no_ttl(self): query = self.factory.create_query(schedule=None) retrieved_at = utcnow() - datetime.timedelta(minutes=10) query_result = self.factory.create_query_result( retrieved_at=retrieved_at, query_text=query.query_text, query_hash=query.query_hash) with patch('redash.tasks.queries.enqueue_query') as add_job_mock: refresh_queries() self.assertFalse(add_job_mock.called)
def test_enqueues_outdated_queries(self): query = query_factory.create(schedule="60") retrieved_at = datetime.datetime.utcnow() - datetime.timedelta(minutes=10) query_result = query_result_factory.create(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) query.latest_query_data = query_result query.save() with patch('redash.tasks.QueryTask.add_task') as add_job_mock: refresh_queries() add_job_mock.assert_called_with(query.query, query.data_source, scheduled=True, metadata=ANY)
def test_enqueues_query_only_once(self): query = self.factory.create_query(schedule="60") query2 = self.factory.create_query(schedule="60", query=query.query, query_hash=query.query_hash) retrieved_at = utcnow() - datetime.timedelta(minutes=10) query_result = self.factory.create_query_result(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) query.latest_query_data = query_result query2.latest_query_data = query_result query.save() query2.save() with patch('redash.tasks.QueryTask.add_task') as add_job_mock: refresh_queries() add_job_mock.assert_called_once_with(query.query, query.data_source, scheduled=True, metadata=ANY)#{'Query ID': query.id, 'Username': '******'})
def test_enqueues_query_only_once(self): query = self.factory.create_query(schedule="60") query2 = self.factory.create_query(schedule="60", query=query.query, query_hash=query.query_hash) retrieved_at = utcnow() - datetime.timedelta(minutes=10) query_result = self.factory.create_query_result(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) query.latest_query_data = query_result query2.latest_query_data = query_result query.save() query2.save() with patch('redash.tasks.queries.enqueue_query') as add_job_mock: refresh_queries() add_job_mock.assert_called_once_with(query.query, query.data_source, query.user_id, scheduled=True, metadata=ANY)#{'Query ID': query.id, 'Username': '******'})
def test_enqueues_only_for_relevant_data_source(self): query = query_factory.create(ttl=60) query2 = query_factory.create(ttl=3600, query=query.query, query_hash=query.query_hash) retrieved_at = datetime.datetime.utcnow() - datetime.timedelta(minutes=10) query_result = query_result_factory.create(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) query.latest_query_data = query_result query2.latest_query_data = query_result query.save() query2.save() with patch('redash.tasks.QueryTask.add_task') as add_job_mock: refresh_queries() add_job_mock.assert_called_once_with(query.query, query.data_source, scheduled=True)
def test_enqueues_only_for_relevant_data_source(self): query = query_factory.create(schedule="60") query2 = query_factory.create(schedule="3600", query=query.query, query_hash=query.query_hash) import psycopg2 retrieved_at = datetime.datetime.utcnow().replace(tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)) - datetime.timedelta(minutes=10) query_result = query_result_factory.create(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) query.latest_query_data = query_result query2.latest_query_data = query_result query.save() query2.save() with patch('redash.tasks.QueryTask.add_task') as add_job_mock: refresh_queries() add_job_mock.assert_called_once_with(query.query, query.data_source, scheduled=True, metadata=ANY)
def test_enqueues_query_with_correct_data_source(self): query = query_factory.create(schedule="60") query2 = query_factory.create(schedule="60", query=query.query, query_hash=query.query_hash) retrieved_at = datetime.datetime.utcnow() - datetime.timedelta(minutes=10) query_result = query_result_factory.create(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) query.latest_query_data = query_result query2.latest_query_data = query_result query.save() query2.save() with patch('redash.tasks.QueryTask.add_task') as add_job_mock: refresh_queries() add_job_mock.assert_has_calls([call(query2.query, query2.data_source, scheduled=True), call(query.query, query.data_source, scheduled=True)], any_order=True) self.assertEquals(2, add_job_mock.call_count)
def test_doesnt_enqueue_parameterized_queries_with_invalid_parameters(self): """ Scheduled queries with invalid parameters are skipped. """ query = self.factory.create_query( query_text="select {{n}}", options={"parameters": [{ "global": False, "type": "text", "name": "n", "value": 42, # <-- should be text! "title": "n"}]}) oq = staticmethod(lambda: [query]) with patch('redash.tasks.queries.enqueue_query') as add_job_mock, \ patch.object(Query, 'outdated_queries', oq): refresh_queries() add_job_mock.assert_not_called()
def test_enqueues_outdated_queries(self): query = self.factory.create_query(schedule="60") retrieved_at = utcnow() - datetime.timedelta(minutes=10) query_result = self.factory.create_query_result( retrieved_at=retrieved_at, query_text=query.query_text, query_hash=query.query_hash) query.latest_query_data = query_result db.session.add(query) with patch('redash.tasks.queries.enqueue_query') as add_job_mock: refresh_queries() add_job_mock.assert_called_with(query.query_text, query.data_source, query.user_id, scheduled=True, metadata=ANY)
def test_enqueues_query_with_correct_data_source(self): query = self.factory.create_query(schedule="60", data_source=self.factory.create_data_source()) query2 = self.factory.create_query(schedule="60", query=query.query, query_hash=query.query_hash) retrieved_at = utcnow() - datetime.timedelta(minutes=10) query_result = self.factory.create_query_result(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) query.latest_query_data = query_result query2.latest_query_data = query_result query.save() query2.save() with patch('redash.tasks.queries.enqueue_query') as add_job_mock: refresh_queries() add_job_mock.assert_has_calls([call(query2.query, query2.data_source, query2.user_id, scheduled=True, metadata=ANY), call(query.query, query.data_source, query.user_id, scheduled=True, metadata=ANY)], any_order=True) self.assertEquals(2, add_job_mock.call_count)
def test_enqueues_outdated_queries(self): """ refresh_queries() launches an execution task for each query returned from Query.outdated_queries(). """ query1 = self.factory.create_query() query2 = self.factory.create_query( query_text="select 42;", data_source=self.factory.create_data_source()) oq = staticmethod(lambda: [query1, query2]) with patch('redash.tasks.queries.enqueue_query') as add_job_mock, \ patch.object(Query, 'outdated_queries', oq): refresh_queries() self.assertEqual(add_job_mock.call_count, 2) add_job_mock.assert_has_calls([ call(query1.query_text, query1.data_source, query1.user_id, scheduled_query=query1, metadata=ANY), call(query2.query_text, query2.data_source, query2.user_id, scheduled_query=query2, metadata=ANY)], any_order=True)
def test_enqueues_parameterized_queries(self): """ Scheduled queries with parameters use saved values. """ query = self.factory.create_query( query_text="select {{n}}", options={"parameters": [{ "global": False, "type": "text", "name": "n", "value": "42", "title": "n"}]}) oq = staticmethod(lambda: [query]) with patch('redash.tasks.queries.enqueue_query') as add_job_mock, \ patch.object(Query, 'outdated_queries', oq): refresh_queries() add_job_mock.assert_called_with( "select 42", query.data_source, query.user_id, scheduled_query=query, metadata=ANY)
def test_doesnt_enqueue_outdated_queries_for_paused_data_source(self): query = self.factory.create_query(schedule="60") retrieved_at = utcnow() - datetime.timedelta(minutes=10) query_result = self.factory.create_query_result(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) query.latest_query_data = query_result query.save() query.data_source.pause() with patch('redash.tasks.queries.enqueue_query') as add_job_mock: refresh_queries() add_job_mock.assert_not_called() query.data_source.resume() with patch('redash.tasks.queries.enqueue_query') as add_job_mock: refresh_queries() add_job_mock.assert_called_with(query.query, query.data_source, query.user_id, scheduled=True, metadata=ANY)
def test_enqueues_only_for_relevant_data_source(self): query = query_factory.create(ttl=60) query2 = query_factory.create(ttl=3600, query=query.query, query_hash=query.query_hash) retrieved_at = datetime.datetime.utcnow() - datetime.timedelta( minutes=10) query_result = query_result_factory.create(retrieved_at=retrieved_at, query=query.query, query_hash=query.query_hash) query.latest_query_data = query_result query2.latest_query_data = query_result query.save() query2.save() with patch('redash.tasks.QueryTask.add_task') as add_job_mock: refresh_queries() add_job_mock.assert_called_once_with(query.query, query.data_source, scheduled=True)
def test_doesnt_enqueue_parameterized_queries_with_dropdown_queries_that_are_detached_from_data_source(self): """ Scheduled queries with a dropdown parameter which points to a query that is detached from its data source are skipped. """ query = self.factory.create_query( query_text="select {{n}}", options={"parameters": [{ "global": False, "type": "query", "name": "n", "queryId": 100, "title": "n"}]}) dropdown_query = self.factory.create_query(id=100, data_source=None) oq = staticmethod(lambda: [query]) with patch('redash.tasks.queries.enqueue_query') as add_job_mock, \ patch.object(Query, 'outdated_queries', oq): refresh_queries() add_job_mock.assert_not_called()
def test_doesnt_enqueue_outdated_queries_for_paused_data_source(self): """ refresh_queries() does not launch execution tasks for queries whose data source is paused. """ query = self.factory.create_query() oq = staticmethod(lambda: [query]) query.data_source.pause() with patch.object(Query, 'outdated_queries', oq): with patch('redash.tasks.queries.enqueue_query') as add_job_mock: refresh_queries() add_job_mock.assert_not_called() query.data_source.resume() with patch('redash.tasks.queries.enqueue_query') as add_job_mock: refresh_queries() add_job_mock.assert_called_with( query.query_text, query.data_source, query.user_id, scheduled_query=query, metadata=ANY)