Skip to content

Commit

Permalink
1005 CockroachDB - enable array tests (#1012)
Browse files Browse the repository at this point in the history
* start refactoring array tests, so they work with CockroachDB

* mark slow array tests
  • Loading branch information
dantownsend authored Jun 10, 2024
1 parent 3e14d9c commit 28f871d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 21 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ ignore_missing_imports = true
[tool.pytest.ini_options]
markers = [
"integration",
"speed"
"speed",
"cockroach_array_slow"
]

[tool.coverage.run]
Expand Down
2 changes: 1 addition & 1 deletion scripts/test-cockroach.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ python3 -m pytest \
--cov-report=xml \
--cov-report=html \
--cov-fail-under=80 \
-m "not integration" \
-m "not integration and not cockroach_array_slow" \
-s $@
82 changes: 63 additions & 19 deletions tests/columns/test_array.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime
from unittest import TestCase

import pytest

from piccolo.columns.column_types import (
Array,
BigInt,
Expand All @@ -10,8 +12,9 @@
Timestamp,
Timestamptz,
)
from piccolo.querystring import QueryString
from piccolo.table import Table
from tests.base import engines_only, sqlite_only
from tests.base import engines_only, engines_skip, sqlite_only


class MyTable(Table):
Expand Down Expand Up @@ -40,12 +43,18 @@ def setUp(self):
def tearDown(self):
MyTable.alter().drop_table().run_sync()

@engines_only("postgres", "sqlite")
@pytest.mark.cockroach_array_slow
def test_storage(self):
"""
Make sure data can be stored and retrieved.
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
In CockroachDB <= v22.2.0 we had this error:
* https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
In newer CockroachDB versions, it runs but is very slow:
* https://github.com/piccolo-orm/piccolo/issues/1005
""" # noqa: E501
MyTable(value=[1, 2, 3]).save().run_sync()
Expand All @@ -54,12 +63,19 @@ def test_storage(self):
assert row is not None
self.assertEqual(row.value, [1, 2, 3])

@engines_only("postgres")
@engines_skip("sqlite")
@pytest.mark.cockroach_array_slow
def test_index(self):
"""
Indexes should allow individual array elements to be queried.
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
In CockroachDB <= v22.2.0 we had this error:
* https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
In newer CockroachDB versions, it runs but is very slow:
* https://github.com/piccolo-orm/piccolo/issues/1005
""" # noqa: E501
MyTable(value=[1, 2, 3]).save().run_sync()
Expand All @@ -68,66 +84,92 @@ def test_index(self):
MyTable.select(MyTable.value[0]).first().run_sync(), {"value": 1}
)

@engines_only("postgres")
@engines_skip("sqlite")
@pytest.mark.cockroach_array_slow
def test_all(self):
"""
Make sure rows can be retrieved where all items in an array match a
given value.
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
In CockroachDB <= v22.2.0 we had this error:
* https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
In newer CockroachDB versions, it runs but is very slow:
* https://github.com/piccolo-orm/piccolo/issues/1005
""" # noqa: E501
MyTable(value=[1, 1, 1]).save().run_sync()

# We have to explicitly specify the type, so CockroachDB works.
self.assertEqual(
MyTable.select(MyTable.value)
.where(MyTable.value.all(1))
.where(MyTable.value.all(QueryString("{}::INTEGER", 1)))
.first()
.run_sync(),
{"value": [1, 1, 1]},
)

# We have to explicitly specify the type, so CockroachDB works.
self.assertEqual(
MyTable.select(MyTable.value)
.where(MyTable.value.all(0))
.where(MyTable.value.all(QueryString("{}::INTEGER", 0)))
.first()
.run_sync(),
None,
)

@engines_only("postgres")
@engines_skip("sqlite")
@pytest.mark.cockroach_array_slow
def test_any(self):
"""
Make sure rows can be retrieved where any items in an array match a
given value.
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
In CockroachDB <= v22.2.0 we had this error:
* https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
In newer CockroachDB versions, it runs but is very slow:
* https://github.com/piccolo-orm/piccolo/issues/1005
""" # noqa: E501

MyTable(value=[1, 2, 3]).save().run_sync()

# We have to explicitly specify the type, so CockroachDB works.
self.assertEqual(
MyTable.select(MyTable.value)
.where(MyTable.value.any(1))
.where(MyTable.value.any(QueryString("{}::INTEGER", 1)))
.first()
.run_sync(),
{"value": [1, 2, 3]},
)

# We have to explicitly specify the type, so CockroachDB works.
self.assertEqual(
MyTable.select(MyTable.value)
.where(MyTable.value.any(0))
.where(MyTable.value.any(QueryString("{}::INTEGER", 0)))
.first()
.run_sync(),
None,
)

@engines_only("postgres")
@engines_skip("sqlite")
@pytest.mark.cockroach_array_slow
def test_cat(self):
"""
Make sure values can be appended to an array.
🐛 Cockroach bug: https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
In CockroachDB <= v22.2.0 we had this error:
* https://github.com/cockroachdb/cockroach/issues/71908 "could not decorrelate subquery" error under asyncpg
In newer CockroachDB versions, it runs but is very slow:
* https://github.com/piccolo-orm/piccolo/issues/1005
""" # noqa: E501
MyTable(value=[1, 1, 1]).save().run_sync()
Expand All @@ -137,7 +179,8 @@ def test_cat(self):
).run_sync()

self.assertEqual(
MyTable.select().run_sync(), [{"id": 1, "value": [1, 1, 1, 2]}]
MyTable.select(MyTable.value).run_sync(),
[{"value": [1, 1, 1, 2]}],
)

# Try plus symbol
Expand All @@ -147,7 +190,8 @@ def test_cat(self):
).run_sync()

self.assertEqual(
MyTable.select().run_sync(), [{"id": 1, "value": [1, 1, 1, 2, 3]}]
MyTable.select(MyTable.value).run_sync(),
[{"value": [1, 1, 1, 2, 3]}],
)

# Make sure non-list values work
Expand All @@ -157,8 +201,8 @@ def test_cat(self):
).run_sync()

self.assertEqual(
MyTable.select().run_sync(),
[{"id": 1, "value": [1, 1, 1, 2, 3, 4]}],
MyTable.select(MyTable.value).run_sync(),
[{"value": [1, 1, 1, 2, 3, 4]}],
)

@sqlite_only
Expand Down

0 comments on commit 28f871d

Please sign in to comment.