def create_adherence_cases(self, data):

        factory = CaseFactory(domain=self.domain)
        cases = factory.create_or_update_cases([
            get_adherence_case_structure(
                case_id,
                self.episode_id,
                adherence_date,
                extra_update={
                    "name": adherence_date,
                    "adherence_value": adherence_value,
                    "source": source,
                    "closure_reason": closure_reason
                }
            )
            for (case_id, adherence_date, adherence_value, source, _, closure_reason, _) in data
        ])

        cases_by_id = {c.case_id: c for c in cases}
        for (case_id, _, _, _, should_close, _, modified_on) in data:
            if should_close:
                factory.close_case(case_id)
            if modified_on:
                cases_by_id[case_id].modified_on = modified_on
                cases_by_id[case_id].save()

        rebuild_indicators(self.data_store.datasource._id)
        queue_async_indicators()
        self.data_store.adapter.refresh_table()
Ejemplo n.º 2
0
    def test_async_invalid_data(self):
        # re-fetch from DB to bust object caches
        self.config = DataSourceConfiguration.get(self.config.data_source_id)

        self.config.validations = [
            Validation.wrap({
                "name": "impossible_condition",
                "error_message": "This condition is impossible to satisfy",
                "expression": {
                    "type": "boolean_expression",
                    "expression": {
                        "type": "property_name",
                        "property_name": "doesnt_exist"
                    },
                    "operator": "in",
                    "property_value": ["nonsense"]
                }
            })
        ]

        self.config.save()
        parent_id, child_id = uuid.uuid4().hex, uuid.uuid4().hex
        since = self.pillow.get_change_feed().get_latest_offsets()
        for i in range(3):
            form, cases = post_case_blocks([
                CaseBlock.deprecated_init(
                    create=i == 0,
                    case_id=parent_id,
                    case_name='parent-name',
                    case_type='bug',
                    update={
                        'update-prop-parent': i
                    },
                ).as_xml(),
                CaseBlock.deprecated_init(create=i == 0,
                                          case_id=child_id,
                                          case_name='child-name',
                                          case_type='bug-child',
                                          index={
                                              'parent': ('bug', parent_id)
                                          },
                                          update={
                                              'update-prop-child': i
                                          }).as_xml()
            ],
                                           domain=self.domain)
        self.pillow.process_changes(since=since, forever=False)

        # run async queue
        queue_async_indicators()
        self.assertEqual(InvalidUCRData.objects.count(), 1)
Ejemplo n.º 3
0
    def _run_iterative_monthly_test(self, case_id, cases):
        start_date = date(2016, 2, 1)
        with mock.patch('custom.icds_reports.ucr.expressions._datetime_now') as now:
            now.return_value = datetime.combine(start_date, datetime.min.time()) + relativedelta(months=1)
            _iteratively_build_table(self.datasource)
            queue_async_indicators()
        query = self._get_query_object().filter_by(doc_id=case_id).order_by(self.adapter.get_table().columns.month)
        self.assertEqual(query.count(), len(cases))

        for index, test_values in cases:
            row = query.all()[index]._asdict()
            self.assertEqual(row['month'], start_date + relativedelta(months=index))
            for key, exp_value in test_values:
                self.assertEqual(exp_value, row[key],
                                 str(index) + ":" + key + ' ' + str(exp_value) + ' != ' + str(row[key]))
Ejemplo n.º 4
0
    def _run_iterative_monthly_test(self, case_id, cases):
        start_date = date(2016, 2, 1)
        with mock.patch('custom.icds_reports.ucr.expressions._datetime_now') as now:
            now.return_value = datetime.combine(start_date, datetime.min.time()) + relativedelta(months=1)
            _iteratively_build_table(self.datasource)
            queue_async_indicators()
        query = self._get_query_object().filter_by(doc_id=case_id).order_by(self.adapter.get_table().columns.month)
        self.assertEqual(query.count(), len(cases))

        for index, test_values in cases:
            row = query.all()[index]._asdict()
            self.assertEqual(row['month'], start_date + relativedelta(months=index))
            for key, exp_value in test_values:
                self.assertEqual(exp_value, row[key],
                                 str(index) + ":" + key + ' ' + str(exp_value) + ' != ' + str(row[key]))
Ejemplo n.º 5
0
    def test_async_save_success(self):
        parent_id, child_id = uuid.uuid4().hex, uuid.uuid4().hex
        for i in range(3):
            since = self.pillow.get_change_feed().get_latest_offsets()
            form, cases = post_case_blocks([
                CaseBlock(
                    create=i == 0,
                    case_id=parent_id,
                    case_name='parent-name',
                    case_type='bug',
                    update={
                        'update-prop-parent': i
                    },
                ).as_xml(),
                CaseBlock(create=i == 0,
                          case_id=child_id,
                          case_name='child-name',
                          case_type='bug-child',
                          index={
                              'parent': ('bug', parent_id)
                          },
                          update={
                              'update-prop-child': i
                          }).as_xml()
            ],
                                           domain=self.domain)
            # ensure indicator is added
            indicators = AsyncIndicator.objects.filter(doc_id=child_id)
            self.assertEqual(indicators.count(), 0)
            self.pillow.process_changes(since=since, forever=False)
            self.assertEqual(indicators.count(), 1)

            # ensure saving document produces a row
            queue_async_indicators()
            rows = self.adapter.get_query_object()
            self.assertEqual(rows.count(), 1)

            # ensure row is correct
            row = rows[0]
            self.assertEqual(int(row.parent_property), i)

            # ensure no errors or anything left in the queue
            errors = PillowError.objects.filter(doc_id=child_id,
                                                pillow=self.pillow.pillow_id)
            self.assertEqual(errors.count(), 0)
            self.assertEqual(indicators.count(), 0)
Ejemplo n.º 6
0
    def test_async_save_fails(self, config):
        # process_changes will generate an exception when trying to use this config
        config.return_value = None
        since = self.pillow.get_change_feed().get_latest_offsets()
        parent_id, child_id = uuid.uuid4().hex, uuid.uuid4().hex
        form, cases = post_case_blocks([
            CaseBlock(
                create=True,
                case_id=parent_id,
                case_name='parent-name',
                case_type='bug',
                update={
                    'update-prop-parent': 0
                },
            ).as_xml(),
            CaseBlock(create=True,
                      case_id=child_id,
                      case_name='child-name',
                      case_type='bug-child',
                      index={
                          'parent': ('bug', parent_id)
                      },
                      update={
                          'update-prop-child': 0
                      }).as_xml()
        ],
                                       domain=self.domain)

        # ensure async indicator is added
        indicators = AsyncIndicator.objects.filter(doc_id=child_id)
        self.assertEqual(indicators.count(), 0)
        self.pillow.process_changes(since=since, forever=False)
        self.assertEqual(indicators.count(), 1)

        # ensure the save errors and fails to produce a row
        with self.assertRaises(AttributeError):
            queue_async_indicators()

        rows = self.adapter.get_query_object()
        self.assertEqual(rows.count(), 0)

        # ensure there is not a pillow error and the async indicator is still there
        errors = PillowError.objects.filter(doc_id=child_id,
                                            pillow=self.pillow.pillow_id)
        self.assertEqual(errors.count(), 0)
        self.assertEqual(indicators.count(), 1)
Ejemplo n.º 7
0
    def test_async_invalid_data(self):
        self.config.validations = [
            Validation.wrap({
                "name": "impossible_condition",
                "error_message": "This condition is impossible to satisfy",
                "expression": {
                    "type": "boolean_expression",
                    "expression": {
                        "type": "property_name",
                        "property_name": "doesnt_exist"
                    },
                    "operator": "in",
                    "property_value": ["nonsense"]
                }
            })
        ]

        self.config.save()
        parent_id, child_id = uuid.uuid4().hex, uuid.uuid4().hex
        since = self.pillow.get_change_feed().get_latest_offsets()
        for i in range(3):
            form, cases = post_case_blocks(
                [
                    CaseBlock(
                        create=i == 0,
                        case_id=parent_id,
                        case_name='parent-name',
                        case_type='bug',
                        update={'update-prop-parent': i},
                    ).as_xml(),
                    CaseBlock(
                        create=i == 0,
                        case_id=child_id,
                        case_name='child-name',
                        case_type='bug-child',
                        index={'parent': ('bug', parent_id)},
                        update={'update-prop-child': i}
                    ).as_xml()
                ], domain=self.domain
            )
        self.pillow.process_changes(since=since, forever=False)

        # run async queue
        queue_async_indicators()
        self.assertEqual(InvalidUCRData.objects.count(), 1)
Ejemplo n.º 8
0
    def test_async_save_success(self):
        parent_id, child_id = uuid.uuid4().hex, uuid.uuid4().hex
        for i in range(3):
            since = self.pillow.get_change_feed().get_latest_offsets()
            form, cases = post_case_blocks(
                [
                    CaseBlock(
                        create=i == 0,
                        case_id=parent_id,
                        case_name='parent-name',
                        case_type='bug',
                        update={'update-prop-parent': i},
                    ).as_xml(),
                    CaseBlock(
                        create=i == 0,
                        case_id=child_id,
                        case_name='child-name',
                        case_type='bug-child',
                        index={'parent': ('bug', parent_id)},
                        update={'update-prop-child': i}
                    ).as_xml()
                ], domain=self.domain
            )
            # ensure indicator is added
            indicators = AsyncIndicator.objects.filter(doc_id=child_id)
            self.assertEqual(indicators.count(), 0)
            self.pillow.process_changes(since=since, forever=False)
            self.assertEqual(indicators.count(), 1)

            # ensure saving document produces a row
            queue_async_indicators()
            rows = self.adapter.get_query_object()
            self.assertEqual(rows.count(), 1)

            # ensure row is correct
            row = rows[0]
            self.assertEqual(int(row.parent_property), i)

            # ensure no errors or anything left in the queue
            errors = PillowError.objects.filter(doc_id=child_id, pillow=self.pillow.pillow_id)
            self.assertEqual(errors.count(), 0)
            self.assertEqual(indicators.count(), 0)
Ejemplo n.º 9
0
    def test_async_save_fails(self, config):
        # process_changes will generate an exception when trying to use this config
        config.return_value = None
        since = self.pillow.get_change_feed().get_latest_offsets()
        parent_id, child_id = uuid.uuid4().hex, uuid.uuid4().hex
        form, cases = post_case_blocks(
            [
                CaseBlock(
                    create=True,
                    case_id=parent_id,
                    case_name='parent-name',
                    case_type='bug',
                    update={'update-prop-parent': 0},
                ).as_xml(),
                CaseBlock(
                    create=True,
                    case_id=child_id,
                    case_name='child-name',
                    case_type='bug-child',
                    index={'parent': ('bug', parent_id)},
                    update={'update-prop-child': 0}
                ).as_xml()
            ], domain=self.domain
        )

        # ensure async indicator is added
        indicators = AsyncIndicator.objects.filter(doc_id=child_id)
        self.assertEqual(indicators.count(), 0)
        self.pillow.process_changes(since=since, forever=False)
        self.assertEqual(indicators.count(), 1)

        queue_async_indicators()

        rows = self.adapter.get_query_object()
        self.assertEqual(rows.count(), 0)

        # ensure there is not a pillow error and the async indicator is still there
        errors = PillowError.objects.filter(doc_id=child_id, pillow=self.pillow.pillow_id)
        self.assertEqual(errors.count(), 0)
        self.assertEqual(indicators.count(), 1)
Ejemplo n.º 10
0
 def _rebuild_indicators(self):
     # rebuild so that adherence UCR data gets updated
     rebuild_indicators(self.data_store.datasource._id)
     queue_async_indicators()
     self.data_store.adapter.refresh_table()
Ejemplo n.º 11
0
 def test_queue_async_indicators(self, patched_build):
     queue_async_indicators()
     patched_build.assert_has_calls(
         patched_build.call(self.doc_ids)
     )