refactor: use by-filepath instead of entities

This commit is contained in:
arkohut 2024-06-04 16:01:27 +08:00
parent 9f3d0819f7
commit 890245d654
3 changed files with 16 additions and 17 deletions

View File

@ -126,7 +126,7 @@ def scan(library_id: int):
# Check if the entity already exists
get_response = httpx.get(
f"http://localhost:8080/libraries/{library_id}/entities",
f"http://localhost:8080/libraries/{library_id}/entities/by-filepath",
params={
"filepath": str(relative_file_path)
}, # Use relative path

View File

@ -88,19 +88,19 @@ def new_entity(
return entity
@app.get("/libraries/{library_id}/entities/{entity_id}", response_model=Entity)
def get_entity_by_id(library_id: int, entity_id: int, db: Session = Depends(get_db)):
entity = crud.get_entity_by_id(entity_id, db)
@app.get("/libraries/{library_id}/entities/by-filepath", response_model=Entity)
def get_entity_by_filepath(
library_id: int, filepath: str, db: Session = Depends(get_db)
):
entity = crud.get_entity_by_filepath(filepath, db)
if entity is None or entity.library_id != library_id:
raise HTTPException(status_code=404, detail="Entity not found")
return entity
@app.get("/libraries/{library_id}/entities", response_model=Entity)
def get_entity_by_filepath(
library_id: int, filepath: str, db: Session = Depends(get_db)
):
entity = crud.get_entity_by_filepath(filepath, db)
@app.get("/libraries/{library_id}/entities/{entity_id}", response_model=Entity)
def get_entity_by_id(library_id: int, entity_id: int, db: Session = Depends(get_db)):
entity = crud.get_entity_by_id(entity_id, db)
if entity is None or entity.library_id != library_id:
raise HTTPException(status_code=404, detail="Entity not found")
return entity

View File

@ -94,7 +94,7 @@ def test_new_entity(client):
# Create a new entity
new_entity = NewEntityParam(
filename="test_entity.txt",
filepath="/tmp/test_entity.txt",
filepath="test_entity.txt",
size=150,
file_created_at="2023-01-01T00:00:00",
file_last_modified_at="2023-01-01T00:00:00",
@ -109,7 +109,7 @@ def test_new_entity(client):
# Check the response data
entity_data = entity_response.json()
assert entity_data["filename"] == "test_entity.txt"
assert entity_data["filepath"] == "/tmp/test_entity.txt"
assert entity_data["filepath"] == "test_entity.txt"
assert entity_data["size"] == 150
assert entity_data["file_created_at"] == "2023-01-01T00:00:00"
assert entity_data["file_last_modified_at"] == "2023-01-01T00:00:00"
@ -131,7 +131,7 @@ def test_update_entity(client):
new_entity = NewEntityParam(
filename="test.txt",
filepath="/tmp/test.txt",
filepath="test.txt",
size=100,
file_created_at="2023-01-01T00:00:00",
file_last_modified_at="2023-01-01T00:00:00",
@ -181,7 +181,7 @@ def test_get_entity_by_filepath(client):
new_entity = NewEntityParam(
filename="test_get.txt",
filepath="/tmp/test_get.txt",
filepath="test_get.txt",
size=100,
file_created_at="2023-01-01T00:00:00",
file_last_modified_at="2023-01-01T00:00:00",
@ -191,8 +191,7 @@ def test_get_entity_by_filepath(client):
entity_response = client.post(f"/libraries/{library_id}/entities", json=new_entity.model_dump(mode="json"))
entity_id = entity_response.json()["id"]
# Get the entity by filepath
get_response = client.get(f"/libraries/{library_id}/entities", params={"filepath": new_entity.filepath})
get_response = client.get(f"/libraries/{library_id}/entities/by-filepath", params={"filepath": new_entity.filepath})
# Check that the response is successful
assert get_response.status_code == 200
@ -206,11 +205,11 @@ def test_get_entity_by_filepath(client):
assert entity_data["file_type"] == new_entity.file_type
# Test for entity not found
invalid_get_response = client.get(f"/libraries/{library_id}/entities", params={"filepath": "nonexistent.txt"})
invalid_get_response = client.get(f"/libraries/{library_id}/entities/by-filepath", params={"filepath": "nonexistent.txt"})
assert invalid_get_response.status_code == 404
assert invalid_get_response.json() == {"detail": "Entity not found"}
# Test for library not found
invalid_get_response = client.get(f"/libraries/9999/entities", params={"filepath": new_entity.filepath})
invalid_get_response = client.get(f"/libraries/9999/entities/by-filepath", params={"filepath": new_entity.filepath})
assert invalid_get_response.status_code == 404
assert invalid_get_response.json() == {"detail": "Entity not found"}