def test_ownership(self): self.assertItemsEqual([self.data_item.get_id], FixtureDataItem.by_user(self.user, wrap=False)) self.assertItemsEqual([self.user.get_id], self.data_item.get_all_users(wrap=False)) fixture, = call_fixture_generator(self.user.to_ota_restore_user()) check_xml_line_by_line( self, """ <fixture id="item-list:district" user_id="%s"> <district_list> <district> <state_name>Delhi_state</state_name> <district_name lang="hin">Delhi_in_HIN</district_name> <district_name lang="eng">Delhi_in_ENG</district_name> <district_id>Delhi_id</district_id> </district> </district_list> </fixture> """ % self.user.user_id, ElementTree.tostring(fixture)) self.data_item.remove_user(self.user) self.assertItemsEqual([], self.data_item.get_all_users()) self.fixture_ownership = self.data_item.add_user(self.user) self.assertItemsEqual([self.user.get_id], self.data_item.get_all_users(wrap=False))
def test_fixture_is_indexed(self): self.data_type.fields[2].is_indexed = True # Set "district_id" as indexed self.data_type.save() fixtures = call_fixture_generator(self.user.to_ota_restore_user()) self.assertEqual(len(fixtures), 2) check_xml_line_by_line( self, """ <fixtures> <schema id="item-list:district"> <indices> <index>district_id</index> </indices> </schema> <fixture id="item-list:district" indexed="true" user_id="{}"> <district_list> <district> <state_name>Delhi_state</state_name> <district_name lang="hin">Delhi_in_HIN</district_name> <district_name lang="eng">Delhi_in_ENG</district_name> <district_id>Delhi_id</district_id> </district> </district_list> </fixture> </fixtures> """.format(self.user.user_id), """ <fixtures> {} {} </fixtures> """.format(*[ElementTree.tostring(fixture).decode('utf-8') for fixture in fixtures]) )
def test_ownership(self): self.assertItemsEqual([self.data_item.get_id], FixtureDataItem.by_user(self.user, wrap=False)) self.assertItemsEqual([self.user.get_id], self.data_item.get_all_users(wrap=False)) fixture, = fixturegenerators.item_lists(self.user) check_xml_line_by_line( self, """ <fixture id="item-list:contact" user_id="%s"> <contact_list> <contact> <name>John</name> <number>+15555555555</number> </contact> </contact_list> </fixture> """ % self.user.user_id, ElementTree.tostring(fixture)) self.data_item.remove_user(self.user) self.assertItemsEqual([], self.data_item.get_all_users()) self.fixture_ownership = self.data_item.add_user(self.user) self.assertItemsEqual([self.user.get_id], self.data_item.get_all_users(wrap=False))
def test_callcenter_fixture_format(self): user = CommCareUser(_id='123') indicator_set = MockIndicatorSet(name='test', indicators=OrderedDict([ ('user_case1', { 'i1': 1, 'i2': 2 }), ('user_case2', { 'i1': 0, 'i2': 3 }) ])) fixture = gen_fixture(user, indicator_set) check_xml_line_by_line( self, """ <fixture date="2014-01-01T00:00:00" id="indicators:test" user_id="{userid}"> <indicators> <case id="user_case1"> <i1>1</i1> <i2>2</i2> </case> <case id="user_case2"> <i1>0</i1> <i2>3</i2> </case> </indicators> </fixture> """.format(userid=user.user_id), ElementTree.tostring(fixture))
def test_open_update_case(self): self.form.source = XFORM_SOURCE self.form.actions.open_case = OpenCaseAction(name_path="/data/question1", external_id=None) self.form.actions.open_case.condition.type = 'always' self.form.actions.update_case = UpdateCaseAction(update={'question1': '/data/question1'}) self.form.actions.update_case.condition.type = 'always' check_xml_line_by_line(self, OPEN_UPDATE_CASE_SOURCE, self.form.render_xform())
def _check_fixture(self, fixture_xml, has_groups=True, item_lists=None): fixture_xml = list(fixture_xml) item_lists = item_lists or [] expected_len = sum([has_groups, len(item_lists)]) self.assertEqual(len(fixture_xml), expected_len) if has_groups: expected = _get_group_fixture(self.user.get_id, [self.group1, self.group2]) check_xml_line_by_line( self, expected, ElementTree.tostring(fixture_xml[0], encoding='utf-8')) if item_lists: for i, item_list_tag in enumerate(item_lists): data_type, data_item = self.item_lists[item_list_tag] item_list_xml = [ ElementTree.tostring(fixture, encoding='utf-8') for fixture in fixture_xml if item_list_tag in fixture.attrib.get("id") ] self.assertEqual(len(item_list_xml), 1) expected = _get_item_list_fixture(self.user.get_id, data_type.tag, data_item) check_xml_line_by_line(self, expected, item_list_xml[0])
def test_callcenter_fixture_commcare_user(self): user = CommCareUser(_id='123', username="******") indicator_set = MockIndicatorSet(name='test', indicators=OrderedDict([ ('user_case1', { 'i1': 1, 'i2': 2 }), ('user_case2', { 'i1': 0, 'i2': 3 }) ])) restore_user = type( 'OTARestoreCommCareUserFake', (OTARestoreCommCareUser, ), { 'project': Domain(name='test', default_timezone='UTC'), 'get_call_center_indicators': lambda self, config: indicator_set, })('test', user) fixture, = call_fixture_generator(mock_indicators_fixture_generator, restore_user) check_xml_line_by_line( self, ElementTree.tostring(fixture), ElementTree.tostring(gen_fixture(restore_user, indicator_set)))
def test_xml(self): check_xml_line_by_line(self, """ <contact> <name>John</name> <number>+15555555555</number> </contact> """, ElementTree.tostring(self.data_item.to_xml()))
def test_ota_multiple_stocks(self): section_ids = sorted(('stock', 'losses', 'consumption')) amounts = [ SohReport(section_id=section_id, product_id=p._id, amount=i * 10) for section_id in section_ids for i, p in enumerate(self.products) ] form_id, report_date = _report_soh(amounts, self.sp.case_id, self.domain.name) self.addCleanup(self.delete_ledger_transactions, form_id) balance_blocks = util.get_ota_balance_xml(self.domain, self.user) self.assertEqual(3, len(balance_blocks)) for i, section_id in enumerate(section_ids): reports = [ report for report in amounts if report.section_id == section_id ] check_xml_line_by_line( self, balance_ota_block( self.sp, section_id, reports, datestring=report_date, ), balance_blocks[i], )
def test_update_case(self): self.form.requires = 'case' self.form.actions.update_case = UpdateCaseAction( update={'question1': '/data/question1'}) self.form.actions.update_case.condition.type = 'always' check_xml_line_by_line(self, self.get_xml('update_case'), self.form.render_xform())
def test_ota_consumption(self): self.ct_settings.consumption_config = ConsumptionConfig( min_transactions=0, min_window=0, optimal_window=60, ) self.ct_settings.ota_restore_config = StockRestoreConfig( section_to_consumption_types={'stock': 'consumption'}) set_default_consumption_for_domain(self.domain.name, 5) amounts = [(p._id, i * 10) for i, p in enumerate(self.products)] report = _report_soh(amounts, self.sp._id, 'stock') balance_blocks = _get_ota_balance_blocks(self.ct_settings, self.user) self.assertEqual(2, len(balance_blocks)) stock_block, consumption_block = balance_blocks check_xml_line_by_line( self, balance_ota_block( self.sp, 'stock', amounts, datestring=json_format_datetime(report.date), ), stock_block, ) check_xml_line_by_line( self, balance_ota_block( self.sp, 'consumption', [(p._id, 5) for p in self.products], datestring=json_format_datetime(report.date), ), consumption_block, )
def test_ota_multiple_stocks(self): user = self.user section_ids = sorted(('stock', 'losses', 'consumption')) amounts = [ SohReport(section_id=section_id, product_id=p._id, amount=i * 10) for section_id in section_ids for i, p in enumerate(self.products) ] report_date = _report_soh(amounts, self.sp.case_id, self.domain.name) balance_blocks = get_ota_balance_xml(self.domain, user) self.assertEqual(3, len(balance_blocks)) for i, section_id in enumerate(section_ids): reports = [ report for report in amounts if report.section_id == section_id ] check_xml_line_by_line( self, balance_ota_block( self.sp, section_id, reports, datestring=report_date, ), balance_blocks[i], )
def test_update_preload_case(self): self.form.requires = 'case' self.form.actions.update_case = UpdateCaseAction(update={'question1': '/data/question1'}) self.form.actions.update_case.condition.type = 'always' self.form.actions.case_preload = PreloadAction(preload={'/data/question1': 'question1'}) self.form.actions.case_preload.condition.type = 'always' check_xml_line_by_line(self, self.get_xml('update_preload_case'), self.form.render_xform())
def test_ota_multiple_stocks(self): user = self.user date = datetime.utcnow() report = StockReport.objects.create( form_id=uuid.uuid4().hex, date=date, type=stockconst.REPORT_TYPE_BALANCE) amounts = [(p._id, i * 10) for i, p in enumerate(self.products)] section_ids = sorted(('stock', 'losses', 'consumption')) for section_id in section_ids: _report_soh(amounts, self.sp._id, section_id, report=report) balance_blocks = get_ota_balance_xml(user) self.assertEqual(3, len(balance_blocks)) for i, section_id in enumerate(section_ids): check_xml_line_by_line( self, balance_ota_block( self.sp, section_id, amounts, datestring=json_format_datetime(date), ), balance_blocks[i], )
def test_ota_consumption(self): self.ct_settings.consumption_config = ConsumptionConfig(min_transactions=0, min_window=0, optimal_window=60) self.ct_settings.ota_restore_config = StockRestoreConfig(section_to_consumption_types={"stock": "consumption"}) set_default_monthly_consumption_for_domain(self.domain.name, 5 * DAYS_IN_MONTH) self._save_settings_and_clear_cache() amounts = [(p._id, i * 10) for i, p in enumerate(self.products)] report = _report_soh(amounts, self.sp.case_id, "stock") balance_blocks = _get_ota_balance_blocks(self.domain, self.user) self.assertEqual(2, len(balance_blocks)) stock_block, consumption_block = balance_blocks check_xml_line_by_line( self, balance_ota_block(self.sp, "stock", amounts, datestring=json_format_datetime(report.date)), stock_block, ) check_xml_line_by_line( self, balance_ota_block( self.sp, "consumption", [(p._id, 150) for p in self.products], datestring=json_format_datetime(report.date), ), consumption_block, )
def testUserRestoreWithCase(self): file_path = os.path.join(os.path.dirname(__file__), "data", "create_short.xml") with open(file_path, "rb") as f: xml_data = f.read() FormProcessorInterface.submit_form_locally(xml_data, self.domain) expected_case_block = """ <case case_id="asdf" date_modified="2010-06-29T13:42:50.000000Z" user_id="foo" xmlns="http://commcarehq.org/case/transaction/v2"> <create> <case_type>test_case_type</case_type> <case_name>test case name</case_name> <owner_id>foo</owner_id> </create> <update> <external_id>someexternal</external_id> </update> </case>""" restore_payload = generate_restore_payload( project=Domain(name=self.domain), user=dummy_user(), items=True, version=V3 ) sync_log_id = SyncLog.view( "phone/sync_logs_by_user", include_docs=True, reduce=False, ).one().get_id check_xml_line_by_line( self, dummy_restore_xml(sync_log_id, expected_case_block, items=4), restore_payload )
def test_suite(self): with open(os.path.join(os.path.dirname(__file__), 'data', 'days_ago_suite.xml')) as f: suiteA = f.read() suiteB = self.app.create_suite() check_xml_line_by_line(self, suiteA, suiteB)
def test_xml(self): check_xml_line_by_line( self, """ <contact> <name>John</name> <number>+15555555555</number> </contact> """, ElementTree.tostring(self.data_item.to_xml()))
def test_close_case_xml(self): time = datetime.utcnow() case_xml = get_close_case_xml(time=time, case_id="uid_blah", uid="uid_blah_1") check_xml_line_by_line( self, case_xml, CLOSE_CASE_XML.format(time=json_format_datetime(time)))
def test_update_preload_case(self): self.form.source = XFORM_SOURCE self.form.requires = 'case' self.form.actions.update_case = UpdateCaseAction(update={'question1': '/data/question1'}) self.form.actions.update_case.condition.type = 'always' self.form.actions.case_preload = PreloadAction(preload={'/data/question1': 'question1'}) self.form.actions.case_preload.condition.type = 'always' check_xml_line_by_line(self, UPDATE_PRELOAD_CASE_SOURCE, self.form.render_xform())
def test_open_update_case(self): self.form.actions.open_case = OpenCaseAction( name_path="/data/question1", external_id=None) self.form.actions.open_case.condition.type = 'always' self.form.actions.update_case = UpdateCaseAction( update={'question1': '/data/question1'}) self.form.actions.update_case.condition.type = 'always' check_xml_line_by_line(self, self.get_xml('open_update_case'), self.form.render_xform())
def test_repeater(self): CommCareCase.get(case_id) def now(): return datetime.utcnow() repeat_records = RepeatRecord.all(domain=self.domain, due_before=now()) self.assertEqual(len(repeat_records), 2) self.clear_log() for repeat_record in repeat_records: repeat_record.fire(post_fn=self.make_post_fn([404, 404, 404])) repeat_record.save() for (url, status, data) in self.log: self.assertEqual(status, 404) self.clear_log() next_check_time = now() + timedelta(minutes=60) repeat_records = RepeatRecord.all(domain=self.domain, due_before=now() + timedelta(minutes=15)) self.assertEqual(len(repeat_records), 0) repeat_records = RepeatRecord.all(domain=self.domain, due_before=next_check_time + timedelta(seconds=2)) self.assertEqual(len(repeat_records), 2) for repeat_record in repeat_records: self.assertLess(abs(next_check_time - repeat_record.next_check), timedelta(seconds=2)) repeat_record.fire(post_fn=self.make_post_fn([404, 200])) repeat_record.save() self.assertEqual(len(self.log), 4) self.assertEqual(self.log[1], (self.form_repeater.url, 200, xform_xml)) self.assertEqual(self.log[3][:2], (self.case_repeater.url, 200)) check_xml_line_by_line(self, self.log[3][2], case_block) repeat_records = RepeatRecord.all(domain=self.domain, due_before=next_check_time) for repeat_record in repeat_records: self.assertEqual(repeat_record.succeeded, True) self.assertEqual(repeat_record.next_check, None) repeat_records = RepeatRecord.all(domain=self.domain, due_before=now()) self.assertEqual(len(repeat_records), 0) self.post_xml(update_xform_xml) repeat_records = RepeatRecord.all(domain=self.domain, due_before=now()) self.assertEqual(len(repeat_records), 2)
def test_xml(self): check_xml_line_by_line(self, """ <district> <state_name>Delhi_state</state_name> <district_name lang="hin">Delhi_in_HIN</district_name> <district_name lang="eng">Delhi_in_ENG</district_name> <district_id>Delhi_id</district_id> </district> """, ElementTree.tostring(self.data_item.to_xml()))
def test_ota_basic(self): user = self.user amounts = [(p._id, i * 10) for i, p in enumerate(self.products)] report = _report_soh(amounts, self.sp.case_id, "stock") check_xml_line_by_line( self, balance_ota_block(self.sp, "stock", amounts, datestring=json_format_datetime(report.date)), get_ota_balance_xml(self.domain, user)[0], )
def test_repeater(self): CommCareCase.get(case_id) def now(): return datetime.utcnow() repeat_records = RepeatRecord.all(domain=self.domain, due_before=now()) self.assertEqual(len(repeat_records), 2) self.clear_log() for repeat_record in repeat_records: repeat_record.fire(post_fn=self.make_post_fn([404, 404, 404])) repeat_record.save() for (url, status, data, headers) in self.log: self.assertEqual(status, 404) self.clear_log() next_check_time = now() + timedelta(minutes=60) repeat_records = RepeatRecord.all(domain=self.domain, due_before=now() + timedelta(minutes=15)) self.assertEqual(len(repeat_records), 0) repeat_records = RepeatRecord.all(domain=self.domain, due_before=next_check_time + timedelta(seconds=2)) self.assertEqual(len(repeat_records), 2) for repeat_record in repeat_records: self.assertLess(abs(next_check_time - repeat_record.next_check), timedelta(seconds=2)) repeat_record.fire(post_fn=self.make_post_fn([404, 200])) repeat_record.save() self.assertEqual(len(self.log), 4) self.assertEqual(self.log[1][:3], (self.form_repeater.url, 200, xform_xml)) self.assertIn('received-on', self.log[1][3]) self.assertEqual(self.log[3][:2], (self.case_repeater.url, 200)) self.assertIn('server-modified-on', self.log[3][3]) check_xml_line_by_line(self, self.log[3][2], case_block) repeat_records = RepeatRecord.all(domain=self.domain, due_before=next_check_time) for repeat_record in repeat_records: self.assertEqual(repeat_record.succeeded, True) self.assertEqual(repeat_record.next_check, None) repeat_records = RepeatRecord.all(domain=self.domain, due_before=now()) self.assertEqual(len(repeat_records), 0) self.post_xml(update_xform_xml) repeat_records = RepeatRecord.all(domain=self.domain, due_before=now()) self.assertEqual(len(repeat_records), 2)
def test_close_referral_xml(self): time = datetime.utcnow() referral_xml = get_close_referral_xml(time=time, case_id="blah", referral_id="blah", referral_type="blah", uid="uid_blah_2") check_xml_line_by_line( self, referral_xml, CLOSE_REFERRAL_XML.format(time=json_format_datetime(time)))
def test_close_referral_xml(self): time = datetime.utcnow() referral_xml = get_close_referral_xml( time=time, case_id="blah", referral_id="blah", referral_type="blah", uid="uid_blah_2" ) check_xml_line_by_line(self, referral_xml, CLOSE_REFERRAL_XML.format(time=json_format_datetime(time)))
def test_suite(self): with open( os.path.join(os.path.dirname(__file__), 'data', 'days_ago_suite.xml')) as f: suiteA = f.read() suiteB = self.app.create_suite() check_xml_line_by_line(self, suiteA, suiteB)
def test_cleaner(self): check_xml_line_by_line(self, """ <dirty_fields> <will_crash>yep</will_crash> <space_cadet>major tom</space_cadet> <yes_no>no, duh</yes_no> <_with_>so fail</_with_> <_crazy___combo__d>just why</_crazy___combo__d> </dirty_fields> """, ElementTree.tostring(self.data_item.to_xml()))
def test_user_restore(self): self.assertEqual(0, self._get_synclog_count()) restore_payload = deprecated_generate_restore_payload( self.project, self.restore_user, items=True) sync_log = self._get_the_first_synclog() check_xml_line_by_line( self, dummy_restore_xml(sync_log.get_id, items=3, user=self.restore_user), restore_payload, )
def test_callcenter_no_group(self): fixture = gen_fixture(self.user, CallCenter('domain', 'user')) check_xml_line_by_line(self, """ <fixture id="indicators:call_center" user_id="{userid}"> <indicators> <casesUpdatedInLastWeek>3</casesUpdatedInLastWeek> <casesUpdatedInWeekPrior>4</casesUpdatedInWeekPrior> </indicators> </fixture> """.format(userid=self.user.user_id), ElementTree.tostring(fixture))
def assert_xml_equiv(self, actual, expected): actual_canonicalized = io.BytesIO() expected_canonicalized = io.BytesIO() parser = lxml.etree.XMLParser(remove_blank_text=True) lxml.etree.fromstring(actual, parser=parser).getroottree().write_c14n(actual_canonicalized) lxml.etree.fromstring(expected, parser=parser).getroottree().write_c14n(expected_canonicalized) if actual_canonicalized.getvalue() != expected_canonicalized.getvalue(): check_xml_line_by_line(self, actual, expected)
def test_callcenter_no_group(self): fixture = gen_fixture(self.user, CallCenter('domain', 'user')) check_xml_line_by_line( self, """ <fixture id="indicators:call_center" user_id="{userid}"> <indicators> <casesUpdatedInLastWeek>3</casesUpdatedInLastWeek> <casesUpdatedInWeekPrior>4</casesUpdatedInWeekPrior> <averageDurationPerCase>7</averageDurationPerCase> </indicators> </fixture> """.format(userid=self.user.user_id), ElementTree.tostring(fixture))
def testParseWithIndices(self): self.testParseCreate() user_id = "bar-user-id" for prereq in ["some_referenced_id", "some_other_referenced_id"]: post_case_blocks([ CaseBlock( create=True, case_id=prereq, user_id=user_id, version=V2).as_xml(format_datetime=json_format_datetime) ]) file_path = os.path.join(os.path.dirname(__file__), "data", "v2", "index_update.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) case = CommCareCase.get("foo-case-id") self.assertEqual(2, len(case.indices)) self.assertTrue(case.has_index("foo_ref")) self.assertTrue(case.has_index("baz_ref")) self.assertEqual("bar", case.get_index("foo_ref").referenced_type) self.assertEqual("some_referenced_id", case.get_index("foo_ref").referenced_id) self.assertEqual("bop", case.get_index("baz_ref").referenced_type) self.assertEqual("some_other_referenced_id", case.get_index("baz_ref").referenced_id) # check the action self.assertEqual(2, len(case.actions)) [_, index_action] = case.actions self.assertEqual(const.CASE_ACTION_INDEX, index_action.action_type) self.assertEqual(2, len(index_action.indices)) # quick test for ota restore v2response = phone_views.xml_for_case(HttpRequest(), case.get_id, version="2.0") expected_v2_response = """ <case case_id="foo-case-id" date_modified="2011-12-07T13:42:50Z" user_id="bar-user-id" xmlns="http://commcarehq.org/case/transaction/v2"> <create> <case_type>v2_case_type</case_type> <case_name>test case name</case_name> <owner_id>bar-user-id</owner_id> </create> <index> <baz_ref case_type="bop">some_other_referenced_id</baz_ref> <foo_ref case_type="bar">some_referenced_id</foo_ref> </index> </case>""" check_xml_line_by_line(self, expected_v2_response, v2response.content)
def testParseWithIndices(self): self._test_parse_create() user_id = "bar-user-id" for prereq in ["some_referenced_id", "some_other_referenced_id"]: post_case_blocks([ CaseBlock(create=True, case_id=prereq, user_id=user_id).as_xml() ]) file_path = os.path.join(os.path.dirname(__file__), "data", "v2", "index_update.xml") with open(file_path, "rb") as f: xml_data = f.read() case = submit_form_locally(xml_data, 'test-domain').case self.assertEqual(2, len(case.indices)) self.assertTrue(case.has_index("foo_ref")) self.assertTrue(case.has_index("baz_ref")) self.assertEqual("bar", case.get_index("foo_ref").referenced_type) self.assertEqual("some_referenced_id", case.get_index("foo_ref").referenced_id) self.assertEqual("bop", case.get_index("baz_ref").referenced_type) self.assertEqual("some_other_referenced_id", case.get_index("baz_ref").referenced_id) if not getattr(settings, 'TESTS_SHOULD_USE_SQL_BACKEND', False): # check the action self.assertEqual(2, len(case.actions)) [_, index_action] = case.actions self.assertEqual(const.CASE_ACTION_INDEX, index_action.action_type) self.assertEqual(2, len(index_action.indices)) # quick test for ota restore v2response = xml.get_case_xml( case, [const.CASE_ACTION_CREATE, const.CASE_ACTION_UPDATE], V2) expected_v2_response = """ <case case_id="foo-case-id" date_modified="2011-12-07T13:42:50.000000Z" user_id="bar-user-id" xmlns="http://commcarehq.org/case/transaction/v2"> <create> <case_type>v2_case_type</case_type> <case_name>test case name</case_name> <owner_id>bar-user-id</owner_id> </create> <update> <date_opened>2011-12-06</date_opened> </update> <index> <baz_ref case_type="bop">some_other_referenced_id</baz_ref> <foo_ref case_type="bar">some_referenced_id</foo_ref> </index> </case>""" check_xml_line_by_line(self, expected_v2_response, v2response)
def test_callcenter_group(self): fixture = gen_fixture(self.user, TestIndicatorSet('domain', 'user', 'case')) check_xml_line_by_line(self, """ <fixture id="indicators:call_center" user_id="{userid}"> <indicators> <case id="321"> <casesUpdatedInLastWeek>3</casesUpdatedInLastWeek> <casesUpdatedInWeekPrior>4</casesUpdatedInWeekPrior> <averageDurationPerCase>7</averageDurationPerCase> </case> </indicators> </fixture> """.format(userid=self.user.user_id), ElementTree.tostring(fixture))
def test_ota_basic(self): user = self.user amounts = [(p._id, i * 10) for i, p in enumerate(self.products)] report = _report_soh(amounts, self.sp._id, 'stock') check_xml_line_by_line( self, balance_ota_block( self.sp, 'stock', amounts, datestring=json_format_datetime(report.date), ), get_ota_balance_xml(user)[0], )
def testParseWithIndices(self): self._test_parse_create() user_id = "bar-user-id" for prereq in ["some_referenced_id", "some_other_referenced_id"]: post_case_blocks([ CaseBlock( create=True, case_id=prereq, user_id=user_id ).as_xml() ]) file_path = os.path.join(os.path.dirname(__file__), "data", "v2", "index_update.xml") with open(file_path, "rb") as f: xml_data = f.read() case = submit_form_locally(xml_data, 'test-domain').case self.assertEqual(2, len(case.indices)) self.assertTrue(case.has_index("foo_ref")) self.assertTrue(case.has_index("baz_ref")) self.assertEqual("bar", case.get_index("foo_ref").referenced_type) self.assertEqual("some_referenced_id", case.get_index("foo_ref").referenced_id) self.assertEqual("bop", case.get_index("baz_ref").referenced_type) self.assertEqual("some_other_referenced_id", case.get_index("baz_ref").referenced_id) if not getattr(settings, 'TESTS_SHOULD_USE_SQL_BACKEND', False): # check the action self.assertEqual(2, len(case.actions)) [_, index_action] = case.actions self.assertEqual(const.CASE_ACTION_INDEX, index_action.action_type) self.assertEqual(2, len(index_action.indices)) # quick test for ota restore v2response = xml.get_case_xml(case, [const.CASE_ACTION_CREATE, const.CASE_ACTION_UPDATE], V2) expected_v2_response = """ <case case_id="foo-case-id" date_modified="2011-12-07T13:42:50.000000Z" user_id="bar-user-id" xmlns="http://commcarehq.org/case/transaction/v2"> <create> <case_type>v2_case_type</case_type> <case_name>test case name</case_name> <owner_id>bar-user-id</owner_id> </create> <update> <date_opened>2011-12-06</date_opened> </update> <index> <baz_ref case_type="bop">some_other_referenced_id</baz_ref> <foo_ref case_type="bar">some_referenced_id</foo_ref> </index> </case>""" check_xml_line_by_line(self, expected_v2_response, v2response)
def testParseWithIndices(self): self.testParseCreate() user_id = "bar-user-id" for prereq in ["some_referenced_id", "some_other_referenced_id"]: post_case_blocks([ CaseBlock( create=True, case_id=prereq, user_id=user_id, version=V2 ).as_xml() ]) file_path = os.path.join(os.path.dirname(__file__), "data", "v2", "index_update.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) case = CommCareCase.get("foo-case-id") self.assertEqual(2, len(case.indices)) self.assertTrue(case.has_index("foo_ref")) self.assertTrue(case.has_index("baz_ref")) self.assertEqual("bar", case.get_index("foo_ref").referenced_type) self.assertEqual("some_referenced_id", case.get_index("foo_ref").referenced_id) self.assertEqual("bop", case.get_index("baz_ref").referenced_type) self.assertEqual("some_other_referenced_id", case.get_index("baz_ref").referenced_id) # check the action self.assertEqual(2, len(case.actions)) [_, index_action] = case.actions self.assertEqual(const.CASE_ACTION_INDEX, index_action.action_type) self.assertEqual(2, len(index_action.indices)) # quick test for ota restore v2response = xml.get_case_xml(case, [const.CASE_ACTION_CREATE, const.CASE_ACTION_UPDATE], V2) expected_v2_response = """ <case case_id="foo-case-id" date_modified="2011-12-07T13:42:50.000000Z" user_id="bar-user-id" xmlns="http://commcarehq.org/case/transaction/v2"> <create> <case_type>v2_case_type</case_type> <case_name>test case name</case_name> <owner_id>bar-user-id</owner_id> </create> <index> <baz_ref case_type="bop">some_other_referenced_id</baz_ref> <foo_ref case_type="bar">some_referenced_id</foo_ref> </index> </case>""" check_xml_line_by_line(self, expected_v2_response, v2response)
def test_callcenter_fixture_commcare_user(self): user = CommCareUser(_id='123', username="******") indicator_set = MockIndicatorSet(name='test', indicators=OrderedDict([ ('user_case1', {'i1': 1, 'i2': 2}), ('user_case2', {'i1': 0, 'i2': 3}) ])) restore_user = type('OTARestoreCommCareUserFake' if six.PY3 else b'OTARestoreCommCareUserFake', (OTARestoreCommCareUser,), { 'project': Domain(name='test', default_timezone='UTC'), 'get_call_center_indicators': lambda self, config: indicator_set, })('test', user) fixture, = call_fixture_generator(mock_indicators_fixture_generator, restore_user) check_xml_line_by_line( self, ElementTree.tostring(fixture), ElementTree.tostring(gen_fixture(restore_user, indicator_set)))
def test_cleaner(self): item_dict = self.data_item.to_json() item_dict['_data_type'] = self.data_item.data_type check_xml_line_by_line( self, """ <dirty_fields> <will_crash>yep</will_crash> <space_cadet>major tom</space_cadet> <yes_no>no, duh</yes_no> <_with_>so fail</_with_> <_crazy___combo__d>just why</_crazy___combo__d> </dirty_fields> """, ElementTree.tostring(item_lists.to_xml(item_dict), encoding='utf-8'))
def test_xml(self): item_dict = self.data_item.to_json() item_dict['_data_type'] = self.data_item.data_type check_xml_line_by_line( self, """ <district> <state_name>Delhi_state</state_name> <district_name lang="hin">Delhi_in_HIN</district_name> <district_name lang="eng">Delhi_in_ENG</district_name> <district_id>Delhi_id</district_id> </district> """, ElementTree.tostring( fixturegenerators.item_lists.to_xml(item_dict), encoding='utf-8'))
def test_ota_basic(self): amounts = [ SohReport(section_id='stock', product_id=p._id, amount=i * 10) for i, p in enumerate(self.products) ] report_date = _report_soh(amounts, self.sp.case_id, self.domain.name) check_xml_line_by_line( self, balance_ota_block( self.sp, 'stock', amounts, datestring=report_date, ), util.get_ota_balance_xml(self.domain, self.user)[0], )
def test_callcenter_keys(self): fixture = gen_fixture(self.user, CallCenter('domain', 'user', 'case', [['123'], ['456']])) check_xml_line_by_line(self, """ <fixture id="indicators:call_center" user_id="{userid}"> <indicators> <case id="123"> <casesUpdatedInLastWeek>3</casesUpdatedInLastWeek> <casesUpdatedInWeekPrior>4</casesUpdatedInWeekPrior> </case> <case id="456"> <casesUpdatedInLastWeek>0</casesUpdatedInLastWeek> <casesUpdatedInWeekPrior>0</casesUpdatedInWeekPrior> </case> </indicators> </fixture> """.format(userid=self.user.user_id), ElementTree.tostring(fixture))
def test_ota_basic(self): amounts = [ SohReport(section_id='stock', product_id=p._id, amount=i*10) for i, p in enumerate(self.products) ] report_date = _report_soh(amounts, self.sp.case_id, self.domain.name) check_xml_line_by_line( self, balance_ota_block( self.sp, 'stock', amounts, datestring=report_date, ), util.get_ota_balance_xml(self.domain, self.user)[0], )
def test_ota_consumption(self): self.ct_settings.sync_consumption_fixtures = True self.ct_settings.consumptionconfig = ConsumptionConfig( min_transactions=0, min_window=0, optimal_window=60, ) self.ct_settings.stockrestoreconfig = StockRestoreConfig( section_to_consumption_types={'stock': 'consumption'}) set_default_monthly_consumption_for_domain(self.domain.name, 5 * DAYS_IN_MONTH) self._save_settings_and_clear_cache() amounts = [ SohReport(section_id='stock', product_id=p._id, amount=i * 10) for i, p in enumerate(self.products) ] form_id, report_date = _report_soh(amounts, self.sp.case_id, self.domain.name) self.addCleanup(self.delete_ledger_transactions, form_id) balance_blocks = _get_ota_balance_blocks(self.domain, self.user) self.assertEqual(2, len(balance_blocks)) stock_block, consumption_block = balance_blocks check_xml_line_by_line( self, balance_ota_block( self.sp, 'stock', amounts, datestring=report_date, ), stock_block, ) check_xml_line_by_line( self, balance_ota_block( self.sp, 'consumption', [ SohReport(section_id='', product_id=p._id, amount=150) for p in self.products ], datestring=report_date, ), consumption_block, )
def testUserRestore(self): self.assertEqual(0, SyncLog.view( "phone/sync_logs_by_user", include_docs=True, reduce=False, ).count()) restore_payload = generate_restore_payload(self.project, dummy_user(), items=True) sync_log = SyncLog.view( "phone/sync_logs_by_user", include_docs=True, reduce=False, ).one() check_xml_line_by_line( self, dummy_restore_xml(sync_log.get_id, items=3), restore_payload, )
def test_xml(self): now = datetime.utcnow() domain = 'test' now_plus_30 = now + timedelta(days=30) now_minus_30 = now - timedelta(days=30) record = new_key_record(None, None, now=now) xml = get_mobile_auth_payload([record], domain, now=now) check_xml_line_by_line( self, xml, """ <OpenRosaResponse xmlns="http://openrosa.org/http/response"> <message nature="submit_success">Here are your keys!</message> <auth_keys domain="{domain}" issued="{now}"> <key_record valid="{now}" expires="{now_plus_30}"> <uuid>{record.uuid}</uuid> <key type="{record.type}">{record.key}</key> </key_record> </auth_keys> </OpenRosaResponse> """.format( now=self.format_datetime_no_usec(now), now_plus_30=self.format_datetime_no_usec(now_plus_30), record=record, domain=domain, )) record = new_key_record(None, None, now=now, valid=now_minus_30) xml = get_mobile_auth_payload([record], domain, now=now) check_xml_line_by_line( self, xml, """ <OpenRosaResponse xmlns="http://openrosa.org/http/response"> <message nature="submit_success">Here are your keys!</message> <auth_keys domain="{domain}" issued="{now}"> <key_record valid="{now_minus_30}" expires="{now_plus_30}"> <uuid>{record.uuid}</uuid> <key type="{record.type}">{record.key}</key> </key_record> </auth_keys> </OpenRosaResponse> """.format( now=self.format_datetime_no_usec(now), now_plus_30=self.format_datetime_no_usec(now_plus_30), now_minus_30=self.format_datetime_no_usec(now_minus_30), record=record, domain=domain, ))
def test_ota_multiple_stocks(self): user = self.user date = datetime.utcnow() report = StockReport.objects.create(form_id=uuid.uuid4().hex, date=date, type=stockconst.REPORT_TYPE_BALANCE) amounts = [(p._id, i * 10) for i, p in enumerate(self.products)] section_ids = sorted(("stock", "losses", "consumption")) for section_id in section_ids: _report_soh(amounts, self.sp.case_id, section_id, report=report) balance_blocks = get_ota_balance_xml(self.domain, user) self.assertEqual(3, len(balance_blocks)) for i, section_id in enumerate(section_ids): check_xml_line_by_line( self, balance_ota_block(self.sp, section_id, amounts, datestring=json_format_datetime(date)), balance_blocks[i], )
def test_fixture_removal(self): """ An empty fixture list should be generated for each fixture that the use does not have access to (within the domain). """ self.data_item.remove_user(self.user) fixtures = call_fixture_generator(self.user.to_ota_restore_user()) self.assertEqual(1, len(fixtures)) check_xml_line_by_line( self, """ <fixture id="item-list:district" user_id="{}"> <district_list /> </fixture> """.format(self.user.user_id), ElementTree.tostring(fixtures[0])) self.fixture_ownership = self.data_item.add_user(self.user)
def test_repeater(self): CommCareCase.get(case_id) now = datetime.utcnow() repeat_records = RepeatRecord.all(domain=self.domain, due_before=now) self.assertEqual(len(repeat_records), 2) self.clear_log() for repeat_record in repeat_records: repeat_record.fire(post_fn=self.make_post_fn([404, 404, 404])) repeat_record.save() for (url, status, data) in self.log: self.assertEqual(status, 404) self.clear_log() in30min = now + timedelta(minutes=30) repeat_records = RepeatRecord.all(domain=self.domain, due_before=now + timedelta(minutes=15)) self.assertEqual(len(repeat_records), 0) repeat_records = RepeatRecord.all(domain=self.domain, due_before=in30min + timedelta(seconds=1)) self.assertEqual(len(repeat_records), 2) for repeat_record in repeat_records: self.assertLess(abs(in30min - repeat_record.next_check), timedelta(seconds=1)) repeat_record.fire(post_fn=self.make_post_fn([404, 200])) repeat_record.save() self.assertEqual(len(self.log), 4) self.assertEqual(self.log[1], (self.form_repeater.url, 200, xform_xml)) self.assertEqual(self.log[3][:2], (self.case_repeater.url, 200)) check_xml_line_by_line(self, self.log[3][2], case_block) repeat_records = RepeatRecord.all(domain=self.domain, due_before=in30min) for repeat_record in repeat_records: self.assertEqual(repeat_record.succeeded, True) self.assertEqual(repeat_record.next_check, None)
def test_xml(self): now = datetime.utcnow() domain = 'test' now_plus_30 = now + timedelta(days=30) now_minus_30 = now - timedelta(days=30) record = new_key_record(None, None, now=now) xml = get_mobile_auth_payload([record], domain, now=now) check_xml_line_by_line(self, xml, """ <OpenRosaResponse xmlns="http://openrosa.org/http/response"> <message nature="submit_success">Here are your keys!</message> <auth_keys domain="{domain}" issued="{now}"> <key_record valid="{now}" expires="{now_plus_30}"> <uuid>{record.uuid}</uuid> <key type="{record.type}">{record.key}</key> </key_record> </auth_keys> </OpenRosaResponse> """.format( now=self.format_datetime_no_usec(now), now_plus_30=self.format_datetime_no_usec(now_plus_30), record=record, domain=domain, )) record = new_key_record(None, None, now=now, valid=now_minus_30) xml = get_mobile_auth_payload([record], domain, now=now) check_xml_line_by_line(self, xml, """ <OpenRosaResponse xmlns="http://openrosa.org/http/response"> <message nature="submit_success">Here are your keys!</message> <auth_keys domain="{domain}" issued="{now}"> <key_record valid="{now_minus_30}" expires="{now_plus_30}"> <uuid>{record.uuid}</uuid> <key type="{record.type}">{record.key}</key> </key_record> </auth_keys> </OpenRosaResponse> """.format( now=self.format_datetime_no_usec(now), now_plus_30=self.format_datetime_no_usec(now_plus_30), now_minus_30=self.format_datetime_no_usec(now_minus_30), record=record, domain=domain, ))
def test_ota_consumption(self): self.ct_settings.sync_consumption_fixtures = True self.ct_settings.consumption_config = ConsumptionConfig( min_transactions=0, min_window=0, optimal_window=60, ) self.ct_settings.ota_restore_config = StockRestoreConfig( section_to_consumption_types={'stock': 'consumption'} ) set_default_monthly_consumption_for_domain(self.domain.name, 5 * DAYS_IN_MONTH) self._save_settings_and_clear_cache() amounts = [ SohReport(section_id='stock', product_id=p._id, amount=i * 10) for i, p in enumerate(self.products) ] report_date = _report_soh(amounts, self.sp.case_id, self.domain.name) balance_blocks = _get_ota_balance_blocks(self.domain, self.user) self.assertEqual(2, len(balance_blocks)) stock_block, consumption_block = balance_blocks check_xml_line_by_line( self, balance_ota_block( self.sp, 'stock', amounts, datestring=report_date, ), stock_block, ) check_xml_line_by_line( self, balance_ota_block( self.sp, 'consumption', [SohReport(section_id='', product_id=p._id, amount=150) for p in self.products], datestring=report_date, ), consumption_block, )
def _check_fixture(self, fixture_xml, has_groups=True, item_lists=None): fixture_xml = list(fixture_xml) item_lists = item_lists or [] expected_len = sum([has_groups, len(item_lists)]) self.assertEqual(len(fixture_xml), expected_len) if has_groups: expected = _get_group_fixture(self.user.get_id, [self.group1, self.group2]) check_xml_line_by_line(self, expected, ElementTree.tostring(fixture_xml[0])) if item_lists: for i, item_list_tag in enumerate(item_lists): data_type, data_item = self.item_lists[item_list_tag] item_list_xml = [ ElementTree.tostring(fixture) for fixture in fixture_xml if item_list_tag in fixture.attrib.get("id") ] self.assertEqual(len(item_list_xml), 1) expected = _get_item_list_fixture(self.user.get_id, data_type.tag, data_item) check_xml_line_by_line(self, expected, item_list_xml[0])
def test_fixture_removal(self): """ An empty fixture list should be generated for each fixture that the use does not have access to (within the domain). """ self.data_item.remove_user(self.user) fixtures = fixturegenerators.item_lists(self.user.to_ota_restore_user(), V2) self.assertEqual(1, len(fixtures)) check_xml_line_by_line( self, """ <fixture id="item-list:district" user_id="{}"> <district_list /> </fixture> """.format(self.user.user_id), ElementTree.tostring(fixtures[0]) ) self.fixture_ownership = self.data_item.add_user(self.user)
def testUserRestoreWithCase(self): file_path = os.path.join(os.path.dirname(__file__), "data", "create_short.xml") with open(file_path, "rb") as f: xml_data = f.read() # implicit length assertion _, _, [newcase] = submit_form_locally(xml_data, domain=self.project.name) expected_case_block = """ <case> <case_id>asdf</case_id> <date_modified>2010-06-29T13:42:50.000000Z</date_modified> <create> <case_type_id>test_case_type</case_type_id> <user_id>foo</user_id> <case_name>test case name</case_name> <external_id>someexternal</external_id> </create> </case>""" check_xml_line_by_line(self, expected_case_block, xml.get_case_xml(newcase, [case_const.CASE_ACTION_CREATE, case_const.CASE_ACTION_UPDATE])) # check v2 expected_v2_case_block = """ <case case_id="asdf" date_modified="2010-06-29T13:42:50.000000Z" user_id="foo" xmlns="http://commcarehq.org/case/transaction/v2" > <create> <case_type>test_case_type</case_type> <case_name>test case name</case_name> <owner_id>foo</owner_id> </create> <update> <external_id>someexternal</external_id> </update> </case>""" check_xml_line_by_line( self, expected_v2_case_block, xml.get_case_xml( newcase, [case_const.CASE_ACTION_CREATE, case_const.CASE_ACTION_UPDATE], version="2.0", ), ) restore_payload = generate_restore_payload( project=self.project, user=dummy_user(), items=True, ) sync_log_id = SyncLog.view( "phone/sync_logs_by_user", include_docs=True, reduce=False, ).one().get_id check_xml_line_by_line( self, dummy_restore_xml(sync_log_id, expected_case_block, items=4), restore_payload )