-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1017 Improve array serialisation in
get_sql_value
(#1018)
* fix arrays * make multidimensional arrays work, and SQLite edgecases * add tests * fix sqlite tests on certain Python versions
- Loading branch information
1 parent
fb7a5ed
commit 70dac99
Showing
2 changed files
with
121 additions
and
29 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,66 @@ | ||
import datetime | ||
from unittest import TestCase | ||
|
||
from tests.base import engines_only | ||
from tests.example_apps.music.tables import Band | ||
|
||
|
||
@engines_only("postgres", "cockroach") | ||
class TestArrayPostgres(TestCase): | ||
|
||
def test_string(self): | ||
self.assertEqual( | ||
Band.name.get_sql_value(["a", "b", "c"]), | ||
'\'{"a","b","c"}\'', | ||
) | ||
|
||
def test_int(self): | ||
self.assertEqual( | ||
Band.name.get_sql_value([1, 2, 3]), | ||
"'{1,2,3}'", | ||
) | ||
|
||
def test_nested(self): | ||
self.assertEqual( | ||
Band.name.get_sql_value([1, 2, 3, [4, 5, 6]]), | ||
"'{1,2,3,{4,5,6}}'", | ||
) | ||
|
||
def test_time(self): | ||
self.assertEqual( | ||
Band.name.get_sql_value([datetime.time(hour=8, minute=0)]), | ||
"'{\"08:00:00\"}'", | ||
) | ||
|
||
|
||
@engines_only("sqlite") | ||
class TestArraySQLite(TestCase): | ||
""" | ||
Note, we use ``.replace(" ", "")`` because we serialise arrays using | ||
Python's json library, and there is inconsistency between Python versions | ||
(some output ``["a", "b", "c"]``, and others ``["a","b","c"]``). | ||
""" | ||
|
||
def test_string(self): | ||
self.assertEqual( | ||
Band.name.get_sql_value(["a", "b", "c"]).replace(" ", ""), | ||
'\'["a","b","c"]\'', | ||
) | ||
|
||
def test_int(self): | ||
self.assertEqual( | ||
Band.name.get_sql_value([1, 2, 3]).replace(" ", ""), | ||
"'[1,2,3]'", | ||
) | ||
|
||
def test_nested(self): | ||
self.assertEqual( | ||
Band.name.get_sql_value([1, 2, 3, [4, 5, 6]]).replace(" ", ""), | ||
"'[1,2,3,[4,5,6]]'", | ||
) | ||
|
||
def test_time(self): | ||
self.assertEqual( | ||
Band.name.get_sql_value([datetime.time(hour=8, minute=0)]), | ||
"'[\"08:00:00\"]'", | ||
) |