@@ -1030,33 +1030,42 @@ def _set_all_coords(self, name, value):
1030
1030
c .__setattr__ (name , value )
1031
1031
1032
1032
def test__last_modified (self ):
1033
- # Ensure they are identical at creation.
1033
+ # Ensure that mesh._last_modified is updated when you update the mesh, and
1034
+ # and meshcoord._last_modified is updated to match that.
1035
+
1034
1036
assert self .meshcoord .mesh ._last_modified == self .timestamp_at_creation
1035
1037
1036
1038
self .coord_on_mesh .points = np .zeros (3 )
1037
1039
1038
1040
assert self .meshcoord .mesh ._last_modified == self .meshcoord ._last_modified
1039
1041
assert self .meshcoord .mesh ._last_modified != self .timestamp_at_creation
1040
1042
1041
- self .meshcoord .standard_name
1042
-
1043
- assert self .meshcoord .mesh ._last_modified == self .meshcoord ._last_modified
1044
- assert self .meshcoord .mesh ._last_modified != self .timestamp_at_creation
1045
-
1046
- def test_points (self ):
1043
+ def test_points (self , mocker ):
1047
1044
zeroes = np .zeros (3 )
1045
+ mocked = mocker .patch .object (MeshCoord , "_load_points_and_bounds" )
1046
+ mocked .return_value = (
1047
+ zeroes ,
1048
+ np .zeros ((3 , 3 )),
1049
+ )
1048
1050
assert self .meshcoord .points .all () != zeroes .all ()
1049
1051
self .coord_on_mesh .points = zeroes
1050
1052
assert self .meshcoord .points .all () == zeroes .all ()
1053
+ mocked .assert_called_once ()
1051
1054
1052
- def test_bounds (self ):
1055
+ def test_bounds (self , mocker ):
1053
1056
zero_bounds = np .zeros (3 )
1054
1057
zero_points = np .zeros (15 )
1058
+ mocked = mocker .patch .object (MeshCoord , "_load_points_and_bounds" )
1059
+ mocked .return_value = (
1060
+ zero_bounds ,
1061
+ np .zeros ((3 , 3 )),
1062
+ )
1055
1063
assert self .meshcoord .bounds .all () != zero_bounds .all ()
1056
1064
# Node coords are used to calculate the meshcoord bounds
1057
1065
for nc in self .meshcoord .mesh .node_coords :
1058
1066
nc .points = zero_points
1059
1067
assert self .meshcoord .bounds .all () == zero_bounds .all ()
1068
+ mocked .assert_called_once ()
1060
1069
1061
1070
@pytest .mark .parametrize (
1062
1071
"metadata_name, value" ,
@@ -1066,23 +1075,37 @@ def test_bounds(self):
1066
1075
("attributes" , {"foo" : 1 }),
1067
1076
],
1068
1077
)
1069
- def test_basic_metadata (self , metadata_name , value ):
1078
+ def test_basic_metadata (self , metadata_name , value , mocker ):
1079
+ mocked = mocker .patch .object (MeshCoord , "_load_points_and_bounds" )
1080
+
1070
1081
self .coord_on_mesh .__setattr__ (metadata_name , value )
1071
1082
assert self .meshcoord .__getattribute__ (
1072
1083
metadata_name
1073
1084
) == self .coord_on_mesh .__getattribute__ (metadata_name )
1074
1085
1075
- def test_units (self ):
1086
+ # Ensure updating metadata doesn't prompt the MeshCoord
1087
+ # to update points and bounds.
1088
+ mocked .assert_not_called ()
1089
+
1090
+ def test_units (self , mocker ):
1091
+ mocked = mocker .patch .object (MeshCoord , "_load_points_and_bounds" )
1076
1092
for c in self .mesh .coords ():
1077
1093
c .units = "radians"
1078
1094
assert self .meshcoord .standard_name == self .coord_on_mesh .standard_name
1095
+ # Ensure updating metadata doesn't prompt the MeshCoord
1096
+ # to update points and bounds.
1097
+ mocked .assert_not_called ()
1079
1098
1080
- def test_standard_name (self ):
1099
+ def test_standard_name (self , mocker ):
1100
+ mocked = mocker .patch .object (MeshCoord , "_load_points_and_bounds" )
1081
1101
for c in self .mesh .coords (axis = "x" ):
1082
1102
c .standard_name = "grid_longitude"
1083
1103
for c in self .mesh .coords (axis = "y" ):
1084
1104
c .standard_name = "grid_latitude"
1085
1105
assert self .meshcoord .standard_name == self .coord_on_mesh .standard_name
1106
+ # Ensure updating metadata doesn't prompt the MeshCoord
1107
+ # to update points and bounds.
1108
+ mocked .assert_not_called ()
1086
1109
1087
1110
def test_updates (self , mocker ):
1088
1111
mocked = mocker .patch .object (MeshCoord , "_load_points_and_bounds" )
@@ -1093,13 +1116,31 @@ def test_updates(self, mocker):
1093
1116
np .zeros ((3 , 3 )),
1094
1117
)
1095
1118
self .coord_on_mesh .points = np .zeros (3 )
1096
- # Only the first update should actually update
1097
- _ = self .meshcoord .update_from_mesh ()
1098
- _ = self .meshcoord .update_from_mesh ()
1119
+ # Only the first time you access an attribute should fetch
1120
+ # update the points and bounds.
1121
+
1122
+ self .meshcoord .standard_name
1123
+ self .meshcoord .standard_name
1099
1124
mocked .assert_called_once ()
1100
1125
1101
1126
# Ensure it updates more than once if the mesh has been updated
1102
- # a second time
1127
+ # a second time.
1103
1128
self .coord_on_mesh .points = np .ones (3 )
1104
- _ = self .meshcoord .update_from_mesh ()
1129
+ self .meshcoord .standard_name
1130
+ assert mocked .call_count == 2
1131
+
1132
+ def test_update_from_mesh (self , mocker ):
1133
+ mocked = mocker .patch .object (MeshCoord , "_load_points_and_bounds" )
1134
+ mocked .return_value = (
1135
+ np .zeros (
1136
+ 3 ,
1137
+ ),
1138
+ np .zeros ((3 , 3 )),
1139
+ )
1140
+ self .coord_on_mesh .points = np .zeros (3 )
1141
+ self .meshcoord .update_from_mesh ()
1142
+ mocked .assert_called_once ()
1143
+
1144
+ # Ensure it forces an update, even if the mesh hasn't been updated.
1145
+ self .meshcoord .update_from_mesh ()
1105
1146
assert mocked .call_count == 2
0 commit comments