def setUpClass(cls): super(LocationGroupTest, cls).setUpClass() cls.domain = create_domain('locations-test') cls.domain.convert_to_commtrack() bootstrap_location_types(cls.domain.name) cls.loc = make_loc('loc', type='outlet', domain=cls.domain.name) cls.user = CommCareUser.create( cls.domain.name, 'username', 'password', first_name='Bob', last_name='Builder', ) cls.user.set_location(cls.loc) cls.test_state = make_loc('teststate', type='state', domain=cls.domain.name) cls.test_village = make_loc('testvillage', type='village', parent=cls.test_state, domain=cls.domain.name) cls.test_outlet = make_loc('testoutlet', type='outlet', parent=cls.test_village, domain=cls.domain.name)
def setUpClass(cls): super(TestUsersByLocation, cls).setUpClass() initialize_index_and_mapping(get_es_new(), USER_INDEX_INFO) cls.domain = 'test-domain' cls.domain_obj = create_domain(cls.domain) bootstrap_location_types(cls.domain) def make_user(name, location): user = CommCareUser.create(cls.domain, name, 'password', None, None) user.set_location(location) return user cls.meereen = make_loc('meereen', type='outlet', domain=cls.domain) cls.pentos = make_loc('pentos', type='outlet', domain=cls.domain) cls.varys = make_user('Varys', cls.pentos) cls.tyrion = make_user('Tyrion', cls.meereen) cls.daenerys = make_user('Daenerys', cls.meereen) cls.george = WebUser.create( cls.domain, username="******", password='******', created_by=None, created_via=None, ) cls.george.set_location(cls.domain, cls.meereen) get_es_new().indices.refresh(USER_INDEX)
def setUp(self): super(XMLTest, self).setUp() self.domain = util.bootstrap_domain(util.TEST_DOMAIN) util.bootstrap_location_types(self.domain.name) util.bootstrap_products(self.domain.name) self.products = sorted(Product.by_domain(self.domain.name), key=lambda p: p._id) self.ct_settings = SQLCommtrackConfig.for_domain(self.domain.name) self.ct_settings.sqlconsumptionconfig = SQLConsumptionConfig( min_transactions=0, min_window=0, optimal_window=60, ) self.ct_settings.save() self.ct_settings.sqlconsumptionconfig.commtrack_settings = self.ct_settings self.ct_settings.sqlconsumptionconfig.save() self.domain = Domain.get(self.domain._id) self.loc = make_loc('loc1') self.sp = self.loc.linked_supply_point() self.users = [ util.bootstrap_user(self, **user_def) for user_def in self.user_definitions ] self.user = self.users[0]
def setUpClass(cls): cls.domain = 'test-domain' cls.domain_obj = create_domain(cls.domain) bootstrap_location_types(cls.domain) delete_all_locations() delete_all_users() def make_user(name, location): user = CommCareUser.create(cls.domain, name, 'password') user.set_location(location) return user cls.meereen = make_loc('meereen', type='outlet', domain=cls.domain) cls.pentos = make_loc('pentos', type='outlet', domain=cls.domain) cls.varys = make_user('Varys', cls.pentos) cls.tyrion = make_user('Tyrion', cls.meereen) cls.daenerys = make_user('Daenerys', cls.meereen) cls.george = WebUser.create( cls.domain, username="******", password='******' ) cls.george.set_location(cls.domain, cls.meereen)
def setUpClass(cls): super(TestUsersByLocation, cls).setUpClass() cls.domain = 'test-domain' cls.domain_obj = create_domain(cls.domain) bootstrap_location_types(cls.domain) def make_user(name, location): user = CommCareUser.create(cls.domain, name, 'password', None, None) user.set_location(location) return user cls.meereen = make_loc('meereen', type='outlet', domain=cls.domain) cls.pentos = make_loc('pentos', type='outlet', domain=cls.domain) cls.varys = make_user('Varys', cls.pentos) cls.tyrion = make_user('Tyrion', cls.meereen) cls.daenerys = make_user('Daenerys', cls.meereen) cls.george = WebUser.create( cls.domain, username="******", password='******', created_by=None, created_via=None, ) cls.george.set_location(cls.domain, cls.meereen)
def setUpClass(cls): super(LocationGroupTest, cls).setUpClass() cls.domain = create_domain('locations-test') cls.domain.convert_to_commtrack() bootstrap_location_types(cls.domain.name) cls.loc = make_loc('loc', type='outlet', domain=cls.domain.name) cls.user = CommCareUser.create( cls.domain.name, 'username', 'password', first_name='Bob', last_name='Builder', ) cls.user.set_location(cls.loc) cls.test_state = make_loc( 'teststate', type='state', domain=cls.domain.name ) cls.test_village = make_loc( 'testvillage', type='village', parent=cls.test_state, domain=cls.domain.name ) cls.test_outlet = make_loc( 'testoutlet', type='outlet', parent=cls.test_village, domain=cls.domain.name )
def setUpClass(cls): delete_all_locations() cls.domain = create_domain('locations-test') cls.domain.locations_enabled = True bootstrap_location_types(cls.domain.name) cls.state = make_loc("Florida", type='state') cls.district = make_loc("Duval", type='district', parent=cls.state) cls.block = make_loc("Jacksonville", type='block', parent=cls.district)
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 setUpClass(cls): super(SMSTests, cls).setUpClass() cls.backend, cls.backend_mapping = setup_default_sms_test_backend() cls.domain = util.bootstrap_domain(util.TEST_DOMAIN) util.bootstrap_location_types(cls.domain.name) util.bootstrap_products(cls.domain.name) cls.products = sorted(Product.by_domain(cls.domain.name), key=lambda p: p._id) cls.loc = util.make_loc('loc1') cls.sp = cls.loc.linked_supply_point() cls.users = [util.bootstrap_user(cls, **user_def) for user_def in cls.user_definitions]
def setUpClass(cls): super().setUpClass() cls.ccdomain = Domain(name='cc_user_domain') cls.ccdomain.save() bootstrap_location_types(cls.ccdomain.name) cls.loc1 = make_loc('spain', domain=cls.ccdomain.name, type='state') cls.loc2 = make_loc('madagascar', domain=cls.ccdomain.name, parent=cls.loc1, type='district')
def test_location_queries(self): test_state1 = make_loc("teststate1", type="state", parent=self.user.location, domain=self.domain.name) test_state2 = make_loc("teststate2", type="state", parent=self.user.location, domain=self.domain.name) test_village1 = make_loc("testvillage1", type="village", parent=test_state1, domain=self.domain.name) test_village1.site_code = "tv1" test_village1.save() test_village2 = make_loc("testvillage2", type="village", parent=test_state2, domain=self.domain.name) def compare(list1, list2): self.assertEqual(set([l._id for l in list1]), set([l._id for l in list2])) # descendants compare([test_state1, test_state2, test_village1, test_village2], self.user.location.descendants) # children compare([test_state1, test_state2], self.user.location.children) # siblings compare([test_state2], test_state1.siblings()) # parent and parent_id self.assertEqual(self.user.location._id, test_state1.parent_id) self.assertEqual(self.user.location._id, test_state1.parent._id) # is_root self.assertTrue(self.user.location.is_root) self.assertFalse(test_state1.is_root) # Location.root_locations compare([self.user.location], Location.root_locations(self.domain.name)) # Location.filter_by_type compare([test_village1, test_village2], Location.filter_by_type(self.domain.name, "village")) compare([test_village1], Location.filter_by_type(self.domain.name, "village", test_state1)) # Location.get_in_domain test_village2.domain = "rejected" bootstrap_location_types("rejected") test_village2.save() self.assertEqual(Location.get_in_domain(self.domain.name, test_village1._id)._id, test_village1._id) self.assertIsNone(Location.get_in_domain(self.domain.name, test_village2._id)) self.assertIsNone(Location.get_in_domain(self.domain.name, "not-a-real-id")) self.assertEqual( {loc._id for loc in [self.user.location, test_state1, test_state2, test_village1]}, set(SQLLocation.objects.filter(domain=self.domain.name).location_ids()), ) # Location.by_site_code self.assertEqual(test_village1._id, Location.by_site_code(self.domain.name, "tv1")._id) self.assertIsNone(None, Location.by_site_code(self.domain.name, "notreal")) # Location.by_domain compare([self.user.location, test_state1, test_state2, test_village1], Location.by_domain(self.domain.name))
def test_location_queries(self): test_state1 = make_loc('teststate1', type='state', parent=self.loc, domain=self.domain.name) test_state2 = make_loc('teststate2', type='state', parent=self.loc, domain=self.domain.name) test_village1 = make_loc('testvillage1', type='village', parent=test_state1, domain=self.domain.name) test_village1.site_code = 'tv1' test_village1.save() test_village2 = make_loc('testvillage2', type='village', parent=test_state2, domain=self.domain.name) def compare(list1, list2): self.assertEqual(set(l.location_id for l in list1), set(l.location_id for l in list2)) # descendants compare([test_state1, test_state2, test_village1, test_village2], self.loc.get_descendants()) # children compare([test_state1, test_state2], self.loc.get_children()) # parent and parent_location_id self.assertEqual(self.loc.location_id, test_state1.parent_location_id) self.assertEqual(self.loc.location_id, test_state1.parent.location_id) compare([self.loc], SQLLocation.objects.filter(domain=self.domain.name, parent=None)) create_domain('rejected') bootstrap_location_types('rejected') test_village2.domain = 'rejected' test_village2.save() self.assertEqual( { loc.location_id for loc in [self.loc, test_state1, test_state2, test_village1] }, set( SQLLocation.objects.filter( domain=self.domain.name).location_ids()), )
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 setUpClass(cls): cls.domain = create_domain('test-domain') bootstrap_location_types(cls.domain.name) def make_user(name, location): user = CommCareUser.create(cls.domain.name, name, 'password') user.set_location(location) return user cls.meereen = make_loc('meereen', type='outlet', domain=cls.domain.name) cls.pentos = make_loc('pentos', type='outlet', domain=cls.domain.name) cls.varys = make_user('Varys', cls.pentos) cls.tyrion = make_user('Tyrion', cls.meereen) cls.daenerys = make_user('Daenerys', cls.meereen)
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(TestPathLineageAndHierarchy, self).setUp() self.domain = create_domain('locations-test') bootstrap_location_types(self.domain.name) locs = [ ('Mass', 'state'), ('Suffolk', 'district'), ('Boston', 'block'), ] parent = None self.all_locs = [] for name, type_ in locs: parent = make_loc(name, type=type_, parent=parent) self.all_locs.append(parent) self.all_loc_ids = [l.location_id for l in self.all_locs] self.loc_id_by_name = {l.name: l.location_id for l in self.all_locs}
def setUpClass(cls): super(StockStateTest, cls).setUpClass() cls.domain_obj = util.bootstrap_domain(cls.domain) util.bootstrap_location_types(cls.domain) util.bootstrap_products(cls.domain) cls.ct_settings = CommtrackConfig.for_domain(cls.domain) cls.ct_settings.use_auto_consumption = True cls.ct_settings.consumption_config = ConsumptionConfig( min_transactions=0, min_window=0, optimal_window=60, min_periods=0, ) cls.ct_settings.save() cls.loc = util.make_loc('loc1', domain=cls.domain) cls.sp = cls.loc.linked_supply_point() cls.products = sorted(Product.by_domain(cls.domain), key=lambda p: p._id)
def setUp(self): super(XMLTest, self).setUp() self.domain = util.bootstrap_domain(util.TEST_DOMAIN) util.bootstrap_location_types(self.domain.name) util.bootstrap_products(self.domain.name) self.products = sorted(Product.by_domain(self.domain.name), key=lambda p: p._id) 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, ) self.ct_settings.save() self.domain = Domain.get(self.domain._id) self.loc = make_loc('loc1') self.sp = self.loc.linked_supply_point() self.users = [util.bootstrap_user(self, **user_def) for user_def in self.user_definitions] self.user = self.users[0]
def setUpClass(cls): cls.domain_obj = create_domain(cls.domain) report = ReportConfiguration(domain=cls.domain) bootstrap_location_types(cls.domain) location_code_name_pairs = ( ('cambridge_ma', 'Cambridge'), ('somerville_ma', 'Somerville'), ('boston_ma', 'Boston'), ) cls.locations = [] choices = [] for location_code, location_name in location_code_name_pairs: location = cls.make_location(location_code, location_name) cls.locations.append(location) choices.append(SearchableChoice(location.location_id, location.sql_location.display_name, searchable_text=[location_code, location_name])) choices.sort(key=lambda choice: choice.display) cls.choice_provider = LocationChoiceProvider(report, None) cls.static_choice_provider = StaticChoiceProvider(choices)
def setUpClass(cls): super(StockStateTest, cls).setUpClass() cls.domain_obj = util.bootstrap_domain(cls.domain) util.bootstrap_location_types(cls.domain) util.bootstrap_products(cls.domain) cls.ct_settings = SQLCommtrackConfig.for_domain(cls.domain) cls.ct_settings.use_auto_consumption = True cls.ct_settings.sqlconsumptionconfig = SQLConsumptionConfig( min_transactions=0, min_window=0, optimal_window=60, ) cls.ct_settings.save() cls.ct_settings.sqlconsumptionconfig.commtrack_settings = cls.ct_settings cls.ct_settings.sqlconsumptionconfig.save() cls.loc = util.make_loc('loc1', domain=cls.domain) cls.sp = cls.loc.linked_supply_point() cls.products = sorted(Product.by_domain(cls.domain), key=lambda p: p._id) cls.process_ledger_changes = process_pillow_changes( 'LedgerToElasticsearchPillow')
def setUpClass(cls): super(LocationsTest, cls).setUpClass() cls.domain = create_domain('locations-test') bootstrap_location_types(cls.domain.name) cls.loc = make_loc('loc', type='outlet', domain=cls.domain.name)
def setUpClass(cls): super(AllCommCareUsersTest, cls).setUpClass() delete_all_users() hard_delete_deleted_users() cls.ccdomain = Domain(name='cc_user_domain') cls.ccdomain.save() cls.other_domain = Domain(name='other_domain') cls.other_domain.save() bootstrap_location_types(cls.ccdomain.name) initialize_domain_with_default_roles(cls.ccdomain.name) cls.user_roles = UserRole.objects.get_by_domain(cls.ccdomain.name) cls.custom_role = UserRole.create(cls.ccdomain.name, "Custom Role") cls.loc1 = make_loc('spain', domain=cls.ccdomain.name, type="district") cls.loc2 = make_loc('madagascar', domain=cls.ccdomain.name, type="district") cls.ccuser_1 = CommCareUser.create( domain=cls.ccdomain.name, username='******', password='******', created_by=None, created_via=None, email='*****@*****.**', ) cls.ccuser_1.set_location(cls.loc1) cls.ccuser_1.save() cls.ccuser_2 = CommCareUser.create( domain=cls.ccdomain.name, username='******', password='******', created_by=None, created_via=None, email='*****@*****.**', ) cls.ccuser_2.set_role(cls.ccdomain.name, cls.custom_role.get_qualified_id()) cls.ccuser_2.set_location(cls.loc2) cls.ccuser_2.save() cls.web_user = WebUser.create( domain=cls.ccdomain.name, username='******', password='******', created_by=None, created_via=None, email='*****@*****.**', ) cls.location_restricted_web_user = WebUser.create( domain=cls.ccdomain.name, username='******', password='******', created_by=None, created_via=None, email='*****@*****.**', ) cls.location_restricted_web_user.add_to_assigned_locations( domain=cls.ccdomain.name, location=cls.loc2) cls.ccuser_other_domain = CommCareUser.create( domain=cls.other_domain.name, username='******', password='******', created_by=None, created_via=None, email='*****@*****.**', ) cls.retired_user = CommCareUser.create( domain=cls.ccdomain.name, username='******', password='******', created_by=None, created_via=None, email='*****@*****.**', ) cls.retired_user.retire(cls.ccdomain.name, deleted_by=None) cls.ccuser_inactive = CommCareUser.create( domain=cls.ccdomain.name, username='******', password='******', created_by=None, created_via=None, email='*****@*****.**', ) cls.ccuser_inactive.is_active = False cls.ccuser_inactive.save() cls.ccuser_inactive.set_location(cls.loc2)
def setUpClass(cls): super(LocationsTest, cls).setUpClass() delete_all_users() cls.domain_obj = bootstrap_domain(cls.domain) bootstrap_location_types(cls.domain)
def test_location_queries(self): test_state1 = make_loc( 'teststate1', type='state', parent=self.loc, domain=self.domain.name ) test_state2 = make_loc( 'teststate2', type='state', parent=self.loc, domain=self.domain.name ) test_village1 = make_loc( 'testvillage1', type='village', parent=test_state1, domain=self.domain.name ) test_village1.site_code = 'tv1' test_village1.save() test_village2 = make_loc( 'testvillage2', type='village', parent=test_state2, domain=self.domain.name ) def compare(list1, list2): self.assertEqual( set(l.location_id for l in list1), set(l.location_id for l in list2) ) # descendants compare( [test_state1, test_state2, test_village1, test_village2], self.loc.get_descendants() ) # children compare( [test_state1, test_state2], self.loc.get_children() ) # parent and parent_location_id self.assertEqual( self.loc.location_id, test_state1.parent_location_id ) self.assertEqual( self.loc.location_id, test_state1.parent.location_id ) compare( [self.loc], SQLLocation.objects.filter(domain=self.domain.name, parent=None) ) create_domain('rejected') bootstrap_location_types('rejected') test_village2.domain = 'rejected' test_village2.save() self.assertEqual( {loc.location_id for loc in [self.loc, test_state1, test_state2, test_village1]}, set(SQLLocation.objects.filter(domain=self.domain.name).location_ids()), )
def test_location_queries(self): test_state1 = make_loc('teststate1', type='state', parent=self.loc, domain=self.domain.name) test_state2 = make_loc('teststate2', type='state', parent=self.loc, domain=self.domain.name) test_village1 = make_loc('testvillage1', type='village', parent=test_state1, domain=self.domain.name) test_village1.site_code = 'tv1' test_village1.save() test_village2 = make_loc('testvillage2', type='village', parent=test_state2, domain=self.domain.name) def compare(list1, list2): self.assertEqual(set([l._id for l in list1]), set([l._id for l in list2])) # descendants compare([test_state1, test_state2, test_village1, test_village2], self.loc.get_descendants()) # children compare([test_state1, test_state2], self.loc.get_children()) # parent and parent_location_id self.assertEqual(self.loc.location_id, test_state1.parent_location_id) self.assertEqual(self.loc.location_id, test_state1.parent._id) # Location.root_locations compare([self.loc], Location.root_locations(self.domain.name)) # Location.filter_by_type compare([test_village1, test_village2], Location.filter_by_type(self.domain.name, 'village')) compare([test_village1], Location.filter_by_type(self.domain.name, 'village', test_state1)) create_domain('rejected') bootstrap_location_types('rejected') test_village2.domain = 'rejected' test_village2.save() self.assertEqual( { loc.location_id for loc in [self.loc, test_state1, test_state2, test_village1] }, set( SQLLocation.objects.filter( domain=self.domain.name).location_ids()), ) # Location.by_site_code self.assertEqual(test_village1._id, Location.by_site_code(self.domain.name, 'tv1')._id) self.assertIsNone(None, Location.by_site_code(self.domain.name, 'notreal')) # Location.by_domain compare([self.loc, test_state1, test_state2, test_village1], Location.by_domain(self.domain.name))
def setUpClass(cls): super(AllCommCareUsersTest, cls).setUpClass() delete_all_users() hard_delete_deleted_users() cls.ccdomain = Domain(name='cc_user_domain') cls.ccdomain.save() cls.other_domain = Domain(name='other_domain') cls.other_domain.save() bootstrap_location_types(cls.ccdomain.name) UserRole.init_domain_with_presets(cls.ccdomain.name) cls.user_roles = UserRole.by_domain(cls.ccdomain.name) cls.custom_role = UserRole.get_or_create_with_permissions( cls.ccdomain.name, Permissions( edit_apps=True, edit_web_users=True, view_web_users=True, view_roles=True, ), "Custom Role") cls.custom_role.save() cls.loc1 = make_loc('spain', domain=cls.ccdomain.name, type="district") cls.loc2 = make_loc('madagascar', domain=cls.ccdomain.name, type="district") cls.ccuser_1 = CommCareUser.create( domain=cls.ccdomain.name, username='******', password='******', email='*****@*****.**', ) cls.ccuser_1.set_location(cls.loc1) cls.ccuser_1.save() cls.ccuser_2 = CommCareUser.create( domain=cls.ccdomain.name, username='******', password='******', email='*****@*****.**', ) cls.ccuser_2.set_role(cls.ccdomain.name, cls.custom_role.get_qualified_id()) cls.ccuser_2.set_location(cls.loc2) cls.ccuser_2.save() cls.web_user = WebUser.create( domain=cls.ccdomain.name, username='******', password='******', email='*****@*****.**', ) cls.ccuser_other_domain = CommCareUser.create( domain=cls.other_domain.name, username='******', password='******', email='*****@*****.**', ) cls.retired_user = CommCareUser.create( domain=cls.ccdomain.name, username='******', password='******', email='*****@*****.**', ) cls.retired_user.retire()
def test_location_queries(self): test_state1 = make_loc( 'teststate1', type='state', parent=self.user.location, domain=self.domain.name ) test_state2 = make_loc( 'teststate2', type='state', parent=self.user.location, domain=self.domain.name ) test_village1 = make_loc( 'testvillage1', type='village', parent=test_state1, domain=self.domain.name ) test_village1.site_code = 'tv1' test_village1.save() test_village2 = make_loc( 'testvillage2', type='village', parent=test_state2, domain=self.domain.name ) def compare(list1, list2): self.assertEqual( set([l._id for l in list1]), set([l._id for l in list2]) ) # descendants compare( [test_state1, test_state2, test_village1, test_village2], self.user.location.descendants ) # children compare( [test_state1, test_state2], self.user.location.children ) # siblings compare( [test_state2], test_state1.siblings() ) # parent and parent_id self.assertEqual( self.user.location._id, test_state1.parent_id ) self.assertEqual( self.user.location._id, test_state1.parent._id ) # is_root self.assertTrue(self.user.location.is_root) self.assertFalse(test_state1.is_root) # Location.root_locations compare( [self.user.location], Location.root_locations(self.domain.name) ) # Location.filter_by_type compare( [test_village1, test_village2], Location.filter_by_type(self.domain.name, 'village') ) compare( [test_village1], Location.filter_by_type(self.domain.name, 'village', test_state1) ) # Location.get_in_domain test_village2.domain = 'rejected' bootstrap_location_types('rejected') test_village2.save() self.assertEqual( Location.get_in_domain(self.domain.name, test_village1._id)._id, test_village1._id ) self.assertIsNone( Location.get_in_domain(self.domain.name, test_village2._id), ) self.assertIsNone( Location.get_in_domain(self.domain.name, 'not-a-real-id'), ) def _all_locations(domain): return Location.view( 'locations/hierarchy', startkey=[domain], endkey=[domain, {}], reduce=False, include_docs=True ).all() compare( [self.user.location, test_state1, test_state2, test_village1], _all_locations(self.domain.name) ) # Location.by_site_code self.assertEqual( test_village1._id, Location.by_site_code(self.domain.name, 'tv1')._id ) self.assertIsNone( None, Location.by_site_code(self.domain.name, 'notreal') ) # Location.by_domain compare( [self.user.location, test_state1, test_state2, test_village1], Location.by_domain(self.domain.name) )
def test_location_queries(self): test_state1 = make_loc( 'teststate1', type='state', parent=self.user.location, domain=self.domain.name ) test_state2 = make_loc( 'teststate2', type='state', parent=self.user.location, domain=self.domain.name ) test_village1 = make_loc( 'testvillage1', type='village', parent=test_state1, domain=self.domain.name ) test_village1.site_code = 'tv1' test_village1.save() test_village2 = make_loc( 'testvillage2', type='village', parent=test_state2, domain=self.domain.name ) def compare(list1, list2): self.assertEqual( set([l._id for l in list1]), set([l._id for l in list2]) ) # descendants compare( [test_state1, test_state2, test_village1, test_village2], self.user.location.descendants ) # children compare( [test_state1, test_state2], self.user.location.get_children() ) # parent and parent_location_id self.assertEqual( self.user.location.location_id, test_state1.parent_location_id ) self.assertEqual( self.user.location.location_id, test_state1.parent._id ) # Location.root_locations compare( [self.user.location], Location.root_locations(self.domain.name) ) # Location.filter_by_type compare( [test_village1, test_village2], Location.filter_by_type(self.domain.name, 'village') ) compare( [test_village1], Location.filter_by_type(self.domain.name, 'village', test_state1) ) create_domain('rejected') bootstrap_location_types('rejected') test_village2.domain = 'rejected' test_village2.save() self.assertEqual( {loc.location_id for loc in [self.user.location, test_state1, test_state2, test_village1]}, set(SQLLocation.objects.filter(domain=self.domain.name).location_ids()), ) # Location.by_site_code self.assertEqual( test_village1._id, Location.by_site_code(self.domain.name, 'tv1')._id ) self.assertIsNone( None, Location.by_site_code(self.domain.name, 'notreal') ) # Location.by_domain compare( [self.user.location, test_state1, test_state2, test_village1], Location.by_domain(self.domain.name) )
def setUpClass(cls): super(FixtureTest, cls).setUpClass() cls.domain_obj = util.bootstrap_domain(cls.domain) util.bootstrap_location_types(cls.domain) util.bootstrap_products(cls.domain) cls.user = create_restore_user(cls.domain)
def test_location_queries(self): test_state1 = make_loc( 'teststate1', type='state', parent=self.user.location, domain=self.domain.name ) test_state2 = make_loc( 'teststate2', type='state', parent=self.user.location, domain=self.domain.name ) test_village1 = make_loc( 'testvillage1', type='village', parent=test_state1, domain=self.domain.name ) test_village1.site_code = 'tv1' test_village1.save() test_village2 = make_loc( 'testvillage2', type='village', parent=test_state2, domain=self.domain.name ) def compare(list1, list2): self.assertEqual( set([l._id for l in list1]), set([l._id for l in list2]) ) # descendants compare( [test_state1, test_state2, test_village1, test_village2], self.user.location.descendants ) # children compare( [test_state1, test_state2], self.user.location.children ) # siblings compare( [test_state2], test_state1.siblings() ) # parent and parent_id self.assertEqual( self.user.location._id, test_state1.parent_id ) self.assertEqual( self.user.location._id, test_state1.parent._id ) # is_root self.assertTrue(self.user.location.is_root) self.assertFalse(test_state1.is_root) # Location.root_locations compare( [self.user.location], Location.root_locations(self.domain.name) ) # Location.filter_by_type compare( [test_village1, test_village2], Location.filter_by_type(self.domain.name, 'village') ) compare( [test_village1], Location.filter_by_type(self.domain.name, 'village', test_state1) ) # Location.get_in_domain test_village2.domain = 'rejected' bootstrap_location_types('rejected') test_village2.save() self.assertEqual( Location.get_in_domain(self.domain.name, test_village1._id)._id, test_village1._id ) self.assertIsNone( Location.get_in_domain(self.domain.name, test_village2._id), ) self.assertIsNone( Location.get_in_domain(self.domain.name, 'not-a-real-id'), ) self.assertEqual( {loc._id for loc in [self.user.location, test_state1, test_state2, test_village1]}, set(SQLLocation.objects.filter(domain=self.domain.name).location_ids()), ) # Location.by_site_code self.assertEqual( test_village1._id, Location.by_site_code(self.domain.name, 'tv1')._id ) self.assertIsNone( None, Location.by_site_code(self.domain.name, 'notreal') ) # Location.by_domain compare( [self.user.location, test_state1, test_state2, test_village1], Location.by_domain(self.domain.name) )