示例#1
0
    def test_add_multiple_documents_to_corpus(self, mocked_post):
        example_document_2 = self.example_document.copy()
        example_document_2['url'] = "http://pypln.example.com/documents/2/"

        mock1, mock2 = Mock(), Mock()
        mock1.status_code = mock2.status_code = 201
        mock1.json.return_value = self.example_document
        mock2.json.return_value = example_document_2
        mocked_post.side_effect = [mock1, mock2]

        corpus = Corpus(session=self.session, **self.example_json)
        result = corpus.add_documents(["content_1", "content_2"])
        documents, errors = result

        self.assertEqual(errors, [])

        self.assertIs(type(documents[0]), Document)
        self.assertIs(type(documents[1]), Document)

        for key, value in self.example_document.items():
            if key == 'properties':
                key = 'properties_url'
            self.assertEqual(value, getattr(documents[0], key))
        for key, value in example_document_2.items():
            if key == 'properties':
                key = 'properties_url'
            self.assertEqual(value, getattr(documents[1], key))
示例#2
0
    def test_add_multiple_documents_to_corpus(self, mocked_post):
        example_document_2 = self.example_document.copy()
        example_document_2['url'] = "http://pypln.example.com/documents/2/"

        mock1, mock2 = Mock(), Mock()
        mock1.status_code = mock2.status_code = 201
        mock1.json.return_value = self.example_document
        mock2.json.return_value = example_document_2
        mocked_post.side_effect = [mock1, mock2]

        corpus = Corpus(session=self.session, **self.example_json)
        result = corpus.add_documents(["content_1", "content_2"])
        documents, errors = result

        self.assertEqual(errors, [])

        self.assertIs(type(documents[0]), Document)
        self.assertIs(type(documents[1]), Document)

        for key, value in self.example_document.items():
            if key == 'properties':
                key = 'properties_url'
            self.assertEqual(value, getattr(documents[0], key))
        for key, value in example_document_2.items():
            if key == 'properties':
                key = 'properties_url'
            self.assertEqual(value, getattr(documents[1], key))
示例#3
0
    def test_adding_document_to_corpus_fails(self, mocked_post):
        mocked_post.return_value.status_code = 403

        session = requests.Session()
        session.auth = ('wrong_user', 'my_precious')
        corpus = Corpus(session=session, **self.example_json)
        with self.assertRaises(RuntimeError):
            corpus.add_document("example.pdf")
示例#4
0
    def test_compare_corpora_with_different_urls(self):
        corpus_1 = Corpus(session=None, **self.example_json)

        json_2 = self.example_json.copy()
        json_2['url'] = 'http://pypln.example.com.br/corpora/1/'
        corpus_2 = Corpus(session=None, **json_2)

        self.assertNotEqual(corpus_1, corpus_2)
示例#5
0
    def test_compare_corpora_with_different_owners(self):
        corpus_1 = Corpus(session=None, **self.example_json)

        json_2 = self.example_json.copy()
        json_2['owner'] = 'admin'
        corpus_2 = Corpus(session=None, **json_2)

        self.assertNotEqual(corpus_1, corpus_2)
示例#6
0
    def test_compare_corpora_with_different_creation_dates(self):
        corpus_1 = Corpus(session=None, **self.example_json)

        json_2 = self.example_json.copy()
        json_2['created_at'] = '2013-10-29T17:00:00.000Z'
        corpus_2 = Corpus(session=None, **json_2)

        self.assertNotEqual(corpus_1, corpus_2)
示例#7
0
    def test_compare_corpora_with_different_descriptions(self):
        corpus_1 = Corpus(session=None, **self.example_json)

        json_2 = self.example_json.copy()
        json_2['description'] = 'Test Corpus 2'
        corpus_2 = Corpus(session=None, **json_2)

        self.assertNotEqual(corpus_1, corpus_2)
示例#8
0
    def test_compare_corpora_with_different_names(self):
        corpus_1 = Corpus(session=None, **self.example_json)

        json_2 = self.example_json.copy()
        json_2['name'] = 'other_name'
        corpus_2 = Corpus(session=None, **json_2)

        self.assertNotEqual(corpus_1, corpus_2)
示例#9
0
    def test_adding_document_to_corpus_fails(self, mocked_post):
        mocked_post.return_value.status_code = 403

        session = requests.Session()
        session.auth = ('wrong_user', 'my_precious')
        corpus = Corpus(session=session, **self.example_json)
        with self.assertRaises(RuntimeError):
            corpus.add_document("example.pdf")
示例#10
0
    def test_instantiating_corpus_from_url_fails(self, mocked_get):
        mocked_get.return_value.status_code = 403
        mocked_get.return_value.json.return_value = self.example_json

        url = self.example_json['url']

        with self.assertRaises(RuntimeError):
            corpus = Corpus.from_url(url, ('wrong_user', 'my_precious'))
示例#11
0
    def test_instantiating_corpus_from_url_fails(self, mocked_get):
        mocked_get.return_value.status_code = 403
        mocked_get.return_value.json.return_value = self.example_json

        url = self.example_json['url']

        with self.assertRaises(RuntimeError):
            corpus = Corpus.from_url(url, ('wrong_user', 'my_precious'))
示例#12
0
    def test_adding_multiple_documents_returns_an_error(self, mocked_add_document):
        results = [self.example_document, RuntimeError]
        mocked_add_document.side_effect = results

        corpus = Corpus(session=self.session, **self.example_json)
        result = corpus.add_documents(["content_1", "content_2"])

        expected_calls = [call("content_1"), call("content_2")]
        mocked_add_document.assert_has_calls(expected_calls)

        expected = ([self.example_document], [("content_2", RuntimeError())])
        # How should we test this? The second element of the 'errors' tuple is
        # a different instance of RuntimeError, so it doesn't evaluate as equal
        # to the one raise in the mock. For now I'll just check everything
        # separatedly.
        self.assertEqual(result[0], expected[0])
        self.assertEqual(result[1][0][0], expected[1][0][0])
        self.assertIsInstance(expected[1][0][1], RuntimeError)
示例#13
0
    def test_adding_multiple_documents_returns_an_error(
            self, mocked_add_document):
        results = [self.example_document, RuntimeError]
        mocked_add_document.side_effect = results

        corpus = Corpus(session=self.session, **self.example_json)
        result = corpus.add_documents(["content_1", "content_2"])

        expected_calls = [call("content_1"), call("content_2")]
        mocked_add_document.assert_has_calls(expected_calls)

        expected = ([self.example_document], [("content_2", RuntimeError())])
        # How should we test this? The second element of the 'errors' tuple is
        # a different instance of RuntimeError, so it doesn't evaluate as equal
        # to the one raise in the mock. For now I'll just check everything
        # separatedly.
        self.assertEqual(result[0], expected[0])
        self.assertEqual(result[1][0][0], expected[1][0][0])
        self.assertIsInstance(expected[1][0][1], RuntimeError)
示例#14
0
    def test_add_document_to_corpus(self, mocked_post):
        mocked_post.return_value.status_code = 201
        mocked_post.return_value.json.return_value = self.example_document

        corpus = Corpus(session=self.session, **self.example_json)
        result = corpus.add_document("content.")

        # requests takes either a file-like object or a string. Both should
        # work.
        files = {"blob": "content."}
        data = {"corpus": corpus.url}

        mocked_post.assert_called_with("http://pypln.example.com/documents/",
                                       data=data, files=files)

        self.assertIs(type(result), Document)

        for key, value in self.example_document.items():
            if key == 'properties':
                key = 'properties_url'
            self.assertEqual(value, getattr(result, key))
示例#15
0
    def test_add_document_to_corpus(self, mocked_post):
        mocked_post.return_value.status_code = 201
        mocked_post.return_value.json.return_value = self.example_document

        corpus = Corpus(session=self.session, **self.example_json)
        result = corpus.add_document("content.")

        # requests takes either a file-like object or a string. Both should
        # work.
        files = {"blob": "content."}
        data = {"corpus": corpus.url}

        mocked_post.assert_called_with("http://pypln.example.com/documents/",
                                       data=data,
                                       files=files)

        self.assertIs(type(result), Document)

        for key, value in self.example_document.items():
            if key == 'properties':
                key = 'properties_url'
            self.assertEqual(value, getattr(result, key))
示例#16
0
    def test_instantiate_corpus_from_url(self, mocked_get):
        mocked_get.return_value.status_code = 200
        mocked_get.return_value.json.return_value = self.example_json

        url = self.example_json['url']

        corpus = Corpus.from_url(url, self.auth)

        mocked_get.assert_called_with(url)

        self.assertIsInstance(corpus, Corpus)

        for k, v in self.example_json.items():
            self.assertEqual(getattr(corpus, k), v)

        self.assertEqual(corpus.session.auth, self.auth)
示例#17
0
    def test_instantiate_corpus_from_url(self, mocked_get):
        mocked_get.return_value.status_code = 200
        mocked_get.return_value.json.return_value = self.example_json

        url = self.example_json['url']

        corpus = Corpus.from_url(url, self.auth)

        mocked_get.assert_called_with(url)

        self.assertIsInstance(corpus, Corpus)

        for k, v in self.example_json.items():
            self.assertEqual(getattr(corpus, k), v)

        self.assertEqual(corpus.session.auth, self.auth)
示例#18
0
    def test_instantiate_corpus_from_json(self):
        corpus = Corpus(session=None, **self.example_json)

        for k, v in self.example_json.items():
            self.assertEqual(getattr(corpus, k), v)
示例#19
0
    def test_compare_equal_corpora(self):
        corpus_1 = Corpus(session=None, **self.example_json)
        corpus_2 = Corpus(session=None, **self.example_json)

        self.assertEqual(corpus_1, corpus_2)