Example #1
0
 def test_new_search(self):
     """Tests that GET /collectionbackups/new_search returns the search parameters for searching the collection backups resource."""
     query_builder = SQLAQueryBuilder('CollectionBackup')
     response = self.app.get(url('/collectionbackups/new_search'), headers=self.json_headers,
                             extra_environ=self.extra_environ_view)
     resp = json.loads(response.body)
     assert resp['search_parameters'] == h.get_search_parameters(query_builder)
Example #2
0
    def edit(self, id):
        """GET /formsearches/id/edit: Return the data necessary to update an existing
        OLD form search.
        """
        """Return a form search and the data needed to update it.

        :URL: ``GET /formsearches/edit`` with optional query string parameters 
        :param str id: the ``id`` value of the form search that will be updated.
        :returns: a dictionary of the form::

                {"form_search": {...}, "data": {...}}

            where the value of the ``form_search`` key is a dictionary
            representation of the form search and the value of the ``data`` key
            is a dictionary containing the data necessary to update a form
            search.

        """
        form_search = h.eagerload_form_search(
            Session.query(FormSearch)).get(id)
        if form_search:
            data = {
                'search_parameters':
                h.get_search_parameters(self.query_builder)
            }
            return {'data': data, 'form_search': form_search}
        else:
            response.status_int = 404
            return {'error': 'There is no form search with id %s' % id}
Example #3
0
    def edit(self, id):
        """GET /formsearches/id/edit: Return the data necessary to update an existing
        OLD form search.
        """
        """Return a form search and the data needed to update it.

        :URL: ``GET /formsearches/edit`` with optional query string parameters 
        :param str id: the ``id`` value of the form search that will be updated.
        :returns: a dictionary of the form::

                {"form_search": {...}, "data": {...}}

            where the value of the ``form_search`` key is a dictionary
            representation of the form search and the value of the ``data`` key
            is a dictionary containing the data necessary to update a form
            search.

        """
        form_search = h.eagerload_form_search(Session.query(FormSearch)).get(id)
        if form_search:
            data = {'search_parameters': h.get_search_parameters(self.query_builder)}
            return {'data': data, 'form_search': form_search}
        else:
            response.status_int = 404
            return {'error': 'There is no form search with id %s' % id}
Example #4
0
 def test_new_search(self):
     """Tests that GET /languages/new_search returns the search parameters for searching the languages resource."""
     query_builder = SQLAQueryBuilder('Language')
     response = self.app.get(url('/languages/new_search'), headers=self.json_headers,
                             extra_environ=self.extra_environ_view)
     resp = json.loads(response.body)
     assert resp['search_parameters'] == h.get_search_parameters(query_builder)
Example #5
0
    def new_search(self):
        """Return the data necessary to search the phonology resources.

        :URL: ``GET /phonologies/new_search``
        :returns: ``{"search_parameters": {"attributes": { ... }, "relations": { ... }}``

        """
        return {'search_parameters': h.get_search_parameters(self.query_builder)}
Example #6
0
    def new_search(self):
        """Return the data necessary to search the morpheme language model
        resources.

        :URL: ``GET /morphemelanguagemodels/new_search``
        :returns: ``{"search_parameters": {"attributes": { ... }, "relations": { ... }}``

        """
        return {'search_parameters': h.get_search_parameters(self.query_builder)}
Example #7
0
    def new(self):
        """GET /formsearches/new: Return the data necessary to create a new OLD
        form search.
        """
        """Return the data necessary to create a new form search.

        :URL: ``GET /formsearches/new`` with optional query string parameters 
        :returns: A dictionary of lists of resources

        """
        return {'search_parameters': h.get_search_parameters(self.query_builder)}
Example #8
0
    def new_search(self):
        """Return the data necessary to search the morpheme language model
        resources.

        :URL: ``GET /morphemelanguagemodels/new_search``
        :returns: ``{"search_parameters": {"attributes": { ... }, "relations": { ... }}``

        """
        return {
            'search_parameters': h.get_search_parameters(self.query_builder)
        }
Example #9
0
    def new(self):
        """GET /formsearches/new: Return the data necessary to create a new OLD
        form search.
        """
        """Return the data necessary to create a new form search.

        :URL: ``GET /formsearches/new`` with optional query string parameters 
        :returns: A dictionary of lists of resources

        """
        return {
            'search_parameters': h.get_search_parameters(self.query_builder)
        }
Example #10
0
    def new_search_corpora(self):
        """Return the data necessary to search across corpus resources.

        :URL: ``GET /corpora/new_search_corpora``
        :returns: ``{"search_parameters": {"attributes": { ... }, "relations": { ... }}``

        .. note::

            Contrast this action with `new_search`, which returns the data
            needed to search across the forms of a corpus.

        """
        return {'search_parameters':
            h.get_search_parameters(self.query_builder_for_ordering)}
Example #11
0
    def new_search_corpora(self):
        """Return the data necessary to search across corpus resources.

        :URL: ``GET /corpora/new_search_corpora``
        :returns: ``{"search_parameters": {"attributes": { ... }, "relations": { ... }}``

        .. note::

            Contrast this action with `new_search`, which returns the data
            needed to search across the forms of a corpus.

        """
        return {
            'search_parameters':
            h.get_search_parameters(self.query_builder_for_ordering)
        }
Example #12
0
    def test_search(self):
        """Tests that corpora search works correctly.

        """

        # Create a corpus defined by ``content`` that contains all sentences
        # with five or more words.

        # Get ids of all sentences with more than 5 words.
        long_sentences = Session.query(model.Form).\
            filter(and_(
                model.Form.syntactic_category.has(model.SyntacticCategory.name==u'S'),
                model.Form.transcription.op('regexp')(u'^([^ ]+ ){5}[^ ]+'))).all()
        long_sentence = long_sentences[0]
        len_long_sentences = len(long_sentences)
        long_sentence_ids = [f.id for f in long_sentences]
        long_sentences = u','.join(map(str, long_sentence_ids))

        # Restrict one of the forms that will be in the corpus.
        restricted_tag = h.get_restricted_tag()
        long_sentence.tags.append(restricted_tag)
        Session.add(long_sentence)
        Session.commit()

        # Create the corpus
        name = u'Sentences with 6 or more words.'
        params = self.corpus_create_params.copy()
        params.update({
            'name': name,
            'content': long_sentences
        })
        params = json.dumps(params)
        original_corpus_count = Session.query(Corpus).count()
        response = self.app.post(url('corpora'), params, self.json_headers,
                                 self.extra_environ_admin)
        resp = json.loads(response.body)
        corpus_id = resp['id']
        new_corpus_count = Session.query(Corpus).count()
        corpus = Session.query(Corpus).get(corpus_id)
        corpus_dir = os.path.join(self.corpora_path, 'corpus_%d' % corpus_id)
        corpus_dir_contents = os.listdir(corpus_dir)
        assert new_corpus_count == original_corpus_count + 1
        assert resp['name'] == name
        assert corpus_dir_contents == []
        assert response.content_type == 'application/json'
        assert resp['content'] == long_sentences
        # The ``forms`` attribute is a collection, no repeats, that's why the following is true:
        assert len(corpus.forms) == len_long_sentences

        # Search the corpus for forms beginning in vowels.
        query = json.dumps({"query": {"filter": ['Form', 'transcription', 'regex', '^[AEIOUaeiou]']},
                "paginator": {'page': 1, 'items_per_page': 10}})
        response = self.app.post(url('/corpora/%d/search' % corpus_id), query,
            self.json_headers, self.extra_environ_admin)
        resp = json.loads(response.body)
        matches = resp['items']
        assert not set([f['id'] for f in matches]) - set(long_sentence_ids)
        assert not filter(
                lambda f: f['transcription'][0].lower() not in ['a', 'e', 'i', 'o', 'u'], matches)
        assert not filter(lambda f: len(f['transcription'].split(' ')) < 6, matches)

        # Vacuous search of the corpus returns everything.
        query = json.dumps({"query": {"filter": ['Form', 'transcription', 'like', '%']}})
        response = self.app.post(url('/corpora/%d/search' % corpus_id), query,
            self.json_headers, self.extra_environ_admin)
        resp = json.loads(response.body)
        assert set([f['id'] for f in resp]) == set(long_sentence_ids)

        # Vacuous search as the viewer returns everything that is not restricted.
        query = json.dumps({"query": {"filter": ['Form', 'transcription', 'like', '%']}})
        response = self.app.post(url('/corpora/%d/search' % corpus_id), query,
            self.json_headers, self.extra_environ_view)
        resp2 = json.loads(response.body)
        # Viewer will get 1 or 2 forms fewer (2 are restricted, 1 assuredly a long sentence.)
        assert len(resp) > len(resp2)

        # Failed search with an invalid corpus id
        query = json.dumps({"query": {"filter": ['Form', 'transcription', 'like', '%']}})
        response = self.app.post(url('/corpora/123456789/search'), query,
            self.json_headers, self.extra_environ_admin, status=404)
        resp = json.loads(response.body)
        assert resp['error'] == u'There is no corpus with id 123456789'

        # Failed search with an invalid query
        query = json.dumps({"query": {"filter": ['Form', 'thingamafracasicle', 'like', '%']}})
        response = self.app.post(url('/corpora/%d/search' % corpus_id), query,
            self.json_headers, self.extra_environ_admin, status=400)
        resp = json.loads(response.body)
        assert resp['errors']['Form.thingamafracasicle'] == 'There is no attribute thingamafracasicle of Form'

        # Request GET /corpora/new_search
        response = self.app.get(url(controller='corpora', action='new_search'),
            headers=self.json_headers, extra_environ=self.extra_environ_admin)
        resp = json.loads(response.body)
        assert resp == {'search_parameters': h.get_search_parameters(SQLAQueryBuilder('Form'))}
Example #13
0
    def test_search(self):
        """Tests that corpora search works correctly.

        """

        # Create a corpus defined by ``content`` that contains all sentences
        # with five or more words.

        # Get ids of all sentences with more than 5 words.
        long_sentences = Session.query(model.Form).\
            filter(and_(
                model.Form.syntactic_category.has(model.SyntacticCategory.name==u'S'),
                model.Form.transcription.op('regexp')(u'^([^ ]+ ){5}[^ ]+'))).all()
        long_sentence = long_sentences[0]
        len_long_sentences = len(long_sentences)
        long_sentence_ids = [f.id for f in long_sentences]
        long_sentences = u','.join(map(str, long_sentence_ids))

        # Restrict one of the forms that will be in the corpus.
        restricted_tag = h.get_restricted_tag()
        long_sentence.tags.append(restricted_tag)
        Session.add(long_sentence)
        Session.commit()

        # Create the corpus
        name = u'Sentences with 6 or more words.'
        params = self.corpus_create_params.copy()
        params.update({'name': name, 'content': long_sentences})
        params = json.dumps(params)
        original_corpus_count = Session.query(Corpus).count()
        response = self.app.post(url('corpora'), params, self.json_headers,
                                 self.extra_environ_admin)
        resp = json.loads(response.body)
        corpus_id = resp['id']
        new_corpus_count = Session.query(Corpus).count()
        corpus = Session.query(Corpus).get(corpus_id)
        corpus_dir = os.path.join(self.corpora_path, 'corpus_%d' % corpus_id)
        corpus_dir_contents = os.listdir(corpus_dir)
        assert new_corpus_count == original_corpus_count + 1
        assert resp['name'] == name
        assert corpus_dir_contents == []
        assert response.content_type == 'application/json'
        assert resp['content'] == long_sentences
        # The ``forms`` attribute is a collection, no repeats, that's why the following is true:
        assert len(corpus.forms) == len_long_sentences

        # Search the corpus for forms beginning in vowels.
        query = json.dumps({
            "query": {
                "filter": ['Form', 'transcription', 'regex', '^[AEIOUaeiou]']
            },
            "paginator": {
                'page': 1,
                'items_per_page': 10
            }
        })
        response = self.app.post(url('/corpora/%d/search' % corpus_id), query,
                                 self.json_headers, self.extra_environ_admin)
        resp = json.loads(response.body)
        matches = resp['items']
        assert not set([f['id'] for f in matches]) - set(long_sentence_ids)
        assert not filter(
            lambda f: f['transcription'][0].lower() not in
            ['a', 'e', 'i', 'o', 'u'], matches)
        assert not filter(lambda f: len(f['transcription'].split(' ')) < 6,
                          matches)

        # Vacuous search of the corpus returns everything.
        query = json.dumps(
            {"query": {
                "filter": ['Form', 'transcription', 'like', '%']
            }})
        response = self.app.post(url('/corpora/%d/search' % corpus_id), query,
                                 self.json_headers, self.extra_environ_admin)
        resp = json.loads(response.body)
        assert set([f['id'] for f in resp]) == set(long_sentence_ids)

        # Vacuous search as the viewer returns everything that is not restricted.
        query = json.dumps(
            {"query": {
                "filter": ['Form', 'transcription', 'like', '%']
            }})
        response = self.app.post(url('/corpora/%d/search' % corpus_id), query,
                                 self.json_headers, self.extra_environ_view)
        resp2 = json.loads(response.body)
        # Viewer will get 1 or 2 forms fewer (2 are restricted, 1 assuredly a long sentence.)
        assert len(resp) > len(resp2)

        # Failed search with an invalid corpus id
        query = json.dumps(
            {"query": {
                "filter": ['Form', 'transcription', 'like', '%']
            }})
        response = self.app.post(url('/corpora/123456789/search'),
                                 query,
                                 self.json_headers,
                                 self.extra_environ_admin,
                                 status=404)
        resp = json.loads(response.body)
        assert resp['error'] == u'There is no corpus with id 123456789'

        # Failed search with an invalid query
        query = json.dumps(
            {"query": {
                "filter": ['Form', 'thingamafracasicle', 'like', '%']
            }})
        response = self.app.post(url('/corpora/%d/search' % corpus_id),
                                 query,
                                 self.json_headers,
                                 self.extra_environ_admin,
                                 status=400)
        resp = json.loads(response.body)
        assert resp['errors'][
            'Form.thingamafracasicle'] == 'There is no attribute thingamafracasicle of Form'

        # Request GET /corpora/new_search
        response = self.app.get(url(controller='corpora', action='new_search'),
                                headers=self.json_headers,
                                extra_environ=self.extra_environ_admin)
        resp = json.loads(response.body)
        assert resp == {
            'search_parameters':
            h.get_search_parameters(SQLAQueryBuilder('Form'))
        }