Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retrieve questions by content uuid #13

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions unicore/ask/service/questions_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@
from pyramid.exceptions import NotFound

from unicore.ask.service.models import Question, QuestionOption
from unicore.ask.service.schema import QuestionSchema
from unicore.ask.service.schema import (
QuestionSchema, QuestionGetSchema, QuestionsGetSchema)


def get_app_object(request):
def get_question_object(request):
uuid = request.matchdict['uuid']
question = request.db.query(Question).get(uuid)
app_uuid = request.validated['app_uuid']
question = request.db.query(
Question).filter_by(_uuid=uuid, app_uuid=app_uuid).first()

if question is None:
raise NotFound

return question


def get_questions(request):
query = {
'app_uuid': request.validated['app_uuid'],
'content_uuid': request.validated['content_uuid']
}
return request.db.query(Question).filter_by(**query)


def get_option_object(request, uuid):
option = request.db.query(QuestionOption).get(uuid)

Expand Down Expand Up @@ -69,14 +80,19 @@ def collection_post(self):
self.request.response.status_int = 201
return new_data

@view(renderer='json')
@view(renderer='json', schema=QuestionGetSchema)
def get(self):
question = get_app_object(self.request)
question = get_question_object(self.request)
return question.to_dict()

@view(renderer='json', schema=QuestionsGetSchema)
def collection_get(self):
questions = get_questions(self.request)
return [q.to_dict() for q in questions]

@view(renderer='json', schema=QuestionSchema)
def put(self):
question = get_app_object(self.request)
question = get_question_object(self.request)
for attr, value in self.request.validated.iteritems():
if value is not None and not attr == 'options':
setattr(question, attr, value)
Expand Down
20 changes: 20 additions & 0 deletions unicore/ask/service/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ class Options(colander.SequenceSchema):
option = QuestionOptionSchema()


class QuestionsGetSchema(colander.MappingSchema):
app_uuid = colander.SchemaNode(
colander.String(),
validator=validators.uuid_validator,
location='querystring')
content_uuid = colander.SchemaNode(
colander.String(),
validator=validators.uuid_validator,
location='querystring')


class QuestionGetSchema(colander.MappingSchema):
app_uuid = colander.SchemaNode(
colander.String(),
validator=validators.uuid_validator,
location='querystring')


class QuestionSchema(colander.MappingSchema):
app_uuid = colander.SchemaNode(
colander.String(),
Expand Down Expand Up @@ -68,10 +86,12 @@ class QuestionResponseGetSchema(colander.MappingSchema):
question_uuid = colander.SchemaNode(
colander.String(),
location="querystring",
type="str",
validator=validators.uuid_validator,
missing=None)
option_uuid = colander.SchemaNode(
colander.String(),
location="querystring",
type="str",
validator=validators.uuid_validator,
missing=None)
89 changes: 61 additions & 28 deletions unicore/ask/service/tests/test_question_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ def create_question(self, session=None, **attrs):

def setUp(self):
super(QuestionApiTestCase, self).setUp()
self.app_uuid = uuid.uuid4().hex

self.question_1 = self.create_question(
self.db, title='What is your name', short_name='name',
question_type='free_text', author_uuid=uuid.uuid4(),
app_uuid=uuid.uuid4(), content_uuid=uuid.uuid4(),
app_uuid=self.app_uuid, content_uuid=uuid.uuid4(),
content_type='page', options=[],
locale='eng_GB')
self.db.flush()
Expand All @@ -27,7 +29,7 @@ def setUp(self):
self.question_2 = self.create_question(
self.db, title='What is your age', short_name='age',
question_type='multiple_choice', author_uuid=uuid.uuid4(),
app_uuid=uuid.uuid4(), content_uuid=uuid.uuid4(),
app_uuid=self.app_uuid, content_uuid=uuid.uuid4(),
content_type='page', options=[],
locale='eng_GB')
self.db.flush()
Expand All @@ -44,7 +46,7 @@ def setUp(self):
self.question_3 = self.create_question(
self.db, title='Which sports do you watch', short_name='sports',
multiple=True, question_type='multiple_choice',
author_uuid=uuid.uuid4(), app_uuid=uuid.uuid4(),
author_uuid=uuid.uuid4(), app_uuid=self.app_uuid,
content_uuid=uuid.uuid4(),
content_type='page', options=[],
locale='eng_GB')
Expand All @@ -64,7 +66,7 @@ def setUp(self):
self.question_4 = self.create_question(
self.db, title='Which country is the best', short_name='country',
multiple=True, question_type='multiple_choice',
author_uuid=uuid.uuid4(), app_uuid=uuid.uuid4(),
author_uuid=uuid.uuid4(), app_uuid=self.app_uuid,
content_uuid=uuid.uuid4(),
content_type='page', options=[],
locale='eng_GB')
Expand All @@ -85,7 +87,7 @@ def setUp(self):
self.question_5 = self.create_question(
self.db, title='How old are you', short_name='age',
question_type='free_text', numeric=True, author_uuid=uuid.uuid4(),
app_uuid=uuid.uuid4(), content_uuid=uuid.uuid4(),
app_uuid=self.app_uuid, content_uuid=uuid.uuid4(),
content_type='page', options=[],
locale='eng_GB')
self.db.flush()
Expand All @@ -104,23 +106,29 @@ def test_uuid(self):
self.assertEqual(the_uuid, self.question_1._uuid)

def test_question_not_found(self):
self.app.get('/questions/%s' % uuid.uuid4(), status=404)
self.app.get(
'/questions/%s' % uuid.uuid4(),
params={'app_uuid': self.app_uuid},
status=404)

def test_free_text_question(self):
resp = self.app.get(
'/questions/%s' % self.question_1.uuid)
'/questions/%s' % self.question_1.uuid,
params={'app_uuid': self.app_uuid})
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.json_body, self.question_1.to_dict())

def test_multiple_choice_question(self):
resp = self.app.get(
'/questions/%s' % self.question_2.uuid)
'/questions/%s' % self.question_2.uuid,
params={'app_uuid': self.app_uuid})
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.json_body, self.question_2.to_dict())

def test_multiple_choice_question_with_multiple_response(self):
resp = self.app.get(
'/questions/%s' % self.question_3.uuid)
'/questions/%s' % self.question_3.uuid,
params={'app_uuid': self.app_uuid})
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.json_body, self.question_3.to_dict())

Expand All @@ -132,15 +140,17 @@ def test_edit_title(self):
'question_type': 'free_text',
'content_type': 'page',
'locale': 'eng_GB',
'app_uuid': uuid.uuid4().hex,
'app_uuid': self.app_uuid,
'author_uuid': uuid.uuid4().hex,
'content_uuid': uuid.uuid4().hex,
})
self.assertEqual(resp.status_int, 200)
self.assertEqual(resp.json_body['title'], 'What is your name?')

# test get also returns same data
resp = self.app.get('/questions/%s' % self.question_1.uuid)
resp = self.app.get(
'/questions/%s' % self.question_1.uuid,
params={'app_uuid': self.app_uuid})
self.assertEqual(resp.json_body['title'], 'What is your name?')

def test_invalid_locale_code(self):
Expand All @@ -151,7 +161,7 @@ def test_invalid_locale_code(self):
'question_type': 'free_text',
'content_type': 'page',
'locale': 'unknown',
'app_uuid': uuid.uuid4().hex,
'app_uuid': self.app_uuid,
'author_uuid': uuid.uuid4().hex,
'content_uuid': uuid.uuid4().hex,
}, status=400)
Expand All @@ -167,7 +177,7 @@ def test_edit_multiple_choice_existing_options(self):
'content_type': 'page',
'locale': 'eng_GB',
'multiple': False,
'app_uuid': uuid.uuid4().hex,
'app_uuid': self.app_uuid,
'author_uuid': uuid.uuid4().hex,
'content_uuid': uuid.uuid4().hex,
'options': [
Expand All @@ -192,7 +202,9 @@ def test_edit_multiple_choice_existing_options(self):
resp.json_body['options'][3]['title'], data['options'][3]['title'])

# test get also returns same data
resp = self.app.get('/questions/%s' % self.question_2.uuid)
resp = self.app.get(
'/questions/%s' % self.question_2.uuid,
params={'app_uuid': self.app_uuid})
self.assertEqual(resp.json_body['title'], data['title'])
self.assertEqual(resp.json_body['short_name'], data['short_name'])
self.assertEqual(resp.json_body['multiple'], data['multiple'])
Expand All @@ -213,7 +225,7 @@ def test_edit_multiple_choice_add_new_options(self):
'content_type': 'page',
'locale': 'eng_GB',
'multiple': False,
'app_uuid': uuid.uuid4().hex,
'app_uuid': self.app_uuid,
'author_uuid': uuid.uuid4().hex,
'content_uuid': uuid.uuid4().hex,
'options': [
Expand Down Expand Up @@ -241,7 +253,9 @@ def test_edit_multiple_choice_add_new_options(self):
resp.json_body['options'][5]['title'], data['options'][5]['title'])

# test get also returns same data
resp = self.app.get('/questions/%s' % self.question_2.uuid)
resp = self.app.get(
'/questions/%s' % self.question_2.uuid,
params={'app_uuid': self.app_uuid})
self.assertEqual(
resp.json_body['options'][0]['title'], data['options'][0]['title'])
self.assertEqual(
Expand All @@ -263,7 +277,7 @@ def test_edit_multiple_choice_invalid_option_uuid(self):
'content_type': 'page',
'locale': 'eng_GB',
'multiple': False,
'app_uuid': uuid.uuid4().hex,
'app_uuid': self.app_uuid,
'author_uuid': uuid.uuid4().hex,
'content_uuid': uuid.uuid4().hex,
'options': [
Expand All @@ -286,7 +300,7 @@ def test_edit_multiple_choice_invalid_option_uuid(self):
'content_type': 'page',
'locale': 'eng_GB',
'multiple': False,
'app_uuid': uuid.uuid4().hex,
'app_uuid': self.app_uuid,
'author_uuid': uuid.uuid4().hex,
'content_uuid': uuid.uuid4().hex,
'options': [
Expand All @@ -309,7 +323,7 @@ def test_create(self):
'question_type': 'free_text',
'content_type': 'page',
'locale': 'eng_GB',
'app_uuid': uuid.uuid4().hex,
'app_uuid': self.app_uuid,
'author_uuid': uuid.uuid4().hex,
'content_uuid': uuid.uuid4().hex,
}
Expand All @@ -327,7 +341,8 @@ def test_create(self):

# test get also returns same data
resp = self.app.get(
'/questions/%s' % resp.json_body['uuid'])
'/questions/%s' % resp.json_body['uuid'],
params={'app_uuid': self.app_uuid})
self.assertEqual(resp.json_body['title'], data['title'])
self.assertEqual(resp.json_body['short_name'], data['short_name'])
self.assertEqual(
Expand Down Expand Up @@ -361,7 +376,7 @@ def test_create_invalid_question_type(self):
'short_name': 'name',
'question_type': 'unknown',
'content_type': 'unknown',
'app_uuid': uuid.uuid4().hex,
'app_uuid': self.app_uuid,
'author_uuid': uuid.uuid4().hex,
'content_uuid': uuid.uuid4().hex,
}
Expand All @@ -379,7 +394,7 @@ def test_create_multiple_choice_invalid(self):
'question_type': 'multiple_choice',
'content_type': 'page',
'locale': 'eng_GB',
'app_uuid': uuid.uuid4().hex,
'app_uuid': self.app_uuid,
'author_uuid': uuid.uuid4().hex,
'content_uuid': uuid.uuid4().hex,
}
Expand All @@ -397,7 +412,7 @@ def test_create_multiple_choice_invalid(self):
'content_type': 'page',
'locale': 'eng_GB',
'options': [{'title': 'very old'}],
'app_uuid': uuid.uuid4().hex,
'app_uuid': self.app_uuid,
'author_uuid': uuid.uuid4().hex,
'content_uuid': uuid.uuid4().hex,
}
Expand All @@ -415,7 +430,7 @@ def test_create_multiple_choice(self):
'content_type': 'page',
'locale': 'eng_GB',
'multiple': True,
'app_uuid': uuid.uuid4().hex,
'app_uuid': self.app_uuid,
'author_uuid': uuid.uuid4().hex,
'content_uuid': uuid.uuid4().hex,
'options': [
Expand All @@ -438,7 +453,8 @@ def test_create_multiple_choice(self):

# test get also returns same data
resp = self.app.get(
'/questions/%s' % resp.json_body['uuid'])
'/questions/%s' % resp.json_body['uuid'],
params={'app_uuid': self.app_uuid})
self.assertEqual(resp.json_body['title'], data['title'])
self.assertEqual(resp.json_body['short_name'], data['short_name'])
self.assertEqual(
Expand All @@ -458,7 +474,7 @@ def test_delete_options(self):
'content_type': 'page',
'locale': 'eng_GB',
'multiple': True,
'app_uuid': uuid.uuid4().hex,
'app_uuid': self.app_uuid,
'author_uuid': uuid.uuid4().hex,
'content_uuid': uuid.uuid4().hex,
'options': [
Expand All @@ -480,7 +496,8 @@ def test_delete_options(self):

# test get also returns same data
resp = self.app.get(
'/questions/%s' % resp.json_body['uuid'])
'/questions/%s' % resp.json_body['uuid'],
params={'app_uuid': self.app_uuid})
options = resp.json_body['options']
self.assertEqual(resp.json_body['title'], data['title'])
self.assertEqual(resp.json_body['short_name'], data['short_name'])
Expand All @@ -492,5 +509,21 @@ def test_delete_options(self):

def test_numeric_free_text_question(self):
resp = self.app.get(
'/questions/%s' % self.question_5.uuid)
'/questions/%s' % self.question_5.uuid,
params={'app_uuid': self.app_uuid})
self.assertEqual(resp.json_body, self.question_5.to_dict())

def test_get_question_for_content(self):
data = {
'app_uuid': self.question_2.app_uuid.hex,
'content_uuid': self.question_2.content_uuid.hex,
}
resp = self.app.get('/questions', params=data)
self.assertEqual(resp.json_body, [self.question_2.to_dict()])

data = {
'app_uuid': self.app_uuid,
'content_uuid': uuid.uuid4().hex,
}
resp = self.app.get('/questions', params=data)
self.assertEqual(resp.json_body, [])
Loading