def test_elasticsearch_index_creation_should_failed_when_no_index_is_provided(
     self, elasticsearch_mock
 ):
     interface = ElasticSearchInterface(["127.0.0.1"])
     interface._es.indices = MagicMock()
     interface._es.indices.exists = MagicMock()
     with self.assertRaisesRegex(Exception, "Index name not defined"):
         interface.create_index()
 def test_elasticsearch_index_creation_should_check_if_index_exists(
     self, elasticsearch_mock
 ):
     interface = ElasticSearchInterface(["127.0.0.1"])
     interface._es.indices = MagicMock()
     interface._es.indices.exists = MagicMock()
     interface.create_index("querido-diario")
     interface._es.indices.exists.assert_called_once_with(index="querido-diario")
 def test_upload_document_to_index(self, elasticsearch_mock):
     interface = ElasticSearchInterface(["127.0.0.1"])
     document_checksum = str(uuid.uuid1())
     interface.index_document(self.fake_document, "querido-diario")
     interface._es.index.assert_called_once_with(
         index="querido-diario",
         body=self.fake_document,
         id=self.fake_document["file_checksum"],
     )
 def test_elasticsearch_index_creation_should_not_recreate_index_if_it_exists(
     self, elasticsearch_mock
 ):
     interface = ElasticSearchInterface(["127.0.0.1"])
     interface._es.indices = MagicMock()
     interface._es.indices.exists = MagicMock(return_value=True)
     interface._es.indices.create = MagicMock()
     interface.create_index("querido-diario")
     interface._es.indices.exists.assert_called_once_with(index="querido-diario")
     interface._es.indices.create.assert_not_called()
 def test_elasticsearch_index_creation_with_default_index_value(
     self, elasticsearch_mock
 ):
     interface = ElasticSearchInterface(
         ["127.0.0.1"], default_index="querido-diario2"
     )
     interface._es.indices = MagicMock()
     interface._es.indices.exists = MagicMock()
     interface.create_index()
     interface._es.indices.exists.assert_called_once_with(index="querido-diario2")
 def test_elasticsearch_index_should_allow_change_default_timeout(
     self, elasticsearch_mock
 ):
     interface = ElasticSearchInterface(["127.0.0.1"], timeout="2m")
     interface._es.indices = MagicMock()
     interface._es.indices.exists = MagicMock(return_value=False)
     interface._es.indices.create = MagicMock()
     interface.create_index("querido-diario")
     interface._es.indices.create.assert_called_once_with(
         index="querido-diario",
         body={"mappings": {"properties": {"date": {"type": "date"}}}},
         timeout="2m",
     )
 def test_elasticsearch_should_create_index_if_it_does_not_exists(
     self, elasticsearch_mock
 ):
     interface = ElasticSearchInterface(["127.0.0.1"])
     interface._es.indices = MagicMock()
     interface._es.indices.exists = MagicMock(return_value=False)
     interface._es.indices.create = MagicMock()
     interface.create_index("querido-diario")
     interface._es.indices.exists.assert_called_once_with(index="querido-diario")
     interface._es.indices.create.assert_called_once_with(
         index="querido-diario",
         body={"mappings": {"properties": {"date": {"type": "date"}}}},
         timeout="30s",
     )
 def test_elasticsearch_should_create_index_with_default_value_with_function_has_no_arguments(
     self, elasticsearch_mock
 ):
     interface = ElasticSearchInterface(
         ["127.0.0.1"], default_index="querido-diario2"
     )
     interface._es.indices = MagicMock()
     interface._es.indices.exists = MagicMock(return_value=False)
     interface._es.indices.create = MagicMock()
     interface.create_index()
     interface._es.indices.exists.assert_called_once_with(index="querido-diario2")
     interface._es.indices.create.assert_called_once_with(
         index="querido-diario2",
         body={"mappings": {"properties": {"date": {"type": "date"}}}},
         timeout="30s",
     )
    def test_index_document(self):
        self.clean_index("querido-diario")
        interface = ElasticSearchInterface(["127.0.0.1"])
        interface.index_document(self.fake_document, "querido-diario")
        self._es.indices.refresh(index="querido-diario")

        self.assertEqual(self._es.count(index="querido-diario")["count"], 1)
        self.assertTrue(
            self._es.exists(
                id=self.fake_document["file_checksum"], index="querido-diario"
            )
        )
        indexed_document = self._es.get(
            index="querido-diario", id=self.fake_document["file_checksum"]
        )
        self.fake_document["date"] = self.fake_document["date"].strftime("%Y-%m-%d")
        self.fake_document["scraped_at"] = self.fake_document["scraped_at"].isoformat()
        self.fake_document["created_at"] = self.fake_document["created_at"].isoformat()
        self.assertEqual(indexed_document["_source"], self.fake_document)
 def test_elasticsearch_connection(self, elasticsearch_mock):
     interface = ElasticSearchInterface(["127.0.0.1"])
     elasticsearch_mock.assert_called_once_with(hosts=["127.0.0.1"])
 def test_elasticsearch_should_implement_index_interface(self):
     self.assertIsInstance(ElasticSearchInterface([]), IndexInterface)
    def test_index_creation(self):
        self.delete_index("querido-diario")

        interface = ElasticSearchInterface(["127.0.0.1"], timeout="5m")
        interface.create_index("querido-diario")
        self.assertTrue(self._es.indices.exists("querido-diario"))