11#
22# Gramps Web API - A RESTful API for the Gramps genealogy program
33#
4- # Copyright (C) 2020 David Straub
4+ # Copyright (C) 2020-2023 David Straub
55#
66# This program is free software; you can redistribute it and/or modify
77# it under the terms of the GNU Affero General Public License as published by
@@ -109,6 +109,25 @@ def test_transaction_add_update_delete(self):
109109 obj_dict = rv .json
110110 self .assertEqual (obj_dict ["handle" ], handle )
111111 self .assertEqual (obj_dict ["text" ]["string" ], "My first note." )
112+ # undo add
113+ rv = self .client .post (
114+ "/api/transactions/?undo=1" , json = trans_dict , headers = headers
115+ )
116+ assert rv .status_code == 200
117+ trans_dict = rv .json
118+ self .assertEqual (len (trans_dict ), 1 )
119+ self .assertEqual (trans_dict [0 ]["handle" ], handle )
120+ self .assertEqual (trans_dict [0 ]["type" ], "delete" )
121+ self .assertEqual (trans_dict [0 ]["_class" ], "Note" )
122+ rv = self .client .get (f"/api/notes/{ handle } " , headers = headers )
123+ self .assertEqual (rv .status_code , 404 )
124+ # undo undo
125+ rv = self .client .post (
126+ "/api/transactions/?undo=1" , json = trans_dict , headers = headers
127+ )
128+ assert rv .status_code == 200
129+ rv = self .client .get (f"/api/notes/{ handle } " , headers = headers )
130+ self .assertEqual (rv .status_code , 200 )
112131 # update
113132 obj_new = deepcopy (obj )
114133 obj_new ["gramps_id" ] = "N2"
@@ -123,11 +142,27 @@ def test_transaction_add_update_delete(self):
123142 ]
124143 rv = self .client .post ("/api/transactions/" , json = trans , headers = headers )
125144 self .assertEqual (rv .status_code , 200 )
145+ trans_dict = rv .json
126146 rv = self .client .get (f"/api/notes/{ handle } " , headers = headers )
127147 self .assertEqual (rv .status_code , 200 )
128148 obj_dict = rv .json
129149 self .assertEqual (obj_dict ["handle" ], handle )
130150 self .assertEqual (obj_dict ["gramps_id" ], "N2" )
151+ # undo update
152+ rv = self .client .post (
153+ "/api/transactions/?undo=1" , json = trans_dict , headers = headers
154+ )
155+ assert rv .status_code == 200
156+ trans_dict = rv .json
157+ rv = self .client .get (f"/api/notes/{ handle } " , headers = headers )
158+ self .assertEqual (rv .status_code , 200 )
159+ obj_dict = rv .json
160+ self .assertEqual (obj_dict ["handle" ], handle )
161+ self .assertEqual (obj_dict ["gramps_id" ], "N1" )
162+ # undo undo
163+ rv = self .client .post (
164+ "/api/transactions/?undo=1" , json = trans_dict , headers = headers
165+ )
131166 # delete
132167 trans = [
133168 {
@@ -140,8 +175,70 @@ def test_transaction_add_update_delete(self):
140175 ]
141176 rv = self .client .post ("/api/transactions/" , json = trans , headers = headers )
142177 self .assertEqual (rv .status_code , 200 )
178+ trans_dict = rv .json
143179 rv = self .client .get (f"/api/notes/{ handle } " , headers = headers )
144180 self .assertEqual (rv .status_code , 404 )
181+ # undo delete
182+ rv = self .client .post (
183+ "/api/transactions/?undo=1" , json = trans_dict , headers = headers
184+ )
185+ self .assertEqual (rv .status_code , 200 )
186+ trans_dict = rv .json
187+ rv = self .client .get (f"/api/notes/{ handle } " , headers = headers )
188+ self .assertEqual (rv .status_code , 200 )
189+ # undo undo
190+ rv = self .client .post (
191+ "/api/transactions/?undo=1" , json = trans_dict , headers = headers
192+ )
193+
194+ def test_family_undo (self ):
195+ """Undo update and subequent deletion of a single note."""
196+ father = {
197+ "_class" : "Person" ,
198+ "handle" : make_handle (),
199+ "gramps_id" : "P1" ,
200+ }
201+ mother = {
202+ "_class" : "Person" ,
203+ "handle" : make_handle (),
204+ "gramps_id" : "P2" ,
205+ }
206+ family = {
207+ "_class" : "Family" ,
208+ "handle" : make_handle (),
209+ "gramps_id" : "F1" ,
210+ "father_handle" : father ["handle" ],
211+ "mother_handle" : mother ["handle" ],
212+ }
213+ headers = get_headers (self .client , "editor" , "123" )
214+ # add objects
215+ rv = self .client .post ("/api/people/" , json = father , headers = headers )
216+ assert rv .status_code == 201
217+ rv = self .client .post ("/api/people/" , json = mother , headers = headers )
218+ assert rv .status_code == 201
219+ rv = self .client .post ("/api/families/" , json = family , headers = headers )
220+ assert rv .status_code == 201
221+ trans_dict = rv .json
222+ rv = self .client .get (f"/api/people/{ father ['handle' ]} " , headers = headers )
223+ assert rv .status_code == 200
224+ assert rv .json ["family_list" ] == [family ["handle" ]]
225+ rv = self .client .get (f"/api/people/{ mother ['handle' ]} " , headers = headers )
226+ assert rv .status_code == 200
227+ assert rv .json ["family_list" ] == [family ["handle" ]]
228+ rv = self .client .get (f"/api/families/{ family ['handle' ]} " , headers = headers )
229+ assert rv .status_code == 200
230+ # undo family post should delete the family and update the people
231+ rv = self .client .post (
232+ "/api/transactions/?undo=1" , json = trans_dict , headers = headers
233+ )
234+ rv = self .client .get (f"/api/people/{ father ['handle' ]} " , headers = headers )
235+ assert rv .status_code == 200
236+ assert rv .json ["family_list" ] == []
237+ rv = self .client .get (f"/api/people/{ mother ['handle' ]} " , headers = headers )
238+ assert rv .status_code == 200
239+ assert rv .json ["family_list" ] == []
240+ rv = self .client .get (f"/api/families/{ family ['handle' ]} " , headers = headers )
241+ assert rv .status_code == 404
145242
146243 def test_modify_two (self ):
147244 """Modify two objects simultaneously."""
0 commit comments