Skip to content

Commit

Permalink
Merge pull request #194 from alenrajsp/main
Browse files Browse the repository at this point in the history
Tests update elevation identification
  • Loading branch information
firefly-cpp authored Jul 31, 2024
2 parents 49589e6 + 41d51ae commit 3c2e2e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def fetch_open_topo_data(self, payload_formatting: bool = True) -> list:
Returns:
list[int]: list of elevations for the given positions.
"""
open_topo_nodes = []
chunks = self.__divide_chunks(self.positions, 100) # into 100 because of the default limit of the API
elevations = []

Expand All @@ -120,7 +119,7 @@ def fetch_open_topo_data(self, payload_formatting: bool = True) -> list:
path = url_parts.path

# Establish connection
if self.open_elevation_api.startswith("http://"):
if self.open_topo_data_api.startswith("http://"):
connection = http.client.HTTPConnection(host)
else:
connection = http.client.HTTPSConnection(host)
Expand Down
45 changes: 35 additions & 10 deletions tests/test_missing_elevation_identification.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
from unittest import TestCase
from sport_activities_features import ElevationIdentification, ElevationApiType # Make sure to import your class from its module

from unittest.mock import patch, MagicMock
from sport_activities_features import ElevationIdentification, ElevationApiType
import json

class TestMissingElevationIdentification(TestCase):
def setUp(self):
Expand All @@ -11,7 +12,21 @@ def setUp(self):
self.open_topo_data_api = ElevationIdentification(positions=self.positions,
elevation_api_type=ElevationApiType.OPEN_TOPO_DATA_API)

def test_fetch_elevation_data_open_elevation_api(self):
@patch('http.client.HTTPSConnection')
def test_fetch_elevation_data_open_elevation_api(self, MockHTTPSConnection):

mock_response = MagicMock()
mock_response.read.return_value = json.dumps({
"results": [
{"latitude": 37.422, "longitude":-122.084058,"elevation":9.0},
{"elevation":147.0,"longitude":-119.417931,"latitude":36.778259},
{"latitude":34.052235,"longitude":-118.243683,"elevation":102.0}
]
}).encode('utf-8')

mock_response.getresponse.return_value = mock_response
MockHTTPSConnection.return_value = mock_response

elevations = self.open_elevation_api.fetch_elevation_data()
self.assertEqual(len(elevations),
len(self.positions)) # Check if number of elevations matches number of positions
Expand All @@ -20,10 +35,20 @@ def test_fetch_elevation_data_open_elevation_api(self):
self.assertEqual(elevations[1], 147)
self.assertEqual(elevations[2], 102)

def test_fetch_elevation_data_open_topo_data_api(self):
elevations = self.open_topo_data_api.fetch_elevation_data()
self.assertEqual(len(elevations),
len(self.positions))
self.assertEqual(elevations[0], 8)
self.assertEqual(elevations[1], 147)
self.assertEqual(elevations[2], 93)
@patch('http.client.HTTPSConnection')
def test_fetch_elevation_data_open_topo_data_api(self, MockHTTPSConnection):
mock_response = MagicMock()
mock_response.read.return_value = json.dumps({
'results': [
{'elevation': 8, 'location': {'lat': 39.7391536, 'lng': -104.9847034}},
{'elevation': 147, 'location': {'lat': 40.748817, 'lng': -73.985428}}
]
}).encode('utf-8')
mock_response.getresponse.return_value = mock_response

MockHTTPSConnection.return_value = mock_response

elevations = self.open_topo_data_api.fetch_open_topo_data()

expected_elevations = [8, 147]
self.assertEqual(elevations, expected_elevations)

0 comments on commit 3c2e2e9

Please sign in to comment.