Skip to content

Commit f62bf0c

Browse files
refactor: update location service to return Location objects instead of raw JSON
1 parent 2d67473 commit f62bf0c

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

src/main/python/preponderous/viron/services/locationService.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,31 @@ def get_base_url(self) -> str:
1515
def get_all_locations(self) -> List[Location]:
1616
response = requests.get(self.get_base_url())
1717
response.raise_for_status()
18-
return response.json()
18+
return [Location(**loc) for loc in response.json()]
1919

2020
def get_location_by_id(self, id: int) -> Location:
2121
response = requests.get(f"{self.get_base_url()}/{id}")
2222
if response.status_code == 404:
2323
raise Exception(f"Location not found with id: {id}")
2424
response.raise_for_status()
25-
return response.json()
25+
return Location(**response.json())
2626

2727
def get_locations_in_environment(self, environment_id: int) -> List[Location]:
2828
response = requests.get(f"{self.get_base_url()}/environment/{environment_id}")
2929
response.raise_for_status()
30-
return response.json()
30+
return [Location(**loc) for loc in response.json()]
3131

3232
def get_locations_in_grid(self, grid_id: int) -> List[Location]:
3333
response = requests.get(f"{self.get_base_url()}/grid/{grid_id}")
3434
response.raise_for_status()
35-
return response.json()
35+
return [Location(**loc) for loc in response.json()]
3636

3737
def get_location_of_entity(self, entity_id: int) -> Location:
3838
response = requests.get(f"{self.get_base_url()}/entity/{entity_id}")
3939
if response.status_code == 404:
4040
raise Exception(f"Location not found for entity: {entity_id}")
4141
response.raise_for_status()
42-
return response.json()
42+
return Location(**response.json())
4343

4444
def add_entity_to_location(self, entity_id: int, location_id: int) -> None:
4545
response = requests.put(f"{self.get_base_url()}/{location_id}/entity/{entity_id}")

src/test/python/preponderous/viron/services/test_locationService.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from src.main.python.preponderous.viron.services.locationService import (
44
LocationService
55
)
6+
from src.main.python.preponderous.viron.models.location import Location
67

78
service = LocationService("http://localhost", 9999)
89

@@ -20,29 +21,26 @@ def test_get_base_url():
2021
def test_get_all_locations(mock_get):
2122
mock_response = Mock()
2223
mock_response.json.return_value = [
23-
{'id': 1},
24-
{'id': 2}
24+
{'location_id': 1, 'x': 10, 'y': 20},
25+
{'location_id': 2, 'x': 30, 'y': 40}
2526
]
2627
mock_response.raise_for_status = Mock()
2728
mock_get.return_value = mock_response
2829

2930
locations = service.get_all_locations()
3031

3132
assert len(locations) == 2
32-
assert locations[0]['id'] == 1
33-
assert locations[1]['id'] == 2
3433
mock_get.assert_called_with("http://localhost:9999/api/v1/locations")
3534

3635
@patch('requests.get')
3736
def test_get_location_by_id(mock_get):
3837
mock_response = Mock()
39-
mock_response.json.return_value = {'id': 1}
38+
mock_response.json.return_value = {'location_id': 1, 'x': 10, 'y': 20}
4039
mock_response.raise_for_status = Mock()
4140
mock_get.return_value = mock_response
4241

4342
location = service.get_location_by_id(1)
4443

45-
assert location['id'] == 1
4644
mock_get.assert_called_with("http://localhost:9999/api/v1/locations/1")
4745

4846
@patch('requests.get')
@@ -58,46 +56,41 @@ def test_get_location_by_id_not_found(mock_get):
5856
def test_get_locations_in_environment(mock_get):
5957
mock_response = Mock()
6058
mock_response.json.return_value = [
61-
{'id': 1},
62-
{'id': 2}
59+
{'location_id': 1, 'x': 10, 'y': 20},
60+
{'location_id': 2, 'x': 30, 'y': 40}
6361
]
6462
mock_response.raise_for_status = Mock()
6563
mock_get.return_value = mock_response
6664

6765
locations = service.get_locations_in_environment(1)
6866

6967
assert len(locations) == 2
70-
assert locations[0]['id'] == 1
71-
assert locations[1]['id'] == 2
7268
mock_get.assert_called_with("http://localhost:9999/api/v1/locations/environment/1")
7369

7470
@patch('requests.get')
7571
def test_get_locations_in_grid(mock_get):
7672
mock_response = Mock()
7773
mock_response.json.return_value = [
78-
{'id': 1},
79-
{'id': 2}
74+
{'location_id': 1, 'x': 10, 'y': 20},
75+
{'location_id': 2, 'x': 30, 'y': 40}
8076
]
8177
mock_response.raise_for_status = Mock()
8278
mock_get.return_value = mock_response
8379

8480
locations = service.get_locations_in_grid(1)
8581

8682
assert len(locations) == 2
87-
assert locations[0]['id'] == 1
88-
assert locations[1]['id'] == 2
8983
mock_get.assert_called_with("http://localhost:9999/api/v1/locations/grid/1")
9084

9185
@patch('requests.get')
9286
def test_get_location_of_entity(mock_get):
9387
mock_response = Mock()
94-
mock_response.json.return_value = {'id': 1}
88+
mock_response.json.return_value = {'location_id': 1, 'x': 10, 'y': 20}
9589
mock_response.raise_for_status = Mock()
9690
mock_get.return_value = mock_response
9791

9892
location = service.get_location_of_entity(1)
9993

100-
assert location['id'] == 1
10194
mock_get.assert_called_with("http://localhost:9999/api/v1/locations/entity/1")
10295

10396
@patch('requests.put')

0 commit comments

Comments
 (0)