Ejemplo n.º 1
0
    def setUp(self):
        self.project_id = 'foo'
        self.dataset_id = 'bar'
        self.sandbox_id = 'baz'

        self.query_class = ObservationSourceConceptIDRowSuppression(
            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 setUp(self):
        self.project_id = 'foo'
        self.dataset_id = 'bar'
        self.sandbox_id = 'baz'
        self.client = None

        self.rule_instance = ObservationSourceConceptIDRowSuppression(
            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()

        cls.insert_fake_participants_tmpls = [
            cls.jinja_env.from_string("""
        INSERT INTO `{{fq_table_name}}`
        (observation_id, person_id, observation_concept_id, observation_date,
         observation_type_concept_id, observation_source_concept_id)
        VALUES
          (801, 337361, 1585899, date('2016-05-01'), 45905771, {{age_prediabetes}}),
          (802, 129884, 1585899, date('2016-05-01'), 45905771, {{meds_prediabetes}}),
          (803, 337361, 1585899, date('2016-05-01'), 45905771, {{now_prediabetes}}),
          (804, 129884, 1585899, date('2016-05-01'), 45905771, null),
          (805, 337361, 1585899, date('2016-05-01'), 45905771, 45),
          (806, 129884, 1585899, date('2016-05-01'), 45905771, {{prefer_not_to_ans}})
        """)
        ]
        # 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('RDR_DATASET_ID')
        cls.dataset_id = dataset_id
        sandbox_id = dataset_id + '_sandbox'
        cls.sandbox_id = sandbox_id

        cls.rule_instance = ObservationSourceConceptIDRowSuppression(
            project_id, dataset_id, sandbox_id)

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

        cls.fq_table_names = [f"{project_id}.{dataset_id}.observation"]
        # call super to set up the client, create datasets, and create
        # empty test tables
        # NOTE:  does not create empty sandbox tables.
        super().setUpClass()
class ObservationSourceConceptIDRowSuppressionTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print('**************************************************************')
        print(cls.__name__)
        print('**************************************************************')

    def setUp(self):
        self.project_id = 'foo'
        self.dataset_id = 'bar'
        self.sandbox_id = 'baz'
        self.client = None

        self.rule_instance = ObservationSourceConceptIDRowSuppression(
            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)

    def test_setup_rule(self):
        # test
        self.rule_instance.setup_rule(self.client)

        # no errors are raised, nothing happens

    def test_get_query_specs(self):
        # pre-conditions
        self.assertEqual(self.rule_instance.affected_datasets,
                         [clean_consts.RDR])

        # test
        result_list = self.rule_instance.get_query_specs()

        # post conditions
        expected_list = [{
            clean_consts.QUERY:
            DROP_SELECTION_QUERY_TMPL.render(
                project=self.project_id,
                dataset=self.dataset_id,
                sandbox=self.sandbox_id,
                drop_table=self.rule_instance.get_sandbox_tablenames()[0],
                obs_concepts=OBS_SRC_CONCEPTS)
        }, {
            clean_consts.QUERY:
            DROP_QUERY_TMPL.render(project=self.project_id,
                                   dataset=self.dataset_id,
                                   obs_concepts=OBS_SRC_CONCEPTS)
        }]

        self.assertEqual(result_list, expected_list)

    def test_log_queries(self):
        # pre-conditions
        self.assertEqual(self.rule_instance.affected_datasets,
                         [clean_consts.RDR])

        store_drops = DROP_SELECTION_QUERY_TMPL.render(
            project=self.project_id,
            dataset=self.dataset_id,
            sandbox=self.sandbox_id,
            drop_table=self.rule_instance.get_sandbox_tablenames()[0],
            obs_concepts=OBS_SRC_CONCEPTS)
        select_saves = DROP_QUERY_TMPL.render(project=self.project_id,
                                              dataset=self.dataset_id,
                                              obs_concepts=OBS_SRC_CONCEPTS)
        # test
        with self.assertLogs(level='INFO') as cm:
            self.rule_instance.log_queries()

            expected = [
                'INFO:cdr_cleaner.cleaning_rules.base_cleaning_rule:Generated SQL Query:\n'
                + store_drops,
                'INFO:cdr_cleaner.cleaning_rules.base_cleaning_rule:Generated SQL Query:\n'
                + select_saves
            ]

            # post condition
            self.assertEqual(cm.output, expected)
Ejemplo n.º 5
0
class ObservationSourceConceptIDRowSuppressionTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print('**************************************************************')
        print(cls.__name__)
        print('**************************************************************')

    def setUp(self):
        self.project_id = 'foo'
        self.dataset_id = 'bar'
        self.sandbox_id = 'baz'

        self.query_class = ObservationSourceConceptIDRowSuppression(
            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_query_specs(self):
        # pre-conditions
        self.assertEqual(self.query_class.get_affected_datasets(),
                         [clean_consts.RDR])

        # test
        result_list = self.query_class.get_query_specs()

        # post conditions
        expected_list = [{
            clean_consts.QUERY:
            DROP_SELECTION_QUERY.format(project=self.project_id,
                                        dataset=self.dataset_id,
                                        sandbox=self.sandbox_id,
                                        drop_table=SAVE_TABLE_NAME)
        }, {
            clean_consts.QUERY:
            DROP_QUERY.format(project=self.project_id,
                              dataset=self.dataset_id),
            clean_consts.DESTINATION_TABLE:
            'observation',
            clean_consts.DESTINATION_DATASET:
            self.dataset_id,
            clean_consts.DISPOSITION:
            WRITE_TRUNCATE
        }]

        self.assertEqual(result_list, expected_list)

    def test_log_queries(self):
        # pre-conditions
        self.assertEqual(self.query_class.get_affected_datasets(),
                         [clean_consts.RDR])

        store_drops = DROP_SELECTION_QUERY.format(project=self.project_id,
                                                  dataset=self.dataset_id,
                                                  sandbox=self.sandbox_id,
                                                  drop_table=SAVE_TABLE_NAME)
        select_saves = DROP_QUERY.format(project=self.project_id,
                                         dataset=self.dataset_id)
        # test
        with self.assertLogs(level='INFO') as cm:
            self.query_class.log_queries()

            expected = [
                'INFO:cdr_cleaner.cleaning_rules.base_cleaning_rule:Generated SQL Query:\n'
                + store_drops,
                'INFO:cdr_cleaner.cleaning_rules.base_cleaning_rule:Generated SQL Query:\n'
                + select_saves
            ]

            # post condition
            self.assertEqual(cm.output, expected)