Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 96170b4

Browse filesBrowse files
committed
feat: added test cases
1 parent 129e41e commit 96170b4
Copy full SHA for 96170b4

File tree

Expand file treeCollapse file tree

12 files changed

+1108
-15
lines changed
Filter options
Expand file treeCollapse file tree

12 files changed

+1108
-15
lines changed

‎noxfile.py

Copy file name to clipboardExpand all lines: noxfile.py
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ def lint_setup_py(session):
6666
def default(session):
6767
# Install all test dependencies, then install this package in-place.
6868
session.install(
69-
"django~=2.2", "mock", "mock-import", "pytest", "pytest-cov"
69+
"django~=2.2",
70+
"mock",
71+
"mock-import",
72+
"pytest",
73+
"pytest-cov",
74+
"coverage",
7075
)
7176
session.install("-e", ".")
7277

@@ -79,7 +84,7 @@ def default(session):
7984
"--cov-append",
8085
"--cov-config=.coveragerc",
8186
"--cov-report=",
82-
"--cov-fail-under=20",
87+
"--cov-fail-under=70",
8388
os.path.join("tests", "unit"),
8489
*session.posargs
8590
)

‎tests/settings.py

Copy file name to clipboard
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Use of this source code is governed by a BSD-style
4+
# license that can be found in the LICENSE file or at
5+
# https://developers.google.com/open-source/licenses/bsd
6+
7+
DEBUG = True
8+
USE_TZ = True
9+
10+
INSTALLED_APPS = [
11+
"django_spanner", # Must be the first entry
12+
"django.contrib.contenttypes",
13+
"django.contrib.auth",
14+
"django.contrib.sites",
15+
"django.contrib.sessions",
16+
"django.contrib.messages",
17+
"django.contrib.staticfiles",
18+
"tests",
19+
]
20+
21+
TIME_ZONE = "UTC"
22+
23+
DATABASES = {
24+
"default": {
25+
"ENGINE": "django_spanner",
26+
"PROJECT": "emulator-local",
27+
"INSTANCE": "django-test-instance",
28+
"NAME": "django-test-db",
29+
}
30+
}
31+
SECRET_KEY = "spanner emulator secret key"
32+
33+
PASSWORD_HASHERS = [
34+
"django.contrib.auth.hashers.MD5PasswordHasher",
35+
]
36+
37+
SITE_ID = 1
38+
39+
CONN_MAX_AGE = 60
40+
41+
ENGINE = "django_spanner"
42+
PROJECT = "emulator-local"
43+
INSTANCE = "django-test-instance"
44+
NAME = "django-test-db"
45+
OPTIONS = {}
46+
AUTOCOMMIT = True

‎tests/unit/django_spanner/__init__.py

Copy file name to clipboardExpand all lines: tests/unit/django_spanner/__init__.py
Whitespace-only changes.

‎tests/unit/django_spanner/models.py

Copy file name to clipboard
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Use of this source code is governed by a BSD-style
4+
# license that can be found in the LICENSE file or at
5+
# https://developers.google.com/open-source/licenses/bsd
6+
"""
7+
Different models used for testing django-spanner code.
8+
"""
9+
import os
10+
from django.db import models
11+
import django
12+
13+
# Load django settings before loading django models.
14+
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings"
15+
django.setup()
16+
17+
18+
# Register transformations for model fields.
19+
class UpperCase(models.Transform):
20+
lookup_name = "upper"
21+
function = "UPPER"
22+
bilateral = True
23+
24+
25+
models.CharField.register_lookup(UpperCase)
26+
models.TextField.register_lookup(UpperCase)
27+
28+
29+
# Models
30+
class ModelDecimalField(models.Model):
31+
field = models.DecimalField()
32+
33+
34+
class ModelCharField(models.Model):
35+
field = models.CharField()
36+
37+
38+
class Item(models.Model):
39+
item_id = models.IntegerField()
40+
name = models.CharField(max_length=10)
41+
created = models.DateTimeField()
42+
modified = models.DateTimeField(blank=True, null=True)
43+
44+
class Meta:
45+
ordering = ["name"]
46+
47+
48+
class Number(models.Model):
49+
num = models.IntegerField()
50+
decimal_num = models.DecimalField(max_digits=5, decimal_places=2)
51+
item = models.ForeignKey(Item, models.CASCADE)
52+
53+
54+
class Author(models.Model):
55+
name = models.CharField(max_length=40)
56+
last_name = models.CharField(max_length=40)
57+
num = models.IntegerField(unique=True)
58+
created = models.DateTimeField()
59+
modified = models.DateTimeField(blank=True, null=True)
60+
61+
62+
class Report(models.Model):
63+
name = models.CharField(max_length=10)
64+
creator = models.ForeignKey(Author, models.CASCADE, null=True)
65+
66+
class Meta:
67+
ordering = ["name"]

‎tests/unit/django_spanner/test_base.py

Copy file name to clipboardExpand all lines: tests/unit/django_spanner/test_base.py
+3-7Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# license that can be found in the LICENSE file or at
55
# https://developers.google.com/open-source/licenses/bsd
66

7-
import sys
87
import unittest
98
import os
109

@@ -13,9 +12,6 @@
1312

1413

1514
@mock_import()
16-
@unittest.skipIf(
17-
sys.version_info < (3, 6), reason="Skipping Python versions <= 3.5"
18-
)
1915
class TestBase(unittest.TestCase):
2016
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
2117
INSTANCE_ID = "instance_id"
@@ -49,7 +45,7 @@ def test_property_instance(self):
4945
_ = db_wrapper.instance
5046
mock_instance.assert_called_once_with(settings_dict["INSTANCE"])
5147

52-
def test_property__nodb_connection(self):
48+
def test_property_nodb_connection(self):
5349
db_wrapper = self._make_one(None)
5450
with self.assertRaises(NotImplementedError):
5551
db_wrapper._nodb_connection()
@@ -86,7 +82,7 @@ def test_create_cursor(self):
8682
db_wrapper.create_cursor()
8783
mock_cursor.assert_called_once_with()
8884

89-
def test__set_autocommit(self):
85+
def test_set_autocommit(self):
9086
db_wrapper = self._make_one(self.settings_dict)
9187
db_wrapper.connection = mock_connection = mock.MagicMock()
9288
mock_connection.autocommit = False
@@ -110,7 +106,7 @@ def test_is_usable(self):
110106
mock_connection.cursor = mock.MagicMock(side_effect=Error)
111107
self.assertFalse(db_wrapper.is_usable())
112108

113-
def test__start_transaction_under_autocommit(self):
109+
def test_start_transaction_under_autocommit(self):
114110
db_wrapper = self._make_one(self.settings_dict)
115111
db_wrapper.connection = mock_connection = mock.MagicMock()
116112
mock_connection.cursor = mock_cursor = mock.MagicMock()

‎tests/unit/django_spanner/test_client.py

Copy file name to clipboardExpand all lines: tests/unit/django_spanner/test_client.py
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
# license that can be found in the LICENSE file or at
55
# https://developers.google.com/open-source/licenses/bsd
66

7-
import sys
87
import unittest
98
import os
9+
from google.cloud.spanner_dbapi.exceptions import NotSupportedError
1010

1111

12-
@unittest.skipIf(
13-
sys.version_info < (3, 6), reason="Skipping Python versions <= 3.5"
14-
)
1512
class TestClient(unittest.TestCase):
1613
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
1714
INSTANCE_ID = "instance_id"
@@ -36,8 +33,6 @@ def _make_one(self, *args, **kwargs):
3633
return self._get_target_class()(*args, **kwargs)
3734

3835
def test_runshell(self):
39-
from google.cloud.spanner_dbapi.exceptions import NotSupportedError
40-
4136
db_wrapper = self._make_one(self.settings_dict)
4237

4338
with self.assertRaises(NotSupportedError):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.