Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/JGreenlee/e-mission-common
Browse files Browse the repository at this point in the history
…into namepatch
  • Loading branch information
jpfleischer committed Oct 23, 2024
2 parents 890299b + e8ae9dc commit 6c363ea
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 30 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# taken from https://github.com/e-mission/e-mission-server/blob/master/.github/workflows/test-with-manual-install.yml

name: unit-tests

# Controls when the action will run. Triggers the workflow on push or pull request
on:
push:
pull_request:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
test-pytest:
# The type of runner that the job will run on
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Run setup.sh
shell: bash -l {0}
run: |
source bin/setup.sh
- name: Run pytest
shell: bash -l {0}
run: |
source bin/run_pytest.sh
test-jest:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '20'

- name: Run setup.sh
shell: bash -l {0}
run: |
source bin/setup.sh
- name: Run jest tests
shell: bash -l {0}
run: |
source bin/run_jest.sh
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This repository uses the [Transcrypt](https://www.transcrypt.org/) library to co

## Setup

```
```bash
. bin/setup.sh
```

Expand Down Expand Up @@ -126,3 +126,7 @@ There may be testing scenarios that must significantly diverge between Python an
```bash
. bin/run_jest.sh
```

### GitHub Actions

The unit tests also run via an Actions workflow, which executes both `run_pytest.sh` and `run_jest.sh` on each commit or PR to `master`.
Empty file modified bin/compile_to_js.sh
100644 → 100755
Empty file.
Empty file modified bin/run_jest.sh
100644 → 100755
Empty file.
Empty file modified bin/run_pytest.sh
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions bin/setup.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ rm -rf e-mission-server

# determine platform
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
TARGET_PLATFORM="linux"
TARGET_PLATFORM="Linux-x86_64"
elif [[ "$OSTYPE" == "darwin"* ]]; then
TARGET_PLATFORM="mac"
TARGET_PLATFORM="MacOSX-x86_64"
else
echo "Unsupported platform $OSTYPE"
exit 1
Expand Down
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 6c363ea

Please sign in to comment.