Ejemplo n.º 1
0
    def setUp(self):
        self.project_id = 'foo_project'
        self.dataset_id = 'bar_project'
        self.sandbox_id = 'baz_sandbox'

        self.query_class = EnsureDateDatetimeConsistency(
            self.project_id, self.dataset_id, self.sandbox_id)

        self.assertEqual(self.query_class.get_project_id(), self.project_id)
        self.assertEqual(self.query_class.get_dataset_id(), self.dataset_id)
        self.assertEqual(self.query_class.get_sandbox_dataset_id(),
                         self.sandbox_id)
Ejemplo n.º 2
0
    def setUp(self):
        self.project_id = 'foo_project'
        self.dataset_id = 'bar_project'
        self.sandbox_id = 'baz_sandbox'
        self.client = None

        self.rule_instance = EnsureDateDatetimeConsistency(
            self.project_id, self.dataset_id, self.sandbox_id)

        self.assertEqual(self.rule_instance.project_id, self.project_id)
        self.assertEqual(self.rule_instance.dataset_id, self.dataset_id)
        self.assertEqual(self.rule_instance.sandbox_dataset_id,
                         self.sandbox_id)
Ejemplo n.º 3
0
    def setUpClass(cls):
        print('**************************************************************')
        print(cls.__name__)
        print('**************************************************************')

        super().initialize_class_vars()

        # set the test project identifier
        project_id = os.environ.get(PROJECT_ID)
        cls.project_id = project_id

        # set the expected test datasets
        dataset_id = os.environ.get('COMBINED_DATASET_ID')
        sandbox_id = dataset_id + '_sandbox'

        cls.query_class = EnsureDateDatetimeConsistency(project_id, dataset_id,
                                                        sandbox_id)

        sb_table_names = cls.query_class.get_sandbox_tablenames()
        for table_name in sb_table_names:
            cls.fq_sandbox_table_names.append(
                f'{project_id}.{sandbox_id}.{table_name}')

        for key in TABLE_DATES:
            cls.fq_table_names.append(f"{project_id}.{dataset_id}.{key}")

        # call super to set up the client, create datasets, and create
        # empty test tables
        # NOTE:  does not create empty sandbox tables.
        super().setUpClass()
Ejemplo n.º 4
0
class EnsureDateDatetime(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print('**************************************************************')
        print(cls.__name__)
        print('**************************************************************')

    def setUp(self):
        self.project_id = 'foo_project'
        self.dataset_id = 'bar_project'
        self.sandbox_id = 'baz_sandbox'

        self.query_class = EnsureDateDatetimeConsistency(
            self.project_id, self.dataset_id, self.sandbox_id)

        self.assertEqual(self.query_class.get_project_id(), self.project_id)
        self.assertEqual(self.query_class.get_dataset_id(), self.dataset_id)
        self.assertEqual(self.query_class.get_sandbox_dataset_id(),
                         self.sandbox_id)

    def test_setup_rule(self):
        # test
        self.query_class.setup_rule()

        # no errors are raised, nothing happens

    def test_get_cols(self):
        # pre conditions
        self.assertEqual(
            self.query_class.get_affected_datasets(),
            [cdr_consts.RDR, cdr_consts.UNIONED, cdr_consts.COMBINED])

        for table in TABLE_DATES:
            # test
            result_list = self.query_class.get_cols(table)

            # post conditions
            table_fields = field_mapping.get_domain_fields(table)

            expected_list = []
            for field in table_fields:
                if field in TABLE_DATES[table]:
                    expected = FIX_NULL_OR_INCORRECT_DATETIME_QUERY.format(
                        field=field, date_field=TABLE_DATES[table][field])
                else:
                    expected = field
                expected_list.append(expected)

            expected_cols = ', '.join(expected_list)

            self.assertEqual(result_list, expected_cols)

    def test_get_query_specs(self):
        # pre conditions
        self.assertEqual(
            self.query_class.get_affected_datasets(),
            [cdr_consts.RDR, cdr_consts.UNIONED, cdr_consts.COMBINED])

        # test
        result_list = self.query_class.get_query_specs()

        # post conditions
        expected_list = []
        for table in TABLE_DATES:
            query = dict()
            query[cdr_consts.QUERY] = FIX_DATETIME_QUERY.format(
                project_id=self.project_id,
                dataset_id=self.dataset_id,
                table_id=table,
                cols=self.query_class.get_cols(table))
            query[cdr_consts.DESTINATION_TABLE] = table
            query[cdr_consts.DISPOSITION] = bq_consts.WRITE_TRUNCATE
            query[cdr_consts.DESTINATION_DATASET] = self.dataset_id
            expected_list.append(query)
        self.assertEqual(result_list, expected_list)