-
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.
feat: added unit test with coverage of 68% (#611)
Add unit tests for many spanner_django modules that add coverage beyond the built-in django tests.
- Loading branch information
Showing
14 changed files
with
1,057 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
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.settings") | ||
|
||
|
||
# `pytest` automatically calls this function once when tests are run. | ||
def pytest_configure(): | ||
settings.DEBUG = False | ||
django.setup() |
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,46 @@ | ||
# 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 | ||
|
||
DEBUG = True | ||
USE_TZ = True | ||
|
||
INSTALLED_APPS = [ | ||
"django_spanner", # Must be the first entry | ||
"django.contrib.contenttypes", | ||
"django.contrib.auth", | ||
"django.contrib.sites", | ||
"django.contrib.sessions", | ||
"django.contrib.messages", | ||
"django.contrib.staticfiles", | ||
"tests", | ||
] | ||
|
||
TIME_ZONE = "UTC" | ||
|
||
DATABASES = { | ||
"default": { | ||
"ENGINE": "django_spanner", | ||
"PROJECT": "emulator-local", | ||
"INSTANCE": "django-test-instance", | ||
"NAME": "django-test-db", | ||
} | ||
} | ||
SECRET_KEY = "spanner emulator secret key" | ||
|
||
PASSWORD_HASHERS = [ | ||
"django.contrib.auth.hashers.MD5PasswordHasher", | ||
] | ||
|
||
SITE_ID = 1 | ||
|
||
CONN_MAX_AGE = 60 | ||
|
||
ENGINE = "django_spanner" | ||
PROJECT = "emulator-local" | ||
INSTANCE = "django-test-instance" | ||
NAME = "django-test-db" | ||
OPTIONS = {} | ||
AUTOCOMMIT = True |
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,61 @@ | ||
# 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 for testing django-spanner code. | ||
""" | ||
from django.db import models | ||
|
||
|
||
# Register transformations for model fields. | ||
class UpperCase(models.Transform): | ||
lookup_name = "upper" | ||
function = "UPPER" | ||
bilateral = True | ||
|
||
|
||
models.CharField.register_lookup(UpperCase) | ||
models.TextField.register_lookup(UpperCase) | ||
|
||
|
||
# Models | ||
class ModelDecimalField(models.Model): | ||
field = models.DecimalField() | ||
|
||
|
||
class ModelCharField(models.Model): | ||
field = models.CharField() | ||
|
||
|
||
class Item(models.Model): | ||
item_id = models.IntegerField() | ||
name = models.CharField(max_length=10) | ||
created = models.DateTimeField() | ||
modified = models.DateTimeField(blank=True, null=True) | ||
|
||
class Meta: | ||
ordering = ["name"] | ||
|
||
|
||
class Number(models.Model): | ||
num = models.IntegerField() | ||
decimal_num = models.DecimalField(max_digits=5, decimal_places=2) | ||
item = models.ForeignKey(Item, models.CASCADE) | ||
|
||
|
||
class Author(models.Model): | ||
name = models.CharField(max_length=40) | ||
last_name = models.CharField(max_length=40) | ||
num = models.IntegerField(unique=True) | ||
created = models.DateTimeField() | ||
modified = models.DateTimeField(blank=True, null=True) | ||
|
||
|
||
class Report(models.Model): | ||
name = models.CharField(max_length=10) | ||
creator = models.ForeignKey(Author, models.CASCADE, null=True) | ||
|
||
class Meta: | ||
ordering = ["name"] |
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,33 @@ | ||
# 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 django_spanner.client import DatabaseClient | ||
from django_spanner.base import DatabaseWrapper | ||
from django_spanner.operations import DatabaseOperations | ||
from unittest import TestCase | ||
import os | ||
|
||
|
||
class SpannerSimpleTestClass(TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] | ||
|
||
cls.INSTANCE_ID = "instance_id" | ||
cls.DATABASE_ID = "database_id" | ||
cls.USER_AGENT = "django_spanner/2.2.0a1" | ||
cls.OPTIONS = {"option": "dummy"} | ||
|
||
cls.settings_dict = { | ||
"PROJECT": cls.PROJECT, | ||
"INSTANCE": cls.INSTANCE_ID, | ||
"NAME": cls.DATABASE_ID, | ||
"user_agent": cls.USER_AGENT, | ||
"OPTIONS": cls.OPTIONS, | ||
} | ||
cls.db_client = DatabaseClient(cls.settings_dict) | ||
cls.db_wrapper = cls.connection = DatabaseWrapper(cls.settings_dict) | ||
cls.db_operations = DatabaseOperations(cls.connection) |
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
Oops, something went wrong.