コード例 #1
0
 def test_get_website_document_with_error(self, mock_elastich):
     mock_elastich.side_effect = mock.Mock(
         side_effect=ElasticsearchException())  # noqa
     with self.assertRaises(ElasticsearchException):
         webiste_nosql = WebsiteNoSql()
         webiste_nosql.get(1)
     self.assertEqual(mock_elastich.call_count, 1)
コード例 #2
0
 def test_update_website_document_with_error(self, mock_elastich):
     mock_elastich.side_effect = mock.Mock(
         side_effect=ElasticsearchException())  # noqa
     with self.assertRaises(ElasticsearchException):
         body = {'urls': ['http://vtrmantovani.com.br']}
         webiste_nosql = WebsiteNoSql()
         webiste_nosql.update(1, body)
     self.assertEqual(mock_elastich.call_count, 1)
コード例 #3
0
    def test_save_website_links_without_website_done(self):
        db.session.add(self.website)
        db.session.commit()
        list_urls = ['http://ibm.com.br']

        website_backend = WebsiteBackend()
        website_backend._save_website_links(self.website, list_urls)
        webiste_nosql = WebsiteNoSql()

        self.assertEqual(Website.query.count(), 2)
        self.assertTrue(webiste_nosql.get(1))
        webiste_nosql.delete(1)
コード例 #4
0
    def test_update_website_document(self):
        webiste_nosql = WebsiteNoSql()
        body = {'urls': ['http://ibm.com.br']}
        webiste_nosql.create(4, body)

        body = {'urls': ['http://vtrmantovani.com.br']}
        webiste_nosql.update(4, body)
        bd_web_site = webiste_nosql.get(4)
        self.assertEquals(bd_web_site['_source'], body)
        webiste_nosql.delete(4)
コード例 #5
0
    def test_delete_website_document(self):
        webiste_nosql = WebsiteNoSql()
        body = {'urls': ['http://ibm.com.br']}
        webiste_nosql.create(2, body)

        webiste_nosql = WebsiteNoSql()
        webiste_nosql.delete(2)
コード例 #6
0
def get(p_website_id):
    try:
        website_db = WebsiteNoSql()
        website = website_db.get(p_website_id)
        if not website:
            raise NotFound("Website id not found")

        urls = website['_source']
        if not urls:
            logger.error("Source not found on website_id={}".format(
                p_website_id))  # noqa
            raise BadRequestGeneric(description="Source not found")
    except ElasticsearchException as e:
        logger.error(
            "ElasticsearchException error in get website_id:{0}. Error:{1}".
            format(p_website_id, e))  # noqa
        raise BadRequestGeneric(description="Some problems in bd")

    return jsonify(website['_source'])
コード例 #7
0
    def test_get_website_document(self):
        webiste_nosql = WebsiteNoSql()
        body = {'urls': ['http://ibm.com.br']}
        webiste_nosql.create(3, body)

        bd_web_site = webiste_nosql.get(3)
        body = {'urls': ['http://ibm.com.br']}
        self.assertEquals(bd_web_site['_source'], body)
        webiste_nosql.delete(3)
コード例 #8
0
    def test_search_website_with_status_done(self):
        website = Website()
        website.url = "http://vtrmantovani.com.br"
        website.status = Website.Status.DONE
        db.session.add(website)
        db.session.commit()
        self.assertEqual(Website.query.count(), 1)
        webiste_nosql = WebsiteNoSql()
        body = {'urls': ['http://ibm.com.br']}
        webiste_nosql.create(1, body)

        params = {"status": "DONE", "limit": 2, "offset": 0}
        response = self.client.post("/api/search",
                                    headers={
                                        'Authorization': self.api_key,
                                        'Content-Type': 'application/json'
                                    },
                                    data=json.dumps(params))
        r = json.loads(response.data.decode('utf-8'))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(r['websites'][0]['website'],
                         "http://vtrmantovani.com.br")  # noqa
        self.assertEqual(r['websites'][0]['urls'][0], "http://ibm.com.br")
        self.assertEqual(r['total_itens'], 1)
        webiste_nosql.delete(1)
コード例 #9
0
def search():
    r = request.json
    status = r['status']
    limit = r['limit']
    offset = r['offset']
    list_websites = []

    try:
        website_list = Website.query.filter(Website.status == status)\
            .limit(limit).offset(offset).all()

        total_itens = Website.query.filter(Website.status == status).count()

        list_urls = []
        for website in website_list:
            if website.status == Website.Status.DONE:
                website_db = WebsiteNoSql()
                website_no_sql = website_db.get(website.id)
                if not website_no_sql:
                    logger.error(
                        "Website not found in search website_id={}".format(
                            website.id))  # noqa
                    raise NotFound("Website id not found")

                urls = website_no_sql['_source']
                list_urls = urls['urls']

            list_websites.append({
                'id': website.id,
                'website': website.url,
                'urls': list_urls
            })
    except ElasticsearchException as e:
        logger.error(
            "ElasticsearchException error in search website, Error: {0}".
            format(e))  # noqa
        raise BadRequestGeneric(description="Some problems in bd")

    return jsonify({'websites': list_websites, 'total_itens': total_itens})
コード例 #10
0
 def test_get_website(self):
     webiste_nosql = WebsiteNoSql()
     body = {'urls': ['http://ibm.com.br']}
     webiste_nosql.create(1, body)
     response = self.client.get(
         '/api/website/1',
         headers={'Authorization': 'PXaLogin apikey="xpo"'})
     r = json.loads(response.data.decode('utf-8'))
     self.assertEquals(r['urls'][0], 'http://ibm.com.br')
     self.assertEqual(response.status_code, 200)
     webiste_nosql.delete(1)
コード例 #11
0
    def test_save_website_links_without_website_nosql_differents_urls(self):
        self.website.status = Website.Status.DONE
        db.session.add(self.website)
        db.session.commit()

        webiste_nosql = WebsiteNoSql()
        list_urls = ['http://vtrmantovani.com.br']
        body = {'urls': list_urls}
        webiste_nosql.create(1, body)

        website_backend = WebsiteBackend()
        website_backend._save_website_links(self.website, list_urls)

        self.assertEqual(Website.query.count(), 1)
        self.assertTrue(webiste_nosql.get(1))
        webiste_nosql.delete(1)
コード例 #12
0
    def _save_website_links(self, website, list_urls):
        website_nosql = WebsiteNoSql()
        body = {'urls': list_urls}

        check_website_exist_done = self._check_if_website_exist_done(
            website.url)  # noqa

        if check_website_exist_done is False:
            self._put_website_links_on_quee(list_urls)
            website_nosql.create(website.id, body)
        else:
            website_done = Website.query\
                .filter(Website.url == website.url)\
                .filter(Website.status == Website.Status.DONE).first()
            website_nosql_db = website_nosql.get(website_done.id)
            if website_nosql_db:
                if website_nosql_db['_source'] != body:
                    self._put_website_links_on_quee(list_urls)
                    website_nosql.create(website.id, body)
                    website_nosql.update(website_done.id, body)
                else:
                    website_nosql.create(website.id, body)
            else:
                logger.error(
                    "Website NoSql not found: on website_id {0}".format(
                        website_done.id))  # noqa
                raise WebsiteBackendException("Website NoSql not found")
コード例 #13
0
 def test_get_website_document_not_found(self):
     webiste_nosql = WebsiteNoSql()
     bd_web_site = webiste_nosql.get(1)
     self.assertEquals(bd_web_site, None)