def test_transform(self): try: CharField.register_lookup(Lower) Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Rhonda') authors = Author.objects.filter(name__lower__exact='john smith') self.assertQuerysetEqual(authors.order_by('name'), ['John Smith'], lambda a: a.name) finally: CharField._unregister_lookup(Lower)
def test_transform(self): try: CharField.register_lookup(Ord) authors = Author.objects.annotate(first_initial=Left('name', 1)) self.assertCountEqual(authors.filter(first_initial__ord=ord('J')), [self.john]) self.assertCountEqual(authors.exclude(first_initial__ord=ord('J')), [self.elena, self.rhonda]) finally: CharField._unregister_lookup(Ord)
def test_transform(self): new_name = self.t1.name.upper() self.assertNotEqual(self.t1.name, new_name) Tag.objects.create(name=new_name) CharField.register_lookup(Lower) try: self.assertCountEqual( Tag.objects.order_by().distinct('name__lower'), [self.t1, self.t2, self.t3, self.t4, self.t5], ) finally: CharField._unregister_lookup(Lower)
def test_transform(self): query = Query(Author) CharField.register_lookup(Lower, 'lower') try: where = query.build_where(~Q(name__lower='foo')) finally: CharField._unregister_lookup(Lower, 'lower') lookup = where.children[0] self.assertIsInstance(lookup, Exact) self.assertIsInstance(lookup.lhs, Lower) self.assertIsInstance(lookup.lhs.lhs, SimpleCol) self.assertEqual(lookup.lhs.lhs.target, Author._meta.get_field('name'))
def test_func_transform_bilateral(self): class UpperBilateral(Upper): bilateral = True try: CharField.register_lookup(UpperBilateral, 'upper') Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Rhonda') authors = Author.objects.filter(name__upper__exact='john smith') self.assertQuerysetEqual(authors.order_by('name'), [ 'John Smith', ], lambda a: a.name) finally: CharField._unregister_lookup(UpperBilateral, 'upper')
def test_upper_transform(self): try: CharField.register_lookup(Upper, 'upper') Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Rhonda') authors = Author.objects.filter(name__upper__exact='JOHN SMITH') self.assertQuerysetEqual( authors.order_by('name'), [ 'John Smith', ], lambda a: a.name ) finally: CharField._unregister_lookup(Upper, 'upper')
def test_length_transform(self): try: CharField.register_lookup(Length, 'length') Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Rhonda') authors = Author.objects.filter(name__length__gt=7) self.assertQuerysetEqual( authors.order_by('name'), [ 'John Smith', ], lambda a: a.name ) finally: CharField._unregister_lookup(Length, 'length')
def test_trim_transform(self): Author.objects.create(name=' John ') Author.objects.create(name='Rhonda') tests = ( (LTrim, 'John '), (RTrim, ' John'), (Trim, 'John'), ) for transform, trimmed_name in tests: with self.subTest(transform=transform): try: CharField.register_lookup(transform) authors = Author.objects.filter(**{'name__%s' % transform.lookup_name: trimmed_name}) self.assertQuerysetEqual(authors, [' John '], lambda a: a.name) finally: CharField._unregister_lookup(transform)
def test_func_transform_bilateral(self): class UpperBilateral(Upper): bilateral = True try: CharField.register_lookup(UpperBilateral, 'upper') Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Rhonda') authors = Author.objects.filter(name__upper__exact='john smith') self.assertQuerysetEqual( authors.order_by('name'), [ 'John Smith', ], lambda a: a.name ) finally: CharField._unregister_lookup(UpperBilateral, 'upper')
def test_ord_transform(self): try: CharField.register_lookup(Ord, 'ord') Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Élena Jordan', alias='elena') Author.objects.create(name='Rhonda') authors = Author.objects.annotate(first_initial=Left('name', 1)) self.assertQuerysetEqual( authors.filter(first_initial__ord=ord('J')), ['John Smith'], lambda x: x.name) self.assertQuerysetEqual( authors.exclude(first_initial__ord=ord('J')), ['Élena Jordan', 'Rhonda'], lambda x: x.name, ordered=False, ) finally: CharField._unregister_lookup(Ord, 'ord')
def uninstall_if_needed(setting, value, enter, **kwargs): """ Undo the effects of PostgresConfig.ready() when django.contrib.postgres is "uninstalled" by override_settings(). """ if (not enter and setting == "INSTALLED_APPS" and "django.contrib.postgres" not in set(value)): connection_created.disconnect(register_type_handlers) CharField._unregister_lookup(Unaccent) TextField._unregister_lookup(Unaccent) CharField._unregister_lookup(SearchLookup) TextField._unregister_lookup(SearchLookup) CharField._unregister_lookup(TrigramSimilar) TextField._unregister_lookup(TrigramSimilar) CharField._unregister_lookup(TrigramWordSimilar) TextField._unregister_lookup(TrigramWordSimilar) CharField._unregister_lookup(TrigramStrictWordSimilar) TextField._unregister_lookup(TrigramStrictWordSimilar) # Disconnect this receiver until the next time this app is installed # and ready() connects it again to prevent unnecessary processing on # each setting change. setting_changed.disconnect(uninstall_if_needed) MigrationWriter.unregister_serializer(RANGE_TYPES)
def uninstall_if_needed(setting, value, enter, **kwargs): """ Undo the effects of PostgresConfig.ready() when django.contrib.postgres is "uninstalled" by override_settings(). """ if not enter and setting == 'INSTALLED_APPS' and 'django.contrib.postgres' not in set(value): connection_created.disconnect(register_type_handlers) CharField._unregister_lookup(Unaccent) TextField._unregister_lookup(Unaccent) CharField._unregister_lookup(SearchLookup) TextField._unregister_lookup(SearchLookup) CharField._unregister_lookup(TrigramSimilar) TextField._unregister_lookup(TrigramSimilar) # Disconnect this receiver until the next time this app is installed # and ready() connects it again to prevent unnecessary processing on # each setting change. setting_changed.disconnect(uninstall_if_needed)