Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
tests: get claims filtering
Browse files Browse the repository at this point in the history
* Tests all possible queries for GET claims. (closes #23)

Signed-off-by: Jose Benito Gonzalez Lopez <jose.benito.gonzalez@cern.ch>
  • Loading branch information
jbenito3 committed Aug 30, 2015
1 parent 3f80f99 commit 178d35e
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 18 deletions.
2 changes: 1 addition & 1 deletion claimstore/static/json/examples/claim.cds.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"claim": {
"predicate": "is_same_as",
"created": "2015-05-25T11:00:00Z",
"created": "2015-03-25T11:00:00Z",
"certainty": 1.0,
"arguments": {
"human": 0,
Expand Down
2 changes: 1 addition & 1 deletion claimstore/static/json/examples/claim.inspire.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"claim": {
"predicate": "is_same_as",
"created": "2015-05-25T11:00:00Z",
"created": "2015-04-25T11:00:00Z",
"certainty": 0.8,
"arguments": {
"human": 1,
Expand Down
4 changes: 2 additions & 2 deletions claimstore/static/json/examples/claim.inspire.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"value": "cond-mat/9906097"
},
"claim": {
"predicate": "is_same_as",
"predicate": "is_cited_by",
"created": "2015-05-25T11:00:00Z",
"certainty": 0.5,
"arguments": {
Expand All @@ -16,6 +16,6 @@
},
"object": {
"type": "CDS_RECORD_ID",
"value": "C10.1103/PhysRevE.62.7422"
"value": "2003192"
}
}
7 changes: 7 additions & 0 deletions claimstore/static/json/examples/claimant.cds.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
"url": "http://cds.cern.ch/author/<CDS_AUTHOR_ID>",
"example_value": "J.R. Ellis.1",
"example_url": "http://cds.cern.ch/author/J.R.Ellis.1"
},
{
"type": "ARXIV_ID",
"description": "ARXIV data set",
"url": "http://arxiv.org/abs/<ARXIV_ID>",
"example_value": "0000.00000",
"example_url": "http://arxiv.org/abs/0000.00000"
}
]
}
7 changes: 7 additions & 0 deletions claimstore/static/json/examples/claimant.inspire.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
"url": "http://inspirehep.net/author/<INSPIRE_AUTHOR_ID>",
"example_value": "J.R. Ellis.1",
"example_url": "http://inspirehep.net/author/J.R.Ellis.1"
},
{
"type": "DOI",
"description": "DOI identifier",
"url": "http://inspirehep.net/doi/<DOI>",
"example_value": "00.0000/INSPIREHEP.DATA.RF5P.6M3K",
"example_url": "http://inspirehep.net/doi/00.0000/INSPIREHEP.DATA.RF5P.6M3K"
}
]
}
187 changes: 173 additions & 14 deletions tests/test_restful_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,69 @@
from .base import ClaimStoreTestCase # isort:skip


JSON_EXAMPLES_PATH = os.path.join(
'claimstore',
'static',
'json',
'examples'
)


@pytest.fixture(scope='module')
def claimant_example(app):
def claimant_example_cds(app):
"""Fixture that returns a JSON example of a claimant."""
with open(os.path.join(
app.config['BASE_DIR'],
'claimstore',
'static',
'json',
'examples',
JSON_EXAMPLES_PATH,
'claimant.cds.json'
)) as f:
return json.loads(f.read())


@pytest.fixture(scope='module')
def claim_example(app):
def claimant_example_inspire(app):
"""Fixture that returns a JSON example of a claimant."""
with open(os.path.join(
app.config['BASE_DIR'],
JSON_EXAMPLES_PATH,
'claimant.inspire.json'
)) as f:
return json.loads(f.read())


@pytest.fixture(scope='module')
def claim_example_cds(app):
"""Fixture that returns a JSON example of a claim."""
with open(os.path.join(
app.config['BASE_DIR'],
'claimstore',
'static',
'json',
'examples',
JSON_EXAMPLES_PATH,
'claim.cds.1.json'
)) as f:
return json.loads(f.read())


@pytest.fixture(scope='module')
def claim_example_isnpire_1(app):
"""Fixture that returns a JSON example of a claim."""
with open(os.path.join(
app.config['BASE_DIR'],
JSON_EXAMPLES_PATH,
'claim.inspire.1.json'
)) as f:
return json.loads(f.read())


@pytest.fixture(scope='module')
def claim_example_isnpire_2(app):
"""Fixture that returns a JSON example of a claim."""
with open(os.path.join(
app.config['BASE_DIR'],
JSON_EXAMPLES_PATH,
'claim.inspire.2.json'
)) as f:
return json.loads(f.read())


class RestfulAPITestCase(ClaimStoreTestCase):

"""Testing claimstore.modules.claims.restful."""
Expand All @@ -68,36 +103,160 @@ def setUp(self):
with self.app.app_context():
self.test_app = TestApp(self.app)

def _populate_for_search(self):
"""Populate database for searching tests."""
# Adding 2 claimants
self.test_app.post_json(
'/subscribe',
claimant_example_cds(self.app)
)
self.test_app.post_json(
'/subscribe',
claimant_example_inspire(self.app)
)
# Adding 3 claims (1 CDS, 2 INSPIRE)
self.test_app.post_json(
'/claims',
claim_example_cds(self.app)
)
self.test_app.post_json(
'/claims',
claim_example_isnpire_1(self.app)
)
self.test_app.post_json(
'/claims',
claim_example_isnpire_2(self.app)
)

def test_submit_claimant(self):
"""Testing `subscribe` api."""
resp = self.test_app.post_json(
'/subscribe',
claimant_example(self.app)
claimant_example_cds(self.app)
)
self.assertEqual(resp.status_code, 200)

# Re-adding the same claimant should fail.
resp = self.test_app.post_json(
'/subscribe',
claimant_example_cds(self.app),
expect_errors=True
)
self.assertEqual(resp.status_code, 400)

def test_submit_claim(self):
"""Testing POST to `claims` api."""
# Firstly we need a claimant, so the claim submission should fail.
resp = self.test_app.post_json(
'/claims',
claim_example(self.app),
claim_example_cds(self.app),
expect_errors=True
)
self.assertEqual(resp.status_code, 400)

# Test when there is a claimant.
resp = self.test_app.post_json(
'/subscribe',
claimant_example(self.app)
claimant_example_cds(self.app)
)
resp = self.test_app.post_json(
'/claims',
claim_example(self.app)
claim_example_cds(self.app)
)
self.assertEqual(resp.status_code, 200)

def test_get_claims(self):
"""Testing GET claims api."""
resp = self.test_app.get('/claims')
self.assertEqual(resp.status_code, 200)

def test_get_claims_by_claimant(self):
"""Testing GET claims filtering by claimant."""
self._populate_for_search()
# There are 1 CDS claim and 2 INSPIRE claims
resp = self.test_app.get('/claims')
self.assertEqual(len(resp.json), 3)
resp = self.test_app.get('/claims?claimant=CDS')
self.assertEqual(len(resp.json), 1)
resp = self.test_app.get('/claims?claimant=INSPIRE')
self.assertEqual(len(resp.json), 2)

def test_get_claims_by_predicate(self):
"""Testing GET claims filtering by predicate."""
self._populate_for_search()
# There are 2 claims is_same_as and 1 is_cited_by
resp = self.test_app.get('/claims')
self.assertEqual(len(resp.json), 3)
resp = self.test_app.get('/claims?predicate=is_same_as')
self.assertEqual(len(resp.json), 2)
resp = self.test_app.get('/claims?predicate=is_cited_by')
self.assertEqual(len(resp.json), 1)

def test_get_claims_by_certainty(self):
"""Testing GET claims filtering by certainty."""
self._populate_for_search()
# There are 3 claims with: 0.5, 0.8 and 1 as certainty.
resp = self.test_app.get('/claims?certainty=0.1')
self.assertEqual(len(resp.json), 3)
resp = self.test_app.get('/claims?certainty=0.5')
self.assertEqual(len(resp.json), 3)
resp = self.test_app.get('/claims?certainty=0.8')
self.assertEqual(len(resp.json), 2)
resp = self.test_app.get('/claims?certainty=1')
self.assertEqual(len(resp.json), 1)

def test_get_claims_by_c(self):
"""Testing GET claims filtering by certainty."""
self._populate_for_search()
# There are 3 claims with: 0.5, 0.8 and 1 as certainty.
resp = self.test_app.get('/claims?certainty=0.1')
self.assertEqual(len(resp.json), 3)
resp = self.test_app.get('/claims?certainty=0.5')
self.assertEqual(len(resp.json), 3)
resp = self.test_app.get('/claims?certainty=0.8')
self.assertEqual(len(resp.json), 2)
resp = self.test_app.get('/claims?certainty=1')
self.assertEqual(len(resp.json), 1)

def test_get_claims_by_human(self):
"""Testing GET claims filtering by human."""
self._populate_for_search()
# There are 2 human reported claims and 1 by an algorithm.
resp = self.test_app.get('/claims?human=0')
self.assertEqual(len(resp.json), 1)
resp = self.test_app.get('/claims?human=1')
self.assertEqual(len(resp.json), 2)

def test_get_claims_by_actor(self):
"""Testing GET claims filtering by actor."""
self._populate_for_search()
# There are 2 actors: John Doe (2 times) and CDS_submission (1).
resp = self.test_app.get('/claims?actor=John%')
self.assertEqual(len(resp.json), 2)
resp = self.test_app.get('/claims?actor=CDS%sub%')
self.assertEqual(len(resp.json), 1)

def test_get_claims_by_type_value(self):
"""Testing GET claims filtering by type."""
self._populate_for_search()
# There are 2 CDS_RECORD_ID, one as subject and one as an object.
resp = self.test_app.get('/claims?type=CDS_RECORD_ID')
self.assertEqual(len(resp.json), 2)
# The type with value `2001192` can be found 1 times.
resp = self.test_app.get('/claims?value=2001192')
self.assertEqual(len(resp.json), 1)
# Filter by type and value
resp = self.test_app.get('/claims?type=CDS_RECORD_ID&value=2001192')
self.assertEqual(len(resp.json), 1)

def test_get_claims_by_subject_object(self):
"""Testing GET claims filtering by subject/object."""
self._populate_for_search()
resp = self.test_app.get('/claims?subject=CDS_RECORD_ID')
self.assertEqual(len(resp.json), 1)
resp = self.test_app.get('/claims?object=CDS_REPORT_NUMBER')
self.assertEqual(len(resp.json), 1)
resp = self.test_app.get(
'/claims?subject=CDS_RECORD_ID&object=CDS_REPORT_NUMBER'
)
self.assertEqual(len(resp.json), 1)

0 comments on commit 178d35e

Please sign in to comment.