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
5 changes: 4 additions & 1 deletion backend/api/models/cell.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from ..models import db


""""This is the reference; we stole this from commenter
https://stackoverflow.com/questions/5756559/how-to-build-many-to-many-relations-using-sqlalchemy-a-good-example"""

Expand Down Expand Up @@ -81,6 +80,10 @@ def get(id):
def find_by_name(name):
return Cell.query.filter_by(name=name).first()

@staticmethod
def search_by_name(pattern):
return Cell.query.filter(Cell.name.ilike(f"%{pattern}%")).all()

@staticmethod
def get_cells_by_user_id(id):
"""Get cells that a user has access to via the cell_user relationship"""
Expand Down
5 changes: 4 additions & 1 deletion backend/api/resources/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ def get(self, user):
userCells = json_data.get("user")
tag_ids = json_data.get("tags") # Comma-separated tag IDs: "1,2,3"
category = json_data.get("category") # Filter by tag category
name = json_data.get("name") # Search by name substring

# Base query
if userCells:
if name:
cells = CellModel.search_by_name(name)
elif userCells:
cells = CellModel.get_cells_by_user_id(user.id)
else:
cells = CellModel.get_all()
Expand Down
18 changes: 18 additions & 0 deletions backend/tests/test_cell.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
from api.models.user import User
from api.models.cell import Cell


def test_cell_search_by_name(setup_cells):
"""
GIVEN cells exist in the database
WHEN searching cells by name pattern
THEN only cells whose names match the pattern are returned
"""
results = Cell.search_by_name("cell")
assert len(results) == 2

results = Cell.search_by_name("cell_1")
assert len(results) == 1
assert results[0].name == "cell_1"

results = Cell.search_by_name("nonexistent")
assert len(results) == 0


def test_cell_post_returns_id_and_name(init_database):
Expand Down
Loading