1616
1717
1818class YamlUtilTest (unittest .TestCase ):
19+ maxDiff = None
20+
1921 @classmethod
2022 def setUpClass (cls ):
2123 cls .tmp_dir = f"/tmp/gitopscli-test-{ uuid .uuid4 ()} "
@@ -117,7 +119,15 @@ def test_update_yaml_file(self):
117119 g: 4 # comment 6
118120 - [hello, world] # comment 7
119121 - foo: # comment 8
120- bar # comment 9"""
122+ bar # comment 9
123+ - list: # comment 10
124+ - key: k1 # comment 11
125+ value: v1 # comment 12
126+ - key: k2 # comment 13
127+ value: v2 # comment 14
128+ - {key: k3+4, value: v3} # comment 15
129+ - key: k3+4 # comment 16
130+ value: v4 # comment 17"""
121131 )
122132
123133 self .assertTrue (update_yaml_file (test_file , "a.b.c" , "2" ))
@@ -132,6 +142,11 @@ def test_update_yaml_file(self):
132142 self .assertTrue (update_yaml_file (test_file , "a.e.[2]" , "replaced object" ))
133143 self .assertFalse (update_yaml_file (test_file , "a.e.[2]" , "replaced object" )) # already updated
134144
145+ self .assertTrue (update_yaml_file (test_file , "a.e.[*].list[?key=='k3+4'].value" , "replaced v3 and v4" ))
146+ self .assertFalse (
147+ update_yaml_file (test_file , "a.e.[*].list[?key=='k3+4'].value" , "replaced v3 and v4" )
148+ ) # already updated
149+
135150 expected = """\
136151 a: # comment 1
137152# comment 2
@@ -144,17 +159,25 @@ def test_update_yaml_file(self):
144159 g: 42 # comment 6
145160 - [hello, tester] # comment 7
146161 - replaced object
162+ - list: # comment 10
163+ - key: k1 # comment 11
164+ value: v1 # comment 12
165+ - key: k2 # comment 13
166+ value: v2 # comment 14
167+ - {key: k3+4, value: replaced v3 and v4} # comment 15
168+ - key: k3+4 # comment 16
169+ value: replaced v3 and v4 # comment 17
147170"""
148171 actual = self ._read_file (test_file )
149172 self .assertEqual (expected , actual )
150173
151174 with pytest .raises (KeyError ) as ex :
152175 update_yaml_file (test_file , "x.y" , "foo" )
153- self .assertEqual ("\" Key 'x' not found in YAML!\" " , str (ex .value ))
176+ self .assertEqual ("\" Key 'x.y ' not found in YAML!\" " , str (ex .value ))
154177
155178 with pytest .raises (KeyError ) as ex :
156179 update_yaml_file (test_file , "[42].y" , "foo" )
157- self .assertEqual ("\" Key '[42]' not found in YAML!\" " , str (ex .value ))
180+ self .assertEqual ("\" Key '[42].y ' not found in YAML!\" " , str (ex .value ))
158181
159182 with pytest .raises (KeyError ) as ex :
160183 update_yaml_file (test_file , "a.x" , "foo" )
@@ -165,12 +188,25 @@ def test_update_yaml_file(self):
165188 self .assertEqual ("\" Key 'a.[42]' not found in YAML!\" " , str (ex .value ))
166189
167190 with pytest .raises (KeyError ) as ex :
168- update_yaml_file (test_file , "a.e.[3]" , "foo" )
169- self .assertEqual ("\" Key 'a.e.[3]' not found in YAML!\" " , str (ex .value ))
191+ update_yaml_file (test_file , "a.e.[100]" , "foo" )
192+ self .assertEqual ("\" Key 'a.e.[100]' not found in YAML!\" " , str (ex .value ))
193+
194+ with pytest .raises (KeyError ) as ex :
195+ update_yaml_file (test_file , "a.e.[*].list[?key=='foo'].value" , "foo" )
196+ self .assertEqual ("\" Key 'a.e.[*].list[?key=='foo'].value' not found in YAML!\" " , str (ex .value ))
170197
171198 with pytest .raises (KeyError ) as ex :
172199 update_yaml_file (test_file , "a.e.[2].[2]" , "foo" )
173- self .assertEqual ("\" Key 'a.e.[2].[2]' not found in YAML!\" " , str (ex .value ))
200+ self .assertEqual (
201+ "\" Key 'a.e.[2].[2]' cannot be updated: 'str' object does not support item assignment!\" " , str (ex .value )
202+ )
203+
204+ with pytest .raises (KeyError ) as ex :
205+ update_yaml_file (test_file , "invalid JSONPath" , "foo" )
206+ self .assertEqual (
207+ "\" Key 'invalid JSONPath' is invalid JSONPath expression: Parse error at 1:8 near token JSONPath (ID)!\" " ,
208+ str (ex .value ),
209+ )
174210
175211 with pytest .raises (KeyError ) as ex :
176212 update_yaml_file (test_file , "" , "foo" )
0 commit comments