示例#1
0
    def test_gps_location(self):
        file_path = os.path.join(os.path.dirname(__file__), "data", "gps_location.xml")
        xml_data = open(file_path, "rb").read()
        with create_and_save_xform(xml_data) as doc_id:
            xform = XFormInstance.get(doc_id)
            self.assertEqual(
                xform.metadata.location,
                # '42.3739063 -71.1109113 0.0 886.0'
                GeoPoint(
                    latitude=Decimal('42.3739063'),
                    longitude=Decimal('-71.1109113'),
                    altitude=Decimal('0.0'),
                    accuracy=Decimal('886.0'),
                )
            )

            self.assertEqual(xform.metadata.to_json(), {
                'username': u'*****@*****.**',
                'doc_type': 'Metadata',
                'instanceID': u'5d3d01561f584e85b53669a48bfc6039',
                'userID': u'f7f0c79e-8b79-11df-b7de-005056c00008',
                'timeEnd': '2013-07-20T00:02:27Z',
                'appVersion': u'2.0',
                'timeStart': '2013-07-19T21:21:31Z',
                'deprecatedID': None,
                'deviceID': u'commconnect',
                'location': '42.3739063 -71.1109113 0.0 886.0',
            })
            xform.delete()
示例#2
0
    def _test(self, name, any_id_ok=False, tz_differs=False):
        with open(os.path.join(os.path.dirname(__file__), 'data', '{name}.xml'.format(name=name))) as f:
            instance = f.read()

        if tz_differs and phone_timezones_should_be_processed():
            expected_name = name + '-tz'
        else:
            expected_name = name

        with open(os.path.join(os.path.dirname(__file__), 'data',
                               '{name}.json'.format(name=expected_name))) as f:
            result = json.load(f)

        with create_and_save_xform(instance) as doc_id:
            xform = XFormInstance.get(doc_id)
            try:
                xform_json = xform.to_json()
                result['received_on'] = xform_json['received_on']
                result['_rev'] = xform_json['_rev']
                if any_id_ok:
                    result['_id'] = xform_json['_id']
                self.assertDictEqual(xform_json, result)
            except Exception:
                # to help when bootstrapping a new test case
                print json.dumps(xform_json)
                raise
            finally:
                xform.delete()
示例#3
0
    def _test(self, name, any_id_ok=False, tz_differs=False):
        with open(
                os.path.join(os.path.dirname(__file__), 'data',
                             '{name}.xml'.format(name=name))) as f:
            instance = f.read()

        if tz_differs and phone_timezones_should_be_processed():
            expected_name = name + '-tz'
        else:
            expected_name = name

        with open(
                os.path.join(os.path.dirname(__file__), 'data',
                             '{name}.json'.format(name=expected_name))) as f:
            result = json.load(f)

        with create_and_save_xform(instance) as doc_id:
            xform = XFormInstance.get(doc_id)
            try:
                xform_json = xform.to_json()
                result['received_on'] = xform_json['received_on']
                result['_rev'] = xform_json['_rev']
                if any_id_ok:
                    result['_id'] = xform_json['_id']
                self.assertDictEqual(xform_json, result)
            except Exception:
                # to help when bootstrapping a new test case
                print json.dumps(xform_json)
                raise
            finally:
                xform.delete()
示例#4
0
    def testDecimalAppVersion(self):
        '''
        Tests that an appVersion that looks like a decimal:
        (a) is not converted to a Decimal by couchdbkit
        (b) does not crash anything
        '''

        file_path = os.path.join(os.path.dirname(__file__), "data", "decimalmeta.xml")
        xml_data = open(file_path, "rb").read()
        with create_and_save_xform(xml_data) as doc_id:
            xform = XFormInstance.get(doc_id)
        self.assertEqual(xform.metadata.appVersion, '2.0')
        self.assertEqual(xform.metadata.to_json(), {
            'username': u'admin',
            'doc_type': 'Metadata',
            'instanceID': None,
            'userID': u'f7f0c79e-8b79-11df-b7de-005056c00008',
            'timeEnd': '2010-07-23T13:55:11.648000Z',
            'appVersion': u'2.0',
            'timeStart': '2010-07-22T13:54:27.971000Z',
            'deprecatedID': None,
            'deviceID': None,
            'clinic_id': u'5020280',
            'location': None,
        })
示例#5
0
 def testDecimalAppVersion(self):
     '''
     Tests that an appVersion that looks like a decimal:
     (a) is not converted to a Decimal by couchdbkit
     (b) does not crash anything
     '''
     
     file_path = os.path.join(os.path.dirname(__file__), "data", "decimalmeta.xml")
     xml_data = open(file_path, "rb").read()
     with create_and_save_xform(xml_data) as doc_id:
         xform = XFormInstance.get(doc_id)
     self.assertEqual(xform.metadata.appVersion, '2.0')
     self.assertEqual(xform.metadata.to_json(), {
         'username': u'admin',
         'doc_type': 'Metadata',
         'instanceID': None,
         'userID': u'f7f0c79e-8b79-11df-b7de-005056c00008',
         'timeEnd': '2010-07-23T13:55:11Z',
         'appVersion': u'2.0',
         'timeStart': '2010-07-22T13:54:27Z',
         'deprecatedID': None,
         'deviceID': None,
         'clinic_id': u'5020280',
         'location': None,
     })
示例#6
0
    def test_gps_location(self):
        file_path = os.path.join(os.path.dirname(__file__), "data", "gps_location.xml")
        xml_data = open(file_path, "rb").read()
        with create_and_save_xform(xml_data) as doc_id:
            xform = XFormInstance.get(doc_id)
            self.assertEqual(
                xform.metadata.location,
                # '42.3739063 -71.1109113 0.0 886.0'
                GeoPoint(
                    latitude=Decimal('42.3739063'),
                    longitude=Decimal('-71.1109113'),
                    altitude=Decimal('0.0'),
                    accuracy=Decimal('886.0'),
                )
            )

            self.assertEqual(xform.metadata.to_json(), {
                'username': u'*****@*****.**',
                'doc_type': 'Metadata',
                'instanceID': u'5d3d01561f584e85b53669a48bfc6039',
                'userID': u'f7f0c79e-8b79-11df-b7de-005056c00008',
                'timeEnd': '2013-07-20T00:02:27.493000Z',
                'appVersion': u'2.0',
                'timeStart': '2013-07-19T21:21:31.188000Z',
                'deprecatedID': None,
                'deviceID': u'commconnect',
                'location': '42.3739063 -71.1109113 0.0 886.0',
            })
            xform.delete()
示例#7
0
 def testMetaDateInDatetimeFields(self):
     file_path = os.path.join(os.path.dirname(__file__), "data", "date_in_meta.xml")
     xml_data = open(file_path, "rb").read()
     with create_and_save_xform(xml_data) as doc_id:
         xform = XFormInstance.get(doc_id)
         self.assertEqual(datetime(2014, 7, 10), xform.metadata.timeStart)
         self.assertEqual(datetime(2014, 7, 11), xform.metadata.timeEnd)
         xform.delete()
示例#8
0
 def testMetaDateInDatetimeFields(self):
     file_path = os.path.join(os.path.dirname(__file__), "data", "date_in_meta.xml")
     xml_data = open(file_path, "rb").read()
     with create_and_save_xform(xml_data) as doc_id:
         xform = XFormInstance.get(doc_id)
         self.assertEqual(datetime(2014, 7, 10), xform.metadata.timeStart)
         self.assertEqual(datetime(2014, 7, 11), xform.metadata.timeEnd)
         xform.delete()
示例#9
0
    def test_empty_gps_location(self):
        file_path = os.path.join(os.path.dirname(__file__), "data", "gps_empty_location.xml")
        xml_data = open(file_path, "rb").read()
        with create_and_save_xform(xml_data) as doc_id:
            xform = XFormInstance.get(doc_id)
            self.assertEqual(
                xform.metadata.location,
                None
            )

            self.assertEqual(xform.metadata.to_json()['location'], None)
            xform.delete()
示例#10
0
    def test_empty_gps_location(self):
        file_path = os.path.join(os.path.dirname(__file__), "data", "gps_empty_location.xml")
        xml_data = open(file_path, "rb").read()
        with create_and_save_xform(xml_data) as doc_id:
            xform = XFormInstance.get(doc_id)
            self.assertEqual(
                xform.metadata.location,
                None
            )

            self.assertEqual(xform.metadata.to_json()['location'], None)
            xform.delete()
示例#11
0
 def testClosed(self):
     file_path = os.path.join(os.path.dirname(__file__), "data", "namespaces.xml")
     xml_data = open(file_path, "rb").read()
     with create_and_save_xform(xml_data) as doc_id:
         xform = XFormInstance.get(doc_id)
     self.assertEqual("http://commcarehq.org/test/ns", xform.xmlns)
     self.assertEqual("no namespace here", xform.xpath("form/empty"))
     self.assertEqual("http://commcarehq.org/test/flatns", xform.xpath("form/flat")["@xmlns"])
     self.assertEqual("http://commcarehq.org/test/parent", xform.xpath("form/parent")["@xmlns"])
     self.assertEqual("cwo", xform.xpath("form/parent/childwithout"))
     self.assertEqual("http://commcarehq.org/test/child1", xform.xpath("form/parent/childwith")["@xmlns"])
     self.assertEqual("http://commcarehq.org/test/child2", xform.xpath("form/parent/childwithelement")["@xmlns"])
     self.assertEqual("gc", xform.xpath("form/parent/childwithelement/grandchild"))
     self.assertEqual("lcwo", xform.xpath("form/parent/lastchildwithout"))
     self.assertEqual("nothing here either", xform.xpath("form/lastempty"))
示例#12
0
 def testClosed(self):
     file_path = os.path.join(os.path.dirname(__file__), "data", "namespaces.xml")
     xml_data = open(file_path, "rb").read()
     with create_and_save_xform(xml_data) as doc_id:
         xform = XFormInstance.get(doc_id)
     self.assertEqual("http://commcarehq.org/test/ns", xform.xmlns)
     self.assertEqual("no namespace here", xform.xpath("form/empty"))
     self.assertEqual("http://commcarehq.org/test/flatns", xform.xpath("form/flat")["@xmlns"])
     self.assertEqual("http://commcarehq.org/test/parent", xform.xpath("form/parent")["@xmlns"])
     self.assertEqual("cwo", xform.xpath("form/parent/childwithout"))
     self.assertEqual("http://commcarehq.org/test/child1", xform.xpath("form/parent/childwith")["@xmlns"])
     self.assertEqual("http://commcarehq.org/test/child2", xform.xpath("form/parent/childwithelement")["@xmlns"])
     self.assertEqual("gc", xform.xpath("form/parent/childwithelement/grandchild"))
     self.assertEqual("lcwo", xform.xpath("form/parent/lastchildwithout"))
     self.assertEqual("nothing here either", xform.xpath("form/lastempty"))
示例#13
0
    def testMetaBadUsername(self):
        file_path = os.path.join(os.path.dirname(__file__), "data", "meta_bad_username.xml")
        xml_data = open(file_path, "rb").read()
        with create_and_save_xform(xml_data) as doc_id:
            xform = XFormInstance.get(doc_id)
            self.assertEqual(xform.metadata.appVersion, '2.0')

            self.assertEqual(xform.metadata.to_json(), {
                'username': u'2013-07-19',
                'doc_type': 'Metadata',
                'instanceID': u'e8afaec3c66745ef80e48062d4b91b56',
                'userID': u'f7f0c79e-8b79-11df-b7de-005056c00008',
                'timeEnd': '2013-07-20T00:02:27.493000Z',
                'appVersion': u'2.0',
                'timeStart': '2013-07-19T21:21:31.188000Z',
                'deprecatedID': None,
                'deviceID': u'commconnect',
                'location': None,
            })
            xform.delete()
示例#14
0
    def testMetaAppVersionDict(self):
        file_path = os.path.join(os.path.dirname(__file__), "data", "meta_dict_appversion.xml")
        xml_data = open(file_path, "rb").read()
        with create_and_save_xform(xml_data) as doc_id:
            xform = XFormInstance.get(doc_id)
            self.assertEqual(xform.metadata.appVersion, '2.0')

            self.assertEqual(xform.metadata.to_json(), {
                'username': u'*****@*****.**',
                'doc_type': 'Metadata',
                'instanceID': u'5d3d01561f584e85b53669a48bfc6039',
                'userID': u'f7f0c79e-8b79-11df-b7de-005056c00008',
                'timeEnd': '2013-07-20T00:02:27Z',
                'appVersion': u'2.0',
                'timeStart': '2013-07-19T21:21:31Z',
                'deprecatedID': None,
                'deviceID': u'commconnect',
                'location': None,
            })
            xform.delete()
示例#15
0
    def testMetaAppVersionDict(self):
        file_path = os.path.join(os.path.dirname(__file__), "data", "meta_dict_appversion.xml")
        xml_data = open(file_path, "rb").read()
        with create_and_save_xform(xml_data) as doc_id:
            xform = XFormInstance.get(doc_id)
            self.assertEqual(xform.metadata.appVersion, '2.0')

            self.assertEqual(xform.metadata.to_json(), {
                'username': u'*****@*****.**',
                'doc_type': 'Metadata',
                'instanceID': u'5d3d01561f584e85b53669a48bfc6039',
                'userID': u'f7f0c79e-8b79-11df-b7de-005056c00008',
                'timeEnd': '2013-07-20T00:02:27.493000Z',
                'appVersion': u'2.0',
                'timeStart': '2013-07-19T21:21:31.188000Z',
                'deprecatedID': None,
                'deviceID': u'commconnect',
                'location': None,
            })
            xform.delete()
示例#16
0
    def testMetaBadUsername(self):
        file_path = os.path.join(os.path.dirname(__file__), "data", "meta_bad_username.xml")
        xml_data = open(file_path, "rb").read()
        with create_and_save_xform(xml_data) as doc_id:
            xform = XFormInstance.get(doc_id)
            self.assertEqual(xform.metadata.appVersion, '2.0')

            self.assertEqual(xform.metadata.to_json(), {
                'username': u'2013-07-19',
                'doc_type': 'Metadata',
                'instanceID': u'e8afaec3c66745ef80e48062d4b91b56',
                'userID': u'f7f0c79e-8b79-11df-b7de-005056c00008',
                'timeEnd': '2013-07-20T00:02:27Z',
                'appVersion': u'2.0',
                'timeStart': '2013-07-19T21:21:31Z',
                'deprecatedID': None,
                'deviceID': u'commconnect',
                'location': None,
            })
            xform.delete()
示例#17
0
 def testClosed(self):
     file_path = os.path.join(os.path.dirname(__file__), "data", "meta.xml")
     xml_data = open(file_path, "rb").read()
     with create_and_save_xform(xml_data) as doc_id:
         xform = XFormInstance.get(doc_id)
     self.assertNotEqual(None, xform.metadata)
     self.assertEqual(date(2010,07,22), xform.metadata.timeStart.date())
     self.assertEqual(date(2010,07,23), xform.metadata.timeEnd.date())
     self.assertEqual("admin", xform.metadata.username)
     self.assertEqual("f7f0c79e-8b79-11df-b7de-005056c00008", xform.metadata.userID)
     self.assertEqual("v1.2.3 (biz bazzle)", xform.metadata.appVersion)
     self.assertEqual(xform.metadata.to_json(), {
         'username': u'admin',
         'doc_type': 'Metadata',
         'instanceID': None,
         'userID': u'f7f0c79e-8b79-11df-b7de-005056c00008',
         'timeEnd': '2010-07-23T13:55:11.648000Z',
         'appVersion': u'v1.2.3 (biz bazzle)',
         'timeStart': '2010-07-22T13:54:27.971000Z',
         'deprecatedID': None,
         'deviceID': None,
         'clinic_id': u'5020280',
         'location': None,
     })
示例#18
0
 def testClosed(self):
     file_path = os.path.join(os.path.dirname(__file__), "data", "meta.xml")
     xml_data = open(file_path, "rb").read()
     with create_and_save_xform(xml_data) as doc_id:
         xform = XFormInstance.get(doc_id)
     self.assertNotEqual(None, xform.metadata)
     self.assertEqual(date(2010,07,22), xform.metadata.timeStart.date())
     self.assertEqual(date(2010,07,23), xform.metadata.timeEnd.date())
     self.assertEqual("admin", xform.metadata.username)
     self.assertEqual("f7f0c79e-8b79-11df-b7de-005056c00008", xform.metadata.userID)
     self.assertEqual("v1.2.3 (biz bazzle)", xform.metadata.appVersion)
     self.assertEqual(xform.metadata.to_json(), {
         'username': u'admin',
         'doc_type': 'Metadata',
         'instanceID': None,
         'userID': u'f7f0c79e-8b79-11df-b7de-005056c00008',
         'timeEnd': '2010-07-23T13:55:11Z',
         'appVersion': u'v1.2.3 (biz bazzle)',
         'timeStart': '2010-07-22T13:54:27Z',
         'deprecatedID': None,
         'deviceID': None,
         'clinic_id': u'5020280',
         'location': None,
     })