@@ -76,6 +76,79 @@ def test_upgrades_0_3_0(self):
7676 self .assertEqual ([], new_data_description .related_data )
7777 self .assertIsNone (new_data_description .data_summary )
7878
79+ def test_upgrades_0_3_0_wrong_field (self ):
80+ """Tests data_description_0.3.0_wrong_field.json is mapped correctly."""
81+ data_description_0_3_0 = self .data_descriptions ["data_description_0.3.0_wrong_field.json" ]
82+ upgrader = DataDescriptionUpgrade (old_data_description_model = data_description_0_3_0 )
83+ # Should complain about platform being None and missing data level
84+ with self .assertRaises (Exception ) as e :
85+ upgrader .upgrade_data_description ()
86+
87+ expected_error_message = (
88+ "ValidationError("
89+ "model='DataDescription', "
90+ "errors=[{"
91+ "'loc': ('platform',), "
92+ "'msg': 'none is not an allowed value', "
93+ "'type': 'type_error.none.not_allowed'"
94+ "}])"
95+ )
96+ self .assertEqual (expected_error_message , repr (e .exception ))
97+
98+ # Should work by setting platform explicitly and DataLevel
99+ new_data_description = upgrader .upgrade_data_description (platform = Platform .ECEPHYS , data_level = DataLevel .RAW )
100+ self .assertEqual (datetime .datetime (2022 , 7 , 26 , 10 , 52 , 15 ), new_data_description .creation_time )
101+ self .assertEqual ("ecephys_624643_2022-07-26_10-52-15" , new_data_description .name )
102+ self .assertEqual (Institution .AIND , new_data_description .institution )
103+ self .assertEqual ([], new_data_description .funding_source )
104+ self .assertEqual (DataLevel .RAW , new_data_description .data_level )
105+ self .assertIsNone (new_data_description .group )
106+ self .assertEqual ([], new_data_description .investigators )
107+ self .assertIsNone (new_data_description .project_name )
108+ self .assertIsNone (new_data_description .restrictions )
109+ self .assertEqual ([Modality .ECEPHYS ], new_data_description .modality )
110+ self .assertEqual ("624643" , new_data_description .subject_id )
111+ self .assertEqual ([], new_data_description .related_data )
112+ self .assertIsNone (new_data_description .data_summary )
113+
114+ # Should also work by inputting legacy
115+ new_data_description2 = upgrader .upgrade_data_description (platform = Platform .ECEPHYS , data_level = "raw level" )
116+ self .assertEqual (DataLevel .RAW , new_data_description2 .data_level )
117+
118+ # Should fail if inputting unknown string
119+ with self .assertRaises (Exception ) as e1 :
120+ upgrader .upgrade_data_description (platform = Platform .ECEPHYS , data_level = "asfnewnjfq" )
121+
122+ expected_error_message1 = (
123+ "ValidationError(model='DataDescription', "
124+ "errors=[{'loc': ('data_level',), "
125+ "'msg': \" 'asfnewnjfq' is not a valid DataLevel\" , "
126+ "'type': 'value_error'}])"
127+ )
128+
129+ self .assertEqual (expected_error_message1 , repr (e1 .exception ))
130+
131+ # Should also fail if inputting wrong type
132+ with self .assertRaises (Exception ) as e2 :
133+ upgrader .upgrade_data_description (platform = Platform .ECEPHYS , data_level = ["raw" ])
134+ expected_error_message2 = (
135+ "ValidationError(model='DataDescription', "
136+ "errors=[{'loc': ('data_level',), "
137+ "'msg': '__init__() takes exactly 3 positional arguments "
138+ "(2 given)', 'type': 'type_error'}])"
139+ )
140+
141+ self .assertEqual (expected_error_message2 , repr (e2 .exception ))
142+
143+ # Should work if data_level is missing in original json doc and
144+ # user sets it explicitly
145+ data_description_dict = data_description_0_3_0 .dict ()
146+ del data_description_dict ["data_level" ]
147+ data_description_0_3_0_no_data_level = DataDescription .construct (** data_description_dict )
148+ upgrader3 = DataDescriptionUpgrade (old_data_description_model = data_description_0_3_0_no_data_level )
149+ new_data_description3 = upgrader3 .upgrade_data_description (platform = Platform .ECEPHYS , data_level = DataLevel .RAW )
150+ self .assertEqual (DataLevel .RAW , new_data_description3 .data_level )
151+
79152 def test_upgrades_0_4_0 (self ):
80153 """Tests data_description_0.4.0.json is mapped correctly."""
81154 data_description_0_4_0 = self .data_descriptions ["data_description_0.4.0.json" ]
0 commit comments