-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
413 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
import os | ||
import django | ||
from django.conf import settings | ||
|
||
# We manually designate which settings we will be using in an environment | ||
# variable. This is similar to what occurs in the `manage.py` file. | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.system.settings") | ||
|
||
|
||
# `pytest` automatically calls this function once when tests are run. | ||
def pytest_configure(): | ||
settings.DEBUG = False | ||
django.setup() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
""" | ||
Different models used by system tests in django-spanner code. | ||
""" | ||
from django.db import models | ||
|
||
|
||
class Author(models.Model): | ||
first_name = models.CharField(max_length=20) | ||
last_name = models.CharField(max_length=20) | ||
ratting = models.DecimalField() | ||
|
||
|
||
class Number(models.Model): | ||
num = models.DecimalField() | ||
|
||
def __str__(self): | ||
return str(self.num) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
from .models import Author, Number | ||
from django.test import TransactionTestCase | ||
from django.db import connection, ProgrammingError | ||
from django.db.utils import IntegrityError | ||
from decimal import Decimal | ||
from tests.system.django_spanner.utils import ( | ||
setup_instance, | ||
teardown_instance, | ||
setup_database, | ||
teardown_database, | ||
USE_EMULATOR, | ||
) | ||
|
||
|
||
class TestDecimal(TransactionTestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
setup_instance() | ||
setup_database() | ||
with connection.schema_editor() as editor: | ||
# Create the tables | ||
editor.create_model(Author) | ||
editor.create_model(Number) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
with connection.schema_editor() as editor: | ||
# delete the table | ||
editor.delete_model(Author) | ||
editor.delete_model(Number) | ||
teardown_database() | ||
teardown_instance() | ||
|
||
def ratting_transform(self, value): | ||
return value["ratting"] | ||
|
||
def values_transform(self, value): | ||
return value.num | ||
|
||
def assertValuesEqual( | ||
self, queryset, expected_values, transformer, ordered=True | ||
): | ||
self.assertQuerysetEqual( | ||
queryset, expected_values, transformer, ordered | ||
) | ||
|
||
def test_insert_and_search_decimal_value(self): | ||
""" | ||
Tests model object creation with Author model. | ||
""" | ||
author_kent = Author( | ||
first_name="Arthur", last_name="Kent", ratting=Decimal("4.1"), | ||
) | ||
author_kent.save() | ||
qs1 = Author.objects.filter(ratting__gte=3).values("ratting") | ||
self.assertValuesEqual( | ||
qs1, [Decimal("4.1")], self.ratting_transform, | ||
) | ||
# Delete data from Author table. | ||
Author.objects.all().delete() | ||
|
||
def test_decimal_filter(self): | ||
""" | ||
Tests decimal filter query. | ||
""" | ||
# Insert data into Number table. | ||
Number.objects.bulk_create( | ||
Number(num=Decimal(i) / Decimal(10)) for i in range(10) | ||
) | ||
qs1 = Number.objects.filter(num__lte=Decimal(2) / Decimal(10)) | ||
self.assertValuesEqual( | ||
qs1, | ||
[Decimal(i) / Decimal(10) for i in range(3)], | ||
self.values_transform, | ||
ordered=False, | ||
) | ||
# Delete data from Number table. | ||
Number.objects.all().delete() | ||
|
||
def test_decimal_precision_limit(self): | ||
""" | ||
Tests decimal object precission limit. | ||
""" | ||
num_val = Number(num=Decimal(1) / Decimal(3)) | ||
if USE_EMULATOR: | ||
msg = "The NUMERIC type supports 38 digits of precision and 9 digits of scale." | ||
with self.assertRaisesRegex(IntegrityError, msg): | ||
num_val.save() | ||
else: | ||
msg = "400 Invalid value for bind parameter a0: Expected NUMERIC." | ||
with self.assertRaisesRegex(ProgrammingError, msg): | ||
num_val.save() | ||
|
||
def test_decimal_update(self): | ||
""" | ||
Tests decimal object update. | ||
""" | ||
author_kent = Author( | ||
first_name="Arthur", last_name="Kent", ratting=Decimal("4.1"), | ||
) | ||
author_kent.save() | ||
author_kent.ratting = Decimal("4.2") | ||
author_kent.save() | ||
qs1 = Author.objects.filter(ratting__gte=Decimal("4.2")).values( | ||
"ratting" | ||
) | ||
self.assertValuesEqual( | ||
qs1, [Decimal("4.2")], self.ratting_transform, | ||
) | ||
# Delete data from Author table. | ||
Author.objects.all().delete() |
Oops, something went wrong.