def _extract_subject_location_summary(row): return SubjectLocationSummary( row['subject_id'], float(row['longitude']), float(row['latitude']), date.fromisoformat(row['date']) )
def test_makes_history_request(): api_key = '123345' summary = SubjectLocationSummary(subject_id='123123123', longitude=109.2, latitude=34.9, date=date(2018, 1, 1)) request = WeatherGateway._make_history_request(api_key, summary) expected_request = ( 'http://api.wunderground.com/api/123345/history_20180101' '/q/34.9,109.2.json') assert expected_request == request
def test_extracts_subject_location_summary(): expected = SubjectLocationSummary('904299266', -118.2437, 34.0522, date(1995, 6, 20)) row = { 'subject_id': expected.subject_id, 'longitude': expected.longitude, 'latitude': expected.latitude, 'date': '1995-06-20' } actual = CsvGateway._extract_subject_location_summary(row) assert expected == actual
def extract_subject_location_summary(filename): """Extract the SubjectLocationSummary contained in 'filename'.""" with open(filename, 'r') as follow_mee_file: reader = csv.DictReader(follow_mee_file) first_row_record = list(reader)[0] try: longitude, latitude = FollowMeeFileGateway._extract_location( first_row_record) date = FollowMeeFileGateway._extract_date(first_row_record) except KeyError: print('Invalid data in {filename}'.format(filename=filename)) return None subject_id = FollowMeeFileGateway._extract_subject_id(filename) return SubjectLocationSummary(subject_id, longitude, latitude, date)
def test_integration_records_weather_summary(): csv_gateway = CsvGateway('/tmp/test_integration_csv_gateway.csv') expected_weather_summary = WeatherSummary(75.1, 90.4, 63.9, 2, 81.3, 96.4, 63.4) subject_location_summary = SubjectLocationSummary('904299266', -118.2437, 34.0522, date(1995, 6, 20)) csv_gateway.record_weather_summary(expected_weather_summary, subject_location_summary) weather_summary = csv_gateway.fetch_weather_summary( subject_location_summary) assert expected_weather_summary == weather_summary
def test_makes_request(): subject_location_summary = SubjectLocationSummary( '904299266', -73.98859, 40.71567, date(1995, 6, 20) ) api_key = 'fake_api_key' expected_request = ('https://api.darksky.net/forecast/fake_api_key/' '40.71567,-73.98859,1995-06-20T00:00:00' '?exclude=currently,minutely,alerts,flags') request = DarkSkyGateway._make_request(subject_location_summary, api_key) assert expected_request == request
def test_integration_extracts_subject_location_summary(): expected = SubjectLocationSummary('98123345', -73.935242, 40.730610, date(2018, 9, 12)) filename = '/tmp/{subject_id}_follow_mee_integration_test.csv'.format( subject_id=expected.subject_id) row_record = { 'Data.Longitude': expected.longitude, 'Data.Latitude': expected.latitude, 'Data.Date': '2018-09-12T22:41:57-04:00' } fieldnames = row_record.keys() with open(filename, 'w') as follow_mee_file: writer = csv.DictWriter(follow_mee_file, fieldnames) writer.writeheader() writer.writerow(row_record) actual = FollowMeeFileGateway.extract_subject_location_summary(filename) assert expected == actual
def test_makes_row(): expected_row = { 'subject_id': '904299266', 'longitude': -118.2437, 'latitude': 34.0522, 'date': '1995-06-20', 'mean_temp': '1.10', 'max_temp': '12.43', 'min_temp': '0.05', 'apparent_mean_temp': None, 'apparent_max_temp': None, 'apparent_min_temp': None, 'precipitation': '0.3300' } weather_summary = WeatherSummary(1.1, 12.43112, 0.049, 0.33) subject_location_summary = SubjectLocationSummary( expected_row['subject_id'], expected_row['longitude'], expected_row['latitude'], date(1995, 6, 20)) row = CsvGateway._make_row(weather_summary, subject_location_summary) assert expected_row == row