def test_logic(self): person = Person(name='Chris Beaven', country='NZ') self.assertEqual(person.country, 'NZ') self.assertNotEqual(person.country, 'ZZ') self.assertTrue(person.country) person.country = '' self.assertFalse(person.country)
def test_serialize(self): person = Person(name='Chris Beaven', country='NZ') serializer = PersonSerializer(person) self.assertEqual( serializer.data, { 'name': 'Chris Beaven', 'country': 'NZ', 'other_country': '', 'favourite_country': 'NZ' })
def test_COUNTRIES_FLAG_URL_setting(self): # Custom relative url person = Person(name='Chris Beaven', country='NZ') with self.settings(COUNTRIES_FLAG_URL='img/flag-{code_upper}.png'): self.assertEqual(person.country.flag, '/static-assets/img/flag-NZ.png') # Custom absolute url with self.settings(COUNTRIES_FLAG_URL='https://flags.example.com/' '{code_upper}.PNG'): self.assertEqual(person.country.flag, 'https://flags.example.com/NZ.PNG')
def test_COUNTRIES_FLAG_URL_setting(self): # Custom relative url person = Person(name="Chris Beaven", country="NZ") with self.settings( COUNTRIES_FLAG_URL="img/flag-{code_upper}.png", STATIC_URL="/static-assets/" ): self.assertEqual(person.country.flag, "/static-assets/img/flag-NZ.png") # Custom absolute url with self.settings( COUNTRIES_FLAG_URL="https://flags.example.com/{code_upper}.PNG" ): self.assertEqual(person.country.flag, "https://flags.example.com/NZ.PNG")
def test_serialize_country_dict(self): person = Person(name='Chris Beaven', other_country='AU') serializer = PersonSerializer(person) self.assertEqual( serializer.data, { 'name': 'Chris Beaven', 'country': '', 'other_country': { 'code': 'AU', 'name': 'Australia' }, })
def test_serialize_country_dict(self): person = Person(name="Chris Beaven", other_country="AU") serializer = PersonSerializer(person) self.assertEqual( serializer.data, { "name": "Chris Beaven", "country": "", "other_country": {"code": "AU", "name": "Australia"}, "favourite_country": "New Zealand", "fantasy_country": "", }, )
def test_serialize(self): person = Person(name="Chris Beaven", country="NZ") serializer = PersonSerializer(person) self.assertEqual( serializer.data, { "name": "Chris Beaven", "country": "NZ", "other_country": "", "favourite_country": "NZ", "fantasy_country": "", }, )
def test_custom_country_pickling(self): chris = Person(name="Chris Beaven", fantasy_country="NV") # django uses pickle.HIGHEST_PROTOCOL which is somewhere between 2 and # 4, depending on python version. Let's use 2 for testing. pickled_neverland = pickle.dumps(chris.fantasy_country, protocol=2) # Different python versions end up with slightly different sizes. Let's # just check the size is smaller than if it also contained the fantasy # countries list in the pickle. self.assertLess(len(pickled_neverland), 300) neverland = pickle.loads(pickled_neverland) self.assertEqual(neverland.code, "NV") self.assertEqual(neverland.name, "Neverland") self.assertEqual(neverland.flag_url, None) self.assertIsInstance(neverland.countries, custom_countries.FantasyCountries)
def test_standard_country_pickling(self): chris = Person(name="Chris Beaven", country="NZ") # django uses pickle.HIGHEST_PROTOCOL which is somewhere between 2 and # 4, depending on python version. Let's use 2 for testing. newly_pickled_zealand = pickle.dumps(chris.country, protocol=2) # Different python versions end up with slightly different sizes. Let's # just check the size is smaller than if it contained the entire # standard countries list in the pickle. self.assertLess(len(newly_pickled_zealand), 200) unpickled = pickle.loads(newly_pickled_zealand) self.assertEqual(unpickled.code, "NZ") self.assertEqual(unpickled.name, "New Zealand") self.assertEqual(unpickled.flag_url, None) self.assertIs(unpickled.countries, countries) self.assertIsNone(unpickled.custom_countries)
def test_name(self): person = Person(name="Chris Beaven", country="NZ") self.assertEqual(person.country.name, "New Zealand")
def test_custom_field_flag_url(self): person = Person(name='Chris Beaven', country='NZ', other_country='US') self.assertEqual(person.other_country.flag, '//flags.example.com/us.gif')
def test_name(self): person = Person(name="Chris Beaven", fantasy_country="NV") self.assertEqual(person.fantasy_country.name, "Neverland")
def test_text(self): person = Person(name="Chris Beaven", country="NZ") self.assertEqual(force_text(person.country), "NZ")
def test_validate_empty(self): person = Person(name="Chris") self.assertRaises(validators.ValidationError, person.full_clean)
def test_validate_invalid(self): person = Person(name="Chris", country=":(") self.assertRaises(validators.ValidationError, person.full_clean)
def test_text(self): person = Person(name='Chris Beaven', country='NZ') self.assertEqual(force_text(person.country), 'NZ')
def test_flag_css(self): person = Person(name='Chris Beaven', country='NZ') self.assertEqual(person.country.flag_css, 'flag-sprite flag-n flag-_z')
def test_flag_css(self): person = Person(name="Chris Beaven", country="NZ") self.assertEqual(person.country.flag_css, "flag-sprite flag-n flag-_z")
def test_unicode_flags(self): person = Person(name="Matthew Schinckel", country="AU", other_country="DE") self.assertEqual(person.country.unicode_flag, "🇦🇺") self.assertEqual(person.other_country.unicode_flag, "🇩🇪")
def test_flag(self): person = Person(name="Chris Beaven", country="NZ") with self.settings(STATIC_URL="/static-assets/"): self.assertEqual(person.country.flag, "/static-assets/flags/nz.gif")
def test_flag(self): person = Person(name='Chris Beaven', country='NZ') with self.settings(STATIC_URL='/static-assets/'): self.assertEqual(person.country.flag, '/static-assets/flags/nz.gif')
def test_name(self): person = Person(name='Chris Beaven', country='NZ') self.assertEqual(person.country.name, 'New Zealand')
def test_custom_field_flag_url(self): person = Person(name="Chris Beaven", country="NZ", other_country="US") self.assertEqual(person.other_country.flag, "//flags.example.com/us.gif")
def test_unicode_flags(self): person = Person(name='Matthew Schinckel', country='AU', other_country='DE') self.assertEqual(person.country.unicode_flag, u'🇦🇺') self.assertEqual(person.other_country.unicode_flag, u'🇩🇪')
def test_unicode_flag_blank(self): person = Person(name="Matthew Schinckel") self.assertEqual(person.country.unicode_flag, "")
def test_len(self): person = Person(name='Chris Beaven', country='NZ') self.assertEqual(len(person.country), 2) person = Person(name='The Outsider', country=None) self.assertEqual(len(person.country), 0)
def test_flag_css_blank(self): person = Person(name="Chris Beaven") self.assertEqual(person.country.flag_css, "")
def test_len(self): person = Person(name="Chris Beaven", country="NZ") self.assertEqual(len(person.country), 2) person = Person(name="The Outsider", country=None) self.assertEqual(len(person.country), 0)
def test_render_modelform_instance(self): person = Person(country='NZ') self.Form(instance=person).as_p()
def test_validate_alpha3(self): person = Person(name="Chris", country="NZL") person.full_clean()