Skip to content

Commit

Permalink
fix: delete and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
gusye1234 committed Sep 12, 2024
1 parent 0da0551 commit 6122847
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: test

on:
push:
branches:
- main
- dev
paths-ignore:
- '**/*.md'
- '**/*.ipynb'
- 'examples/**'
pull_request:
branches:
- main
- dev
paths-ignore:
- '**/*.md'
- '**/*.ipynb'
- 'examples/**'

jobs:
test:
name: Tests on ${{ matrix.os }} for ${{ matrix.python-version }}
strategy:
matrix:
python-version: [3.9, 3.11]
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements-dev.txt
- name: Lint with flake8
run: |
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
- name: Build and Test
run: |
python -m pytest -o log_cli=true -o log_cli_level="INFO" --cov=nano_vectordb --cov-report=xml -v ./
- name: Check codecov file
id: check_files
uses: andstor/file-existence-action@v1
with:
files: './coverage.xml'
- name: Upload coverage from test to Codecov
uses: codecov/codecov-action@v2
with:
file: ./coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion nano_vectordb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .dbs import NanoVectorDB

__version__ = "0.0.2"
__version__ = "0.0.3"
__author__ = "Jianbai Ye"
__url__ = "https://github.com/gusye1234/nano-vectordb"
5 changes: 3 additions & 2 deletions nano_vectordb/dbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ def delete(self, ids: list[str]):
if data[f_ID] in ids:
delete_index.append(i)
ids.remove(data[f_ID])
if len(ids) == 0:
break
else:
left_data.append(data)
self.__storage["data"] = left_data
Expand All @@ -136,6 +134,9 @@ def save(self):
with open(self.storage_file, "w") as f:
json.dump(storage, f, ensure_ascii=False)

def __len__(self):
return len(self.__storage["data"])

def query(
self, query: np.ndarray, top_k: int = 10, better_than_threshold: float = None
):
Expand Down
4 changes: 3 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pytest
pytest
pytest-cov
flake8
33 changes: 31 additions & 2 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ def test_init():
a = NanoVectorDB(fake_dim)

start = time()
r = a.query(np.random.rand(fake_dim), 10)
r = a.query(np.random.rand(fake_dim), 10, better_than_threshold=0.01)
print("Query", time() - start)
assert len(r) > 0
print(r)
os.remove("nano-vectordb.json")

Expand All @@ -47,3 +46,33 @@ def test_same_upsert():
fakes_data = [{"__vector__": fake_embeds[i]} for i in range(data_len)]
r2 = a.upsert(fakes_data)
assert r2["update"] == r1["insert"]


def test_get():
a = NanoVectorDB(1024)
a.upsert(
[
{"__vector__": np.random.rand(1024), "__id__": str(i), "content": i}
for i in range(100)
]
)
r = a.get(["0", "1", "2"])
assert len(r) == 3
assert r[0]["content"] == 0
assert r[1]["content"] == 1
assert r[2]["content"] == 2


def test_delete():
a = NanoVectorDB(1024)
a.upsert(
[
{"__vector__": np.random.rand(1024), "__id__": str(i), "content": i}
for i in range(100)
]
)
a.delete(["0", "50", "99"])

r = a.get(["0", "50", "99"])
assert len(r) == 0
assert len(a) == 97

0 comments on commit 6122847

Please sign in to comment.