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

feat: add unit tests for schema, functions and introspection #606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ad001a8
feat: updated nox file for docs and docfx and added unit tests for cl…
vi3k6i5 Apr 6, 2021
17aa057
Merge branch 'master' into unit_tests
vi3k6i5 Apr 6, 2021
924f1ee
Merge pull request #1 from googleapis/master
vi3k6i5 Apr 6, 2021
df919be
fix: lint_setup_py was failing in Kokoro is not fixed
vi3k6i5 Apr 6, 2021
2bc42b0
feat: updated nox file for docs and docfx and added unit tests for cl…
vi3k6i5 Apr 6, 2021
cd3951c
feat: added docfx build in nox file
vi3k6i5 Apr 7, 2021
0de5617
feat: added docfx build in nox file
vi3k6i5 Apr 7, 2021
d8e2864
feat: added docfx build in nox file
vi3k6i5 Apr 7, 2021
00e2ee8
Merge branch 'unit_tests' of github.com:vi3k6i5/python-spanner-django…
vi3k6i5 Apr 7, 2021
ad5f15c
Merge pull request #2 from googleapis/master
vi3k6i5 Apr 7, 2021
ecb0eec
feat: updated nox file for docs and docfx and added unit tests for cl…
vi3k6i5 Apr 6, 2021
a816ab9
feat: added docfx build in nox file
vi3k6i5 Apr 7, 2021
798e88d
feat: fixed nox file conflict
vi3k6i5 Apr 7, 2021
ec28c1c
feat: adding unit tests for django spanner
vi3k6i5 Apr 21, 2021
5ec9841
Merge pull request #3 from googleapis/master
vi3k6i5 Apr 25, 2021
34c4dda
Merge pull request #4 from googleapis/master
vi3k6i5 Apr 27, 2021
caf5a43
Merge pull request #5 from googleapis/master
vi3k6i5 Apr 28, 2021
129e41e
Merge pull request #6 from googleapis/master
vi3k6i5 Apr 28, 2021
fad6cf3
feat: updated nox file for docs and docfx and added unit tests for cl…
vi3k6i5 Apr 6, 2021
cef35a5
feat: added docfx build in nox file
vi3k6i5 Apr 7, 2021
30a62ae
feat: updated nox file for docs and docfx and added unit tests for cl…
vi3k6i5 Apr 6, 2021
56bd658
feat: added docfx build in nox file
vi3k6i5 Apr 7, 2021
588ee4b
feat: updated nox file for docs and docfx and added unit tests for cl…
vi3k6i5 Apr 6, 2021
7fdc7dd
feat: adding unit tests for django spanner
vi3k6i5 Apr 21, 2021
2ee4870
bug: fixed schema tests settings to create instance and delete it aft…
vi3k6i5 Apr 30, 2021
cab2cc1
Merge branch 'unit_tests' of github.com:vi3k6i5/python-spanner-django…
vi3k6i5 Apr 30, 2021
8b69cc8
bug: test fixes for schema file
vi3k6i5 May 3, 2021
2bb3fff
feat: added unit test coverage for functions, introspection and schema
vi3k6i5 May 4, 2021
92c18ef
Merge branch 'master' of github.com:googleapis/python-spanner-django …
vi3k6i5 May 18, 2021
cfb532b
refactor: lint the code
vi3k6i5 May 18, 2021
7e41463
refactor: removed unrelated code from PR
vi3k6i5 May 18, 2021
827d981
Merge branch 'master' of github.com:googleapis/python-spanner-django …
vi3k6i5 May 19, 2021
40d1eef
refactor: changed unit tests to use SpannerSimpleTestClass as base class
vi3k6i5 May 19, 2021
f8ee959
refactor: lint corrections in test files
vi3k6i5 May 19, 2021
0811c18
Merge branch 'master' into unit_tests
vi3k6i5 May 20, 2021
2a8b420
Merge branch 'master' of github.com:googleapis/python-spanner-django …
vi3k6i5 May 20, 2021
52f78b3
Merge branch 'unit_tests' of github.com:vi3k6i5/python-spanner-django…
vi3k6i5 May 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 4 noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def default(session):
"--cov-append",
"--cov-config=.coveragerc",
"--cov-report=",
"--cov-fail-under=65",
"--cov-fail-under=80",
os.path.join("tests", "unit"),
*session.posargs,
)
Expand Down Expand Up @@ -157,7 +157,7 @@ def cover(session):
test runs (not system test runs), and then erases coverage data.
"""
session.install("coverage", "pytest-cov")
session.run("coverage", "report", "--show-missing", "--fail-under=65")
session.run("coverage", "report", "--show-missing", "--fail-under=80")

session.run("coverage", "erase")

Expand Down
233 changes: 233 additions & 0 deletions 233 tests/unit/django_spanner/test_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
# 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 tests.unit.django_spanner.simple_test import SpannerSimpleTestClass
from django_spanner.compiler import SQLCompiler
from django.db.models import CharField, FloatField, Value
from django.db.models.functions import (
Cast,
Concat,
Cot,
Degrees,
Log,
Ord,
Pi,
Radians,
StrIndex,
Substr,
Left,
Right,
)
from .models import Author


class TestUtils(SpannerSimpleTestClass):
def test_cast_with_max_length(self):
"""
Tests cast field with max length.
"""
q1 = Author.objects.values("name").annotate(
name_as_prefix=Cast("name", output_field=CharField(max_length=10)),
)
compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.name, SUBSTR(CAST(tests_author.name AS "
+ "STRING), 0, 10) AS name_as_prefix FROM tests_author",
)
self.assertEqual(params, ())

def test_cast_without_max_length(self):
"""
Tests cast field without max length.
"""
q1 = Author.objects.values("num").annotate(
num_as_float=Cast("num", output_field=FloatField()),
)
compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.num, CAST(tests_author.num AS FLOAT64) "
+ "AS num_as_float FROM tests_author",
)
self.assertEqual(params, ())

def test_concatpair(self):
"""
Tests concatinating pair of columns.
"""
q1 = Author.objects.values("name").annotate(
full_name=Concat(
"name", Value(" "), "last_name", output_field=CharField()
),
)
compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.name, CONCAT(IFNULL(tests_author.name, %s), "
+ "IFNULL(CONCAT(IFNULL(%s, %s), IFNULL(tests_author.last_name, "
+ "%s)), %s)) AS full_name FROM tests_author",
)
self.assertEqual(params, ("", " ", "", "", ""))

def test_cot(self):
"""
Tests cot function on a column.
"""
q1 = Author.objects.values("num").annotate(num_cot=Cot("num"),)
compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.num, (1 / TAN(tests_author.num)) AS num_cot "
+ "FROM tests_author",
)
self.assertEqual(params, ())

def test_degrees(self):
"""
Tests degrees function on a column.
"""
q1 = Author.objects.values("num").annotate(num_degrees=Degrees("num"),)
compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.num, ((tests_author.num) * 180 / "
+ "3.141592653589793) AS num_degrees FROM tests_author",
)
self.assertEqual(params, ())

def test_left(self):
"""
Tests left function applied to a column.
"""
q1 = Author.objects.values("num").annotate(
first_initial=Left("name", 1),
)
compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.num, SUBSTR(tests_author.name, %s, %s) AS "
+ "first_initial FROM tests_author",
)
self.assertEqual(params, (1, 1))

def test_right(self):
"""
Tests right function applied to a column.
"""
q1 = Author.objects.values("num").annotate(
last_letter=Right("name", 1),
)
compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.num, SUBSTR(tests_author.name, (%s * %s)) "
+ "AS last_letter FROM tests_author",
)
self.assertEqual(params, (1, -1))

def test_log(self):
"""
Tests log function applied to a column.
"""
q1 = Author.objects.values("num").annotate(log=Log("num", Value(10)))

compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.num, LOG(%s, tests_author.num) AS log FROM "
+ "tests_author",
)
self.assertEqual(params, (10,))

def test_ord(self):
"""
Tests ord function applied to a column.
"""
q1 = Author.objects.values("name").annotate(
name_code_point=Ord("name")
)

compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.name, TO_CODE_POINTS(tests_author.name)"
+ "[OFFSET(0)] AS name_code_point FROM tests_author",
)
self.assertEqual(params, ())

def test_pi(self):
"""
Tests pi function applied to a column.
"""
q1 = Author.objects.filter(num=Pi()).values("num")

compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.num FROM tests_author WHERE tests_author.num "
+ "= (3.141592653589793)",
)
self.assertEqual(params, ())

def test_radians(self):
"""
Tests radians function applied to a column.
"""
q1 = Author.objects.values("num").annotate(num_radians=Radians("num"))

compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.num, ((tests_author.num) * 3.141592653589793 "
"/ 180) AS num_radians FROM tests_author",
)
self.assertEqual(params, ())

def test_strindex(self):
"""
Tests str index function applied to a column.
"""
q1 = Author.objects.values("name").annotate(
smith_index=StrIndex("name", Value("Smith"))
)

compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.name, STRPOS(tests_author.name, %s) AS "
+ "smith_index FROM tests_author",
)
self.assertEqual(params, ("Smith",))

def test_substr(self):
"""
Tests substr function applied to a column.
"""
q1 = Author.objects.values("name").annotate(
name_prefix=Substr("name", 1, 5)
)

compiler = SQLCompiler(q1.query, self.connection, "default")
sql_query, params = compiler.query.as_sql(compiler, self.connection)
self.assertEqual(
sql_query,
"SELECT tests_author.name, SUBSTR(tests_author.name, %s, %s) AS "
+ "name_prefix FROM tests_author",
)
self.assertEqual(params, (1, 5))
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.