Compare commits
4 Commits
pagination
...
feature_36
Author | SHA1 | Date |
---|---|---|
|
b985e7590a | |
|
0b09021d5c | |
|
97af8ea80f | |
|
0ae18d01d2 |
13
crud.py
13
crud.py
|
@ -42,6 +42,19 @@ def get_all_movies(db: Session):
|
|||
return db_movie.all()
|
||||
|
||||
|
||||
def search_movie(db: Session, term: str):
|
||||
exp = f"%{term}%"
|
||||
# Use regex instead ? still \W is had to escape
|
||||
db_movie = db.query(models.Movie).where(
|
||||
sqlalchemy.or_(
|
||||
models.Movie.title.like(exp),
|
||||
models.Movie.description.like(exp),
|
||||
)
|
||||
)
|
||||
|
||||
return db_movie
|
||||
|
||||
|
||||
def get_movie_by_id(db: Session, id_: str = ""):
|
||||
try:
|
||||
id_ = int(id_)
|
||||
|
|
|
@ -122,34 +122,88 @@ def test_list_movies():
|
|||
assert all(movies_by_title[name] for name in names)
|
||||
|
||||
|
||||
def test_search_movies():
|
||||
def test_search_movies_exact():
|
||||
clear_db()
|
||||
response = client.get("/movies/")
|
||||
# assert response.json() == []
|
||||
|
||||
radix = rand_name()
|
||||
|
||||
name = radix + "test_search"
|
||||
title = radix + "test_search_title"
|
||||
|
||||
desc = radix + "test_desription"
|
||||
desc = radix + "test_search_desription"
|
||||
|
||||
with db_context() as db:
|
||||
movie_name = crud.create_movie(
|
||||
db, title=name, genres=["Animated", "Paropaganda"]
|
||||
movie_title = crud.create_movie(
|
||||
db, title=title, genres=["Animated", "Paropaganda"]
|
||||
)
|
||||
|
||||
movie_desc = crud.create_movie(
|
||||
db, title=radix, description=desc, genres=["Animated", "Paropaganda"]
|
||||
)
|
||||
movie_desc_id = movie_desc.id
|
||||
|
||||
for term, target in zip((name, desc), (movie_name, movie_desc)):
|
||||
with db_context() as db:
|
||||
found = crud.search_movie(db, desc).all()
|
||||
assert len(found) == 1
|
||||
assert target == found[0]
|
||||
found = crud.search_movie(db, title).all()
|
||||
assert len(found) == 1
|
||||
|
||||
movies = client.get("movies").json()["movies"]
|
||||
movies_by_title = {m["title"]: m for m in movies}
|
||||
assert all(movies_by_title[name] for name in names)
|
||||
assert found[0] == movie_title
|
||||
|
||||
found = crud.search_movie(db, desc).all()
|
||||
assert len(found) == 1
|
||||
|
||||
assert found[0] == movie_desc
|
||||
|
||||
|
||||
def test_search_movies_token():
|
||||
clear_db()
|
||||
response = client.get("/movies/")
|
||||
# assert response.json() == []
|
||||
|
||||
radix = rand_name()
|
||||
|
||||
title = radix + "test_search_title titletoken"
|
||||
|
||||
desc = radix + "test_search_desription desctoken"
|
||||
|
||||
with db_context() as db:
|
||||
movie_title = crud.create_movie(
|
||||
db, title=title, genres=["Animated", "Paropaganda"]
|
||||
)
|
||||
|
||||
movie_desc = crud.create_movie(
|
||||
db, title=radix, description=desc, genres=["Animated", "Paropaganda"]
|
||||
)
|
||||
movie_desc_id = movie_desc.id
|
||||
|
||||
found = crud.search_movie(db, "titletoken").all()
|
||||
assert len(found) == 1
|
||||
|
||||
assert found[0] == movie_title
|
||||
|
||||
found = crud.search_movie(db, "desctoken").all()
|
||||
assert len(found) == 1
|
||||
|
||||
assert found[0] == movie_desc
|
||||
|
||||
|
||||
def test_search_movies_token_period():
|
||||
return
|
||||
clear_db()
|
||||
response = client.get("/movies/")
|
||||
# assert response.json() == []
|
||||
|
||||
radix = rand_name()
|
||||
|
||||
title = radix + "test_search_title title3tokenperiod."
|
||||
|
||||
with db_context() as db:
|
||||
movie_title = crud.create_movie(
|
||||
db, title=title, genres=["Animated", "Paropaganda"]
|
||||
)
|
||||
|
||||
found = crud.search_movie(db, "title3tokenpreriod").all()
|
||||
assert len(found) == 1
|
||||
assert found[0] == movie_title
|
||||
|
||||
|
||||
def test_sample_import_toy_story():
|
||||
|
|
Loading…
Reference in New Issue