forked from JGreenlee/e-mission-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(see README) Enables a way for tests to be written once in python. They can be run in pytest (with bin/run_pytest) or in jest (with bin/run_jest, which compiles the py test suites into JS, then jest runs on the output JS) Add tests for base_modes and emcommon.util as the first examples of how this will work. The existing tests that already have separate .js files can be kept how they are.
- Loading branch information
Showing
12 changed files
with
190 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ __pycache__/ | |
e_mission_common.egg-info/ | ||
node_modules/ | ||
setup/ | ||
test_js* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ dependencies: | |
- pip | ||
- python=3.9 | ||
- pytest | ||
- pytest-asyncio | ||
- requests | ||
- pip: | ||
- Transcrypt==3.9.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/bin/bash | ||
|
||
. setup/activate_conda.sh && conda activate emcommon | ||
|
||
echo "Running JavaScript tests..." | ||
|
||
ROOT_DIR_ABSOLUTE=$(realpath) | ||
|
||
# remove any existing test_js_* directories (from previous runs) | ||
rm -rf $ROOT_DIR_ABSOLUTE/test_js_* | ||
|
||
|
||
for file in test/**/test_*.py; do | ||
# if there is a .js file in the same directory, skip | ||
if [ -f "${file%.*}.js" ]; then | ||
echo "Found dedicated .js version of test $file, not compiling" | ||
continue | ||
else | ||
echo "Compiling $file" | ||
fi | ||
# filename without directory and extension | ||
suffix=$(basename $file) | ||
PYTHONPATH=./src transcrypt --nomin --ecom --xreex $file --xpath test -od $ROOT_DIR_ABSOLUTE/test_js_$suffix | ||
done | ||
npx node --experimental-vm-modules node_modules/jest/bin/jest.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
. setup/activate_conda.sh && conda activate emcommon | ||
|
||
PYTHONPATH=./src pytest test --asyncio-mode=auto |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
def _expect(condition): | ||
assert condition # __: skip | ||
'''? | ||
expect(condition).toBeTruthy() | ||
?''' | ||
|
||
|
||
def expectEqual(a, b): | ||
assert a == b # __: skip | ||
'''? | ||
expect(a).toBe(b) | ||
?''' | ||
|
||
|
||
def expectAlmostEqual(a, b, delta=0.001): | ||
assert abs(a - b) < delta # __: skip | ||
'''? | ||
expect(a).toBeCloseTo(b, delta) | ||
?''' | ||
|
||
|
||
def jest_test(test): | ||
'''? | ||
window['jest_tests'] = window['jest_tests'] or [] | ||
window['jest_tests'].append(test) | ||
?''' | ||
return test | ||
|
||
|
||
def jest_describe(describe_name): | ||
'''? | ||
tests = window['jest_tests'] or [] | ||
expect(tests.length).toBeGreaterThan(0) | ||
def run_tests(): | ||
for t in tests: | ||
test(t.js_name, t) | ||
describe(describe_name, run_tests); | ||
?''' | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# __pragma__('jsiter') | ||
|
||
import emcommon.diary.base_modes as emcdb | ||
from ..__testing import jest_test, jest_describe, expectEqual | ||
|
||
|
||
@jest_test | ||
def test_get_base_mode_by_key(): | ||
base_mode = emcdb.get_base_mode_by_key("BUS") | ||
expected_base_mode = { | ||
"icon": 'bus-side', | ||
"color": emcdb.mode_colors['magenta'], | ||
"met": emcdb.NON_ACTIVE_METS, | ||
"footprint": {"transit": ["MB", "RB", "CB"]}, | ||
} | ||
for key in expected_base_mode: | ||
expectEqual(str(base_mode[key]), str(expected_base_mode[key])) | ||
|
||
|
||
@jest_test | ||
def test_get_rich_mode_car(): | ||
fake_label_option = {'base_mode': "CAR", 'passengers': 2} | ||
rich_mode = emcdb.get_rich_mode(fake_label_option) | ||
|
||
expected_rich_mode = { | ||
"base_mode": "CAR", | ||
"passengers": 2, | ||
"icon": 'car', | ||
"color": emcdb.mode_colors['red'], | ||
"met": emcdb.NON_ACTIVE_METS, | ||
"footprint": emcdb.CAR_FOOTPRINT, | ||
} | ||
for key in expected_rich_mode: | ||
expectEqual(str(rich_mode[key]), str(expected_rich_mode[key])) | ||
|
||
# ensure the original object was not mutated | ||
expectEqual(str(fake_label_option), str({'base_mode': "CAR", 'passengers': 2})) | ||
|
||
|
||
@jest_test | ||
def test_get_rich_mode_e_car(): | ||
fake_label_option = { | ||
'baseMode': "E_CAR", # with baseMode as camelCase, should still work | ||
'passengers': 1, | ||
'color': '#000000', | ||
'footprint': {'gasoline': {'wh_per_km': 500, 'weight': 1}} | ||
} | ||
rich_mode = emcdb.get_rich_mode(fake_label_option) | ||
|
||
expected_rich_mode = { | ||
"baseMode": "E_CAR", | ||
"passengers": 1, | ||
"color": '#000000', | ||
"footprint": {'gasoline': {'wh_per_km': 500, 'weight': 1}}, | ||
"icon": 'car-electric', | ||
"met": emcdb.NON_ACTIVE_METS, | ||
} | ||
for key in expected_rich_mode: | ||
expectEqual(str(rich_mode[key]), str(expected_rich_mode[key])) | ||
|
||
# ensure the original object was not mutated | ||
expectEqual(str(fake_label_option), | ||
str({ | ||
'baseMode': "E_CAR", | ||
'passengers': 1, | ||
'color': '#000000', | ||
'footprint': {'gasoline': {'wh_per_km': 500, 'weight': 1}} | ||
})) | ||
|
||
|
||
jest_describe('test_base_modes') | ||
|
||
# __pragma__('nojsiter') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import emcommon.logger as Log | ||
import emcommon.util as emcu | ||
from .__testing import jest_test, _expect as expect, jest_describe | ||
|
||
|
||
@jest_test | ||
async def test_read_json_resource(): | ||
result = await emcu.read_json_resource('label-options.default.json') | ||
# result['MODE'] should contain a mode with value 'taxi' | ||
expect(any([mode['value'] == 'taxi' for mode in result['MODE']])) | ||
|
||
|
||
@jest_test | ||
async def test_fetch_url(): | ||
url = "https://jsonplaceholder.typicode.com/posts/1" | ||
result = await emcu.fetch_url(url) | ||
expect(result['title'] == | ||
'sunt aut facere repellat provident occaecati excepturi optio reprehenderit') | ||
|
||
|
||
jest_describe('test_example') |