Skip to content

Commit

Permalink
change import paths (#256)
Browse files Browse the repository at this point in the history
* change import paths

* fix tests

* leftover
  • Loading branch information
masci authored Jan 23, 2024
1 parent 430b24a commit 89e1c2f
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 91 deletions.
18 changes: 11 additions & 7 deletions integrations/pinecone/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Documentation = "https://github.com/deepset-ai/haystack-core-integrations/tree/m
Issues = "https://github.com/deepset-ai/haystack-core-integrations/issues"
Source = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/pinecone"

[tool.hatch.build.targets.wheel]
packages = ["src/haystack_integrations"]

[tool.hatch.version]
source = "vcs"
tag-pattern = 'integrations\/pinecone-v(?P<version>.*)'
Expand Down Expand Up @@ -74,7 +77,7 @@ dependencies = [
"numpy",
]
[tool.hatch.envs.lint.scripts]
typing = "mypy --install-types --non-interactive {args:src/pinecone_haystack tests}"
typing = "mypy --install-types --non-interactive --explicit-package-bases {args:src/ tests}"
style = [
"ruff {args:.}",
"black --check --diff {args:.}",
Expand Down Expand Up @@ -143,26 +146,26 @@ unfixable = [
]

[tool.ruff.isort]
known-first-party = ["pinecone_haystack"]
known-first-party = ["haystack_integrations"]

[tool.ruff.flake8-tidy-imports]
ban-relative-imports = "all"
ban-relative-imports = "parents"

[tool.ruff.per-file-ignores]
# Tests can use magic values, assertions, and relative imports
"tests/**/*" = ["PLR2004", "S101", "TID252"]

[tool.coverage.run]
source_pkgs = ["pinecone_haystack", "tests"]
source_pkgs = ["src", "tests"]
branch = true
parallel = true
omit = [
"example"
"examples"
]

[tool.coverage.paths]
pinecone_haystack = ["src/pinecone_haystack", "*/pinecone_haystack/src/pinecone_haystack"]
tests = ["tests", "*/pinecone_haystack/tests"]
pinecone_haystack = ["src/*"]
tests = ["tests"]

[tool.coverage.report]
exclude_lines = [
Expand All @@ -182,6 +185,7 @@ markers = [
module = [
"pinecone.*",
"haystack.*",
"haystack_integrations.*",
"pytest.*"
]
ignore_missing_imports = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .dense_retriever import PineconeDenseRetriever

__all__ = ["PineconeDenseRetriever"]
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from haystack import component, default_from_dict, default_to_dict
from haystack.dataclasses import Document

from pinecone_haystack.document_store import PineconeDocumentStore
from haystack_integrations.document_stores.pinecone import PineconeDocumentStore


@component
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-FileCopyrightText: 2023-present deepset GmbH <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0
from .document_store import PineconeDocumentStore

__all__ = ["PineconeDocumentStore"]
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
from typing import Any, Dict, List, Optional

import pandas as pd
import pinecone
from haystack import default_to_dict
from haystack.dataclasses import Document
from haystack.document_stores.types import DuplicatePolicy
from haystack.utils.filters import convert

from pinecone_haystack.filters import _normalize_filters
import pinecone

from .filters import _normalize_filters

logger = logging.getLogger(__name__)

Expand Down
7 changes: 0 additions & 7 deletions integrations/pinecone/src/pinecone_haystack/__init__.py

This file was deleted.

2 changes: 1 addition & 1 deletion integrations/pinecone/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from haystack.document_stores.types import DuplicatePolicy

from pinecone_haystack.document_store import PineconeDocumentStore
from haystack_integrations.document_stores.pinecone import PineconeDocumentStore

# This is the approximate time it takes for the documents to be available
SLEEP_TIME = 20
Expand Down
16 changes: 8 additions & 8 deletions integrations/pinecone/tests/test_dense_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from haystack.dataclasses import Document

from pinecone_haystack.dense_retriever import PineconeDenseRetriever
from pinecone_haystack.document_store import PineconeDocumentStore
from haystack_integrations.components.retrievers.pinecone import PineconeDenseRetriever
from haystack_integrations.document_stores.pinecone import PineconeDocumentStore


def test_init_default():
Expand All @@ -17,7 +17,7 @@ def test_init_default():
assert retriever.top_k == 10


@patch("pinecone_haystack.document_store.pinecone")
@patch("haystack_integrations.document_stores.pinecone.document_store.pinecone")
def test_to_dict(mock_pinecone):
mock_pinecone.Index.return_value.describe_index_stats.return_value = {"dimension": 512}
document_store = PineconeDocumentStore(
Expand All @@ -31,7 +31,7 @@ def test_to_dict(mock_pinecone):
retriever = PineconeDenseRetriever(document_store=document_store)
res = retriever.to_dict()
assert res == {
"type": "pinecone_haystack.dense_retriever.PineconeDenseRetriever",
"type": "haystack_integrations.components.retrievers.pinecone.dense_retriever.PineconeDenseRetriever",
"init_parameters": {
"document_store": {
"init_parameters": {
Expand All @@ -41,18 +41,18 @@ def test_to_dict(mock_pinecone):
"batch_size": 50,
"dimension": 512,
},
"type": "pinecone_haystack.document_store.PineconeDocumentStore",
"type": "haystack_integrations.document_stores.pinecone.document_store.PineconeDocumentStore",
},
"filters": {},
"top_k": 10,
},
}


@patch("pinecone_haystack.document_store.pinecone")
@patch("haystack_integrations.document_stores.pinecone.document_store.pinecone")
def test_from_dict(mock_pinecone, monkeypatch):
data = {
"type": "pinecone_haystack.dense_retriever.PineconeDenseRetriever",
"type": "haystack_integrations.components.retrievers.pinecone.dense_retriever.PineconeDenseRetriever",
"init_parameters": {
"document_store": {
"init_parameters": {
Expand All @@ -62,7 +62,7 @@ def test_from_dict(mock_pinecone, monkeypatch):
"batch_size": 50,
"dimension": 512,
},
"type": "pinecone_haystack.document_store.PineconeDocumentStore",
"type": "haystack_integrations.document_stores.pinecone.document_store.PineconeDocumentStore",
},
"filters": {},
"top_k": 10,
Expand Down
134 changes: 69 additions & 65 deletions integrations/pinecone/tests/test_document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,75 @@
from haystack import Document
from haystack.testing.document_store import CountDocumentsTest, DeleteDocumentsTest, WriteDocumentsTest

from pinecone_haystack.document_store import PineconeDocumentStore


from haystack_integrations.document_stores.pinecone import PineconeDocumentStore


@patch("haystack_integrations.document_stores.pinecone.document_store.pinecone")
def test_init(mock_pinecone):
mock_pinecone.Index.return_value.describe_index_stats.return_value = {"dimension": 30}

document_store = PineconeDocumentStore(
api_key="fake-api-key",
environment="gcp-starter",
index="my_index",
namespace="test",
batch_size=50,
dimension=30,
metric="euclidean",
)

mock_pinecone.init.assert_called_with(api_key="fake-api-key", environment="gcp-starter")

assert document_store.environment == "gcp-starter"
assert document_store.index == "my_index"
assert document_store.namespace == "test"
assert document_store.batch_size == 50
assert document_store.dimension == 30
assert document_store.index_creation_kwargs == {"metric": "euclidean"}


@patch("haystack_integrations.document_stores.pinecone.document_store.pinecone")
def test_init_api_key_in_environment_variable(mock_pinecone, monkeypatch):
monkeypatch.setenv("PINECONE_API_KEY", "fake-api-key")

PineconeDocumentStore(
environment="gcp-starter",
index="my_index",
namespace="test",
batch_size=50,
dimension=30,
metric="euclidean",
)

mock_pinecone.init.assert_called_with(api_key="fake-api-key", environment="gcp-starter")


@patch("haystack_integrations.document_stores.pinecone.document_store.pinecone")
def test_to_dict(mock_pinecone):
mock_pinecone.Index.return_value.describe_index_stats.return_value = {"dimension": 30}
document_store = PineconeDocumentStore(
api_key="fake-api-key",
environment="gcp-starter",
index="my_index",
namespace="test",
batch_size=50,
dimension=30,
metric="euclidean",
)
assert document_store.to_dict() == {
"type": "haystack_integrations.document_stores.pinecone.document_store.PineconeDocumentStore",
"init_parameters": {
"environment": "gcp-starter",
"index": "my_index",
"dimension": 30,
"namespace": "test",
"batch_size": 50,
"metric": "euclidean",
},
}


@pytest.mark.integration
class TestDocumentStore(CountDocumentsTest, DeleteDocumentsTest, WriteDocumentsTest):
def test_write_documents(self, document_store: PineconeDocumentStore):
docs = [Document(id="1")]
Expand All @@ -21,44 +87,6 @@ def test_write_documents_duplicate_fail(self, document_store: PineconeDocumentSt
def test_write_documents_duplicate_skip(self, document_store: PineconeDocumentStore):
...

@patch("pinecone_haystack.document_store.pinecone")
def test_init(self, mock_pinecone):
mock_pinecone.Index.return_value.describe_index_stats.return_value = {"dimension": 30}

document_store = PineconeDocumentStore(
api_key="fake-api-key",
environment="gcp-starter",
index="my_index",
namespace="test",
batch_size=50,
dimension=30,
metric="euclidean",
)

mock_pinecone.init.assert_called_with(api_key="fake-api-key", environment="gcp-starter")

assert document_store.environment == "gcp-starter"
assert document_store.index == "my_index"
assert document_store.namespace == "test"
assert document_store.batch_size == 50
assert document_store.dimension == 30
assert document_store.index_creation_kwargs == {"metric": "euclidean"}

@patch("pinecone_haystack.document_store.pinecone")
def test_init_api_key_in_environment_variable(self, mock_pinecone, monkeypatch):
monkeypatch.setenv("PINECONE_API_KEY", "fake-api-key")

PineconeDocumentStore(
environment="gcp-starter",
index="my_index",
namespace="test",
batch_size=50,
dimension=30,
metric="euclidean",
)

mock_pinecone.init.assert_called_with(api_key="fake-api-key", environment="gcp-starter")

def test_init_fails_wo_api_key(self, monkeypatch):
api_key = None
monkeypatch.delenv("PINECONE_API_KEY", raising=False)
Expand All @@ -69,30 +97,6 @@ def test_init_fails_wo_api_key(self, monkeypatch):
index="my_index",
)

@patch("pinecone_haystack.document_store.pinecone")
def test_to_dict(self, mock_pinecone):
mock_pinecone.Index.return_value.describe_index_stats.return_value = {"dimension": 30}
document_store = PineconeDocumentStore(
api_key="fake-api-key",
environment="gcp-starter",
index="my_index",
namespace="test",
batch_size=50,
dimension=30,
metric="euclidean",
)
assert document_store.to_dict() == {
"type": "pinecone_haystack.document_store.PineconeDocumentStore",
"init_parameters": {
"environment": "gcp-starter",
"index": "my_index",
"dimension": 30,
"namespace": "test",
"batch_size": 50,
"metric": "euclidean",
},
}

def test_embedding_retrieval(self, document_store: PineconeDocumentStore):
query_embedding = [0.1] * 768
most_similar_embedding = [0.8] * 768
Expand Down
1 change: 1 addition & 0 deletions integrations/pinecone/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)


@pytest.mark.integration
class TestFilters(FilterDocumentsTest):
def assert_documents_are_equal(self, received: List[Document], expected: List[Document]):
for doc in received:
Expand Down

0 comments on commit 89e1c2f

Please sign in to comment.