Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdavidzeng committed Jun 17, 2020
1 parent 9df379a commit 59dc537
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
10 changes: 10 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ def is_sqlite(db_uri):
return False


@pytest.fixture(scope='session')
def is_sqlalchemy_1_3_or_higer():
try:
from sqlalchemy import JSON # noqa: F401
except ImportError:
return False
else:
return True


@pytest.fixture(scope='session')
def db_engine_options(db_uri, is_postgresql):
if is_postgresql:
Expand Down
37 changes: 27 additions & 10 deletions test/interface/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ def multiple_corges_inserted(session, is_postgresql):


@pytest.fixture
def multiple_tils_inserted(session):
til_1 = Til(id=1, name='name_1', refer_info=[])
til_2 = Til(id=2, name='name_2', refer_info=[1])
til_3 = Til(id=3, name='name_3', refer_info=[2, 3])
til_4 = Til(id=4, name='name_4', refer_info=['foo', 'baz'])
session.add_all([til_1, til_2, til_3, til_4])
session.commit()
def multiple_tils_inserted(session, is_sqlalchemy_1_3_or_higer):
if is_sqlalchemy_1_3_or_higer:

til_1 = Til(id=1, name='name_1', refer_info=[])
til_2 = Til(id=2, name='name_2', refer_info=[1])
til_3 = Til(id=3, name='name_3', refer_info=[2, 3])
til_4 = Til(id=4, name='name_4', refer_info=['foo', 'baz'])
session.add_all([til_1, til_2, til_3, til_4])
session.commit()


class TestFiltersNotApplied:
Expand Down Expand Up @@ -1331,13 +1333,18 @@ def test_filter_by_hybrid_methods(self, session):
class TestApplyJsonContainsFilter:

@pytest.mark.usefixtures('multiple_tils_inserted')
def test_til_not_contains_value(self, session, is_sqlite, is_postgresql):
def test_til_not_contains_value(
self, session, is_sqlite, is_postgresql, is_sqlalchemy_1_3_or_higer
):
if is_sqlite:
pytest.skip()

if is_postgresql:
pytest.skip()

if not is_sqlalchemy_1_3_or_higer:
pytest.skip()

query = session.query(Til)
filters = [
{'field': 'refer_info', 'op': 'json_contains', 'value': 'invalid'}
Expand All @@ -1349,13 +1356,18 @@ def test_til_not_contains_value(self, session, is_sqlite, is_postgresql):
assert len(result) == 0

@pytest.mark.usefixtures('multiple_tils_inserted')
def test_til_contains_int_value(self, session, is_sqlite, is_postgresql):
def test_til_contains_int_value(
self, session, is_sqlite, is_postgresql, is_sqlalchemy_1_3_or_higer
):
if is_sqlite:
pytest.skip()

if is_postgresql:
pytest.skip()

if not is_sqlalchemy_1_3_or_higer:
pytest.skip()

query = session.query(Til)
filters = [
{'field': 'refer_info', 'op': 'json_contains', 'value': 1}
Expand All @@ -1368,13 +1380,18 @@ def test_til_contains_int_value(self, session, is_sqlite, is_postgresql):
assert result[0].id == 2

@pytest.mark.usefixtures('multiple_tils_inserted')
def test_til_contains_str_value(self, session, is_sqlite, is_postgresql):
def test_til_contains_str_value(
self, session, is_sqlite, is_postgresql, is_sqlalchemy_1_3_or_higer
):
if is_sqlite:
pytest.skip()

if is_postgresql:
pytest.skip()

if not is_sqlalchemy_1_3_or_higer:
pytest.skip()

query = session.query(Til)
filters = [
{'field': 'refer_info', 'op': 'json_contains', 'value': "foo"}
Expand Down
15 changes: 11 additions & 4 deletions test/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

from sqlalchemy import (
Column, Date, DateTime, ForeignKey, Integer, String, Time, JSON
Column, Date, DateTime, ForeignKey, Integer, String, Time
)
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.ext.declarative import declarative_base
Expand Down Expand Up @@ -64,8 +64,15 @@ class Corge(BasePostgresqlSpecific):
tags = Column(ARRAY(String, dimensions=1))


class Til(Base):
try:
from sqlalchemy import JSON
except ImportError:
class Til(Base):

__tablename__ = 'til'
__tablename__ = 'til'
else:
class Til(Base):

refer_info = Column(JSON)
__tablename__ = 'til'

refer_info = Column(JSON)

0 comments on commit 59dc537

Please sign in to comment.