Exemple #1
0
    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'
         })
Exemple #3
0
 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')
Exemple #4
0
 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")
Exemple #5
0
 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'
             },
         })
Exemple #6
0
 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": "",
         },
     )
Exemple #8
0
    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)
Exemple #9
0
    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)
Exemple #10
0
 def test_name(self):
     person = Person(name="Chris Beaven", country="NZ")
     self.assertEqual(person.country.name, "New Zealand")
Exemple #11
0
 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')
Exemple #12
0
 def test_name(self):
     person = Person(name="Chris Beaven", fantasy_country="NV")
     self.assertEqual(person.fantasy_country.name, "Neverland")
Exemple #13
0
 def test_text(self):
     person = Person(name="Chris Beaven", country="NZ")
     self.assertEqual(force_text(person.country), "NZ")
Exemple #14
0
 def test_validate_empty(self):
     person = Person(name="Chris")
     self.assertRaises(validators.ValidationError, person.full_clean)
Exemple #15
0
 def test_validate_invalid(self):
     person = Person(name="Chris", country=":(")
     self.assertRaises(validators.ValidationError, person.full_clean)
Exemple #16
0
 def test_text(self):
     person = Person(name='Chris Beaven', country='NZ')
     self.assertEqual(force_text(person.country), 'NZ')
Exemple #17
0
 def test_flag_css(self):
     person = Person(name='Chris Beaven', country='NZ')
     self.assertEqual(person.country.flag_css, 'flag-sprite flag-n flag-_z')
Exemple #18
0
 def test_flag_css(self):
     person = Person(name="Chris Beaven", country="NZ")
     self.assertEqual(person.country.flag_css, "flag-sprite flag-n flag-_z")
Exemple #19
0
 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, "🇩🇪")
Exemple #20
0
 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")
Exemple #21
0
 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')
Exemple #22
0
 def test_name(self):
     person = Person(name='Chris Beaven', country='NZ')
     self.assertEqual(person.country.name, 'New Zealand')
Exemple #23
0
 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")
Exemple #24
0
 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'🇩🇪')
Exemple #25
0
 def test_unicode_flag_blank(self):
     person = Person(name="Matthew Schinckel")
     self.assertEqual(person.country.unicode_flag, "")
Exemple #26
0
    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)
Exemple #27
0
 def test_flag_css_blank(self):
     person = Person(name="Chris Beaven")
     self.assertEqual(person.country.flag_css, "")
Exemple #28
0
    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()
Exemple #30
0
 def test_validate_alpha3(self):
     person = Person(name="Chris", country="NZL")
     person.full_clean()