Skip to content

Commit

Permalink
feat: search hikes (icontains on title & body)
Browse files Browse the repository at this point in the history
  • Loading branch information
open-dynaMIX committed May 25, 2023
1 parent daa9038 commit 74f988b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 8 deletions.
1 change: 1 addition & 0 deletions hiking/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def main():
commands.command_show(
args.ids,
args.daterange,
args.search,
args.table_style,
args.order_key,
args.plot,
Expand Down
7 changes: 7 additions & 0 deletions hiking/arg_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ def format_list(data: List[str]):
default=SlimDateRange(datetime.date.min, datetime.date.max),
)

show.add_argument(
"-s",
"--search",
help="Search for text in name and body (case insensitive)",
type=str,
)

show.add_argument(
"-t",
"--table-style",
Expand Down
3 changes: 2 additions & 1 deletion hiking/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def detail_view(collection: HikeCollection):
def command_show(
ids: List[int],
daterange: "SlimDateRange",
search: Optional[str],
table_style: box,
order_params: Tuple[str, bool],
plot_params: Tuple[Optional[str], Optional[str]],
Expand All @@ -190,7 +191,7 @@ def command_show(
'No hikes in DB. Add some hikes with "create" or "import"'
)

collection = HikeCollection(hikes=get_filtered_query(ids, daterange))
collection = HikeCollection(hikes=get_filtered_query(ids, daterange, search))
if not collection.hikes.first():
raise HikingException("No hikes found with given parameters")

Expand Down
10 changes: 9 additions & 1 deletion hiking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Optional

import gpxpy
from sqlalchemy import Column, Date, Float, Integer, Interval, String, Text
from sqlalchemy import Column, Date, Float, Integer, Interval, String, Text, or_
from sqlalchemy.orm import load_only

from hiking.db_utils import Base, engine, session
Expand Down Expand Up @@ -200,13 +200,21 @@ def __repr__(self) -> str:
def get_filtered_query(
ids: Optional[List[int]] = None,
daterange: Optional["SlimDateRange"] = None,
search: Optional[str] = None,
load_all_columns: bool = False,
):
query = session.query(Hike)
if daterange:
query = query.filter(Hike.date >= daterange.lower).filter(
Hike.date <= daterange.upper
)
if search:
query = query.filter(
or_(
Hike.name.icontains(search, autoescape=True),
Hike.body.icontains(search, autoescape=True),
)
)
if ids:
query = query.filter(Hike.id.in_(ids))

Expand Down
32 changes: 27 additions & 5 deletions hiking/tests/__snapshots__/test_commands.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@

'''
# ---
# name: test_command_show_list[date-False-daterange0-table_style0-plot_params0]
# name: test_command_show_list[date-False-daterange0-None-table_style0-plot_params0]
'''
Hikes
──────────────────────────────────────────────────────────────────────────────
Expand All @@ -463,7 +463,7 @@

'''
# ---
# name: test_command_show_list[date-True-daterange1-table_style1-plot_params1]
# name: test_command_show_list[date-False-daterange5-Paul-table_style5-plot_params5]
'''
Hikes
─────────────────────────────────────────────────────────────────
Expand All @@ -474,7 +474,29 @@

'''
# ---
# name: test_command_show_list[distance-True-daterange2-table_style2-plot_params2]
# name: test_command_show_list[date-False-daterange6-FOO-table_style6-plot_params6]
'''
Hikes
─────────────────────────────────────────────────────────────────
ID Date Name ➡ km ⬈ m ⬊ m ⏱ km/h
─────────────────────────────────────────────────────────────────
1 2010-05-20 Paul Diaz 17.4 3268 2652 03:50 4.53
─────────────────────────────────────────────────────────────────

'''
# ---
# name: test_command_show_list[date-True-daterange1-None-table_style1-plot_params1]
'''
Hikes
─────────────────────────────────────────────────────────────────
ID Date Name ➡ km ⬈ m ⬊ m ⏱ km/h
─────────────────────────────────────────────────────────────────
1 2010-05-20 Paul Diaz 17.4 3268 2652 03:50 4.53
─────────────────────────────────────────────────────────────────

'''
# ---
# name: test_command_show_list[distance-True-daterange2-None-table_style2-plot_params2]
'''
Hikes
╔════╦════════════╦══════════════╦════════╦════════╦════════╦═════════╦════════╗
Expand All @@ -493,7 +515,7 @@

'''
# ---
# name: test_command_show_list[elevation_gain-False-daterange3-table_style3-plot_params3]
# name: test_command_show_list[elevation_gain-False-daterange3-None-table_style3-plot_params3]
'''
Hikes
╔════╦════════════╦══════════════╦════════╦════════╦════════╦═════════╦════════╗
Expand Down Expand Up @@ -536,7 +558,7 @@

'''
# ---
# name: test_command_show_list[speed-True-daterange4-table_style4-plot_params4]
# name: test_command_show_list[speed-True-daterange4-None-table_style4-plot_params4]
'''
Hikes
──────────────────────────────────────────────────────────────────────────────
Expand Down
31 changes: 30 additions & 1 deletion hiking/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ def test_command_show_detail(
commands.command_show(
[hike.id],
SlimDateRange(datetime.date.min, datetime.date.max),
None,
DEFAULT_BOX_STYLE,
("date", False),
tuple(),
Expand All @@ -341,43 +342,64 @@ def test_command_show_detail(


@pytest.mark.parametrize(
"order_param, reverse, daterange, table_style, plot_params",
"order_param, reverse, daterange, search, table_style, plot_params",
[
(
"date",
False,
SlimDateRange(datetime.date.min, datetime.date.max),
None,
DEFAULT_BOX_STYLE,
tuple(),
),
(
"date",
True,
SlimDateRange(datetime.date(2002, 6, 1), datetime.date(2012, 8, 1)),
None,
DEFAULT_BOX_STYLE,
tuple(),
),
(
"distance",
True,
SlimDateRange(datetime.date.min, datetime.date.max),
None,
box.DOUBLE,
tuple(),
),
(
"elevation_gain",
False,
SlimDateRange(datetime.date.min, datetime.date.max),
None,
box.DOUBLE,
("duration", "distance"),
),
(
"speed",
True,
SlimDateRange(datetime.date.min, datetime.date.max),
None,
DEFAULT_BOX_STYLE,
("date", "distance"),
),
(
"date",
False,
SlimDateRange(datetime.date.min, datetime.date.max),
"Paul",
DEFAULT_BOX_STYLE,
tuple(),
),
(
"date",
False,
SlimDateRange(datetime.date.min, datetime.date.max),
"FOO",
DEFAULT_BOX_STYLE,
tuple(),
),
],
)
def test_command_show_list(
Expand All @@ -387,12 +409,17 @@ def test_command_show_list(
order_param,
reverse,
daterange,
search,
table_style,
plot_params,
):
hike = collection.hikes.first()
hike.body = "foo"
hike.save()
commands.command_show(
[],
daterange,
search,
table_style,
(order_param, reverse),
plot_params,
Expand All @@ -408,6 +435,7 @@ def test_command_show_no_hikes():
commands.command_show(
[],
SlimDateRange(datetime.date.min, datetime.date.max),
None,
DEFAULT_BOX_STYLE,
tuple(),
tuple(),
Expand All @@ -421,6 +449,7 @@ def test_command_show_no_hikes_with_params(hike):
commands.command_show(
[hike.id + 1], # no hike with this ID
SlimDateRange(datetime.date.min, datetime.date.max),
None,
DEFAULT_BOX_STYLE,
tuple(),
tuple(),
Expand Down

0 comments on commit 74f988b

Please sign in to comment.