Skip to content

Commit

Permalink
update expected values for transit tests
Browse files Browse the repository at this point in the history
In b4805ed, the NTD intensities were adjusted. Thus, the expected values in all tests that perform transit calculations must be updated.

I noticed that the second argument of Jest's `toBeCloseTo` is supposed to be a # of decimal places, not a delta as we were using before. So I changed our `expectAlmostEqual` wrapper to use `places` rather than `delta`. This ensures consistent pass/fail conditions whether the test is running in python or JS.
  • Loading branch information
JGreenlee committed Oct 23, 2024
1 parent eb37a5c commit 5724700
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
8 changes: 5 additions & 3 deletions test/__testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions test/metrics/test_egrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand All @@ -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])

Expand Down
16 changes: 8 additions & 8 deletions test/metrics/test_footprint_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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])

Expand Down Expand Up @@ -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")
14 changes: 7 additions & 7 deletions test/metrics/test_transit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down
14 changes: 7 additions & 7 deletions test/metrics/test_transit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit 5724700

Please sign in to comment.