diff --git a/test/__testing.py b/test/__testing.py index 941db0b..7b88627 100644 --- a/test/__testing.py +++ b/test/__testing.py @@ -12,11 +12,13 @@ def expectEqual(a, b): ?''' -def expectAlmostEqual(a, b, delta=0.001): - assert abs(a - b) < delta # __: skip +# __pragma__('kwargs') +def expectAlmostEqual(a, b, places=3): + assert abs(a - b) < (.5 / 10 ** places) # __: skip '''? - expect(a).toBeCloseTo(b, delta) + expect(a).toBeCloseTo(b, places) ?''' +# __pragma__('nokwargs') def jest_test(test): diff --git a/test/metrics/test_egrid.py b/test/metrics/test_egrid.py index faefea4..3c7c7d7 100644 --- a/test/metrics/test_egrid.py +++ b/test/metrics/test_egrid.py @@ -29,7 +29,7 @@ async def test_egrid_intensity_cincinnati_2022(): "requested_coords": coords, "egrid_region": "RFCW", } - expectAlmostEqual(kg_per_kwh, expected_kg_per_kwh, delta=0.01) + expectAlmostEqual(kg_per_kwh, expected_kg_per_kwh, places=2) for key in expected_metadata.keys(): expectEqual(metadata[key], expected_metadata[key]) @@ -51,7 +51,7 @@ async def test_egrid_intensity_eagle_point_2023(): "requested_coords": coords, "egrid_region": "NWPP", } - expectAlmostEqual(kg_per_kwh, expected_kg_per_kwh, delta=0.01) + expectAlmostEqual(kg_per_kwh, expected_kg_per_kwh, places=2) for key in expected_metadata.keys(): expectEqual(metadata[key], expected_metadata[key]) diff --git a/test/metrics/test_footprint_calculations.py b/test/metrics/test_footprint_calculations.py index 1d62b6c..e36594e 100644 --- a/test/metrics/test_footprint_calculations.py +++ b/test/metrics/test_footprint_calculations.py @@ -26,7 +26,7 @@ async def test_car_footprint(): expected_footprint = {'kwh': 0.899, 'kg_co2': 0.291} for key in expected_footprint.keys(): - expectAlmostEqual(footprint[key], expected_footprint[key], delta=0.001) + expectAlmostEqual(footprint[key], expected_footprint[key], places=3) # with 2 passengers, the footprint should be halved fake_label_options['MODE'][0]['passengers'] = 2 @@ -35,7 +35,7 @@ async def test_car_footprint(): expected_footprint = {'kwh': 0.899 / 2, 'kg_co2': 0.291 / 2} for key in expected_footprint.keys(): - expectAlmostEqual(footprint[key], expected_footprint[key], delta=0.001) + expectAlmostEqual(footprint[key], expected_footprint[key], places=3) @jest_test @@ -60,7 +60,7 @@ async def test_ebike_footprint(): expected_footprint = {'kwh': 0.014, 'kg_co2': 0.006} for key in expected_footprint.keys(): - expectAlmostEqual(footprint[key], expected_footprint[key], delta=0.001) + expectAlmostEqual(footprint[key], expected_footprint[key], places=3) @jest_test @@ -88,7 +88,7 @@ async def test_custom_car_footprint(): 'kg_co2': (0.1 / 1000) * emcmfu.FUELS_KG_CO2_PER_MWH['gasoline'] } for key in expected_footprint.keys(): - expectAlmostEqual(footprint[key], expected_footprint[key], delta=0.01) + expectAlmostEqual(footprint[key], expected_footprint[key], places=2) @jest_test @@ -111,7 +111,7 @@ async def test_nyc_bus_footprint(): (footprint, metadata) = await emcmff.calc_footprint_for_trip(fake_trip, fake_label_options) - expected_footprint = {'kwh': 12.93, 'kg_co2': 2.80} + expected_footprint = {'kwh': 12.82, 'kg_co2': 2.78} expected_metadata = { "data_sources": ["ntd2022", "egrid2022"], "is_provisional": False, @@ -120,7 +120,7 @@ async def test_nyc_bus_footprint(): "ntd_modes": ["MB", "RB", "CB"], } for key in expected_footprint.keys(): - expectAlmostEqual(footprint[key], expected_footprint[key], delta=0.01) + expectAlmostEqual(footprint[key], expected_footprint[key], places=2) for key in expected_metadata.keys(): expectEqual(metadata[key], expected_metadata[key]) @@ -156,8 +156,8 @@ async def test_impact_of_ebike_replacing_car(): kwh_saved = replaced_mode_footprint['kwh'] - mode_footprint['kwh'] kg_co2_saved = replaced_mode_footprint['kg_co2'] - mode_footprint['kg_co2'] - expectAlmostEqual(kwh_saved, 0.885, delta=0.001) - expectAlmostEqual(kg_co2_saved, 0.285, delta=0.001) + expectAlmostEqual(kwh_saved, 0.885, places=3) + expectAlmostEqual(kg_co2_saved, 0.285, places=3) jest_describe("test_footprint_calculations") diff --git a/test/metrics/test_transit.js b/test/metrics/test_transit.js index 2d146cc..ba94143 100644 --- a/test/metrics/test_transit.js +++ b/test/metrics/test_transit.js @@ -21,43 +21,43 @@ describe('TestTransit', () => { it('test_bus_nyc', async () => { const [intensities, metadata] = await emcft.get_transit_intensities_for_uace(2022, NYC_UACE_CODE, BUS_MODES); - expect(intensities['overall']['wh_per_km']).toBeCloseTo(646.80, 2); + expect(intensities['overall']['wh_per_km']).toBeCloseTo(641.24, 2); expect(metadata['ntd_ids'].length).toEqual(22); }); it('test_bus_chicago', async () => { const [intensities, metadata] = await emcft.get_transit_intensities_for_uace(2022, CHICAGO_UACE_CODE, BUS_MODES); - expect(intensities['overall']['wh_per_km']).toBeCloseTo(1048.12, 2); + expect(intensities['overall']['wh_per_km']).toBeCloseTo(1039.10, 2); expect(metadata['ntd_ids'].length).toEqual(2); }); it('test_bus_nationwide', async () => { const [intensities, metadata] = await emcft.get_transit_intensities_for_uace(2022, null, BUS_MODES); - expect(intensities['overall']['wh_per_km']).toBeCloseTo(811.85, 2); + expect(intensities['overall']['wh_per_km']).toBeCloseTo(804.91, 2); expect(metadata['ntd_ids'].length).toEqual(410); }); it('test_train_nyc', async () => { const [intensities, metadata] = await emcft.get_transit_intensities_for_uace(2022, NYC_UACE_CODE, TRAIN_MODES); - expect(intensities['overall']['wh_per_km']).toBeCloseTo(24.79, 2); + expect(intensities['overall']['wh_per_km']).toBeCloseTo(24.75, 2); expect(metadata['ntd_ids'].length).toEqual(6); }); it('test_train_chicago', async () => { const [intensities, metadata] = await emcft.get_transit_intensities_for_uace(2022, CHICAGO_UACE_CODE, TRAIN_MODES); - expect(intensities['overall']['wh_per_km']).toBeCloseTo(159.04, 2); + expect(intensities['overall']['wh_per_km']).toBeCloseTo(158.14, 2); expect(metadata['ntd_ids'].length).toEqual(3); }); it('test_train_nationwide', async () => { const [intensities, metadata] = await emcft.get_transit_intensities_for_uace(2022, null, TRAIN_MODES); - expect(intensities['overall']['wh_per_km']).toBeCloseTo(68.06, 2); + expect(intensities['overall']['wh_per_km']).toBeCloseTo(67.87, 2); expect(metadata['ntd_ids'].length).toEqual(49); }); it('test_all_modes_nationwide', async () => { const [intensities, metadata] = await emcft.get_transit_intensities_for_uace(2022, null, null); - expect(intensities['overall']['wh_per_km']).toBeCloseTo(486.96, 2); + expect(intensities['overall']['wh_per_km']).toBeCloseTo(483.04, 2); expect(metadata['ntd_ids'].length).toEqual(517); }); }); diff --git a/test/metrics/test_transit.py b/test/metrics/test_transit.py index bc9e2bc..5bcd95d 100644 --- a/test/metrics/test_transit.py +++ b/test/metrics/test_transit.py @@ -19,38 +19,38 @@ async def test_get_uace_by_coords(self): async def test_bus_nyc(self): (intensities, metadata) = await emcmft.get_transit_intensities_for_uace(2022, NYC_UACE_CODE, BUS_MODES) - self.assertAlmostEqual(intensities['overall']['wh_per_km'], 646.80, places=2) + self.assertAlmostEqual(intensities['overall']['wh_per_km'], 641.24, places=2) self.assertEqual(len(metadata['ntd_ids']), 22) async def test_bus_chicago(self): (intensities, metadata) = await emcmft.get_transit_intensities_for_uace(2022, CHICAGO_UACE_CODE, BUS_MODES) - self.assertAlmostEqual(intensities['overall']['wh_per_km'], 1048.12, places=2) + self.assertAlmostEqual(intensities['overall']['wh_per_km'], 1039.10, places=2) self.assertEqual(len(metadata['ntd_ids']), 2) async def test_bus_nationwide(self): (intensities, metadata) = await emcmft.get_transit_intensities_for_uace(2022, None, BUS_MODES) - self.assertAlmostEqual(intensities['overall']['wh_per_km'], 811.85, places=2) + self.assertAlmostEqual(intensities['overall']['wh_per_km'], 804.91, places=2) self.assertEqual(len(metadata['ntd_ids']), 410) async def test_train_nyc(self): (intensities, metadata) = await emcmft.get_transit_intensities_for_uace(2022, NYC_UACE_CODE, TRAIN_MODES) - self.assertAlmostEqual(intensities['overall']['wh_per_km'], 24.79, places=2) + self.assertAlmostEqual(intensities['overall']['wh_per_km'], 24.75, places=2) self.assertEqual(len(metadata['ntd_ids']), 6) async def test_train_chicago(self): (intensities, metadata) = await emcmft.get_transit_intensities_for_uace(2022, CHICAGO_UACE_CODE, TRAIN_MODES) - self.assertAlmostEqual(intensities['overall']['wh_per_km'], 159.04, places=2) + self.assertAlmostEqual(intensities['overall']['wh_per_km'], 158.14, places=2) # 3 passenger rail systems in Chicago - "the L", Metra, and South Shore Line self.assertEqual(len(metadata['ntd_ids']), 3) async def test_train_nationwide(self): (intensities, metadata) = await emcmft.get_transit_intensities_for_uace(2022, None, TRAIN_MODES) - self.assertAlmostEqual(intensities['overall']['wh_per_km'], 68.06, places=2) + self.assertAlmostEqual(intensities['overall']['wh_per_km'], 67.87, places=2) self.assertEqual(len(metadata['ntd_ids']), 49) async def test_all_modes_nationwide(self): (intensities, metadata) = await emcmft.get_transit_intensities_for_uace(2022, None, None) - self.assertAlmostEqual(intensities['overall']['wh_per_km'], 486.96, places=2) + self.assertAlmostEqual(intensities['overall']['wh_per_km'], 483.04, places=2) self.assertEqual(len(metadata['ntd_ids']), 517)