Skip to content

Commit

Permalink
Initial test cases for scaffolding.py
Browse files Browse the repository at this point in the history
- Executes all the unit test files available in the tests directory
- Test get_time_query() with the use of patch for LocalData and TimeComponentQuery
- Basic test case to assert cases for MODE mapping to en-translations
  • Loading branch information
iantei committed Nov 12, 2024
1 parent bba61b6 commit e590384
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion viz_scripts/docker/start_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ conda env list
cd saved-notebooks/tests || exit 1

echo "Starting unit tests..."
PYTHONPATH=../.. coverage run -m pytest test_plots.py -v
PYTHONPATH=../.. coverage run -m pytest . -v

coverage report
66 changes: 66 additions & 0 deletions viz_scripts/tests/test_scaffolding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import unittest.mock as mock
import emission.core.wrapper.localdate as ecwl
import emission.storage.timeseries.tcquery as esttc
import importlib
import pandas as pd
import numpy as np
import collections as colls

# Dynamically import saved-notebooks.plots
scaffolding = importlib.import_module('saved-notebooks.scaffolding')

@mock.patch('emission.core.wrapper.localdate.LocalDate')
@mock.patch('emission.storage.timeseries.tcquery.TimeComponentQuery')
def test_get_time_query(mock_tcquery, mock_localdate):
mock_localdate.return_value = ecwl.LocalDate({"year": 2022, "month": 6})
mock_tcquery.return_value = esttc.TimeComponentQuery("data.start_local_dt", mock_localdate.return_value, mock_localdate.return_value)

result = scaffolding.get_time_query(2022, 6)

assert result == mock_tcquery.return_value
mock_localdate.assert_called_with({"year": 2022, "month": 6})
mock_tcquery.assert_called_with("data.start_local_dt", mock_localdate.return_value, mock_localdate.return_value)

def test_mapping_labels():
dynamic_labels = {
"MODE": [
{"value":"gas_car", "base_mode": "CAR",
"baseMode":"CAR", "met_equivalent":"IN_VEHICLE", "kgCo2PerKm": 0.22031},
{"value":"motorcycle", "base_mode": "MOPED", "footprint": { "gasoline": { "wh_per_km": 473.17 }},
"baseMode":"MOPED", "met_equivalent":"IN_VEHICLE", "kgCo2PerKm": 0.113143309},
{"value":"walk", "base_mode": "WALKING",
"baseMode":"WALKING", "met_equivalent":"WALKING", "kgCo2PerKm": 0},
{"value":"e_car", "base_mode": "E_CAR",
"baseMode":"E_CAR", "met_equivalent":"IN_VEHICLE", "kgCo2PerKm": 0.08216},
{"value":"taxi", "base_mode": "TAXI",
"baseMode":"TAXI", "met_equivalent":"IN_VEHICLE", "kgCo2PerKm": 0.30741},
{"value":"bike", "base_mode": "BICYCLING",
"baseMode":"BICYCLING", "met_equivalent":"BICYCLING", "kgCo2PerKm": 0},
{"value":"air", "base_mode": "AIR",
"baseMode":"AIR", "met_equivalent":"IN_VEHICLE", "kgCo2PerKm": 0.09975}
],
"translations": {
"en": {
"walk": "Walk",
"motorcycle":"Motorcycle",
"bike": "Bicycle",
"gas_car": "Car",
"e_car": "Electric Car",
"taxi": "Taxi",
"air": "Airplane"
}
}
}

result_mode = scaffolding.mapping_labels(dynamic_labels, "MODE")

expected_result_mode = colls.defaultdict(lambda: 'Other', {
"gas_car": "Car",
"motorcycle": "Motorcycle",
"walk": "Walk",
"e_car": "Electric Car",
"taxi": "Taxi",
"bike": "Bicycle",
"air": "Airplane"
})
assert result_mode == expected_result_mode

0 comments on commit e590384

Please sign in to comment.