Example #1
0
 def test_edit_location_with_duplicated_code(self):
     with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
         locations = json.loads(f.read())
         location = Location(locations[8])
         district = Location(locations[7])
     self.api_object.location_sync(location)
     ews_district = self.api_object.location_sync(district)
     self.assertNotEqual(ews_district.site_code, "duplicated")
Example #2
0
    def test_edit_non_facility_location(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Location(json.loads(f.read())[0])

        ewsghana_location = self.api_object.location_sync(location)
        self.assertEqual(ewsghana_location.name, "Test country")
        self.assertEqual(ewsghana_location.site_code, "testcountry")

        location.name = "edited"
        location.code = "edited"
        ewsghana_location = self.api_object.location_sync(location)

        self.assertEqual(ewsghana_location.name, "edited")
        self.assertEqual(ewsghana_location.site_code, "edited")
    def test_edit_non_facility_location(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Location(json.loads(f.read())[0])

        ewsghana_location = self.api_object.location_sync(location)
        self.assertEqual(ewsghana_location.name, "Test country")
        self.assertEqual(ewsghana_location.site_code, "testcountry")

        location.name = "edited"
        location.code = "edited"
        ewsghana_location = self.api_object.location_sync(location)

        self.assertEqual(ewsghana_location.name, "edited")
        self.assertEqual(ewsghana_location.site_code, "edited")
Example #4
0
    def test_facility_with_inactive_and_active_supply_point(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Location(json.loads(f.read())[6])

        ewsghana_location = self.api_object.location_sync(location)
        self.assertEqual("tsactive", ewsghana_location.site_code)
        self.assertEqual("Active Test hospital", ewsghana_location.name)
        self.assertFalse(ewsghana_location.is_archived)
Example #5
0
    def test_location_with_products(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Location(json.loads(f.read())[1])

        ews_location = self.api_object.location_sync(location)
        self.assertListEqual(
            list(
                ews_location.sql_location.products.values_list(
                    'code', flat=True).order_by('code')), ['ad', 'al'])
Example #6
0
 def test_locations_with_duplicated_site_code(self):
     with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
         location = Location(json.loads(f.read())[8])
     ewsghana_location = self.api_object.location_sync(location)
     self.assertEqual(
         SQLLocation.objects.filter(site_code='duplicated',
                                    domain=TEST_DOMAIN).count(), 1)
     self.assertEqual(ewsghana_location.site_code, "duplicated")
     self.assertNotEqual(ewsghana_location.parent.site_code, "duplicated")
Example #7
0
    def test_facility_in_charge(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Location(json.loads(f.read())[1])

        ewsghana_location = self.api_object.location_sync(location)
        in_charges = FacilityInCharge.objects.filter(
            location=ewsghana_location.sql_location)
        self.assertEqual(in_charges.count(), 1)
        user = CommCareUser.get_by_user_id(in_charges[0].user_id)
        self.assertIsNotNone(user)
    def test_edit_reporting_location(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Location(json.loads(f.read())[1])

        self.api_object.location_sync(location)
        location.name = 'edited'
        location.code = 'edited'
        edited = self.api_object.location_sync(location)
        # shouldn't change because we use name and code from supply point not from location
        self.assertEqual(edited.name, 'Central Regional Medical Store')
        self.assertEqual(edited.site_code, 'crms')

        location.supply_points[0].name = 'edited'
        location.supply_points[0].code = 'edited'
        location.supply_points[0].products = ['alk', 'abc', 'ad']
        edited = self.api_object.location_sync(location)
        self.assertEqual(edited.name, 'edited')
        self.assertEqual(edited.site_code, 'edited')
        self.assertEqual(edited.sql_location.products.count(), 3)
        self.assertListEqual(list(edited.sql_location.products.values_list('code', flat=True).order_by('code')),
                             ['abc', 'ad', 'alk'])
Example #9
0
 def test_parse_location_json(self):
     with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
         location = Location(json.loads(f.read())[1])
     self.assertEqual(location.id, 620)
     self.assertEqual(location.name, "Test facility")
     self.assertEqual(location.type, "facility")
     self.assertEqual(location.parent_id, 369)
     self.assertEqual(location.latitude, "1.15")
     self.assertEqual(location.longitude, "3.14")
     self.assertEqual(location.code, "testfacility")
     self.assertEqual(location.supervised_by, 591)
     self.assertEqual(location.groups, [])
     self.assertIsNotNone(location.supply_points)
Example #10
0
    def setUpClass(cls):
        cls.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy')
        cls.api_object = EWSApi(TEST_DOMAIN, cls.endpoint)
        cls.datapath = os.path.join(os.path.dirname(__file__), 'data')
        initial_bootstrap(TEST_DOMAIN)
        cls.api_object.prepare_commtrack_config()
        cls.api_object.create_or_edit_roles()
        with open(os.path.join(cls.datapath, 'sample_products.json')) as f:
            for p in json.loads(f.read()):
                cls.api_object.product_sync(Product(p))

        with open(os.path.join(cls.datapath, 'sample_locations.json')) as f:
            for loc in json.loads(f.read()):
                cls.api_object.location_sync(Location(loc))
Example #11
0
    def test_edit_reporting_location(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Location(json.loads(f.read())[1])

        self.api_object.location_sync(location)
        location.name = 'edited'
        location.code = 'edited'
        edited = self.api_object.location_sync(location)
        # shouldn't change because we use name and code from supply point not from location
        self.assertEqual(edited.name, 'Central Regional Medical Store')
        self.assertEqual(edited.site_code, 'crms')

        location.supply_points[0].name = 'edited'
        location.supply_points[0].code = 'edited'
        location.supply_points[0].products = ['alk', 'abc', 'ad']
        edited = self.api_object.location_sync(location)
        self.assertEqual(edited.name, 'edited')
        self.assertEqual(edited.site_code, 'edited')
        self.assertEqual(edited.sql_location.products.count(), 3)
        self.assertListEqual(
            list(
                edited.sql_location.products.values_list(
                    'code', flat=True).order_by('code')), ['abc', 'ad', 'alk'])
Example #12
0
    def test_create_region_with_two_supply_points(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Location(json.loads(f.read())[4])

        ewsghana_location = self.api_object.location_sync(location)
        self.assertEqual(
            2,
            SQLLocation.objects.filter(
                domain=TEST_DOMAIN,
                location_type__administrative=False).count())
        self.assertIsNone(ewsghana_location.linked_supply_point())
        self.assertIsNone(ewsghana_location.sql_location.supply_point_id)
        self.assertEqual(location.name, ewsghana_location.sql_location.name)
        self.assertEqual(location.code,
                         ewsghana_location.sql_location.site_code)
        self.assertFalse(ewsghana_location.is_archived)
Example #13
0
    def test_create_non_facility_location(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Location(json.loads(f.read())[0])

        ewsghana_location = self.api_object.location_sync(location)
        self.assertEqual(ewsghana_location.name, location.name)
        self.assertEqual(ewsghana_location.location_type, location.type)
        self.assertEqual(ewsghana_location.longitude,
                         float(location.longitude))
        self.assertEqual(ewsghana_location.latitude, float(location.latitude))
        self.assertEqual(ewsghana_location.parent, location.parent_id)
        self.assertFalse(ewsghana_location.is_archived)

        sql_location = ewsghana_location.sql_location
        self.assertEqual(ewsghana_location.get_id, sql_location.location_id)
        self.assertIsNotNone(sql_location.id)
        self.assertIsNone(ewsghana_location.linked_supply_point())
        self.assertIsNone(sql_location.supply_point_id)
Example #14
0
    def test_facility_without_supply_point(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Location(json.loads(f.read())[5])

        ewsghana_location = self.api_object.location_sync(location)
        self.assertEqual(
            1,
            SQLLocation.objects.filter(
                domain=TEST_DOMAIN,
                location_type__administrative=False).count())

        supply_point = ewsghana_location.linked_supply_point()
        self.assertIsNotNone(supply_point)
        self.assertIsNotNone(ewsghana_location.sql_location.supply_point_id)

        self.assertEqual(supply_point.external_id, '')
        self.assertEqual(ewsghana_location.name, location.name)
        self.assertEqual(ewsghana_location.site_code, location.code)
        self.assertTrue(ewsghana_location.is_archived)
Example #15
0
    def test_create_facility_location(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Location(json.loads(f.read())[1])

        with open(os.path.join(self.datapath, 'sample_products.json')) as f:
            for p in json.loads(f.read()):
                self.api_object.product_sync(Product(p))
        self.assertEqual(8,
                         SQLProduct.objects.filter(domain=TEST_DOMAIN).count())
        ewsghana_location = self.api_object.location_sync(location)
        self.assertEqual(ewsghana_location.name,
                         location.supply_points[0].name)
        self.assertEqual(ewsghana_location.site_code,
                         location.supply_points[0].code)
        self.assertEqual("Hospital", ewsghana_location.location_type)
        self.assertEqual(ewsghana_location.longitude,
                         float(location.longitude))
        self.assertEqual(ewsghana_location.latitude, float(location.latitude))
        self.assertFalse(ewsghana_location.is_archived)

        sql_location = ewsghana_location.sql_location
        self.assertEqual(ewsghana_location.get_id, sql_location.location_id)
        self.assertEqual(int(sql_location.parent.external_id),
                         location.parent_id)
        self.assertIsNotNone(sql_location.id)
        self.assertIsNotNone(sql_location.supply_point_id)
        supply_point = get_supply_point_case_by_location_id(
            TEST_DOMAIN, sql_location.location_id)
        self.assertIsNotNone(supply_point)
        self.assertEqual(supply_point.location, ewsghana_location)
        self.assertEqual(location.supply_points[0].id,
                         int(supply_point.external_id))
        self.assertEqual(location.supply_points[0].name, supply_point.name)
        self.assertSetEqual(set(location.supply_points[0].products), {
            product.code
            for product in ewsghana_location.sql_location.products
        })
    def setUpClass(cls):
        cls.domain = initial_bootstrap(TEST_DOMAIN)
        db = get_db()
        if db.doc_exist(DOMAIN_MODULE_KEY):
            module_config = db.open_doc(DOMAIN_MODULE_KEY)
            module_map = module_config.get('module_map')
            if module_map:
                module_map[TEST_DOMAIN] = 'custom.ewsghana'
            else:
                module_config['module_map'][TEST_DOMAIN] = 'custom.ewsghana'
        else:
            module_config = db.save_doc({
                '_id': DOMAIN_MODULE_KEY,
                'module_map': {
                    'ewsghana-test-input-stock': 'custom.ewsghana'
                }
            })
        db.save_doc(module_config)
        generator.instantiate_accounting_for_tests()
        account = BillingAccount.get_or_create_account_by_domain(
            cls.domain.name,
            created_by="automated-test",
        )[0]
        plan = DefaultProductPlan.get_default_plan_by_domain(
            cls.domain, edition=SoftwarePlanEdition.ENTERPRISE)
        subscription = Subscription.new_domain_subscription(
            account, cls.domain.name, plan)
        subscription.is_active = True
        subscription.save()
        cls.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy')
        cls.api_object = EWSApi(TEST_DOMAIN, cls.endpoint)
        cls.api_object.prepare_commtrack_config()
        cls.api_object.prepare_custom_fields()
        cls.datapath = os.path.join(os.path.dirname(__file__), 'data')

        with open(os.path.join(cls.datapath, 'sample_products.json')) as f:
            for p in json.loads(f.read()):
                cls.api_object.product_sync(Product(p))

        with open(os.path.join(cls.datapath, 'sample_locations.json')) as f:
            for loc in json.loads(f.read()):
                cls.api_object.location_sync(Location(loc))

        cls.test_facility3 = SQLLocation.objects.get(domain=TEST_DOMAIN,
                                                     site_code='tsactive')
        cls.testregion2 = SQLLocation.objects.get(domain=TEST_DOMAIN,
                                                  site_code='testregion2')
        cls.rsp = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='rsp')
        cls.test_district = SQLLocation.objects.get(domain=TEST_DOMAIN,
                                                    site_code='testdistrict')

        cls.username1 = 'ews_user1'
        cls.password1 = 'dummy'
        cls.web_user1 = WebUser.create(TEST_DOMAIN, cls.username1,
                                       cls.password1)

        cls.web_user1.eula.signed = True
        cls.web_user1.save()

        cls.username2 = 'ews_user2'
        cls.password2 = 'dummy'
        cls.web_user2 = WebUser.create(TEST_DOMAIN, cls.username2,
                                       cls.password2)
        cls.web_user2.get_domain_membership(
            TEST_DOMAIN).location_id = cls.test_facility3.location_id

        cls.web_user2.eula.signed = True
        cls.web_user2.save()

        cls.username3 = 'ews_user3'
        cls.password3 = 'dummy'
        cls.web_user3 = WebUser.create(TEST_DOMAIN, cls.username3,
                                       cls.password3)
        cls.web_user3.get_domain_membership(
            TEST_DOMAIN).location_id = cls.testregion2.location_id

        cls.web_user3.eula.signed = True
        cls.web_user3.save()

        cls.username4 = 'ews_user4'
        cls.password4 = 'dummy'
        cls.web_user4 = WebUser.create(TEST_DOMAIN, cls.username4,
                                       cls.password4)
        cls.web_user4.get_domain_membership(
            TEST_DOMAIN).location_id = cls.rsp.location_id

        cls.web_user4.eula.signed = True
        cls.web_user4.save()

        cls.username5 = 'ews_user5'
        cls.password5 = 'dummy'
        cls.web_user5 = WebUser.create(TEST_DOMAIN, cls.username5,
                                       cls.password5)
        domain_membership = cls.web_user5.get_domain_membership(TEST_DOMAIN)
        domain_membership.location_id = cls.test_district.location_id
        domain_membership.role_id = UserRole.get_read_only_role_by_domain(
            cls.domain.name).get_id

        cls.web_user5.eula.signed = True
        cls.web_user5.save()

        cls.ad = SQLProduct.objects.get(domain=TEST_DOMAIN, code='ad')
        cls.al = SQLProduct.objects.get(domain=TEST_DOMAIN, code='al')

        cls.client = Client()
Example #17
0
    def test_location_without_products(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Location(json.loads(f.read())[8])

        ews_location = self.api_object.location_sync(location)
        self.assertEqual(ews_location.sql_location.products.count(), 0)