def bootstrap_user(username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, ): from corehq.apps.commtrack.helpers import make_supply_point user_data = user_data or {} user = CommCareUser.create( domain, username, password, phone_numbers=[TEST_NUMBER], user_data=user_data, first_name=first_name, last_name=last_name ) if not SupplyPointCase.get_by_location(home_loc): make_supply_point(domain, home_loc) home_loc.save() user.set_location(home_loc) user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) return CommCareUser.wrap(user.to_json())
def bootstrap_user( username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, ): from corehq.apps.commtrack.helpers import make_supply_point user_data = user_data or {} user = CommCareUser.create(domain, username, password, phone_numbers=[TEST_NUMBER], user_data=user_data, first_name=first_name, last_name=last_name) if not SupplyPointCase.get_by_location(home_loc): make_supply_point(domain, home_loc) home_loc.save() user.set_location(home_loc) user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) return CommCareUser.wrap(user.to_json())
def bootstrap_user( setup, username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, ): user_data = user_data or {} user = CommTrackUser.create(domain, username, password, phone_numbers=[TEST_NUMBER], user_data=user_data, first_name=first_name, last_name=last_name) if home_loc == setup.loc.site_code: if not SupplyPointCase.get_by_location(setup.loc): make_supply_point(domain, setup.loc) user.add_location(setup.loc) user.save() user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) return CommTrackUser.wrap(user.to_json())
def test_location_removal_only_submits_if_it_existed(self): user = self.user # can't test with the original since the user already owns it loc = make_loc('secondloc') make_supply_point(self.domain.name, loc) with patch('corehq.apps.users.models.CommCareUser.submit_location_block') as submit_blocks: user.remove_location_delegate(loc) self.assertEqual(submit_blocks.call_count, 0)
def test_location_removal_only_submits_if_it_existed(self): user = self.reporters['fixed'] # can't test with the original since the user already owns it loc = make_loc('secondloc') make_supply_point(self.domain, loc) with patch('corehq.apps.commtrack.models.CommTrackUser.submit_location_block') as submit_blocks: user.remove_location(loc) self.assertEqual(submit_blocks.call_count, 0)
def test_location_removal_only_submits_if_it_existed(self): user = self.user # can't test with the original since the user already owns it loc = make_loc('secondloc') make_supply_point(self.domain.name, loc) with patch('corehq.apps.commtrack.models.CommTrackUser.submit_location_block') as submit_blocks: user.remove_location(loc) self.assertEqual(submit_blocks.call_count, 0)
def post_loc_created(sender, loc=None, **kwargs): # circular imports from corehq.apps.commtrack.helpers import make_supply_point from corehq.apps.domain.models import Domain if not Domain.get_by_name(loc.domain).commtrack_enabled: return # exclude non-leaf locs if loc.location_type == 'outlet': # TODO 'outlet' is PSI-specific make_supply_point(loc.domain, loc)
def test_setting_new_list_causes_submit(self): """ this test mostly exists to make sure the one testing no submits doesn't silently stop actually working """ user = self.reporters['fixed'] loc1 = make_loc('secondloc') make_supply_point(self.domain, loc1) with patch('corehq.apps.commtrack.models.CommTrackUser.submit_location_block') as submit_blocks: user.set_locations([loc1]) self.assertEqual(submit_blocks.call_count, 1)
def test_setting_new_list_causes_submit(self): """ this test mostly exists to make sure the one testing no submits doesn't silently stop actually working """ user = self.user loc1 = make_loc('secondloc') make_supply_point(self.domain.name, loc1) with patch('corehq.apps.users.models.CommCareUser.submit_location_block') as submit_blocks: user.create_location_delegates([loc1]) self.assertEqual(submit_blocks.call_count, 1)
def post_loc_created(sender, loc=None, **kwargs): # circular imports from corehq.apps.commtrack.helpers import make_supply_point from corehq.apps.domain.models import Domain domain = Domain.get_by_name(loc.domain) if not domain.commtrack_enabled: return config = domain.commtrack_settings # exclude administrative-only locs if loc.location_type in [loc_type.name for loc_type in config.location_types if not loc_type.administrative]: make_supply_point(loc.domain, loc)
def test_setting_new_list_causes_submit(self): """ this test mostly exists to make sure the one testing no submits doesn't silently stop actually working """ user = self.user loc1 = make_loc('secondloc') make_supply_point(self.domain.name, loc1) with patch('corehq.apps.commtrack.models.CommTrackUser.submit_location_block') as submit_blocks: user.set_locations([loc1]) self.assertEqual(submit_blocks.call_count, 1)
def sync_facility_to_supply_point(domain, facility): supply_point = get_supply_point(domain, facility) facility_dict = { 'domain': domain, 'location_type': facility.type, 'external_id': facility.code, 'name': facility.name, 'site_code': facility.code, # todo: do they have a human readable code? 'latitude': facility.latitude, 'longitude': facility.longitude, } parent_sp = None if facility.parent_id: parent_sp = get_supply_point(domain, facility.parent_id) if not parent_sp: raise BadParentException('No matching supply point with code %s found' % facility.parent_id) if supply_point is None: if parent_sp: facility_dict['parent'] = parent_sp.location facility_loc = Location(**facility_dict) facility_loc.save() return make_supply_point(domain, facility_loc) else: facility_loc = supply_point.location if parent_sp and facility_loc.parent_id != parent_sp.location._id: raise BadParentException('You are trying to move a location. This is currently not supported.') should_save = apply_updates(facility_loc, facility_dict) if should_save: facility_loc.save() return supply_point
def setUp(self): # might as well clean house before doing anything delete_all_xforms() delete_all_cases() StockReport.objects.all().delete() StockTransaction.objects.all().delete() self.backend = test.bootstrap(TEST_BACKEND, to_console=True) self.domain = bootstrap_domain() self.ct_settings = CommtrackConfig.for_domain(self.domain.name) if self.requisitions_enabled: self.ct_settings.requisition_config = get_default_requisition_config() self.ct_settings.save() self.loc = make_loc('loc1') self.sp = make_supply_point(self.domain.name, self.loc) self.users = [bootstrap_user(self, **user_def) for user_def in self.user_definitions] if False: # bootstrap additional users for requisitions # needs to get reinserted for requisition stuff later self.approver = bootstrap_user(self, **APPROVER_USER) self.packer = bootstrap_user(self, **PACKER_USER) self.users += [self.approver, self.packer] # everyone should be in a group. self.group = Group(domain=TEST_DOMAIN, name='commtrack-folks', users=[u._id for u in self.users], case_sharing=True) self.group.save() self.sp.owner_id = self.group._id self.sp.save() self.products = sorted(Product.by_domain(self.domain.name), key=lambda p: p._id) self.assertEqual(3, len(self.products))
def setUp(self): # might as well clean house before doing anything delete_all_xforms() delete_all_cases() self.backend = test.bootstrap(TEST_BACKEND, to_console=True) self.domain = bootstrap_domain(requisitions_enabled=self.requisitions_enabled) self.loc = make_loc('loc1') self.reporters = dict((k, bootstrap_user(self, **v)) for k, v in REPORTING_USERS.iteritems()) # backwards compatibility self.user = self.reporters['roaming'] # bootstrap additional users for requisitions self.approver = bootstrap_user(self, **APPROVER_USER) self.packer = bootstrap_user(self, **PACKER_USER) self.users = self.reporters.values() + [self.approver, self.packer] # everyone should be in a group. self.group = Group(domain=TEST_DOMAIN, name='commtrack-folks', users=[u._id for u in self.users], case_sharing=True) self.group.save() self.sp = make_supply_point(self.domain.name, self.loc, owner_id=self.group._id) self.products = Product.by_domain(self.domain.name) self.assertEqual(3, len(self.products)) self.spps = {} for p in self.products: self.spps[p.code] = make_supply_point_product(self.sp, p._id) self.assertEqual(self.spps[p.code].owner_id, self.group._id)
def post_loc_created(sender, loc=None, **kwargs): # circular imports from corehq.apps.commtrack.helpers import make_supply_point from corehq.apps.domain.models import Domain domain = Domain.get_by_name(loc.domain) if not domain.commtrack_enabled: return config = domain.commtrack_settings # exclude administrative-only locs if loc.location_type in [ loc_type.name for loc_type in config.location_types if not loc_type.administrative ]: make_supply_point(loc.domain, loc)
def setUp(self): self.domain = create_domain('locations-test') self.domain.locations_enabled = True self.domain.location_types = [ LocationType( name='state', allowed_parents=[''], administrative=True ), LocationType( name='village', allowed_parents=['state'], administrative=True ), LocationType( name='outlet', allowed_parents=['village'] ), ] self.domain.save() self.loc = make_loc('loc', type='outlet', domain=self.domain.name) self.sp = make_supply_point(self.domain.name, self.loc) self.user = CommCareUser.create( self.domain.name, 'username', 'password', first_name='Bob', last_name='Builder', ) self.user.set_location(self.loc)
def testCreateVirtualFacility(self): loc = Location(site_code='1234', name='beavis', domain=self.domain, location_type='chw') loc.save() sp = make_supply_point(self.domain, loc) self.assertTrue(sync_supply_point_to_openlmis(sp, self.api)) self.assertTrue(sync_supply_point_to_openlmis(sp, self.api, False))
def setUp(self): # might as well clean house before doing anything delete_all_xforms() delete_all_cases() self.backend = test.bootstrap(TEST_BACKEND, to_console=True) self.domain = bootstrap_domain(requisitions_enabled=self.requisitions_enabled) self.user = bootstrap_user(**MAIN_USER) self.verified_number = self.user.get_verified_number() # bootstrap additional users for requisitions self.approver = bootstrap_user(**APPROVER_USER) self.packer = bootstrap_user(**PACKER_USER) self.users = [self.user, self.approver, self.packer] # everyone should be in a group. self.group = Group(domain=TEST_DOMAIN, name='commtrack-folks', users=[u._id for u in self.users], case_sharing=True) self.group.save() self.loc = make_loc('loc1') self.sp = make_supply_point(self.domain.name, self.loc, owner_id=self.group._id) self.products = Product.by_domain(self.domain.name) self.assertEqual(3, len(self.products)) self.spps = {} for p in self.products: self.spps[p.code] = make_supply_point_product(self.sp, p._id) self.assertEqual(self.spps[p.code].owner_id, self.group._id)
def test_can_set_locations(self): user = self.user loc1 = make_loc('secondloc') sp1 = make_supply_point(self.domain.name, loc1) loc2 = make_loc('thirdloc') sp2 = make_supply_point(self.domain.name, loc2) user.set_locations([loc1, loc2]) # should only have the two new cases self.assertEqual(len(user.locations), 2) # and will have access to these two self.check_supply_point(user, sp1._id) self.check_supply_point(user, sp2._id)
def test_setting_existing_list_does_not_submit(self): user = self.user user.clear_locations() loc1 = make_loc('secondloc') make_supply_point(self.domain.name, loc1) loc2 = make_loc('thirdloc') make_supply_point(self.domain.name, loc2) user.add_location(loc1) user.add_location(loc2) with patch('corehq.apps.commtrack.models.CommTrackUser.submit_location_block') as submit_blocks: user.set_locations([loc1, loc2]) self.assertEqual(submit_blocks.call_count, 0)
def test_setting_existing_list_does_not_submit(self): user = self.user user.clear_location_delegates() loc1 = make_loc('secondloc') make_supply_point(self.domain.name, loc1) loc2 = make_loc('thirdloc') make_supply_point(self.domain.name, loc2) user.add_location_delegate(loc1) user.add_location_delegate(loc2) with patch('corehq.apps.users.models.CommCareUser.submit_location_block') as submit_blocks: user.create_location_delegates([loc1, loc2]) self.assertEqual(submit_blocks.call_count, 0)
def test_can_set_locations(self): user = self.user loc1 = make_loc('secondloc') sp1 = make_supply_point(self.domain.name, loc1) loc2 = make_loc('thirdloc') sp2 = make_supply_point(self.domain.name, loc2) user.create_location_delegates([loc1, loc2]) # should only have the two new cases self.assertEqual(len(user.locations), 2) # and will have access to these two self.check_supply_point(user, sp1._id) self.check_supply_point(user, sp2._id)
def test_setting_existing_list_does_not_submit(self): user = self.reporters['fixed'] user.clear_locations() loc1 = make_loc('secondloc') make_supply_point(self.domain, loc1) loc2 = make_loc('thirdloc') make_supply_point(self.domain, loc2) user.add_location(loc1) user.add_location(loc2) with patch('corehq.apps.commtrack.models.CommTrackUser.submit_location_block') as submit_blocks: user.set_locations([loc1, loc2]) self.assertEqual(submit_blocks.call_count, 0)
def test_commtrack_user_has_multiple_locations(self): user = self.user loc = make_loc('secondloc') sp = make_supply_point(self.domain.name, loc) user.add_location(loc) self.check_supply_point(user, sp._id) self.assertTrue(len(user.locations), 2) self.assertEqual(user.locations[1].name, 'secondloc')
def setUp(self): self.domain = create_domain("locations-test") self.domain.convert_to_commtrack() bootstrap_location_types(self.domain.name) self.loc = make_loc("loc", type="outlet", domain=self.domain.name) self.sp = make_supply_point(self.domain.name, self.loc) self.user = CommCareUser.create(self.domain.name, "username", "password", first_name="Bob", last_name="Builder") self.user.set_location(self.loc)
def test_commtrack_user_has_multiple_locations(self): user = self.user loc = make_loc('secondloc') sp = make_supply_point(self.domain.name, loc) user.add_location_delegate(loc) self.check_supply_point(user, sp._id) self.assertTrue(len(user.locations), 2) self.assertEqual(user.locations[1].name, 'secondloc')
def test_location_migration(self): user = CommCareUser.create( self.domain.name, 'commcareuser', 'password', phone_numbers=['123123'], user_data={}, first_name='test', last_name='user' ) loc = make_loc('someloc') make_supply_point(self.domain.name, loc) user.commtrack_location = loc._id ct_user = CommTrackUser.wrap(user.to_json()) self.assertEqual(1, len(ct_user.locations)) self.assertEqual('someloc', ct_user.locations[0].name) self.assertFalse(hasattr(ct_user, 'commtrack_location'))
def test_archive_flips_sp_cases(self): loc = make_loc('someloc') sp = make_supply_point(self.domain.name, loc) self.assertFalse(sp.closed) loc.archive() sp = SupplyPointCase.get(sp._id) self.assertTrue(sp.closed) loc.unarchive() sp = SupplyPointCase.get(sp._id) self.assertFalse(sp.closed)
def setUp(self): self.backend = test.bootstrap(TEST_BACKEND, to_console=True) self.domain = bootstrap_domain() self.user = bootstrap_user() self.verified_number = self.user.get_verified_number() self.loc = make_loc('loc1') self.sp = make_supply_point(self.domain.name, self.loc) self.products = Product.by_domain(self.domain.name) self.assertEqual(3, len(self.products)) self.spps = {} for p in self.products: self.spps[p.code] = make_supply_point_product(self.sp, p._id)
def testMakeSupplyPoint(self): sp = make_supply_point(TEST_DOMAIN, self.loc) self.assertEqual("CommCareCase", sp.doc_type) self.assertEqual(TEST_DOMAIN, sp.domain) self.assertEqual(const.SUPPLY_POINT_CASE_TYPE, sp.type) self.assertEqual([self.loc._id], sp.location_) self.assertEqual(get_commtrack_user_id(TEST_DOMAIN), sp.user_id) self.assertEqual(None, sp.owner_id) self.assertFalse(sp.closed) self.assertTrue(len(sp.actions) > 0) for dateprop in ('opened_on', 'modified_on', 'server_modified_on'): self.assertTrue(getattr(sp, dateprop) is not None) self.assertTrue(isinstance(getattr(sp, dateprop), datetime))
def get_or_create_by_location(cls, location): sp = location.linked_supply_point() if not sp: sp = make_supply_point(location.domain, location) if not settings.UNIT_TESTING: _supply_point_dynamically_created(False, 'supply_point_dynamically_created, {}, {}, {}'.format( location.name, sp.case_id, location.domain, )) return sp
def get_or_create_by_location(cls, location): sp = SupplyPointSQL.get_by_location(location) if not sp: sp = make_supply_point(location.domain, location) # todo: if you come across this after july 2015 go search couchlog # and see how frequently this is happening. # if it's not happening at all we should remove it. logging.warning( "supply_point_dynamically_created, {}, {}, {}".format(location.name, sp.case_id, location.domain) ) return sp
def setUpClass(cls): cls.domain = create_domain(TEST_DOMAIN) cls.couch_user = WebUser.create(None, "report_test", "foobar") cls.couch_user.add_domain_membership(TEST_DOMAIN, is_admin=True) cls.couch_user.save() cls.products = { 'pA': make_product(TEST_DOMAIN, 'prod A', 'pA'), 'pB': make_product(TEST_DOMAIN, 'prod B', 'pB') } test_setup = { 'A': { 'A-a': { 'A-a-1': {'pA': 4, 'pB': 0}, 'A-a-2': {'pB': 3}, }, 'A-b': { 'A-b-1': {'pA': 2} } }, 'B': { 'B-a': { 'B-a-1': {'pA': 1, 'pB': 1} } } } cls.sites = {} cls.regions = {} cls.districts = {} for region_name, districts in test_setup.items(): region = make_loc(region_name, type='region') cls.regions[region_name] = region for district_name, sites in districts.items(): district = make_loc(district_name, type='district', parent=region) cls.districts[district_name] = district for site_name, products in sites.items(): site = make_loc(site_name, type='site', parent=district, domain=TEST_DOMAIN) cls.sites[site_name] = (site, products) supply_point = make_supply_point(TEST_DOMAIN, site) for p_code, stock in products.items(): prod = cls.products[p_code] StockState.objects.create( section_id='stock', case_id=supply_point._id, product_id=prod._id, stock_on_hand=stock, last_modified_date=datetime.utcnow(), )
def bootstrap_user(setup, username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, ): user_data = user_data or {} user = CommCareUser.create( domain, username, password, phone_numbers=[TEST_NUMBER], user_data=user_data, first_name=first_name, last_name=last_name ) if home_loc == setup.loc.site_code: if not SupplyPointCase.get_by_location(setup.loc): make_supply_point(domain, setup.loc) user.set_location(setup.loc) user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) return CommCareUser.wrap(user.to_json())
def test_locations_can_be_removed(self): user = self.user # can't test with the original since the user already owns it loc = make_loc('secondloc') sp = make_supply_point(self.domain.name, loc) user.add_location(loc) self.check_supply_point(user, sp._id) user.remove_location(loc) self.check_supply_point(user, sp._id, False) self.assertEqual(len(user.locations), 1)
def test_locations_can_be_removed(self): user = self.user # can't test with the original since the user already owns it loc = make_loc('secondloc') sp = make_supply_point(self.domain.name, loc) user.add_location_delegate(loc) self.check_supply_point(user, sp._id) user.remove_location_delegate(loc) self.check_supply_point(user, sp._id, False) self.assertEqual(len(user.locations), 1)
def testMakeSupplyPoint(self): sp = make_supply_point(TEST_DOMAIN, self.loc) self.assertEqual("CommCareCase", sp.doc_type) self.assertEqual(self.loc.name, sp.name) self.assertEqual(TEST_DOMAIN, sp.domain) self.assertEqual(const.SUPPLY_POINT_CASE_TYPE, sp.type) self.assertEqual([self.loc._id], sp.location_) self.assertEqual(get_commtrack_user_id(TEST_DOMAIN), sp.user_id) self.assertEqual(sp.user_id, sp.owner_id) self.assertFalse(sp.closed) self.assertTrue(len(sp.actions) > 0) for dateprop in ('opened_on', 'modified_on', 'server_modified_on'): self.assertTrue(getattr(sp, dateprop) is not None) self.assertTrue(isinstance(getattr(sp, dateprop), datetime))
def setUp(self): # might as well clean house before doing anything delete_all_xforms() delete_all_cases() StockReport.objects.all().delete() StockTransaction.objects.all().delete() self.backend = test.bootstrap(TEST_BACKEND, to_console=True) self.domain = bootstrap_domain() self.ct_settings = CommtrackConfig.for_domain(self.domain.name) self.ct_settings.consumption_config = ConsumptionConfig( min_transactions=0, min_window=0, optimal_window=60, min_periods=0, ) if self.requisitions_enabled: self.ct_settings.requisition_config = get_default_requisition_config( ) self.ct_settings.save() self.domain = Domain.get(self.domain._id) self.loc = make_loc('loc1') self.sp = make_supply_point(self.domain.name, self.loc) self.users = [ bootstrap_user(self, **user_def) for user_def in self.user_definitions ] if False: # bootstrap additional users for requisitions # needs to get reinserted for requisition stuff later self.approver = bootstrap_user(self, **APPROVER_USER) self.packer = bootstrap_user(self, **PACKER_USER) self.users += [self.approver, self.packer] # everyone should be in a group. self.group = Group(domain=TEST_DOMAIN, name='commtrack-folks', users=[u._id for u in self.users], case_sharing=True) self.group.save() self.sp.owner_id = self.group._id self.sp.save() self.products = sorted(Product.by_domain(self.domain.name), key=lambda p: p._id) self.assertEqual(3, len(self.products))
def setUp(self): self.domain = create_domain('locations-test') self.loc = make_loc('loc') self.sp = make_supply_point(self.domain.name, self.loc) self.user = CommCareUser.create( self.domain.name, 'username', 'password', first_name='Bob', last_name='Builder' ) self.user.save() self.user.add_location(self.loc)
def setUp(self): self.domain = create_domain('locations-test') bootstrap_location_types(self.domain.name) self.loc = make_loc('loc', type='outlet', domain=self.domain.name) self.sp = make_supply_point(self.domain.name, self.loc) self.user = CommCareUser.create( self.domain.name, 'username', 'password', first_name='Bob', last_name='Builder', ) self.user.set_location(self.loc)
def get_or_create_by_location(cls, location): sp = SupplyPointSQL.get_by_location(location) if not sp: sp = make_supply_point(location.domain, location) # todo: if you come across this after july 2015 go search couchlog # and see how frequently this is happening. # if it's not happening at all we should remove it. logging.warning('supply_point_dynamically_created, {}, {}, {}'.format( location.name, sp.case_id, location.domain, )) return sp
def test_should_import_consumption(self): existing = make_loc('existingloc', type='state') sp = make_supply_point(self.loc.domain, existing) data = {'id': existing._id, 'name': 'existingloc', 'default_pp': 77} import_location(self.domain.name, 'state', data) self.assertEqual( get_default_consumption( self.domain.name, Product.get_by_code(self.domain.name, 'pp')._id, 'state', sp._id, ), 77)
def setUp(self): self.domain = create_domain('locations-test') self.domain.convert_to_commtrack() bootstrap_location_types(self.domain.name) self.loc = make_loc('loc', type='outlet', domain=self.domain.name) self.sp = make_supply_point(self.domain.name, self.loc) self.user = CommCareUser.create( self.domain.name, 'username', 'password', first_name='Bob', last_name='Builder', ) self.user.set_location(self.loc)
def setUp(self): super(UserLocMapTest, self).setUp() self.user = CommCareUser.create(self.domain.name, 'commcareuser', 'password', phone_numbers=['123123'], user_data={}, first_name='test', last_name='user') self.ct_user = CommTrackUser.wrap(self.user.to_json()) self.loc = make_loc('secondloc') self.sp = make_supply_point(self.domain.name, self.loc) self.cache = LocationCache() self.mapping = UserLocMapping(self.user.username, self.user.domain, self.cache)
def setUpClass(cls): cls.domain = create_domain(TEST_DOMAIN) cls.couch_user = WebUser.create(None, "report_test", "foobar") cls.couch_user.add_domain_membership(TEST_DOMAIN, is_admin=True) cls.couch_user.save() cls.products = { 'pA': make_product(TEST_DOMAIN, 'prod A', 'pA'), 'pB': make_product(TEST_DOMAIN, 'prod B', 'pB') } test_setup = { 'A': { 'A-a': { 'A-a-1': {'pA': 4, 'pB': 0}, 'A-a-2': {'pB': 3}, }, 'A-b': { 'A-b-1': {'pA': 2} } }, 'B': { 'B-a': { 'B-a-1': {'pA': 1, 'pB': 1} } } } cls.sites = {} cls.regions = {} cls.districts = {} for region_name, districts in test_setup.items(): region = make_loc(region_name, type='region') cls.regions[region_name] = region for district_name, sites in districts.items(): district = make_loc(district_name, type='district', parent=region) cls.districts[district_name] = district for site_name, products in sites.items(): site = make_loc(site_name, type='site', parent=district) cls.sites[site_name] = (site, products) supply_point = make_supply_point(TEST_DOMAIN, site) for p_code, stock in products.items(): prod = cls.products[p_code] spp = make_supply_point_product(supply_point, prod._id) update_supply_point_product_stock_level(spp, stock)
def setUp(self): super(UserLocMapTest, self).setUp() self.user = CommCareUser.create(self.domain.name, 'commcareuser', 'password', phone_numbers=['123123'], user_data={}, first_name='test', last_name='user') MULTIPLE_LOCATIONS_PER_USER.set(self.user.domain, True, NAMESPACE_DOMAIN) self.loc = make_loc('secondloc') self.sp = make_supply_point(self.domain.name, self.loc) self.cache = SiteCodeToSupplyPointCache(self.domain.name) self.mapping = UserLocMapping(self.user.username, self.user.domain, self.cache)
def test_should_import_consumption(self): existing = make_loc('existingloc', type='state') sp = make_supply_point(self.loc.domain, existing) data = { 'site_code': existing.site_code, 'name': 'existingloc', 'consumption': { 'pp': 77 }, } import_location(self.domain.name, 'state', data) self.assertEqual( float( get_default_consumption( self.domain.name, Product.get_by_code(self.domain.name, 'pp')._id, 'state', sp._id, )), 77 / DAYS_IN_MONTH)
def testMakeOwnedSupplyPoint(self): sp = make_supply_point(TEST_DOMAIN, self.loc, 'some-other-owner') self.assertEqual(get_commtrack_user_id(TEST_DOMAIN), sp.user_id) self.assertEqual('some-other-owner', sp.owner_id)
def setUp(self): self.loc = make_loc('loc1') self.sp = make_supply_point(TEST_DOMAIN, self.loc) self.product = make_product(TEST_DOMAIN, 'product 1', 'p1')
def create_from_location(cls, domain, location): from corehq.apps.commtrack.helpers import make_supply_point return make_supply_point(domain, location)
def get_or_create_by_location(cls, location): sp = location.linked_supply_point() if not sp: sp = make_supply_point(location.domain, location) return sp
def get_or_create_by_location(cls, location): sp = SupplyPointSQL.get_by_location(location) if not sp: sp = make_supply_point(location.domain, location) return sp