forked from googleapis/python-spanner-django
-
Notifications
You must be signed in to change notification settings - Fork 1
test: unit test for SQLCompiler.get_combinator_sql
method override
#22
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
Open
mf2199
wants to merge
4
commits into
master
Choose a base branch
from
test-django-compiler
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,92 @@ | ||
# Copyright 2020 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 | ||
|
||
"""Django-Spanner SQLCompiler class unit tests.""" | ||
|
||
import unittest | ||
from unittest import mock | ||
|
||
|
||
class TestSQLCompiler(unittest.TestCase): | ||
def test_get_combinator_sql(self): | ||
from django.core.exceptions import EmptyResultSet | ||
from django.db.utils import DatabaseError | ||
from django_spanner.compiler import SQLCompiler | ||
|
||
def _empty_result_set(): | ||
from django.core.exceptions import EmptyResultSet | ||
|
||
raise EmptyResultSet | ||
|
||
query = mock.MagicMock() | ||
query.values_select = False | ||
connection = mock.MagicMock() | ||
using = 'using' | ||
|
||
compiler = SQLCompiler(query, connection, using) | ||
self.assertIsInstance(compiler, SQLCompiler) | ||
|
||
mock_cquery = mock.MagicMock() | ||
mock_cquery.get_compiler = mock_compiler = mock.MagicMock() | ||
part_sql = 'part_sql' | ||
part_args = 'part_args' | ||
mock_compiler.return_value.as_sql.return_value = (part_sql, part_args) | ||
mock_cquery.is_empty = lambda: False | ||
|
||
compiler.query = mock_query = mock.MagicMock() | ||
mock_query.combined_queries = [mock_cquery] | ||
|
||
combinator = 'union' | ||
all_ = True | ||
|
||
res, params = compiler.get_combinator_sql(combinator, all_) | ||
self.assertEqual(res, ['({})'.format(part_sql)]) | ||
self.assertEqual(params, [c for c in part_args]) | ||
|
||
compiler.connection.features.supports_slicing_ordering_in_compound = False | ||
q = compiler.query.combined_queries[0] | ||
q.low_mark = q.high_mark = True | ||
with self.assertRaises(DatabaseError): | ||
compiler.get_combinator_sql(combinator, all_) | ||
q.low_mark = q.high_mark = False | ||
mock_compiler.return_value.get_order_by.return_value = True | ||
with self.assertRaises(DatabaseError): | ||
compiler.get_combinator_sql(combinator, all_) | ||
|
||
compiler.connection.features.supports_slicing_ordering_in_compound = True | ||
mock_compiler.return_value.query.values_select = False | ||
compiler.query.values_select = [0] | ||
mock_compiler.return_value.query.set_values = mock_set_values = mock.MagicMock() | ||
compiler.get_combinator_sql(combinator, all_) | ||
mock_set_values.assert_called_once_with((0,)) | ||
|
||
compiler.query.combined_queries = [] | ||
with self.assertRaises(EmptyResultSet): | ||
compiler.get_combinator_sql(combinator, all_) | ||
|
||
compiler.connection.features.supports_slicing_ordering_in_compound = False | ||
compiler.query.combined_queries = [mock_cquery, mock_cquery] | ||
mock_cquery.return_value.get_compiler.return_value = [0, 0] | ||
mock_compiler.return_value.get_order_by.return_value = False | ||
res, params = compiler.get_combinator_sql(combinator, all_) | ||
self.assertEqual(res[0].split()[0], '({})'.format(part_sql)) | ||
self.assertEqual(params[:9], [c for c in part_args]) | ||
|
||
compiler.connection.features.supports_parentheses_in_compound = False | ||
res, params = compiler.get_combinator_sql(combinator, all_) | ||
self.assertEqual(res[0].split()[0], 'SELECT') | ||
self.assertEqual(params[:9], [c for c in part_args]) | ||
|
||
mock_compiler.return_value.query.combinator = False | ||
res, params = compiler.get_combinator_sql(combinator, all_) | ||
self.assertEqual(res[0].split()[0], part_sql) | ||
self.assertEqual(params[:9], [c for c in part_args]) | ||
|
||
mock_compiler.return_value.as_sql = _empty_result_set | ||
with self.assertRaises(EmptyResultSet): | ||
compiler.get_combinator_sql(combinator, all_) | ||
with self.assertRaises(EmptyResultSet): | ||
compiler.get_combinator_sql(None, all_) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed to prevent version requirement exception: