示例#1
0
    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]
示例#2
0
    def test_program_fixture(self):
        user = bootstrap_user(self, phone_number="1234567890")
        Program(
            domain=user.domain,
            name="test1",
            code="t1"
        ).save()

        program_list = Program.by_domain(user.domain)
        program_xml = ''
        for program in program_list:
            program_xml += '''
                <program id="{id}">
                    <name>{name}</name>
                    <code>{code}</code>
                </program>
            '''.format(
                id=program.get_id,
                name=program.name,
                code=program.code
            )

        fixture = program_fixture_generator(user, V1, None)

        self.assertXmlEqual('''<fixture id="commtrack:programs" user_id="{user_id}">
                                    <programs>
                                        {programs}
                                    </programs>
                                </fixture>'''.format(user_id=user.user_id, programs=program_xml),
                            ElementTree.tostring(fixture[0]))
示例#3
0
    def test_product_fixture(self):
        user = bootstrap_user(self, phone_number="1234567890")
        xml = self.generate_product_fixture_xml(user)
        fixture = product_fixture_generator(user, V1, None, None)

        self.assertXmlEqual(
            xml,
            ElementTree.tostring(fixture[0])
        )
示例#4
0
    def test_product_fixture(self):
        user = bootstrap_user(self, phone_number="1234567890")
        user_id = user.user_id
        products = ""
        product_list = Product.by_domain(user.domain)
        self._initialize_product_names(len(product_list))
        for i, product in enumerate(product_list):
            product_id = product._id
            product_name = self.product_names.next()
            product_unit = self._random_string(20)
            product_code = self._random_string(20)
            product_description = self._random_string(20)
            product_category = self._random_string(20)
            product_program_id = self._random_string(20)
            product_cost = 0 if i == 0 else float("%g" % random.uniform(1, 100))

            products += """
                <product id="{id}">
                    <name>{name}</name>
                    <unit>{unit}</unit>
                    <code>{code}</code>
                    <description>{description}</description>
                    <category>{category}</category>
                    <program_id>{program_id}</program_id>
                    <cost>{cost}</cost>
                </product>
            """.format(
                id=product_id,
                name=product_name,
                unit=product_unit,
                code=product_code,
                description=product_description,
                category=product_category,
                program_id=product_program_id,
                cost=product_cost,
            )

            product.name = product_name
            product.unit = product_unit
            product.code = product_code
            product.description = product_description
            product.category = product_category
            product.program_id = product_program_id
            product.cost = product_cost
            product.save()
        fixture = product_fixture_generator(user, V1, None)

        self.assertXmlEqual(
            """<fixture id="commtrack:products" user_id="{user_id}">
                                    <products>
                                        {products}
                                    </products>
                                </fixture>""".format(
                user_id=user_id, products=products
            ),
            ElementTree.tostring(fixture[0]),
        )
示例#5
0
    def test_selective_product_sync(self):
        user = bootstrap_user(self, phone_number="1234567890")

        expected_xml = self.generate_product_fixture_xml(user)

        product_list = Product.by_domain(user.domain)
        self._initialize_product_names(len(product_list))

        fixture_original = product_fixture_generator(user, V1, None)
        generate_restore_payload(user.to_casexml_user())
        self.assertXmlEqual(
            expected_xml,
            ElementTree.tostring(fixture_original[0])
        )

        first_sync = sorted(SyncLog.view(
            "phone/sync_logs_by_user",
            include_docs=True,
            reduce=False
        ).all(), key=lambda x: x.date)[-1]

        # make sure the time stamp on this first sync is
        # not on the same second that the products were created
        first_sync.date += datetime.timedelta(seconds=1)

        # second sync is before any changes are made, so there should
        # be no products synced
        fixture_pre_change = product_fixture_generator(user, V1, first_sync)
        generate_restore_payload(user.to_casexml_user())
        self.assertEqual(
            [],
            fixture_pre_change,
            "Fixture was not empty on second sync"
        )

        second_sync = sorted(SyncLog.view(
            "phone/sync_logs_by_user",
            include_docs=True,
            reduce=False
        ).all(), key=lambda x: x.date)[-1]

        self.assertTrue(first_sync._id != second_sync._id)

        # save should make the product more recently updated than the
        # last sync
        for product in product_list:
            product.save()

        # now that we've updated a product, we should get
        # product data in sync again
        fixture_post_change = product_fixture_generator(user, V1, second_sync)

        # regenerate the fixture xml to make sure it is still legit
        self.assertXmlEqual(
            expected_xml,
            ElementTree.tostring(fixture_post_change[0])
        )
示例#6
0
    def test_product_fixture(self):
        user = bootstrap_user(self, phone_number="1234567890")
        xml = self.generate_product_fixture_xml(user)
        fixture = product_fixture_generator(user, V1, None)

        self.assertXmlEqual(
            xml,
            ElementTree.tostring(fixture[0])
        )
示例#7
0
    def test_selective_product_sync(self):
        user = bootstrap_user(self, phone_number="1234567890")

        expected_xml = self.generate_product_fixture_xml(user)

        product_list = Product.by_domain(user.domain)
        self._initialize_product_names(len(product_list))

        fixture_original = product_fixture_generator(user, V1, None, None)
        generate_restore_payload(user.to_casexml_user())
        self.assertXmlEqual(
            expected_xml,
            ElementTree.tostring(fixture_original[0])
        )

        first_sync = sorted(SyncLog.view(
            "phone/sync_logs_by_user",
            include_docs=True,
            reduce=False
        ).all(), key=lambda x: x.date)[-1]

        # make sure the time stamp on this first sync is
        # not on the same second that the products were created
        first_sync.date += datetime.timedelta(seconds=1)

        # second sync is before any changes are made, so there should
        # be no products synced
        fixture_pre_change = product_fixture_generator(user, V1, None, first_sync)
        generate_restore_payload(user.to_casexml_user())
        self.assertEqual(
            [],
            fixture_pre_change,
            "Fixture was not empty on second sync"
        )

        second_sync = sorted(SyncLog.view(
            "phone/sync_logs_by_user",
            include_docs=True,
            reduce=False
        ).all(), key=lambda x: x.date)[-1]

        self.assertTrue(first_sync._id != second_sync._id)

        # save should make the product more recently updated than the
        # last sync
        for product in product_list:
            product.save()

        # now that we've updated a product, we should get
        # product data in sync again
        fixture_post_change = product_fixture_generator(user, V1, None, second_sync)

        # regenerate the fixture xml to make sure it is still legit
        self.assertXmlEqual(
            expected_xml,
            ElementTree.tostring(fixture_post_change[0])
        )
 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]
示例#9
0
 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]
示例#10
0
    def test_program_fixture(self):
        user = bootstrap_user(self, phone_number="1234567890")
        Program(domain=user.domain, name="test1", code="t1").save()

        program_list = Program.by_domain(user.domain)
        program_xml = self.generate_program_xml(program_list, user)

        fixture = program_fixture_generator(user, V1)

        self.assertXmlEqual(program_xml, ElementTree.tostring(fixture[0]))
示例#11
0
    def test_product_fixture(self):
        user = bootstrap_user(self, phone_number="1234567890")
        user_id = user.user_id
        products = ''
        product_list = Product.by_domain(user.domain)
        self._initialize_product_names(len(product_list))
        for i, product in enumerate(product_list):
            product_id = product._id
            product_name = self.product_names.next()
            product_unit = self._random_string(20)
            product_code = self._random_string(20)
            product_description = self._random_string(20)
            product_category = self._random_string(20)
            product_program_id = self._random_string(20)
            product_cost = 0 if i == 0 else float('%g' %
                                                  random.uniform(1, 100))

            products += '''
                <product id="{id}">
                    <name>{name}</name>
                    <unit>{unit}</unit>
                    <code>{code}</code>
                    <description>{description}</description>
                    <category>{category}</category>
                    <program_id>{program_id}</program_id>
                    <cost>{cost}</cost>
                </product>
            '''.format(id=product_id,
                       name=product_name,
                       unit=product_unit,
                       code=product_code,
                       description=product_description,
                       category=product_category,
                       program_id=product_program_id,
                       cost=product_cost)

            product.name = product_name
            product.unit = product_unit
            product.code = product_code
            product.description = product_description
            product.category = product_category
            product.program_id = product_program_id
            product.cost = product_cost
            product.save()
        fixture = product_fixture_generator(user, V1, None)

        self.assertXmlEqual(
            '''<fixture id="commtrack:products" user_id="{user_id}">
                                    <products>
                                        {products}
                                    </products>
                                </fixture>'''.format(user_id=user_id,
                                                     products=products),
            ElementTree.tostring(fixture[0]))
示例#12
0
 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)
示例#13
0
    def test_program_fixture(self):
        user = bootstrap_user(self, phone_number="1234567890")
        Program(
            domain=user.domain,
            name="test1",
            code="t1"
        ).save()

        program_list = Program.by_domain(user.domain)
        program_xml = self.generate_program_xml(program_list, user)

        fixture = program_fixture_generator(user, V1, None, None)

        self.assertXmlEqual(
            program_xml,
            ElementTree.tostring(fixture[0])
        )
示例#14
0
    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]
示例#15
0
    def test_program_fixture(self):
        user = bootstrap_user(self, phone_number="1234567890")
        Program(domain=user.domain, name="test1", code="t1").save()

        program_list = Program.by_domain(user.domain)
        program_xml = ''
        for program in program_list:
            program_xml += '''
                <program id="{id}">
                    <name>{name}</name>
                    <code>{code}</code>
                </program>
            '''.format(id=program.get_id, name=program.name, code=program.code)

        fixture = program_fixture_generator(user, V1, None)

        self.assertXmlEqual(
            '''<fixture id="commtrack:programs" user_id="{user_id}">
                                    <programs>
                                        {programs}
                                    </programs>
                                </fixture>'''.format(user_id=user.user_id,
                                                     programs=program_xml),
            ElementTree.tostring(fixture[0]))
示例#16
0
    def test_product_fixture(self):
        user = bootstrap_user(self, phone_number="1234567890")
        user_id = user.user_id
        products = ''
        product_list = Product.by_domain(user.domain)
        self._initialize_product_names(len(product_list))
        for i, product in enumerate(product_list):
            product_id = product._id
            product_name = self.product_names.next()
            product_unit = self._random_string(20)
            product_code = self._random_string(20)
            product_description = self._random_string(20)
            product_category = self._random_string(20)
            product_program_id = self._random_string(20)
            product_cost = 0 if i == 0 else float('%g' %
                                                  random.uniform(1, 100))

            # only set this on one product, so we can also test that
            # this node doesn't get sent down on every product
            if i == 0:
                product_data = {'special_number': '555111'}

                custom_data_xml = '''
                    <product_data>
                        <special_number>555111</special_number>
                    </product_data>
                '''
            else:
                product_data = {}
                custom_data_xml = ''

            products += '''
                <product id="{id}">
                    <name>{name}</name>
                    <unit>{unit}</unit>
                    <code>{code}</code>
                    <description>{description}</description>
                    <category>{category}</category>
                    <program_id>{program_id}</program_id>
                    <cost>{cost}</cost>
                    {custom_data}
                </product>
            '''.format(id=product_id,
                       name=product_name,
                       unit=product_unit,
                       code=product_code,
                       description=product_description,
                       category=product_category,
                       program_id=product_program_id,
                       cost=product_cost,
                       custom_data=custom_data_xml)

            product.name = product_name
            product.unit = product_unit
            product.code = product_code
            product.description = product_description
            product.category = product_category
            product.program_id = product_program_id
            product.cost = product_cost
            product.product_data = product_data
            product.save()
        fixture = product_fixture_generator(user, V1, None)

        self.assertXmlEqual(
            '''<fixture id="commtrack:products" user_id="{user_id}">
                                    <products>
                                        {products}
                                    </products>
                                </fixture>'''.format(user_id=user_id,
                                                     products=products),
            ElementTree.tostring(fixture[0]))
示例#17
0
    def test_product_fixture(self):
        user = bootstrap_user(self, phone_number="1234567890")
        user_id = user.user_id
        products = ''
        product_list = Product.by_domain(user.domain)
        self._initialize_product_names(len(product_list))
        for i, product in enumerate(product_list):
            product_id = product._id
            product_name = self.product_names.next()
            product_unit = self._random_string(20)
            product_code = self._random_string(20)
            product_description = self._random_string(20)
            product_category = self._random_string(20)
            product_program_id = self._random_string(20)
            product_cost = 0 if i == 0 else float('%g' % random.uniform(1, 100))

            # only set this on one product, so we can also test that
            # this node doesn't get sent down on every product
            if i == 0:
                product_data = {
                    'special_number': '555111'
                }

                custom_data_xml = '''
                    <product_data>
                        <special_number>555111</special_number>
                    </product_data>
                '''
            else:
                product_data = {}
                custom_data_xml = ''

            products += '''
                <product id="{id}">
                    <name>{name}</name>
                    <unit>{unit}</unit>
                    <code>{code}</code>
                    <description>{description}</description>
                    <category>{category}</category>
                    <program_id>{program_id}</program_id>
                    <cost>{cost}</cost>
                    {custom_data}
                </product>
            '''.format(
                id=product_id,
                name=product_name,
                unit=product_unit,
                code=product_code,
                description=product_description,
                category=product_category,
                program_id=product_program_id,
                cost=product_cost,
                custom_data=custom_data_xml
            )

            product.name = product_name
            product.unit = product_unit
            product.code = product_code
            product.description = product_description
            product.category = product_category
            product.program_id = product_program_id
            product.cost = product_cost
            product.product_data = product_data
            product.save()
        fixture = product_fixture_generator(user, V1, None)

        self.assertXmlEqual('''<fixture id="commtrack:products" user_id="{user_id}">
                                    <products>
                                        {products}
                                    </products>
                                </fixture>'''.format(user_id=user_id, products=products),
                            ElementTree.tostring(fixture[0]))