def test_typical_range_consistent_is_true_only_for_consistent_typical_ranges(): s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) river = "River X" town = "My Town" trange = (-2.3, 3.4445) s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) assert s.typical_range_consistent() trange = (2.3, -3.4445) s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) assert not s.typical_range_consistent() s = MonitoringStation(s_id, m_id, label, coord, None, river, town) assert not s.typical_range_consistent()
def test_dictionary_build(): s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) trange = (-2.3, 3.4445) river = "River X" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) rivertest = {} rivertest.setdefault(s.river, []).append(s.name) assert rivertest["River X"] == ['some station']
def test_stations_within_radius(): # Create 2 stations s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) trange = (-2.3, 3.4445) river = "River X" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) s_id = "test-s-id2" m_id = "test-m-id2" label = "A station" coord = (2.0, -4.05) trange = (-2.3, 3.4445) river = "River Y" town = "My Town B" t = MonitoringStation(s_id, m_id, label, coord, trange, river, town) #Build an empty list for storing tuples later Example_build_list = [] Example_build_list.append(s) Example_build_list.append(t) #calling the function to be tested Example_list = stations_within_radius(Example_build_list, (2.0, -4.0), 10) Alphabetic_Example_List = sorted(Example_list) #tests that the list only shows statioins within radius assert Alphabetic_Example_List[0] == "A station" #calling the function to be tested Example_list = stations_within_radius(Example_build_list, (2.0, -4.0), 1000) Alphabetic_Example_List = sorted(Example_list) #tests that the list sorts it alphabetically assert Alphabetic_Example_List[0] == "A station" assert Alphabetic_Example_List[1] == "some station"
def test_plot_water_level_with_fit(self): station2 = MonitoringStation(station_id='test_station_id_2', measure_id='test_measure_id_2', label='Test Station 2', coord=(0., 1.), typical_range=(0., 1.), river='test_river_2', town='test_town_2') dates = num2date([5, 4, 3, 2, 1]) levels = [16, 9, 4, 1, 0] show(plot_water_level_with_fit(station2, dates, levels, 2)) assert path.isfile(station2.name + ".html")
def test_stations_highest_rel_level(): s_id = "test-s-id" m_id = "test-m-id" label = "Station 1" coord = (-2.0, 4.0) trange = (-2.3, 3.4445) river = "River X" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) s_id = "test-s-id2" m_id = "test-m-id2" label = "Station 2" coord = (2.0, -4.05) trange = (-2.3, 3.4445) river = "River Y" town = "My Town B" t = MonitoringStation(s_id, m_id, label, coord, trange, river, town) s_id = "test-s-id3" m_id = "test-m-id3" label = "Station 3" coord = (-2.0, 4.0) trange = (-2.3, 3.4445) river = "River X" town = "My Town" u = MonitoringStation(s_id, m_id, label, coord, trange, river, town) #Build an empty list for storing tuples later Example_build_list = [] Example_build_list.append(s) Example_build_list.append(t) Example_build_list.append(u) a = stations_highest_rel_level(Example_build_list, 5) assert a == []
def test_create_monitoring_station(): # Create a station s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) trange = (1, 3) river = "Cam" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) s.latest_level = None assert s.station_id == s_id assert s.measure_id == m_id assert s.name == label assert s.coord == coord assert s.typical_range == trange assert s.river == river assert s.town == town assert s.typical_range_consistent() == False assert s.relative_water_level() == None
def build_station_list(use_cache=True): """Build and return a list of all river level monitoring stations based on data fetched from the Environment agency. Each station is represented as a MonitoringStation object. The available data for some station is incomplete or not available. """ # Fetch station data data = fetch_station_data(use_cache) # Build list of MonitoringStation objects stations = [] for e in data["items"]: # Extract town string (not always available) town = None if 'town' in e: town = e['town'] # Extract river name (not always available) river = None if 'riverName' in e: river = e['riverName'] # Attempt to extract typical range (low, high) try: typical_range = (float(e['stageScale']['typicalRangeLow']), float(e['stageScale']['typicalRangeHigh'])) except Exception: typical_range = None try: # Create mesure station object if all required data is # available, and add to list s = MonitoringStation(station_id=e['@id'], measure_id=e['measures'][-1]['@id'], label=e['label'], coord=(float(e['lat']), float(e['long'])), typical_range=typical_range, river=river, town=town) stations.append(s) except Exception: # Not all required data on the station was available, so # skip over pass return stations
def test_relative_water_level(): s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) river = "River X" town = "My Town" trange1 = (3, 1) s1 = MonitoringStation(s_id, m_id, label, coord, trange1, river, town) s1.latest_level = 1 assert s1.relative_water_level() is None trange2 = (1, 3) s2 = MonitoringStation(s_id, m_id, label, coord, trange2, river, town) s2.latest_level = 1.4 expected_relative_water_level = 0.2 actual_relative_water_level = round(s2.relative_water_level(), 2) assert expected_relative_water_level == actual_relative_water_level
def test_create_monitoring_station(): # Create a station s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) trange = (-2.3, 3.4445) river = "River X" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) assert s.station_id == s_id assert s.measure_id == m_id assert s.name == label assert s.coord == coord assert s.typical_range == trange assert s.river == river assert s.town == town
def test_rivers_with_station_returns_all_rivers(): stations = [] rivers = ["A", "B", "C", "A", "B", "D", "E"] for i in range(len(rivers)): # Create stations s_id = "test-s-id-" + str(i) m_id = "test-m-id-" + str(i) label = "some station " + str(i) coord = (0.0, 0.0) trange = (-2.3, 3.4445) river = rivers[i] town = "My Town " + str(i) stations.append( MonitoringStation(s_id, m_id, label, coord, trange, river, town)) actual_rivers = geo.rivers_with_station(stations) expected_rivers = {"A", "B", "C", "D", "E"} assert actual_rivers == expected_rivers
def test_stations_within_radius_returns_all_stations_within_given_radius(): stations = [] coords = [(-10.0, 0.0), (5.0, 5.0), (9.9, 9.9), (0.0, 0.0)] for i in range(len(coords)): # Create stations s_id = "test-s-id-" + str(i) m_id = "test-m-id-" + str(i) label = "some station " + str(i) coord = coords[i] trange = (-2.3, 3.4445) river = "River " + str(i) town = "My Town " + str(i) stations.append( MonitoringStation(s_id, m_id, label, coord, trange, river, town)) centre = (10.0, 10.0) actual_stations = geo.stations_within_radius(stations, centre, 1000.0) expected_stations = [stations[1], stations[2]] assert expected_stations == actual_stations
def test_relative_water_level(): #Create station s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) trange = (2, 3) river = "River X" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) assert s.relative_water_level() is None s.latest_level = s.typical_range[0] assert round(s.relative_water_level()) == 0 s.latest_level = s.typical_range[1] assert round(s.relative_water_level()) == 1
def test_typical_range_consistent(): #Create a station with invalid data s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) trange = None river = "River X" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) assert not s.typical_range_consistent() #Change range to be another type of inconsistent s.typical_range = (3, -2) assert not s.typical_range_consistent() #Change range to be consistent s.typical_range = (-2, 3) assert s.typical_range_consistent()
def test_plot_water_levels_does_not_crash_if_levels_and_dates_are_not_the_same_length( ): # Create a station s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) trange = (-2.3, 3.4445) river = "River X" town = "My Town" station = MonitoringStation(s_id, m_id, label, coord, trange, river, town) dates = [ dt(2025, 4, 3, 2, 1, 0), dt(2025, 4, 3, 2, 1, 1), dt(2025, 4, 3, 2, 1, 2), dt(2025, 4, 3, 2, 1, 3), dt(2025, 4, 3, 2, 1, 4) ] levels = [0, 1, 2, 3, 4, 5] plot_water_levels(station, dates, levels)
def test_rivers_by_station_number_returns_a_list_of_river_names_with_the_number_of_stations_in_each( ): stations = [] rivers = ["A", "B", "C", "A", "B", "D", "E", "B", "A", "C", "B", "D"] for i in range(len(rivers)): # Create stations s_id = "test-s-id-" + str(i) m_id = "test-m-id-" + str(i) label = "some station " + str(i) coord = (0.0, 0.0) trange = (-2.3, 3.4445) river = rivers[i] town = "My Town " + str(i) stations.append( MonitoringStation(s_id, m_id, label, coord, trange, river, town)) actual_rivers_by_station_number = geo.rivers_by_station_number(stations, 3) expected_rivers_by_station_number = [("B", 4), ("A", 3), ("C", 2), ("D", 2)] print(actual_rivers_by_station_number) assert actual_rivers_by_station_number == expected_rivers_by_station_number
def test_stations_by_distance(): # Create a station s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) trange = (-2.3, 3.4445) river = "River X" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) #Build an empty list for storing tuples later Example_build_list = [] Example_build_list.append(s) #calling the function to be tested Example_list = stations_by_distance(Example_build_list, (2.0, -4.0)) #testing the values of the 2 elements in the tuple in the list for s, d in Example_list[:]: assert s.name == "some station" assert d == 994.3960016965921
def test_stations_by_distance_returns_given_stations_in_order_of_distance_from_a_coordinate_p( ): stations = [] coords = [(-10.0, 0.0), (5.0, 5.0), (9.9, 9.9), (0.0, 0.0)] for i in range(len(coords)): # Create stations s_id = "test-s-id-" + str(i) m_id = "test-m-id-" + str(i) label = "some station " + str(i) coord = coords[i] trange = (-2.3, 3.4445) river = "River " + str(i) town = "My Town " + str(i) stations.append( MonitoringStation(s_id, m_id, label, coord, trange, river, town)) p = (10.0, 10.0) actual_sorted_stations = geo.stations_by_distance(stations, p) expected_sorted_stations = [(stations[2], haversine(p, stations[2].coord)), (stations[1], haversine(p, stations[1].coord)), (stations[3], haversine(p, stations[3].coord)), (stations[0], haversine(p, stations[0].coord))] assert expected_sorted_stations == actual_sorted_stations
def test_stations_by_river_returns_a_dictionary_of_stations_by_rivers(): stations = [] rivers = ["A", "B", "C", "A", "B", "D", "E"] for i in range(len(rivers)): # Create stations s_id = "test-s-id-" + str(i) m_id = "test-m-id-" + str(i) label = "some station " + str(i) coord = (0.0, 0.0) trange = (-2.3, 3.4445) river = rivers[i] town = "My Town " + str(i) stations.append( MonitoringStation(s_id, m_id, label, coord, trange, river, town)) actual_stations_by_rivers = geo.stations_by_river(stations) expected_stations_by_rivers = dict() expected_stations_by_rivers["A"] = [stations[0], stations[3]] expected_stations_by_rivers["B"] = [stations[1], stations[4]] expected_stations_by_rivers["C"] = [stations[2]] expected_stations_by_rivers["D"] = [stations[5]] expected_stations_by_rivers["E"] = [stations[6]] assert actual_stations_by_rivers == expected_stations_by_rivers
def test_highest_rel_level_lists_stations_in_order_from_the_highest_to_lowest_relative_water_level(): N = 3 s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) river = "River X" town = "My Town" trange1 = (1.1, 3.0) trange2 = (-1.2, 2.5) trange3 = (-1.67, 3.14) trange4 = (0, 1) s1 = MonitoringStation(s_id, m_id, label, coord, trange1, river, town) s2 = MonitoringStation(s_id, m_id, label, coord, trange2, river, town) s3 = MonitoringStation(s_id, m_id, label, coord, trange3, river, town) s4 = MonitoringStation(s_id, m_id, label, coord, trange4, river, town) s1.latest_level = 4.1 s2.latest_level = 1.0 s3.latest_level = 1.9 s4.latest_level = None stations = [s1, s2, s3, s4] expected_stations = [s1, s3, s2] actual_stations = stations_highest_rel_level(stations, N) assert expected_stations == actual_stations
def test_typical_range_consistent(): new_monitoring_station = MonitoringStation( None, None, None, None, [5, 0], None, None ) #this is inconsistent #we instantiate a dummy monitoring station #print(new_monitoring_station.typical_range_consistent) assert new_monitoring_station.typical_range_consistent( ) == False #asserting if inconsistent data is inconsistent new_monitoring_station2 = MonitoringStation( None, None, None, None, [0, 5], None, None) #this is consistent #checking for low < second #print(new_monitoring_station2.typical_range_consistent) assert new_monitoring_station2.typical_range_consistent( ) == True #asserting if consistent data is consistent new_monitoring_station3 = MonitoringStation( None, None, None, None, None, None, None) #this is inconsistent #checking for None #print(new_monitoring_station3.typical_range_consistent) assert new_monitoring_station3.typical_range_consistent( ) == False #asserting if no data is inconsistent new_monitoring_station4 = MonitoringStation( None, None, None, None, [5, 5], None, None) #this is consistent #checking for equality #print(new_monitoring_station4.typical_range_consistent) assert new_monitoring_station4.typical_range_consistent( ) == True #asserting if equality is consistent print("it works")
def test_stations_highest_rel_level(): s1 = MonitoringStation("station_1", "measure_id", "station_1", (-2.0, 4.0), (0.0, 1.0), "river", "town") s2 = MonitoringStation("station_2", "measure_id", "station_2", (-2.0, 4.0), (0.0, 1.0), "river", "town") s3 = MonitoringStation("station_3", "measure_id", "station_3", (-2.0, 4.0), (0.0, 1.0), "river", "town") s4 = MonitoringStation("station_4", "measure_id", "station_4", (-2.0, 4.0), (0.0, 1.0), "river", "town") s1.latest_level = 3.0 s2.latest_level = 1.0 s3.latest_level = 4.0 s4.latest_level = 2.0 stations = [s1, s2, s3, s4] stations_at_risk = stations_highest_rel_level(stations, 4) assert stations_at_risk == [('station_3', 4.0), ('station_1', 3.0), ('station_4', 2.0), ('station_2', 1.0)]
def test_relative_water_level(): s1 = MonitoringStation("station_1", "measure_id", "station_1", (-2.0, 4.0), None, "river", "town") s2 = MonitoringStation("station_2", "measure_id", "station_2", (-2.0, 4.0), (1.0, 2.0), "river", "town") s3 = MonitoringStation("station_3", "measure_id", "station_3", (-2.0, 4.0), (1.0, 2.0), "river", "town") s1.latest_level = None s2.latest_level = 1.0 s3.latest_level = 4.0 #print(s2.typical_range) #print(s2.relative_water_level()) assert s1.relative_water_level() == None assert s2.relative_water_level() == 0 assert s3.relative_water_level() == 3
import datetime import pytest from floodsystem.plot import plot_water_levels, plot_water_level_with_fit from floodsystem.stationdata import build_station_list, update_water_levels from floodsystem.station import MonitoringStation from floodsystem.datafetcher import fetch_measure_levels s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (1, 1) trange = (-2.3, 3.4445) river = "River X" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) list_s = [] list_s.append(s) list_s = list_s * 3 def test_plot_water_levels(): # Build list of stations stations = build_station_list() # Find station 'Cam' for station in stations: if station.name == 'Cam': station_cam = station break
def test_stations_by_risk_level(): s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) trange = ( 0, 1) # because trange is 0,1 relative range is equal to latest level river = "River X" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) label = "some station 1" river = "River Y" s1 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) label = "some station 4" river = "River A" s4 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) label = "some station 5" river = "River A" s5 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) s.latest_level = 0.4 s1.latest_level = 0.75 s4.latest_level = 4 s5.latest_level = 0 stations = [s, s1, s4, s5] correct_order = [s4, s5, s1, s] ordered_stations = stations_by_risk_level(stations) for i in range(0, len(correct_order)): assert correct_order[i] == ordered_stations[i]
@author: James """ import pytest from floodsystem.stationdata import build_station_list from floodsystem.flood import stations_level_over_threshold from floodsystem.flood import stations_highest_rel_level from floodsystem.station import MonitoringStation s1 = MonitoringStation('Bourton Dickler', None, None, (51.874767, -1.740083), (0.068, 0.42), 'River Glen', 'Surfleet Seas End') s1.latest_level=0.42 s2 = MonitoringStation('Hello', None, None, (51.874767, -1.740083), (0.068, 0.42), 'River Glen', 'Surfleet Seas End') s2.latest_level=-7
def test_determine_numerical_risk(): s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) trange = ( 0, 1) # because trange is 0,1 relative range is equal to latest level river = "River X" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) label = "some station 1" river = "River Y" s1 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) label = "some station 4" river = "River A" s4 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) label = "some station 5" river = "River A" s5 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) s.latest_level = 0.4 s1.latest_level = 0.75 s4.latest_level = 4 s5.latest_level = 0 stations = [s, s1, s4, s5] determine_numerical_risk(stations) assert round(s.flood_risk_factor, 1) == round(0.4 * 0.5 - 0.1, 1) assert round(s1.flood_risk_factor, 3) == round(0.75 * 0.5 + (0.75 - 0.5), 3) assert round(s5.flood_risk_factor) == round((4 + 0) / 2 - 0.5)
def test_readable_risk(): s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (6.0, 4.0) trange = None river = "River X" town = "My Town" s = MonitoringStation(s_id, m_id, label, coord, trange, river, town) s.flood_risk_factor = None assert s.readable_risk() == "Unknown" s.flood_risk_factor = 0.25 - 0.01 assert s.readable_risk() == "Low" s.flood_risk_factor = 0.25 * 2.5 - 0.01 assert s.readable_risk() == "Moderate" s.flood_risk_factor = 0.25 * 4 - 0.01 assert s.readable_risk() == "High" s.flood_risk_factor = 0.25 * 4 + 0.01 assert s.readable_risk() == "Severe"
def stations_highest_rel_level(self): station1 = MonitoringStation(station_id='test_station_id_1', measure_id='test_measure_id_1', label='Test Station 1', coord=(0., 1.), typical_range=(0., 1.), river='test_river_1', town='test_town_1') station2 = MonitoringStation(station_id='test_station_id_2', measure_id='test_measure_id_2', label='Test Station 2', coord=(1., 1.), typical_range=(0., 1.), river='test_river_2', town='test_town_2') station3 = MonitoringStation(station_id='test_station_id_3', measure_id='test_measure_id_3', label='Test Station 3', coord=(1., 1.), typical_range=(0., 1.), river='test_river_3', town='test_town_3') station1.latest_level = 0.5 station2.latest_level = 0.6 station3.latest_level = 0.7 stations = [station1, station2, station3] assert stations_highest_rel_level(stations, 3) == [station3, station2, station1] assert stations_highest_rel_level(stations, 2) == [station3, station2] assert stations_highest_rel_level(stations, 1) == [station3] station1.latest_level = 0.5 station2.latest_level = 0.6 station3.latest_level = 0.2 assert stations_highest_rel_level(stations, 3) == [station2, station1, station3] assert stations_highest_rel_level(stations, 2) == [station2, station1]
def test_geo(): # Create a station s_id = "test-s-id-1" m_id = "test-m-id-1" label = "Station X" coord = (-1.0,2.0) trange = (-2.0, 3.5) river = "River X" town = "Town X" s1 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) #Create a second station s_id = "test-s-id-2" m_id = "test-m-id-2" label = "Station Y" coord = (-2.0,4.0) trange = (-4.0, 7.0) river = "River Y" town = "Town Y" s2 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) #Create a third station with no river s_id = "test-s-id-2" m_id = "test-m-id-2" label = "Station Z" coord = (-3.0,5.0) trange = (-5.0, 8.0) town = "Town Z" river = None s3 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) #Create a fourth station with same river as second river s_id = "test-s-id-2" m_id = "test-m-id-2" label = "Station V" coord = (-6.0,10.0) trange = (-5.0, 8.0) river = "River Y" town = "Town V" s4 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) #Creates a list of the above four stations stations = (s1, s2, s3, s4) #Returns a list of tuples of the four stations with their distances from the coordinate (-0.5, 0.5) stations_distance = geo.stations_by_distance(stations, (-0.5, 0.5)) #Checks the output is as expected assert stations_distance[0]==("Station X", "Town X", 175.80079981056087) assert stations_distance[1]==("Station Y", "Town Y", 423.32287103817487) assert stations_distance[2]==("Station Z", "Town Z", 572.1728231982844) assert stations_distance[3]==("Station V", "Town V", 1218.7887493406365) #Returns a list of rivers within a radius of 200 from the coordinate (-0.5, 0.5) stations_radius = geo.stations_within_radius(stations, (-0.5, 0.5), 200) #Checks the output is as expected - station X is the only one within range assert stations_radius[0] == "Station X" #gets a set of rivers with at least one monitoring station rivers_stations = sorted(geo.rivers_with_station(stations)) #checks the output is as expected - Station Z has no river and Station V has same river as Station Y assert rivers_stations == ["River X", "River Y"] #returns a dictionary of the rivers matched with their stations stations_rivers = geo.stations_by_river(stations) #checks the output is as expected assert stations_rivers == {"River X": ["Station X"], "River Y": ["Station Y", "Station V"]} #Create a fifth station with a new river s_id = "test-s-id-1" m_id = "test-m-id-1" label = "Station W" coord = (-1.0,2.0) trange = (-2.0, 3.5) river = "River W" town = "Town W" s5 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) #Create a sixth station with the same river as station X s_id = "test-s-id-1" m_id = "test-m-id-1" label = "Station A" coord = (-1.0,2.0) trange = (-2.0, 3.5) river = "River X" town = "Town A" s6 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) stations = (s1, s2, s3, s4, s5, s6) #returns a list of the river with the greatest number of stations -- unless some of the other rivers # also have the same number of stations, in which case they are also included rivers_number_stations = geo.rivers_by_station_number(stations, 1) #checks the output is correct assert rivers_number_stations == [["River X", 2], ["River Y", 2]] #checks whether the list is just returned if the number of rivers is less than or equal to N -- #here the list of rivers with stations is 3, so trying to get the first 3 stations will be the entire #list, hence we just return the whole list rivers_number_stations_2 = geo.rivers_by_station_number(stations, 4) #N is greater than number of rivers rivers_number_stations_3 = geo.rivers_by_station_number(stations, 3) #N is the same as the number of rivers #checks the output is correct - i.e. the entire list of rivers with number of stations if N is greater #or the same as the number of rivers assert rivers_number_stations_2 == [["River X", 2], ["River Y", 2], ["River W", 1]] assert rivers_number_stations_3 == [["River X", 2], ["River Y", 2], ["River W", 1]]
def test_level_over_threshold_returns_an_ordered_list_of_stations_that_are_over_the_threshold_level(): test_tol = 0.5 s_id = "test-s-id" m_id = "test-m-id" label = "some station" coord = (-2.0, 4.0) river = "River X" town = "My Town" trange = (0, 1) s1 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) s2 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) s3 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) s4 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) s5 = MonitoringStation(s_id, m_id, label, coord, trange, river, town) s1.latest_level = 0.9 s2.latest_level = 0.4 s3.latest_level = 1.5 s4.latest_level = -1.0 s5.latest_level = None stations = [s1, s2, s3, s4, s5] expected_stations = [(s3, 1.5), (s1, 0.9)] actual_stations = stations_level_over_threshold(stations, test_tol) assert len(expected_stations) == len(actual_stations) for i in range(len(expected_stations)): assert expected_stations[i][0] == actual_stations[i][0] assert expected_stations[i][1] == round(actual_stations[i][1], 2)