|
| 1 | +import inspect |
| 2 | + |
1 | 3 | import pytest |
2 | 4 |
|
| 5 | +from parcels import Field, FieldSet |
3 | 6 | from tests.utils import create_fieldset_unit_mesh |
4 | 7 |
|
5 | 8 | fieldset = create_fieldset_unit_mesh() |
|
14 | 17 | "_chunk_set", |
15 | 18 | ] |
16 | 19 |
|
17 | | -private_fieldset_attrs = [ |
18 | | - "_completed", |
19 | | -] |
20 | 20 |
|
| 21 | +class FieldPrivate: |
| 22 | + attributes = [ |
| 23 | + "_dataFiles", |
| 24 | + "_loaded_time_indices", |
| 25 | + "_creation_log", |
| 26 | + "_data_chunks", |
| 27 | + "_c_data_chunks", |
| 28 | + "_chunk_set", |
| 29 | + ] |
| 30 | + methods = [ |
| 31 | + "_get_dim_filenames", |
| 32 | + "_collect_timeslices", |
| 33 | + "_reshape", |
| 34 | + "_calc_cell_edge_sizes", |
| 35 | + "_search_indices_vertical_z", |
| 36 | + "_search_indices_vertical_s", |
| 37 | + "_reconnect_bnd_indices", |
| 38 | + "_search_indices_rectilinear", |
| 39 | + "_search_indices_curvilinear", |
| 40 | + "_search_indices", |
| 41 | + "_interpolator2D", |
| 42 | + "_interpolator3D", |
| 43 | + "_ccode_eval", |
| 44 | + "_ccode_convert", |
| 45 | + "_get_block_id", |
| 46 | + "_get_block", |
| 47 | + "_chunk_setup", |
| 48 | + "_chunk_data", |
| 49 | + "_rescale_and_set_minmax", |
| 50 | + "_data_concatenate", |
| 51 | + "_spatial_interpolation", |
| 52 | + "_time_index", |
| 53 | + ] |
21 | 54 |
|
22 | | -@pytest.mark.parametrize("private_attribute", private_field_attrs) |
23 | | -def test_private_attribute_field(private_attribute): |
| 55 | + |
| 56 | +class FieldSetPrivate: |
| 57 | + attributes = [ |
| 58 | + "_completed", |
| 59 | + ] |
| 60 | + methods = [ |
| 61 | + "_add_UVfield", |
| 62 | + "_parse_wildcards", |
| 63 | + "_check_complete", |
| 64 | + ] |
| 65 | + |
| 66 | + |
| 67 | +def assert_private_public_attribute_equiv(obj, private_attribute: str): |
24 | 68 | assert private_attribute.startswith("_") |
25 | 69 | attribute = private_attribute.lstrip("_") |
26 | 70 |
|
27 | 71 | with pytest.raises(DeprecationWarning): |
28 | | - assert hasattr(field, attribute) |
29 | | - assert hasattr(field, private_attribute) |
30 | | - assert getattr(field, attribute) is getattr(field, private_attribute) |
| 72 | + assert hasattr(obj, attribute) |
| 73 | + assert hasattr(obj, private_attribute) |
| 74 | + assert getattr(obj, attribute) is getattr(obj, private_attribute) |
| 75 | + |
| 76 | + |
| 77 | +def assert_public_method_calls_private(type_, private_method): |
| 78 | + """Looks at the source code to ensure that `public_method` calls `private_method`. |
| 79 | +
|
| 80 | + Looks for the string `.{method_name}(` in the source code of `public_method`. |
| 81 | + """ |
| 82 | + assert private_method.startswith("_") |
| 83 | + public_method_str = private_method.lstrip("_") |
| 84 | + private_method_str = private_method |
| 85 | + |
| 86 | + public_method = getattr(type_, public_method_str) |
| 87 | + private_method = getattr(type_, private_method_str) |
| 88 | + |
| 89 | + assert callable(public_method) |
| 90 | + assert callable(private_method) |
31 | 91 |
|
| 92 | + assert f".{private_method_str}(" in inspect.getsource(public_method) |
32 | 93 |
|
33 | | -@pytest.mark.parametrize("private_attribute", private_fieldset_attrs) |
| 94 | + |
| 95 | +@pytest.mark.parametrize("private_attribute", FieldPrivate.attributes) |
| 96 | +def test_private_attribute_field(private_attribute): |
| 97 | + assert_private_public_attribute_equiv(field, private_attribute) |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize("private_attribute", FieldSetPrivate.attributes) |
34 | 101 | def test_private_attribute_fieldset(private_attribute): |
35 | | - assert private_attribute.startswith("_") |
36 | | - attribute = private_attribute.lstrip("_") |
| 102 | + assert_private_public_attribute_equiv(fieldset, private_attribute) |
37 | 103 |
|
38 | | - with pytest.raises(DeprecationWarning): |
39 | | - assert hasattr(fieldset, attribute) |
40 | | - assert hasattr(fieldset, private_attribute) |
41 | | - assert getattr(fieldset, attribute) is getattr(fieldset, private_attribute) |
| 104 | + |
| 105 | +@pytest.mark.parametrize("private_method", FieldPrivate.methods) |
| 106 | +def test_private_method_field(private_method): |
| 107 | + assert_public_method_calls_private(Field, private_method) |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.parametrize("private_method", FieldSetPrivate.methods) |
| 111 | +def test_private_method_fieldset(private_method): |
| 112 | + assert_public_method_calls_private(FieldSet, private_method) |
0 commit comments