Skip to content

Commit

Permalink
Merge pull request #118 from smartcar/batch-handle-nested-paths
Browse files Browse the repository at this point in the history
Fix: Add Checks for `/batch` calls
  • Loading branch information
JacobAndrewSmith92 authored May 8, 2023
2 parents 8015c33 + 726a594 commit 900a7f6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
8 changes: 6 additions & 2 deletions smartcar/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,16 @@ def format_path_and_attribute_for_batch(raw_path: str) -> tuple:
2. "NORMAL" raw_path == '/odometer' -> ('odometer', 'odometer')
3. "NESTED" raw_path == '/engine/oil' -> ('engine/oil', 'engine_oil')
"""
# mapper holds unique situations where the path does not exactly line up with the function to call
# we have a set_charge_limit but are not concerned with it in batch calls
mapper = {
"battery/capacity": "battery_capacity",
"engine/oil": "engine_oil",
"charge/limit": "get_charge_limit",
"tires/pressure": "tire_pressure",
"": "attributes",
}
formatted_path = raw_path[1:] if raw_path[0] == "/" else raw_path
formatted_attribute = mapper.get(formatted_path, formatted_path)
if "/" in formatted_attribute:
formatted_attribute = formatted_attribute.replace("/", "_")

return formatted_path, formatted_attribute
28 changes: 26 additions & 2 deletions tests/e2e/test_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,40 @@ def test_set_charge_limit(ford_car):


def test_batch_success(chevy_volt):
batch = chevy_volt.batch(["/odometer", "/location"])
batch = chevy_volt.batch(
[
"/odometer",
"/location",
"/charge/limit",
"/engine/oil",
"/battery/capacity",
"/tires/pressure",
]
)
assert batch is not None
assert batch._fields == ("odometer", "location", "meta")
assert batch._fields == (
"odometer",
"location",
"get_charge_limit",
"engine_oil",
"battery_capacity",
"tire_pressure",
"meta",
)
assert isinstance(batch.meta, tuple)
assert isinstance(batch.odometer().meta, tuple)
assert batch.odometer().distance is not None
assert batch.odometer().meta.request_id is not None
assert batch.location().longitude is not None
assert batch.location().latitude is not None
assert batch.location().meta.request_id is not None
assert batch.get_charge_limit().limit is not None
assert batch.engine_oil().life_remaining is not None
assert batch.battery_capacity().capacity is not None
assert batch.tire_pressure().front_right is not None
assert batch.tire_pressure().front_left is not None
assert batch.tire_pressure().back_right is not None
assert batch.tire_pressure().back_left is not None


def test_batch_misspelled_permission(chevy_volt):
Expand Down

0 comments on commit 900a7f6

Please sign in to comment.