예제 #1
0
    def test_with_one_dashboard(self) -> None:
        def mock_api_get(url: str, *args: Any, **kwargs: Any) -> MockApiResponse:
            if 'test-dash' in url:
                return MockApiResponse({
                    'id': 123,
                    'widgets': [
                        {
                            'visualization': {
                                'query': {
                                    'data_source_id': 1,
                                    'id': '1234',
                                    'name': 'Test Query',
                                    'query': 'SELECT id FROM users'
                                }
                            },
                            'options': {}
                        }
                    ]
                })

            return MockApiResponse({
                'page': 1,
                'count': 1,
                'page_size': 50,
                'results': [
                    {
                        'id': 123,
                        'name': 'Test Dash',
                        'slug': 'test-dash',
                        'created_at': '2020-01-01T00:00:00.000Z',
                        'updated_at': '2020-01-02T00:00:00.000Z',
                        'is_archived': False,
                        'is_draft': False,
                        'user': {'email': '*****@*****.**'}
                    }
                ]
            })

        redash_base_url = 'https://redash.example.com'
        config = ConfigFactory.from_dict({
            'extractor.redash_dashboard.redash_base_url': redash_base_url,
            'extractor.redash_dashboard.api_base_url': redash_base_url,  # probably not but doesn't matter
            'extractor.redash_dashboard.api_key': 'abc123',
            'extractor.redash_dashboard.table_parser':
                'tests.unit.extractor.dashboard.redash.test_redash_dashboard_extractor.dummy_tables'
        })

        with patch('databuilder.rest_api.rest_api_query.requests.get') as mock_get:
            mock_get.side_effect = mock_api_get

            extractor = RedashDashboardExtractor()
            extractor.init(Scoped.get_scoped_conf(conf=config, scope=extractor.get_scope()))

            # DashboardMetadata
            record = extractor.extract()
            self.assertEqual(record.dashboard_id, 123)
            self.assertEqual(record.dashboard_name, 'Test Dash')
            self.assertEqual(record.dashboard_group_id, RedashDashboardExtractor.DASHBOARD_GROUP_ID)
            self.assertEqual(record.dashboard_group, RedashDashboardExtractor.DASHBOARD_GROUP_NAME)
            self.assertEqual(record.product, RedashDashboardExtractor.PRODUCT)
            self.assertEqual(record.cluster, RedashDashboardExtractor.DEFAULT_CLUSTER)
            self.assertEqual(record.created_timestamp, 1577836800)
            self.assertTrue(redash_base_url in record.dashboard_url)
            self.assertTrue('test-dash' in record.dashboard_url)

            # DashboardLastModified
            record = extractor.extract()
            identity: Dict[str, Any] = {
                'dashboard_id': 123,
                'dashboard_group_id': RedashDashboardExtractor.DASHBOARD_GROUP_ID,
                'product': RedashDashboardExtractor.PRODUCT,
                'cluster': u'prod'
            }
            expected_timestamp = DashboardLastModifiedTimestamp(
                last_modified_timestamp=1577923200,
                **identity
            )
            self.assertEqual(record.__repr__(), expected_timestamp.__repr__())

            # DashboardOwner
            record = extractor.extract()
            expected_owner = DashboardOwner(email='*****@*****.**', **identity)
            self.assertEqual(record.__repr__(), expected_owner.__repr__())

            # DashboardQuery
            record = extractor.extract()
            expected_query = DashboardQuery(
                query_id='1234',
                query_name='Test Query',
                url=u'{base}/queries/1234'.format(base=redash_base_url),
                query_text='SELECT id FROM users',
                **identity
            )
            self.assertEqual(record.__repr__(), expected_query.__repr__())

            # DashboardTable
            record = extractor.extract()
            expected_table = DashboardTable(
                table_ids=[TableRelationData('some_db', 'prod', 'public', 'users').key],
                **identity
            )
            self.assertEqual(record.__repr__(), expected_table.__repr__())
예제 #2
0
def dummy_tables(*args: Any) -> List[TableRelationData]:
    return [TableRelationData('some_db', 'prod', 'public', 'users')]
예제 #3
0
 def test_table_relation_data(self) -> None:
     tr = TableRelationData('db', 'cluster', 'schema', 'tbl')
     self.assertEqual(tr.key, 'db://cluster.schema/tbl')
예제 #4
0
def dummy_tables(*args):
    return [TableRelationData('some_db', 'prod', 'public', 'users')]