diff options
| author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
| commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
| tree | f0506b668a013d0063e5fba3dbf4863b466713ba /tests/db_functions/comparison | |
| parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'tests/db_functions/comparison')
| -rw-r--r-- | tests/db_functions/comparison/test_cast.py | 115 | ||||
| -rw-r--r-- | tests/db_functions/comparison/test_coalesce.py | 61 | ||||
| -rw-r--r-- | tests/db_functions/comparison/test_collate.py | 36 | ||||
| -rw-r--r-- | tests/db_functions/comparison/test_greatest.py | 69 | ||||
| -rw-r--r-- | tests/db_functions/comparison/test_json_object.py | 105 | ||||
| -rw-r--r-- | tests/db_functions/comparison/test_least.py | 61 | ||||
| -rw-r--r-- | tests/db_functions/comparison/test_nullif.py | 40 |
7 files changed, 274 insertions, 213 deletions
diff --git a/tests/db_functions/comparison/test_cast.py b/tests/db_functions/comparison/test_cast.py index ffba8af316..afb6b473b9 100644 --- a/tests/db_functions/comparison/test_cast.py +++ b/tests/db_functions/comparison/test_cast.py @@ -13,40 +13,52 @@ from ..models import Author, DTModel, Fan, FloatModel class CastTests(TestCase): @classmethod def setUpTestData(self): - Author.objects.create(name='Bob', age=1, alias='1') + Author.objects.create(name="Bob", age=1, alias="1") def test_cast_from_value(self): - numbers = Author.objects.annotate(cast_integer=Cast(models.Value('0'), models.IntegerField())) + numbers = Author.objects.annotate( + cast_integer=Cast(models.Value("0"), models.IntegerField()) + ) self.assertEqual(numbers.get().cast_integer, 0) def test_cast_from_field(self): - numbers = Author.objects.annotate(cast_string=Cast('age', models.CharField(max_length=255)),) - self.assertEqual(numbers.get().cast_string, '1') + numbers = Author.objects.annotate( + cast_string=Cast("age", models.CharField(max_length=255)), + ) + self.assertEqual(numbers.get().cast_string, "1") def test_cast_to_char_field_without_max_length(self): - numbers = Author.objects.annotate(cast_string=Cast('age', models.CharField())) - self.assertEqual(numbers.get().cast_string, '1') + numbers = Author.objects.annotate(cast_string=Cast("age", models.CharField())) + self.assertEqual(numbers.get().cast_string, "1") # Silence "Truncated incorrect CHAR(1) value: 'Bob'". - @ignore_warnings(module='django.db.backends.mysql.base') - @skipUnlessDBFeature('supports_cast_with_precision') + @ignore_warnings(module="django.db.backends.mysql.base") + @skipUnlessDBFeature("supports_cast_with_precision") def test_cast_to_char_field_with_max_length(self): - names = Author.objects.annotate(cast_string=Cast('name', models.CharField(max_length=1))) - self.assertEqual(names.get().cast_string, 'B') + names = Author.objects.annotate( + cast_string=Cast("name", models.CharField(max_length=1)) + ) + self.assertEqual(names.get().cast_string, "B") - @skipUnlessDBFeature('supports_cast_with_precision') + @skipUnlessDBFeature("supports_cast_with_precision") def test_cast_to_decimal_field(self): FloatModel.objects.create(f1=-1.934, f2=3.467) float_obj = FloatModel.objects.annotate( - cast_f1_decimal=Cast('f1', models.DecimalField(max_digits=8, decimal_places=2)), - cast_f2_decimal=Cast('f2', models.DecimalField(max_digits=8, decimal_places=1)), + cast_f1_decimal=Cast( + "f1", models.DecimalField(max_digits=8, decimal_places=2) + ), + cast_f2_decimal=Cast( + "f2", models.DecimalField(max_digits=8, decimal_places=1) + ), ).get() - self.assertEqual(float_obj.cast_f1_decimal, decimal.Decimal('-1.93')) - self.assertEqual(float_obj.cast_f2_decimal, decimal.Decimal('3.5')) + self.assertEqual(float_obj.cast_f1_decimal, decimal.Decimal("-1.93")) + self.assertEqual(float_obj.cast_f2_decimal, decimal.Decimal("3.5")) author_obj = Author.objects.annotate( - cast_alias_decimal=Cast('alias', models.DecimalField(max_digits=8, decimal_places=2)), + cast_alias_decimal=Cast( + "alias", models.DecimalField(max_digits=8, decimal_places=2) + ), ).get() - self.assertEqual(author_obj.cast_alias_decimal, decimal.Decimal('1')) + self.assertEqual(author_obj.cast_alias_decimal, decimal.Decimal("1")) def test_cast_to_integer(self): for field_class in ( @@ -61,14 +73,14 @@ class CastTests(TestCase): models.PositiveSmallIntegerField, ): with self.subTest(field_class=field_class): - numbers = Author.objects.annotate(cast_int=Cast('alias', field_class())) + numbers = Author.objects.annotate(cast_int=Cast("alias", field_class())) self.assertEqual(numbers.get().cast_int, 1) def test_cast_to_duration(self): duration = datetime.timedelta(days=1, seconds=2, microseconds=3) DTModel.objects.create(duration=duration) dtm = DTModel.objects.annotate( - cast_duration=Cast('duration', models.DurationField()), + cast_duration=Cast("duration", models.DurationField()), cast_neg_duration=Cast(-duration, models.DurationField()), ).get() self.assertEqual(dtm.cast_duration, duration) @@ -78,7 +90,7 @@ class CastTests(TestCase): dt_value = datetime.datetime(2018, 9, 28, 12, 42, 10, 234567) DTModel.objects.create(start_datetime=dt_value) dtm = DTModel.objects.annotate( - start_datetime_as_date=Cast('start_datetime', models.DateField()) + start_datetime_as_date=Cast("start_datetime", models.DateField()) ).first() self.assertEqual(dtm.start_datetime_as_date, datetime.date(2018, 9, 28)) @@ -86,27 +98,39 @@ class CastTests(TestCase): dt_value = datetime.datetime(2018, 9, 28, 12, 42, 10, 234567) DTModel.objects.create(start_datetime=dt_value) dtm = DTModel.objects.annotate( - start_datetime_as_time=Cast('start_datetime', models.TimeField()) + start_datetime_as_time=Cast("start_datetime", models.TimeField()) ).first() - rounded_ms = int(round(.234567, connection.features.time_cast_precision) * 10**6) - self.assertEqual(dtm.start_datetime_as_time, datetime.time(12, 42, 10, rounded_ms)) + rounded_ms = int( + round(0.234567, connection.features.time_cast_precision) * 10**6 + ) + self.assertEqual( + dtm.start_datetime_as_time, datetime.time(12, 42, 10, rounded_ms) + ) def test_cast_from_db_date_to_datetime(self): dt_value = datetime.date(2018, 9, 28) DTModel.objects.create(start_date=dt_value) - dtm = DTModel.objects.annotate(start_as_datetime=Cast('start_date', models.DateTimeField())).first() - self.assertEqual(dtm.start_as_datetime, datetime.datetime(2018, 9, 28, 0, 0, 0, 0)) + dtm = DTModel.objects.annotate( + start_as_datetime=Cast("start_date", models.DateTimeField()) + ).first() + self.assertEqual( + dtm.start_as_datetime, datetime.datetime(2018, 9, 28, 0, 0, 0, 0) + ) def test_cast_from_db_datetime_to_date_group_by(self): - author = Author.objects.create(name='John Smith', age=45) + author = Author.objects.create(name="John Smith", age=45) dt_value = datetime.datetime(2018, 9, 28, 12, 42, 10, 234567) - Fan.objects.create(name='Margaret', age=50, author=author, fan_since=dt_value) - fans = Fan.objects.values('author').annotate( - fan_for_day=Cast('fan_since', models.DateField()), - fans=models.Count('*') - ).values() - self.assertEqual(fans[0]['fan_for_day'], datetime.date(2018, 9, 28)) - self.assertEqual(fans[0]['fans'], 1) + Fan.objects.create(name="Margaret", age=50, author=author, fan_since=dt_value) + fans = ( + Fan.objects.values("author") + .annotate( + fan_for_day=Cast("fan_since", models.DateField()), + fans=models.Count("*"), + ) + .values() + ) + self.assertEqual(fans[0]["fan_for_day"], datetime.date(2018, 9, 28)) + self.assertEqual(fans[0]["fans"], 1) def test_cast_from_python_to_date(self): today = datetime.date.today() @@ -117,30 +141,39 @@ class CastTests(TestCase): now = datetime.datetime.now() dates = Author.objects.annotate(cast_datetime=Cast(now, models.DateTimeField())) time_precision = datetime.timedelta( - microseconds=10**(6 - connection.features.time_cast_precision) + microseconds=10 ** (6 - connection.features.time_cast_precision) ) self.assertAlmostEqual(dates.get().cast_datetime, now, delta=time_precision) def test_cast_from_python(self): - numbers = Author.objects.annotate(cast_float=Cast(decimal.Decimal(0.125), models.FloatField())) + numbers = Author.objects.annotate( + cast_float=Cast(decimal.Decimal(0.125), models.FloatField()) + ) cast_float = numbers.get().cast_float self.assertIsInstance(cast_float, float) self.assertEqual(cast_float, 0.125) - @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL test') + @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL test") def test_expression_wrapped_with_parentheses_on_postgresql(self): """ The SQL for the Cast expression is wrapped with parentheses in case it's a complex expression. """ with CaptureQueriesContext(connection) as captured_queries: - list(Author.objects.annotate( - cast_float=Cast(models.Avg('age'), models.FloatField()), - )) + list( + Author.objects.annotate( + cast_float=Cast(models.Avg("age"), models.FloatField()), + ) + ) self.assertIn( '(AVG("db_functions_author"."age"))::double precision', - captured_queries[0]['sql'], + captured_queries[0]["sql"], ) def test_cast_to_text_field(self): - self.assertEqual(Author.objects.values_list(Cast('age', models.TextField()), flat=True).get(), '1') + self.assertEqual( + Author.objects.values_list( + Cast("age", models.TextField()), flat=True + ).get(), + "1", + ) diff --git a/tests/db_functions/comparison/test_coalesce.py b/tests/db_functions/comparison/test_coalesce.py index 1093079d68..9125923847 100644 --- a/tests/db_functions/comparison/test_coalesce.py +++ b/tests/db_functions/comparison/test_coalesce.py @@ -11,25 +11,25 @@ lorem_ipsum = """ class CoalesceTests(TestCase): - def test_basic(self): - Author.objects.create(name='John Smith', alias='smithj') - Author.objects.create(name='Rhonda') - authors = Author.objects.annotate(display_name=Coalesce('alias', 'name')) + Author.objects.create(name="John Smith", alias="smithj") + Author.objects.create(name="Rhonda") + authors = Author.objects.annotate(display_name=Coalesce("alias", "name")) self.assertQuerysetEqual( - authors.order_by('name'), ['smithj', 'Rhonda'], - lambda a: a.display_name + authors.order_by("name"), ["smithj", "Rhonda"], lambda a: a.display_name ) def test_gt_two_expressions(self): - with self.assertRaisesMessage(ValueError, 'Coalesce must take at least two expressions'): - Author.objects.annotate(display_name=Coalesce('alias')) + with self.assertRaisesMessage( + ValueError, "Coalesce must take at least two expressions" + ): + Author.objects.annotate(display_name=Coalesce("alias")) def test_mixed_values(self): - a1 = Author.objects.create(name='John Smith', alias='smithj') - a2 = Author.objects.create(name='Rhonda') + a1 = Author.objects.create(name="John Smith", alias="smithj") + a2 = Author.objects.create(name="Rhonda") ar1 = Article.objects.create( - title='How to Django', + title="How to Django", text=lorem_ipsum, written=timezone.now(), ) @@ -37,42 +37,33 @@ class CoalesceTests(TestCase): ar1.authors.add(a2) # mixed Text and Char article = Article.objects.annotate( - headline=Coalesce('summary', 'text', output_field=TextField()), + headline=Coalesce("summary", "text", output_field=TextField()), ) self.assertQuerysetEqual( - article.order_by('title'), [lorem_ipsum], - lambda a: a.headline + article.order_by("title"), [lorem_ipsum], lambda a: a.headline ) # mixed Text and Char wrapped article = Article.objects.annotate( - headline=Coalesce(Lower('summary'), Lower('text'), output_field=TextField()), + headline=Coalesce( + Lower("summary"), Lower("text"), output_field=TextField() + ), ) self.assertQuerysetEqual( - article.order_by('title'), [lorem_ipsum.lower()], - lambda a: a.headline + article.order_by("title"), [lorem_ipsum.lower()], lambda a: a.headline ) def test_ordering(self): - Author.objects.create(name='John Smith', alias='smithj') - Author.objects.create(name='Rhonda') - authors = Author.objects.order_by(Coalesce('alias', 'name')) - self.assertQuerysetEqual( - authors, ['Rhonda', 'John Smith'], - lambda a: a.name - ) - authors = Author.objects.order_by(Coalesce('alias', 'name').asc()) - self.assertQuerysetEqual( - authors, ['Rhonda', 'John Smith'], - lambda a: a.name - ) - authors = Author.objects.order_by(Coalesce('alias', 'name').desc()) - self.assertQuerysetEqual( - authors, ['John Smith', 'Rhonda'], - lambda a: a.name - ) + Author.objects.create(name="John Smith", alias="smithj") + Author.objects.create(name="Rhonda") + authors = Author.objects.order_by(Coalesce("alias", "name")) + self.assertQuerysetEqual(authors, ["Rhonda", "John Smith"], lambda a: a.name) + authors = Author.objects.order_by(Coalesce("alias", "name").asc()) + self.assertQuerysetEqual(authors, ["Rhonda", "John Smith"], lambda a: a.name) + authors = Author.objects.order_by(Coalesce("alias", "name").desc()) + self.assertQuerysetEqual(authors, ["John Smith", "Rhonda"], lambda a: a.name) def test_empty_queryset(self): - Author.objects.create(name='John Smith') + Author.objects.create(name="John Smith") tests = [ Author.objects.none(), Subquery(Author.objects.none()), diff --git a/tests/db_functions/comparison/test_collate.py b/tests/db_functions/comparison/test_collate.py index b680ed9a05..25f55f6b2d 100644 --- a/tests/db_functions/comparison/test_collate.py +++ b/tests/db_functions/comparison/test_collate.py @@ -9,35 +9,31 @@ from ..models import Author class CollateTests(TestCase): @classmethod def setUpTestData(cls): - cls.author1 = Author.objects.create(alias='a', name='Jones 1') - cls.author2 = Author.objects.create(alias='A', name='Jones 2') + cls.author1 = Author.objects.create(alias="a", name="Jones 1") + cls.author2 = Author.objects.create(alias="A", name="Jones 2") def test_collate_filter_ci(self): - collation = connection.features.test_collations.get('ci') + collation = connection.features.test_collations.get("ci") if not collation: - self.skipTest( - 'This backend does not support case-insensitive collations.' - ) - qs = Author.objects.filter(alias=Collate(Value('a'), collation)) + self.skipTest("This backend does not support case-insensitive collations.") + qs = Author.objects.filter(alias=Collate(Value("a"), collation)) self.assertEqual(qs.count(), 2) def test_collate_order_by_cs(self): - collation = connection.features.test_collations.get('cs') + collation = connection.features.test_collations.get("cs") if not collation: - self.skipTest( - 'This backend does not support case-sensitive collations.' - ) - qs = Author.objects.order_by(Collate('alias', collation)) + self.skipTest("This backend does not support case-sensitive collations.") + qs = Author.objects.order_by(Collate("alias", collation)) self.assertSequenceEqual(qs, [self.author2, self.author1]) def test_language_collation_order_by(self): - collation = connection.features.test_collations.get('swedish_ci') + collation = connection.features.test_collations.get("swedish_ci") if not collation: - self.skipTest('This backend does not support language collations.') - author3 = Author.objects.create(alias='O', name='Jones') - author4 = Author.objects.create(alias='Ö', name='Jones') - author5 = Author.objects.create(alias='P', name='Jones') - qs = Author.objects.order_by(Collate(F('alias'), collation), 'name') + self.skipTest("This backend does not support language collations.") + author3 = Author.objects.create(alias="O", name="Jones") + author4 = Author.objects.create(alias="Ö", name="Jones") + author5 = Author.objects.create(alias="P", name="Jones") + qs = Author.objects.order_by(Collate(F("alias"), collation), "name") self.assertSequenceEqual( qs, [self.author1, self.author2, author3, author5, author4], @@ -46,11 +42,11 @@ class CollateTests(TestCase): def test_invalid_collation(self): tests = [ None, - '', + "", 'et-x-icu" OR ', '"schema"."collation"', ] msg = "Invalid collation name: %r." for value in tests: with self.subTest(value), self.assertRaisesMessage(ValueError, msg % value): - Collate(F('alias'), value) + Collate(F("alias"), value) diff --git a/tests/db_functions/comparison/test_greatest.py b/tests/db_functions/comparison/test_greatest.py index f11e7b824c..c37514adf7 100644 --- a/tests/db_functions/comparison/test_greatest.py +++ b/tests/db_functions/comparison/test_greatest.py @@ -12,79 +12,90 @@ from ..models import Article, Author, DecimalModel, Fan class GreatestTests(TestCase): - def test_basic(self): now = timezone.now() before = now - timedelta(hours=1) - Article.objects.create(title='Testing with Django', written=before, published=now) - articles = Article.objects.annotate(last_updated=Greatest('written', 'published')) + Article.objects.create( + title="Testing with Django", written=before, published=now + ) + articles = Article.objects.annotate( + last_updated=Greatest("written", "published") + ) self.assertEqual(articles.first().last_updated, now) - @skipUnlessDBFeature('greatest_least_ignores_nulls') + @skipUnlessDBFeature("greatest_least_ignores_nulls") def test_ignores_null(self): now = timezone.now() - Article.objects.create(title='Testing with Django', written=now) - articles = Article.objects.annotate(last_updated=Greatest('written', 'published')) + Article.objects.create(title="Testing with Django", written=now) + articles = Article.objects.annotate( + last_updated=Greatest("written", "published") + ) self.assertEqual(articles.first().last_updated, now) - @skipIfDBFeature('greatest_least_ignores_nulls') + @skipIfDBFeature("greatest_least_ignores_nulls") def test_propagates_null(self): - Article.objects.create(title='Testing with Django', written=timezone.now()) - articles = Article.objects.annotate(last_updated=Greatest('written', 'published')) + Article.objects.create(title="Testing with Django", written=timezone.now()) + articles = Article.objects.annotate( + last_updated=Greatest("written", "published") + ) self.assertIsNone(articles.first().last_updated) def test_coalesce_workaround(self): past = datetime(1900, 1, 1) now = timezone.now() - Article.objects.create(title='Testing with Django', written=now) + Article.objects.create(title="Testing with Django", written=now) articles = Article.objects.annotate( last_updated=Greatest( - Coalesce('written', past), - Coalesce('published', past), + Coalesce("written", past), + Coalesce("published", past), ), ) self.assertEqual(articles.first().last_updated, now) - @skipUnless(connection.vendor == 'mysql', "MySQL-specific workaround") + @skipUnless(connection.vendor == "mysql", "MySQL-specific workaround") def test_coalesce_workaround_mysql(self): past = datetime(1900, 1, 1) now = timezone.now() - Article.objects.create(title='Testing with Django', written=now) + Article.objects.create(title="Testing with Django", written=now) past_sql = RawSQL("cast(%s as datetime)", (past,)) articles = Article.objects.annotate( last_updated=Greatest( - Coalesce('written', past_sql), - Coalesce('published', past_sql), + Coalesce("written", past_sql), + Coalesce("published", past_sql), ), ) self.assertEqual(articles.first().last_updated, now) def test_all_null(self): - Article.objects.create(title='Testing with Django', written=timezone.now()) - articles = Article.objects.annotate(last_updated=Greatest('published', 'updated')) + Article.objects.create(title="Testing with Django", written=timezone.now()) + articles = Article.objects.annotate( + last_updated=Greatest("published", "updated") + ) self.assertIsNone(articles.first().last_updated) def test_one_expressions(self): - with self.assertRaisesMessage(ValueError, 'Greatest must take at least two expressions'): - Greatest('written') + with self.assertRaisesMessage( + ValueError, "Greatest must take at least two expressions" + ): + Greatest("written") def test_related_field(self): - author = Author.objects.create(name='John Smith', age=45) - Fan.objects.create(name='Margaret', age=50, author=author) - authors = Author.objects.annotate(highest_age=Greatest('age', 'fans__age')) + author = Author.objects.create(name="John Smith", age=45) + Fan.objects.create(name="Margaret", age=50, author=author) + authors = Author.objects.annotate(highest_age=Greatest("age", "fans__age")) self.assertEqual(authors.first().highest_age, 50) def test_update(self): - author = Author.objects.create(name='James Smith', goes_by='Jim') - Author.objects.update(alias=Greatest('name', 'goes_by')) + author = Author.objects.create(name="James Smith", goes_by="Jim") + Author.objects.update(alias=Greatest("name", "goes_by")) author.refresh_from_db() - self.assertEqual(author.alias, 'Jim') + self.assertEqual(author.alias, "Jim") def test_decimal_filter(self): - obj = DecimalModel.objects.create(n1=Decimal('1.1'), n2=Decimal('1.2')) + obj = DecimalModel.objects.create(n1=Decimal("1.1"), n2=Decimal("1.2")) self.assertCountEqual( DecimalModel.objects.annotate( - greatest=Greatest('n1', 'n2'), - ).filter(greatest=Decimal('1.2')), + greatest=Greatest("n1", "n2"), + ).filter(greatest=Decimal("1.2")), [obj], ) diff --git a/tests/db_functions/comparison/test_json_object.py b/tests/db_functions/comparison/test_json_object.py index 2c7c1beae9..7a10657317 100644 --- a/tests/db_functions/comparison/test_json_object.py +++ b/tests/db_functions/comparison/test_json_object.py @@ -8,75 +8,90 @@ from django.utils import timezone from ..models import Article, Author -@skipUnlessDBFeature('has_json_object_function') +@skipUnlessDBFeature("has_json_object_function") class JSONObjectTests(TestCase): @classmethod def setUpTestData(cls): - Author.objects.create(name='Ivan Ivanov', alias='iivanov') + Author.objects.create(name="Ivan Ivanov", alias="iivanov") def test_empty(self): obj = Author.objects.annotate(json_object=JSONObject()).first() self.assertEqual(obj.json_object, {}) def test_basic(self): - obj = Author.objects.annotate(json_object=JSONObject(name='name')).first() - self.assertEqual(obj.json_object, {'name': 'Ivan Ivanov'}) + obj = Author.objects.annotate(json_object=JSONObject(name="name")).first() + self.assertEqual(obj.json_object, {"name": "Ivan Ivanov"}) def test_expressions(self): - obj = Author.objects.annotate(json_object=JSONObject( - name=Lower('name'), - alias='alias', - goes_by='goes_by', - salary=Value(30000.15), - age=F('age') * 2, - )).first() - self.assertEqual(obj.json_object, { - 'name': 'ivan ivanov', - 'alias': 'iivanov', - 'goes_by': None, - 'salary': 30000.15, - 'age': 60, - }) + obj = Author.objects.annotate( + json_object=JSONObject( + name=Lower("name"), + alias="alias", + goes_by="goes_by", + salary=Value(30000.15), + age=F("age") * 2, + ) + ).first() + self.assertEqual( + obj.json_object, + { + "name": "ivan ivanov", + "alias": "iivanov", + "goes_by": None, + "salary": 30000.15, + "age": 60, + }, + ) def test_nested_json_object(self): - obj = Author.objects.annotate(json_object=JSONObject( - name='name', - nested_json_object=JSONObject( - alias='alias', - age='age', - ), - )).first() - self.assertEqual(obj.json_object, { - 'name': 'Ivan Ivanov', - 'nested_json_object': { - 'alias': 'iivanov', - 'age': 30, + obj = Author.objects.annotate( + json_object=JSONObject( + name="name", + nested_json_object=JSONObject( + alias="alias", + age="age", + ), + ) + ).first() + self.assertEqual( + obj.json_object, + { + "name": "Ivan Ivanov", + "nested_json_object": { + "alias": "iivanov", + "age": 30, + }, }, - }) + ) def test_nested_empty_json_object(self): - obj = Author.objects.annotate(json_object=JSONObject( - name='name', - nested_json_object=JSONObject(), - )).first() - self.assertEqual(obj.json_object, { - 'name': 'Ivan Ivanov', - 'nested_json_object': {}, - }) + obj = Author.objects.annotate( + json_object=JSONObject( + name="name", + nested_json_object=JSONObject(), + ) + ).first() + self.assertEqual( + obj.json_object, + { + "name": "Ivan Ivanov", + "nested_json_object": {}, + }, + ) def test_textfield(self): Article.objects.create( - title='The Title', - text='x' * 4000, + title="The Title", + text="x" * 4000, written=timezone.now(), ) - obj = Article.objects.annotate(json_object=JSONObject(text=F('text'))).first() - self.assertEqual(obj.json_object, {'text': 'x' * 4000}) + obj = Article.objects.annotate(json_object=JSONObject(text=F("text"))).first() + self.assertEqual(obj.json_object, {"text": "x" * 4000}) -@skipIfDBFeature('has_json_object_function') +@skipIfDBFeature("has_json_object_function") class JSONObjectNotSupportedTests(TestCase): def test_not_supported(self): - msg = 'JSONObject() is not supported on this database backend.' + msg = "JSONObject() is not supported on this database backend." with self.assertRaisesMessage(NotSupportedError, msg): Author.objects.annotate(json_object=JSONObject()).get() diff --git a/tests/db_functions/comparison/test_least.py b/tests/db_functions/comparison/test_least.py index e0318e25c6..eb7514187a 100644 --- a/tests/db_functions/comparison/test_least.py +++ b/tests/db_functions/comparison/test_least.py @@ -12,81 +12,84 @@ from ..models import Article, Author, DecimalModel, Fan class LeastTests(TestCase): - def test_basic(self): now = timezone.now() before = now - timedelta(hours=1) - Article.objects.create(title='Testing with Django', written=before, published=now) - articles = Article.objects.annotate(first_updated=Least('written', 'published')) + Article.objects.create( + title="Testing with Django", written=before, published=now + ) + articles = Article.objects.annotate(first_updated=Least("written", "published")) self.assertEqual(articles.first().first_updated, before) - @skipUnlessDBFeature('greatest_least_ignores_nulls') + @skipUnlessDBFeature("greatest_least_ignores_nulls") def test_ignores_null(self): now = timezone.now() - Article.objects.create(title='Testing with Django', written=now) + Article.objects.create(title="Testing with Django", written=now) articles = Article.objects.annotate( - first_updated=Least('written', 'published'), + first_updated=Least("written", "published"), ) self.assertEqual(articles.first().first_updated, now) - @skipIfDBFeature('greatest_least_ignores_nulls') + @skipIfDBFeature("greatest_least_ignores_nulls") def test_propagates_null(self): - Article.objects.create(title='Testing with Django', written=timezone.now()) - articles = Article.objects.annotate(first_updated=Least('written', 'published')) + Article.objects.create(title="Testing with Django", written=timezone.now()) + articles = Article.objects.annotate(first_updated=Least("written", "published")) self.assertIsNone(articles.first().first_updated) def test_coalesce_workaround(self): future = datetime(2100, 1, 1) now = timezone.now() - Article.objects.create(title='Testing with Django', written=now) + Article.objects.create(title="Testing with Django", written=now) articles = Article.objects.annotate( last_updated=Least( - Coalesce('written', future), - Coalesce('published', future), + Coalesce("written", future), + Coalesce("published", future), ), ) self.assertEqual(articles.first().last_updated, now) - @skipUnless(connection.vendor == 'mysql', "MySQL-specific workaround") + @skipUnless(connection.vendor == "mysql", "MySQL-specific workaround") def test_coalesce_workaround_mysql(self): future = datetime(2100, 1, 1) now = timezone.now() - Article.objects.create(title='Testing with Django', written=now) + Article.objects.create(title="Testing with Django", written=now) future_sql = RawSQL("cast(%s as datetime)", (future,)) articles = Article.objects.annotate( last_updated=Least( - Coalesce('written', future_sql), - Coalesce('published', future_sql), + Coalesce("written", future_sql), + Coalesce("published", future_sql), ), ) self.assertEqual(articles.first().last_updated, now) def test_all_null(self): - Article.objects.create(title='Testing with Django', written=timezone.now()) - articles = Article.objects.annotate(first_updated=Least('published', 'updated')) + Article.objects.create(title="Testing with Django", written=timezone.now()) + articles = Article.objects.annotate(first_updated=Least("published", "updated")) self.assertIsNone(articles.first().first_updated) def test_one_expressions(self): - with self.assertRaisesMessage(ValueError, 'Least must take at least two expressions'): - Least('written') + with self.assertRaisesMessage( + ValueError, "Least must take at least two expressions" + ): + Least("written") def test_related_field(self): - author = Author.objects.create(name='John Smith', age=45) - Fan.objects.create(name='Margaret', age=50, author=author) - authors = Author.objects.annotate(lowest_age=Least('age', 'fans__age')) + author = Author.objects.create(name="John Smith", age=45) + Fan.objects.create(name="Margaret", age=50, author=author) + authors = Author.objects.annotate(lowest_age=Least("age", "fans__age")) self.assertEqual(authors.first().lowest_age, 45) def test_update(self): - author = Author.objects.create(name='James Smith', goes_by='Jim') - Author.objects.update(alias=Least('name', 'goes_by')) + author = Author.objects.create(name="James Smith", goes_by="Jim") + Author.objects.update(alias=Least("name", "goes_by")) author.refresh_from_db() - self.assertEqual(author.alias, 'James Smith') + self.assertEqual(author.alias, "James Smith") def test_decimal_filter(self): - obj = DecimalModel.objects.create(n1=Decimal('1.1'), n2=Decimal('1.2')) + obj = DecimalModel.objects.create(n1=Decimal("1.1"), n2=Decimal("1.2")) self.assertCountEqual( DecimalModel.objects.annotate( - least=Least('n1', 'n2'), - ).filter(least=Decimal('1.1')), + least=Least("n1", "n2"), + ).filter(least=Decimal("1.1")), [obj], ) diff --git a/tests/db_functions/comparison/test_nullif.py b/tests/db_functions/comparison/test_nullif.py index 36f881ed57..a65885a3ec 100644 --- a/tests/db_functions/comparison/test_nullif.py +++ b/tests/db_functions/comparison/test_nullif.py @@ -9,32 +9,44 @@ from ..models import Author class NullIfTests(TestCase): - @classmethod def setUpTestData(cls): - Author.objects.create(name='John Smith', alias='smithj') - Author.objects.create(name='Rhonda', alias='Rhonda') + Author.objects.create(name="John Smith", alias="smithj") + Author.objects.create(name="Rhonda", alias="Rhonda") def test_basic(self): - authors = Author.objects.annotate(nullif=NullIf('alias', 'name')).values_list('nullif') + authors = Author.objects.annotate(nullif=NullIf("alias", "name")).values_list( + "nullif" + ) self.assertSequenceEqual( - authors, [ - ('smithj',), - ('' if connection.features.interprets_empty_strings_as_nulls else None,) - ] + authors, + [ + ("smithj",), + ( + "" + if connection.features.interprets_empty_strings_as_nulls + else None, + ), + ], ) def test_null_argument(self): - authors = Author.objects.annotate(nullif=NullIf('name', Value(None))).values_list('nullif') - self.assertSequenceEqual(authors, [('John Smith',), ('Rhonda',)]) + authors = Author.objects.annotate( + nullif=NullIf("name", Value(None)) + ).values_list("nullif") + self.assertSequenceEqual(authors, [("John Smith",), ("Rhonda",)]) def test_too_few_args(self): msg = "'NullIf' takes exactly 2 arguments (1 given)" with self.assertRaisesMessage(TypeError, msg): - NullIf('name') + NullIf("name") - @skipUnless(connection.vendor == 'oracle', 'Oracle specific test for NULL-literal') + @skipUnless(connection.vendor == "oracle", "Oracle specific test for NULL-literal") def test_null_literal(self): - msg = 'Oracle does not allow Value(None) for expression1.' + msg = "Oracle does not allow Value(None) for expression1." with self.assertRaisesMessage(ValueError, msg): - list(Author.objects.annotate(nullif=NullIf(Value(None), 'name')).values_list('nullif')) + list( + Author.objects.annotate(nullif=NullIf(Value(None), "name")).values_list( + "nullif" + ) + ) |
