Skip to content

Commit

Permalink
start some argument checking and error handling; parameters on get_ev…
Browse files Browse the repository at this point in the history
…aluations, add .travis.yml
  • Loading branch information
jellegerbrandy committed Apr 20, 2016
1 parent 5c6df64 commit b2de706
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 24 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Config file for automatic testing at travis-ci.org

language: python
python: 2.7
env:
- TOX_ENV=py27
- TOX_ENV=style
- TOX_ENV=docs
install:
- pip install tox
script:
- tox -e $TOX_ENV
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ WebOb==1.6.0
WebTest==2.0.21
zope.deprecation==4.1.2
zope.interface==4.1.3
colander==1.2
6 changes: 6 additions & 0 deletions restapi/tests/common.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import os
import unittest
import logging
from restapi import protocol
from webtest import TestApp

from restapi import main


# shut up peewee
peewee_logger = logging.getLogger('peewee')
peewee_logger.setLevel(logging.ERROR)


class APITestCase(unittest.TestCase):
"""Base class for testing API functions"""

Expand Down
59 changes: 47 additions & 12 deletions restapi/tests/test_evaluations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@


class TestEvaluations(APITestCase):
@property
def url_collection(self):
return URL_EVALUATION_COLLECTION.format(contract=self.contract_name)

def test_workflow(self):
# test creating and getting evaluations
app = self.app

url_collection = URL_EVALUATION_COLLECTION.format(contract=self.contract_name)

def url_resource(evaluation_id):
return URL_EVALUATION_RESOURCE.format(
contract=self.contract_name,
Expand All @@ -20,39 +21,73 @@ def url_resource(evaluation_id):
user = self.contract.create_user()
contribution = self.contract.create_contribution(user=user)

# create a evaluation
# create an evaluation
response = app.post(
url_collection,
self.url_collection,
{
'evaluator_id': user.id,
'contribution_id': contribution.id,
'value': 10,
'value': 1,
}
)
self.assertEqual(response.json['value'], 10)
self.assertEqual(response.json['value'], 1)
evaluation_id = response.json['id']

# get the evaluation info
response = app.get(url_resource(evaluation_id))

# get the evaluation collection
response = app.get(url_collection)
response = app.get(self.url_collection)
self.assertEqual(response.json.get('count'), 1)

def test_evaluation_data(self):
# test that GETting an evaluation returns all expected data
user = self.contract.create_user()
contribution = self.contract.create_contribution(user=user)
value = 3.14
evaluation = self.contract.create_evaluation(
contribution=contribution, value=value, user=user)
contribution=contribution, value=1, user=user)

url = URL_EVALUATION_RESOURCE.format(id=evaluation.id, contract=self.contract_name)
info = self.app.get(url).json
self.assertEqual(info['value'], value)
self.assertEqual(info['value'], 1)
self.assertEqual(info['contribution']['id'], contribution.id)
self.assertEqual(info['contribution']['score'], 0.0)
self.assertEqual(info['contribution']['score'], 1.0)
self.assertEqual(info['contribution']['engaged_reputation'], user.reputation)
self.assertEqual(info['evaluator']['id'], user.id)
self.assertEqual(info['evaluator']['tokens'], 49)
self.assertEqual(info['evaluator']['tokens'], 99)
self.assertEqual(info['evaluator']['reputation'], 1)

def test_evaluation_collection_get(self):
# add some data
user0 = self.contract.create_user()
user1 = self.contract.create_user()
contribution0 = self.contract.create_contribution(user=user0)
contribution1 = self.contract.create_contribution(user=user1)
self.contract.create_evaluation(contribution=contribution0, value=1, user=user0)
self.contract.create_evaluation(contribution=contribution0, value=1, user=user1)
self.contract.create_evaluation(contribution=contribution1, value=1, user=user0)

response = self.app.get(self.url_collection)
self.assertEqual(response.json.get('count'), 3)
response = self.app.get(self.url_collection, {'contribution_id': contribution0.id})
self.assertEqual(response.json.get('count'), 2)
response = self.app.get(self.url_collection, {'contributor_id': user0.id})
self.assertEqual(response.json.get('count'), 2)
response = self.app.get(self.url_collection, {'contributor_id': user1.id})
self.assertEqual(response.json.get('count'), 1)

def test_evaluation_errors(self):
user = self.contract.create_user()
contribution = self.contract.create_contribution(user=user)

# try to create an evaluation with an illegal value
response = self.app.post(
self.url_collection,
{
'evaluator_id': user.id,
'contribution_id': contribution.id,
'value': 10000,
},
expect_errors=True,
)
self.assertTrue(response.status, 'xx')
39 changes: 27 additions & 12 deletions restapi/views/evaluations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@

import config
from utils import get_contract

from colander import MappingSchema, SchemaNode, Float, Integer

evaluation_collection_service = Service(name='Evaluation Collection', path=config.URL_EVALUATION_COLLECTION, description="Evaluations")
evaluation_resource_service = Service(name='Evaluation Resource', path=config.URL_EVALUATION_RESOURCE, description="Evaluations")


@evaluation_collection_service.get(validators=(get_contract,))
class EvaluationCollectionGetSchema(MappingSchema):
contribution_id = SchemaNode(Integer(), location='querystring', type='int', missing=None)
contributor_id = SchemaNode(Integer(), location='querystring', type='int', missing=None)


@evaluation_collection_service.get(validators=(get_contract,), schema=EvaluationCollectionGetSchema)
def collection_get(request):
"""Get a list of users"""
evaluations = request.contract.get_evaluations()
evaluations = request.contract.get_evaluations(**request.validated)
return {
'count': len(evaluations),
'items': [evaluation_to_dict(evaluation, request) for evaluation in evaluations],
}


@evaluation_collection_service.post(validators=(get_contract,))
class Evaluationschema(MappingSchema):
value = SchemaNode(Float(), location='body', type='int')
evaluator_id = SchemaNode(Integer(), location='body', type='int')
contribution_id = SchemaNode(Integer(), location='body', type='int')


@evaluation_collection_service.post(validators=(get_contract,), schema=Evaluationschema)
def collection_post(request):
"""Create a new evaluation.
Expand All @@ -37,14 +48,18 @@ def collection_post(request):
information about the added evaluation
"""
user = request.contract.get_user(request.POST['evaluator_id'])
contribution = request.contract.get_contribution(request.POST['contribution_id'])
value = request.POST['value']
evaluation = request.contract.create_evaluation(
user=user,
contribution=contribution,
value=value,
)
user = request.contract.get_user(request.validated['evaluator_id'])
contribution = request.contract.get_contribution(request.validated['contribution_id'])
value = request.validated['value']
try:
evaluation = request.contract.create_evaluation(
user=user,
contribution=contribution,
value=value,
)
except ValueError as error:
request.errors.add('query', 'value error', unicode(error))
return
return evaluation_to_dict(evaluation, request)


Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'cornice',
'backfeed-protocol',
'BeautifulSoup4',
'colander',
]

setup(
Expand Down

0 comments on commit b2de706

Please sign in to comment.