Skip to content

Commit

Permalink
tests: simple JSON test
Browse files Browse the repository at this point in the history
* Adds test for JSON/JSONB fields.  (closes #19)

Signed-off-by: Jiri Kuncar <jiri.kuncar@cern.ch>
  • Loading branch information
jirikuncar committed Jun 30, 2016
1 parent 0c779ea commit dc0d5de
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio-DB
# Copyright (C) 2015 CERN
# Copyright (C) 2015, 2016 CERN
#
# Invenio is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -48,8 +48,8 @@ env:

python:
- "2.7"
- "3.3"
- "3.4"
# - "3.3"
# - "3.4"
- "3.5"

before_install:
Expand Down
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@

[pytest]
addopts = --pep8 --ignore=docs --cov=invenio_db --cov-report=term-missing
pep8ignore = W601
10 changes: 3 additions & 7 deletions requirements-devel.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015 CERN.
# Copyright (C) 2015, 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
Expand All @@ -21,9 +21,5 @@
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
#
# TODO: Add development versions of some important dependencies here to get a
# warning when there are breaking upstream changes, e.g.:
#
# -e git+git://github.com/mitsuhiko/werkzeug.git#egg=Werkzeug
# -e git+git://github.com/mitsuhiko/jinja2.git#egg=Jinja2

-e git+https://github.com/zzzeek/sqlalchemy.git#egg=sqlalchemy
43 changes: 43 additions & 0 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,46 @@ def test_local_proxy(app, db):
z=LocalProxy(lambda: None),
).fetchone()
assert result == (True, True, True, True)


@pytest.mark.skipif(hasattr(sa, 'JSON'), reason='Requires SQLAlchemy>=1.1.0b1')
def test_json(db, app):
"""Test extension initialization."""
from sqlalchemy.dialects import postgresql

InvenioDB(app, db=db)

class TestJSON(db.Model):
__tablename__ = 'test_json'

pk = sa.Column(sa.Integer, primary_key=True)
js = sa.Column(sa.JSON().with_variant(postgresql.JSONB, 'postgresql'))

with app.app_context():
db.drop_all()
db.create_all()

with app.app_context():
db.session.add(TestJSON(pk=1, js={'foo': {'bar': 1}}))
db.session.add(TestJSON(pk=2, js={'baz': 2}))
db.session.commit()

result = TestJSON.query.get(1)
assert 1 == result.js['foo']['bar']

import pytest
pytest.set_trace()
if hasattr(TestJSON.js['foo'], 'astext'):
result = TestJSON.query.filter(
TestJSON.js[('foo', 'bar')].astext.cast(Integer) == 1
).first()
assert 1 == result.pk

if hasattr(TestJSON.js, 'has_key'):
result = TestJSON.query.filter(
TestJSON.js.has_key('baz')
).first()
assert 2 == result.pk

with app.app_context():
db.drop_all()
4 changes: 2 additions & 2 deletions tests/test_versioning.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015 CERN.
# Copyright (C) 2015, 2016 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -42,7 +42,7 @@ def test_disabled_versioning(db, app):


@pytest.mark.parametrize("versioning,tables", [
(False, 1), (True, 3)
(False, 1), (True, 3)
])
def test_disabled_versioning_with_custom_table(db, app, versioning, tables):
"""Test SQLAlchemy-Continuum table loading."""
Expand Down

0 comments on commit dc0d5de

Please sign in to comment.