def _get_test_models(): default_models = { 'tags': h.get_tags(), 'speakers': h.get_speakers(), 'sources': h.get_sources(), 'elicitation_methods': h.get_elicitation_methods(), 'syntactic_categories': h.get_syntactic_categories(), 'files': h.get_files() } return default_models
def test_index(self): """Tests that GET /syntacticcategories returns an array of all syntactic categories and that order_by and pagination parameters work correctly.""" # Add 100 syntactic categories. def create_syntactic_category_from_index(index): syntactic_category = model.SyntacticCategory() syntactic_category.name = u'sc%d' % index syntactic_category.type = u'lexical' syntactic_category.description = u'description %d' % index return syntactic_category syntactic_categories = [create_syntactic_category_from_index(i) for i in range(1, 101)] Session.add_all(syntactic_categories) Session.commit() syntactic_categories = h.get_syntactic_categories(True) syntactic_categories_count = len(syntactic_categories) # Test that GET /syntacticcategories gives us all of the syntactic categories. response = self.app.get(url('syntacticcategories'), headers=self.json_headers, extra_environ=self.extra_environ_view) resp = json.loads(response.body) assert len(resp) == syntactic_categories_count assert resp[0]['name'] == u'sc1' assert resp[0]['id'] == syntactic_categories[0].id assert response.content_type == 'application/json' # Test the paginator GET params. paginator = {'items_per_page': 23, 'page': 3} response = self.app.get(url('syntacticcategories'), paginator, headers=self.json_headers, extra_environ=self.extra_environ_view) resp = json.loads(response.body) assert len(resp['items']) == 23 assert resp['items'][0]['name'] == syntactic_categories[46].name assert response.content_type == 'application/json' # Test the order_by GET params. order_by_params = {'order_by_model': 'SyntacticCategory', 'order_by_attribute': 'name', 'order_by_direction': 'desc'} response = self.app.get(url('syntacticcategories'), order_by_params, headers=self.json_headers, extra_environ=self.extra_environ_view) resp = json.loads(response.body) result_set = sorted([sc.name for sc in syntactic_categories], reverse=True) assert result_set == [sc['name'] for sc in resp] # Test the order_by *with* paginator. params = {'order_by_model': 'SyntacticCategory', 'order_by_attribute': 'name', 'order_by_direction': 'desc', 'items_per_page': 23, 'page': 3} response = self.app.get(url('syntacticcategories'), params, headers=self.json_headers, extra_environ=self.extra_environ_view) resp = json.loads(response.body) assert result_set[46] == resp['items'][0]['name'] assert response.content_type == 'application/json' # Expect a 400 error when the order_by_direction param is invalid order_by_params = {'order_by_model': 'SyntacticCategory', 'order_by_attribute': 'name', 'order_by_direction': 'descending'} response = self.app.get(url('syntacticcategories'), order_by_params, status=400, headers=self.json_headers, extra_environ=self.extra_environ_view) resp = json.loads(response.body) assert resp['errors']['order_by_direction'] == u"Value must be one of: asc; desc (not u'descending')" assert response.content_type == 'application/json' # Expect the default BY id ASCENDING ordering when the order_by_model/Attribute # param is invalid. order_by_params = {'order_by_model': 'SyntacticCategoryist', 'order_by_attribute': 'nominal', 'order_by_direction': 'desc'} response = self.app.get(url('syntacticcategories'), order_by_params, headers=self.json_headers, extra_environ=self.extra_environ_view) resp = json.loads(response.body) assert resp[0]['id'] == syntactic_categories[0].id assert response.content_type == 'application/json' # Expect a 400 error when the paginator GET params are empty # or are integers less than 1 paginator = {'items_per_page': u'a', 'page': u''} response = self.app.get(url('syntacticcategories'), paginator, headers=self.json_headers, extra_environ=self.extra_environ_view, status=400) resp = json.loads(response.body) assert resp['errors']['items_per_page'] == u'Please enter an integer value' assert resp['errors']['page'] == u'Please enter a value' paginator = {'items_per_page': 0, 'page': -1} response = self.app.get(url('syntacticcategories'), paginator, headers=self.json_headers, extra_environ=self.extra_environ_view, status=400) resp = json.loads(response.body) assert resp['errors']['items_per_page'] == u'Please enter a number that is 1 or greater' assert resp['errors']['page'] == u'Please enter a number that is 1 or greater' assert response.content_type == 'application/json'