def test_validate_parents(self, location_type_mock, locations_mock, *_): location_type_mock.by_domain.return_value = self.location_types location_type_mock.select_related.return_value.filter.return_value = self.location_types locations_mock.select_related.return_value.filter.return_value = [ Location(site_code='13', location_type=self.state_location_type) ] with tempfile.TemporaryFile() as file: export_raw(self.headers, self.rows, file, format=Format.XLS_2007) file.seek(0) workbook = get_workbook(file) parser = Parser(self.domain, workbook) errors = parser.parse() self.assertIn('Unexpected non-state parent 1 set for supervisor', errors, "missing location found") self.assertIn('Unexpected state parent 13 set for awc', errors, "incorrect parent type not flagged") self.assertIn('Parent 12 is marked for archival', errors, "archived parent not caught")
def test_validate_new_site_codes_type(self, location_type_mock, locations_mock, *_): location_type_mock.by_domain.return_value = self.location_types location_type_mock.select_related.return_value.filter.return_value = self.location_types locations_mock.select_related.return_value.filter.return_value = [ Location(site_code='13', location_type=self.state_location_type) ] with tempfile.TemporaryFile() as file: export_raw(self.headers, self.rows, file, format=Format.XLS_2007) file.seek(0) workbook = get_workbook(file) parser = Parser(self.domain, workbook) errors = parser.parse() self.assertIn('state 13 used as supervisor', errors) self.assertIn('state 13 used as awc', errors) self.assertNotIn('state 13 used as state', errors)
def test_parser(self, location_type_mock, _): type_codes = ['state', 'supervisor', 'awc'] location_type_mock.return_value = list( map(lambda site_code: LocationType(code=site_code), type_codes)) with tempfile.TemporaryFile() as file: export_raw(self.headers, self.rows, file, format=Format.XLS_2007) file.seek(0) workbook = get_workbook(file) parser = Parser(self.domain, workbook) errors = parser.parse() self.assertEqual(parser.valid_transitions['awc']['Move'], {'131': '112'}) self.assertEqual(parser.valid_transitions['awc']['Merge'], {'132': ['113', '114']}) self.assertEqual(parser.valid_transitions['supervisor']['Move'], {'13': '12'}) self.assertEqual(errors, [ "No change in location code for Extract, got old: '111' and new: '111'", "New location 132 reused with different information", "Missing location code for Split, got old: '11' and new: ''", "Invalid Operation Unknown" ])
def _get_parser(self, action_type, workbook): if action_type == LocationReassignmentRequestForm.REASSIGN_HOUSEHOLDS: return HouseholdReassignmentParser(self.domain, workbook) elif action_type == LocationReassignmentRequestForm.REASSIGN_OTHER_CASES: return OtherCasesReassignmentParser(self.domain, workbook) return Parser(self.domain, workbook)
def test_parser(self, location_type_mock, _): location_type_mock.by_domain.return_value = self.location_types location_type_mock.select_related.return_value.filter.return_value = self.location_types with tempfile.TemporaryFile() as file: export_raw(self.headers, self.rows, file, format=Format.XLS_2007) file.seek(0) workbook = get_workbook(file) parser = Parser(self.domain, workbook) errors = parser.parse() self.assertEqual(len(parser.valid_transitions['awc']), 2) awc_transitions = [attr.asdict(t) for t in parser.valid_transitions['awc']] self.assertEqual( awc_transitions, [ { 'domain': self.domain, 'location_type_code': 'awc', 'operation': 'Move', 'old_site_codes': ['112'], 'new_site_codes': ['131'], 'new_location_details': { '131': {'name': 'AWC 3 [131]', 'parent_site_code': '13', 'lgd_code': 'AWC-131', 'sub_district_name': None}}, 'user_transitions': {'username2': 'username3'} }, { 'domain': self.domain, 'location_type_code': 'awc', 'operation': 'Move', 'old_site_codes': ['115'], 'new_site_codes': ['133'], 'new_location_details': { '133': {'name': 'AWC 8 [133]', 'parent_site_code': '12', 'lgd_code': 'AWC-133', 'sub_district_name': None}}, 'user_transitions': {'username6': 'username7'} } ] ) self.assertEqual(len(parser.valid_transitions['supervisor']), 1) supervisor_transition = attr.asdict(parser.valid_transitions['supervisor'][0]) self.assertEqual( supervisor_transition, {'domain': self.domain, 'location_type_code': 'supervisor', 'operation': 'Move', 'old_site_codes': ['12'], 'new_site_codes': ['13'], 'new_location_details': { '13': { 'name': 'Supervisor 3 [13]', 'parent_site_code': '1', 'lgd_code': 'Sup-13', 'sub_district_name': None } }, 'user_transitions': {'username5': 'username6'}} ) self.assertEqual(len(parser.valid_transitions['state']), 1) state_transition = attr.asdict(parser.valid_transitions['state'][0]) self.assertEqual( state_transition, {'domain': self.domain, 'location_type_code': 'state', 'operation': 'Move', 'old_site_codes': ['2'], 'new_site_codes': ['3'], 'new_location_details': { '3': { 'name': 'State 3', 'parent_site_code': '', 'lgd_code': '', 'sub_district_name': None } }, 'user_transitions': {}} ) self.assertEqual(errors, [ "Invalid Operation Unknown", "Missing location code for operation Split. Got old: '11' and new: ''", "Got invalid location code 'invalid $ite #code' for operation Move", "Got invalid location code 'new-- $ite #code' for operation Move", "Got invalid parent location code '?--123--?' for new location " "'new-- $ite #code' for operation Move", "No change in location code for operation Extract. Got old: '111' and new: '111'", "New location 132 passed with different information", "Missing new location name for 134" ])