Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""NX AI - FastAPI/Python/Postgres/tsvector"""

# Current Version
__version__ = "1.0.9"
__version__ = "1.1.0"
File renamed without changes.
File renamed without changes.
17 changes: 15 additions & 2 deletions app/api/products/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

router = APIRouter()

CSV_PATH = os.path.join(os.path.dirname(__file__), 'start_data.csv')
CSV_PATH = os.path.abspath(CSV_PATH)
CSV_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data/seed.csv'))

@router.get("/products/seed", status_code=status.HTTP_200_OK)
def seed_products() -> dict:
Expand Down Expand Up @@ -61,6 +60,20 @@ def seed_products() -> dict:
""",
row
)
# Add tsvector column for full-text search
cur.execute('''
ALTER TABLE product
ADD COLUMN IF NOT EXISTS search_vector tsvector;
''')
# Populate tsvector column (example: combine title, Pack_Description, Hierarchy1-3)
cur.execute('''
UPDATE product SET search_vector =
to_tsvector('english', coalesce(title,'') || ' ' || coalesce(Pack_Description,'') || ' ' || coalesce(Hierarchy1,'') || ' ' || coalesce(Hierarchy2,'') || ' ' || coalesce(Hierarchy3,''));
''')
# Create GIN index for fast search
cur.execute('''
CREATE INDEX IF NOT EXISTS idx_product_search_vector ON product USING GIN(search_vector);
''')
conn.commit()
cur.execute('SELECT * FROM product;')
if cur.description is None:
Expand Down
10 changes: 8 additions & 2 deletions app/api/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ def root() -> dict:
epoch = int(time.time() * 1000)
meta = {
"severity": "success",
"title": "NX-AI says hi",
"title": "How can NX-AI help?",
"version": __version__,
"base_url": base_url,
"time": epoch,
}
endpoints = [
{"name": "docs", "url": f"{base_url}/docs"},
{"name": "health", "url": f"{base_url}/health"},
{"name": "products", "url": f"{base_url}/products"}
{
"name": "products",
"url": f"{base_url}/products",
"children": [
{"name": "seed", "url": f"{base_url}/products/seed"}
]
}
]
return {"meta": meta, "data": endpoints}
2 changes: 0 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from app import __version__
"""NX-AI Open Source, production ready Python FastAPI/Postgres app for NX"""

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
Expand Down
13 changes: 10 additions & 3 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
"""Unit and integration tests for NX AI routes."""

from unittest.mock import MagicMock

from fastapi.testclient import TestClient

from app.api.routes import get_db_connection
from app.main import app

client = TestClient(app)


def test_root_returns_welcome_message() -> None:
"""GET / should return a welcome message."""
response = client.get("/")
Expand All @@ -25,3 +22,13 @@ def test_health_returns_ok() -> None:
assert response.status_code == 200
assert response.json() == {"status": "ok"}


def test_products_returns_list() -> None:
"""GET /products should return a list of products (possibly empty)."""
response = client.get("/products")
assert response.status_code == 200
json_data = response.json()
assert "meta" in json_data
assert "data" in json_data
assert isinstance(json_data["data"], list)

Loading