@@ -1070,3 +1070,105 @@ def test_extract_h5nc_encoding(self):
1070
1070
{'least_sigificant_digit' : 2 })
1071
1071
with self .assertRaisesRegexp (ValueError , 'unexpected encoding' ):
1072
1072
_extract_nc4_encoding (var , raise_on_invalid = True )
1073
+
1074
+
1075
+ class MiscObject :
1076
+ pass
1077
+
1078
+ @requires_netCDF4
1079
+ class TestValidateAttrs (TestCase ):
1080
+ def test_validating_attrs (self ):
1081
+ def new_dataset ():
1082
+ return Dataset ({'data' : ('y' , np .arange (10.0 ))})
1083
+
1084
+ def new_dataset_and_dataset_attrs ():
1085
+ ds = new_dataset ()
1086
+ return ds , ds .attrs
1087
+
1088
+ def new_dataset_and_data_attrs ():
1089
+ ds = new_dataset ()
1090
+ return ds , ds .data .attrs
1091
+
1092
+ def new_dataset_and_coord_attrs ():
1093
+ ds = new_dataset ()
1094
+ return ds , ds .coords ['y' ].attrs
1095
+
1096
+ for new_dataset_and_attrs in [new_dataset_and_dataset_attrs ,
1097
+ new_dataset_and_data_attrs ,
1098
+ new_dataset_and_coord_attrs ]:
1099
+ ds , attrs = new_dataset_and_attrs ()
1100
+
1101
+ attrs [123 ] = 'test'
1102
+ with self .assertRaisesRegexp (TypeError , 'Invalid name for attr' ):
1103
+ ds .to_netcdf ('test.nc' )
1104
+
1105
+ ds , attrs = new_dataset_and_attrs ()
1106
+ attrs [MiscObject ()] = 'test'
1107
+ with self .assertRaisesRegexp (TypeError , 'Invalid name for attr' ):
1108
+ ds .to_netcdf ('test.nc' )
1109
+
1110
+ ds , attrs = new_dataset_and_attrs ()
1111
+ attrs ['' ] = 'test'
1112
+ with self .assertRaisesRegexp (ValueError , 'Invalid name for attr' ):
1113
+ ds .to_netcdf ('test.nc' )
1114
+
1115
+ # This one should work
1116
+ ds , attrs = new_dataset_and_attrs ()
1117
+ attrs ['test' ] = 'test'
1118
+ with create_tmp_file () as tmp_file :
1119
+ ds .to_netcdf (tmp_file )
1120
+
1121
+ ds , attrs = new_dataset_and_attrs ()
1122
+ attrs ['test' ] = {'a' : 5 }
1123
+ with self .assertRaisesRegexp (TypeError , 'Invalid value for attr' ):
1124
+ ds .to_netcdf ('test.nc' )
1125
+
1126
+ ds , attrs = new_dataset_and_attrs ()
1127
+ attrs ['test' ] = MiscObject ()
1128
+ with self .assertRaisesRegexp (TypeError , 'Invalid value for attr' ):
1129
+ ds .to_netcdf ('test.nc' )
1130
+
1131
+ ds , attrs = new_dataset_and_attrs ()
1132
+ attrs ['test' ] = 5
1133
+ with create_tmp_file () as tmp_file :
1134
+ ds .to_netcdf (tmp_file )
1135
+
1136
+ ds , attrs = new_dataset_and_attrs ()
1137
+ attrs ['test' ] = 3.14
1138
+ with create_tmp_file () as tmp_file :
1139
+ ds .to_netcdf (tmp_file )
1140
+
1141
+ ds , attrs = new_dataset_and_attrs ()
1142
+ attrs ['test' ] = [1 , 2 , 3 , 4 ]
1143
+ with create_tmp_file () as tmp_file :
1144
+ ds .to_netcdf (tmp_file )
1145
+
1146
+ ds , attrs = new_dataset_and_attrs ()
1147
+ attrs ['test' ] = (1.9 , 2.5 )
1148
+ with create_tmp_file () as tmp_file :
1149
+ ds .to_netcdf (tmp_file )
1150
+
1151
+ ds , attrs = new_dataset_and_attrs ()
1152
+ attrs ['test' ] = np .arange (5 )
1153
+ with create_tmp_file () as tmp_file :
1154
+ ds .to_netcdf (tmp_file )
1155
+
1156
+ ds , attrs = new_dataset_and_attrs ()
1157
+ attrs ['test' ] = np .arange (12 ).reshape (3 , 4 )
1158
+ with create_tmp_file () as tmp_file :
1159
+ ds .to_netcdf (tmp_file )
1160
+
1161
+ ds , attrs = new_dataset_and_attrs ()
1162
+ attrs ['test' ] = 'This is a string'
1163
+ with create_tmp_file () as tmp_file :
1164
+ ds .to_netcdf (tmp_file )
1165
+
1166
+ ds , attrs = new_dataset_and_attrs ()
1167
+ attrs ['test' ] = ''
1168
+ with create_tmp_file () as tmp_file :
1169
+ ds .to_netcdf (tmp_file )
1170
+
1171
+ ds , attrs = new_dataset_and_attrs ()
1172
+ attrs ['test' ] = np .arange (12 ).reshape (3 , 4 )
1173
+ with create_tmp_file () as tmp_file :
1174
+ ds .to_netcdf (tmp_file )
0 commit comments