-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/subset-to-numpy
- Loading branch information
Showing
2 changed files
with
77 additions
and
1 deletion.
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
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,76 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# (C) Copyright 2020 ECMWF. | ||
# | ||
# This software is licensed under the terms of the Apache Licence Version 2.0 | ||
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
# In applying this licence, ECMWF does not waive the privileges and immunities | ||
# granted to it by virtue of its status as an intergovernmental organisation | ||
# nor does it submit to any jurisdiction. | ||
# | ||
|
||
import pytest | ||
|
||
from earthkit.data.core.order import build_remapping | ||
|
||
data = {"type": "cf", "number": 0, "name": "t2m"} | ||
|
||
|
||
def fn(key): | ||
return data[key] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"remapping,expected_values", | ||
[ | ||
({}, {"type": "cf", "number": 0, "name": "t2m"}), | ||
(None, {"type": "cf", "number": 0, "name": "t2m"}), | ||
({"type": "pf"}, {"type": "pf", "number": 0, "name": "t2m"}), | ||
({"type": "{type}{number}"}, {"type": "cf0", "name": "t2m"}), | ||
({"type": "{type}_{number}"}, {"type": "cf_0", "name": "t2m"}), | ||
({"type": lambda: 12}, {"type": 12, "name": "t2m"}), | ||
], | ||
) | ||
def test_remapping_dict(remapping, expected_values): | ||
r = build_remapping(remapping) | ||
_fn = r(fn) | ||
|
||
for k, v in expected_values.items(): | ||
assert _fn(k) == v | ||
|
||
|
||
def test_remapping_repeated(): | ||
remapping = {"type": "{type}{number}"} | ||
r = build_remapping(remapping) | ||
r = build_remapping(r) | ||
_fn = r(fn) | ||
|
||
assert _fn("type") == "cf0" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"remapping,patch,expected_values", | ||
[ | ||
({}, {"type": {"cf": "pf"}, "number": 12, "name": None}, {"type": "pf", "number": 12, "name": None}), | ||
({"type": "{type}{number}"}, {"type": {"cf0": "x"}}, {"type": "x", "number": 0, "name": "t2m"}), | ||
({}, {"type": lambda x: "x"}, {"type": "x", "number": 0, "name": "t2m"}), | ||
({}, {"type": True}, {"type": True, "number": 0, "name": "t2m"}), | ||
(None, {"type": True}, {"type": True, "number": 0, "name": "t2m"}), | ||
], | ||
) | ||
def test_remapping_patch(remapping, patch, expected_values): | ||
r = build_remapping(remapping, patch) | ||
_fn = r(fn) | ||
|
||
for k, v in expected_values.items(): | ||
assert _fn(k) == v | ||
|
||
|
||
def test_remapping_patch_repeated(): | ||
remapping = {"type": "{type}{number}"} | ||
patch = {"type": {"cf0": "x"}} | ||
r = build_remapping(remapping, patch) | ||
r = build_remapping(r) | ||
_fn = r(fn) | ||
|
||
assert _fn("type") == "x" |