def test_should_remove_right_to_left_mark_character_while_importing_from_excel( self): constraint = GeoCodeConstraint() # the string is '49.418607\u200e' self.assertEqual((90.0, 49.418607), constraint.validate("90 ", u'49.418607')) self.assertEqual((49.418607, 130.0), constraint.validate(u'49.418607', " 130 "))
def test_longitude_should_be_between_minus_180_and_180(self): with self.assertRaises(LongitudeNotInRange) as e: constraint = GeoCodeConstraint() constraint.validate("-10", "190") self.assertEqual(("190", ), e.exception.data) with self.assertRaises(LongitudeNotInRange) as e: constraint = GeoCodeConstraint() constraint.validate("90", "-190") self.assertEqual(("-190", ), e.exception.data)
def validate(self, lat_long_string): if lat_long_string is None: raise GeoCodeFormatException(self.code) lat_long = lat_long_string.strip().split() if len(lat_long) < 2: raise GeoCodeFormatException(self.code) return GeoCodeConstraint().validate(latitude=lat_long[0], longitude=lat_long[1])
def validate(self, lat_long_string): Field.validate(self, lat_long_string) if not lat_long_string: return None lat_long = lat_long_string.replace(",", " ") lat_long = re.sub(' +', ' ', lat_long).split(" ") if len(lat_long) != 2: raise GeoCodeFormatException(self.code) return GeoCodeConstraint().validate(latitude=lat_long[0], longitude=lat_long[1])
def __call__(self, value): lat_long_string = self.clean(value) lat_long = lat_long_string.replace(",", " ") lat_long = re.sub(' +', ' ', lat_long).split(" ") try: if len(lat_long) != 2: raise Exception GeoCodeConstraint().validate(latitude=lat_long[0], longitude=lat_long[1]) except Exception: raise ValidationError(_( "Incorrect GPS format. The GPS coordinates must be in the following format: xx.xxxx,yy.yyyy. Example -18.8665,47.5315")) return lat_long_string
def clean_geocode(self): geo_code_field_code = get_geo_code_field_question_code(self.form_model) lat_long_string = self.cleaned_data[geo_code_field_code] lat_long = lat_long_string.replace(",", " ").strip().split() try: if len(lat_long) < 2: raise Exception GeoCodeConstraint().validate(latitude=lat_long[0], longitude=lat_long[1]) except Exception: raise ValidationError( _("Incorrect GPS format. The GPS coordinates must be in the following format: xx.xxxx,yy.yyyy. Example -18.8665,47.5315" )) return self.cleaned_data[geo_code_field_code]
def test_longitude_should_be_between_minus_180_and_180(self): with self.assertRaises(LongitudeNotInRange) as e: constraint = GeoCodeConstraint() constraint.validate("-10", "190") self.assertEqual(("190",), e.exception.data) with self.assertRaises(LongitudeNotInRange) as e: constraint = GeoCodeConstraint() constraint.validate("90", "-190") self.assertEqual(("-190",), e.exception.data)
def test_should_strip_white_spaces(self): constraint = GeoCodeConstraint() self.assertEqual((90.0, 130.0), constraint.validate("90 ", " 130")) self.assertEqual((90.0, 130.0), constraint.validate(" 90 ", " 130 "))
def test_should_invalidate_non_float_longitude(self): with self.assertRaises(LongitudeNotFloat) as e: constraint = GeoCodeConstraint() constraint.validate("1.2", "asasasas") self.assertEqual(("asasasas", ), e.exception.data)
def test_latitude_and_longitude_is_float(self): constraint = GeoCodeConstraint() expected_response = (90.0, 130.0) actual_response = constraint.validate("90", "130") self.assertEqual(expected_response, actual_response)
def test_should_invalidate_non_float_longitude(self): with self.assertRaises(LongitudeNotFloat) as e: constraint = GeoCodeConstraint() constraint.validate("1.2", "asasasas") self.assertEqual(("asasasas",), e.exception.data)
def test_should_raise_error_for_non_ascii_characters(self): # the string is '49.418607\u200e' with self.assertRaises(UnicodeEncodeError) as e: constraint = GeoCodeConstraint() constraint.validate(u'23º', u'43º') self.assertEqual("ordinal not in range(128)", e.exception.reason)