@@ -273,6 +273,48 @@ def _insert_selection(
273273 )
274274 self .wp10db .commit ()
275275
276+ def _setup_failed_zim_regeneration_scenario (
277+ self ,
278+ zim_schedule_id = b"schedule-123" ,
279+ old_task_id = "old-task-id" ,
280+ create_new_selection = False ,
281+ ):
282+ self ._insert_zim_schedule (zim_schedule_id , self .builder .b_id )
283+
284+ with self .wp10db .cursor () as cursor :
285+ # Insert selection v1 with failed ZIM
286+ cursor .execute (
287+ """INSERT INTO selections
288+ (s_id, s_builder_id, s_updated_at, s_content_type, s_version,
289+ s_object_key, s_article_count)
290+ VALUES (%s, %s, '20250102000000', 'text/tab-separated-values', 1,
291+ 'old.tsv', 100)""" ,
292+ (1 , self .builder .b_id ),
293+ )
294+ cursor .execute (
295+ """INSERT INTO zim_tasks
296+ (z_selection_id, z_zim_schedule_id, z_status, z_task_id)
297+ VALUES (%s, %s, 'FAILED', %s)""" ,
298+ (1 , zim_schedule_id , old_task_id ),
299+ )
300+ # Optionally create selection v2
301+ if create_new_selection :
302+ cursor .execute (
303+ """INSERT INTO selections
304+ (s_id, s_builder_id, s_updated_at, s_content_type, s_version,
305+ s_object_key, s_article_count)
306+ VALUES (%s, %s, '20250103000000', 'text/tab-separated-values', 2,
307+ 'new.tsv', 100)""" ,
308+ (2 , self .builder .b_id ),
309+ )
310+ cursor .execute (
311+ "UPDATE builders SET b_current_version = 2 WHERE b_id = %s" ,
312+ (self .builder .b_id ,),
313+ )
314+
315+ self .wp10db .commit ()
316+ return zim_schedule_id
317+
276318 def _get_builder_by_user_id (self ):
277319 with self .wp10db .cursor () as cursor :
278320 cursor .execute (
@@ -1805,40 +1847,9 @@ def test_regenerate_zim_updates_old_task_when_selection_version_changed(
18051847 Ensure the existing zim_task is updated (not dupplicated) when the selection version changes.
18061848 """
18071849 self ._insert_builder ()
1808-
1809- with self .wp10db .cursor () as cursor :
1810- cursor .execute (
1811- """INSERT INTO selections
1812- (s_id, s_builder_id, s_updated_at, s_content_type, s_version, s_object_key, s_article_count)
1813- VALUES (%s, %s, '20250102000000', 'text/tab-separated-values', 1, 'old.tsv', 100)""" ,
1814- (1 , self .builder .b_id ),
1815- )
1816- self .wp10db .commit ()
1817-
1818- zim_schedule_id = b"schedule-123"
1819- self ._insert_zim_schedule (zim_schedule_id , self .builder .b_id )
1820-
1821- with self .wp10db .cursor () as cursor :
1822- cursor .execute (
1823- """INSERT INTO zim_tasks
1824- (z_selection_id, z_zim_schedule_id, z_status, z_task_id)
1825- VALUES (%s, %s, 'FAILED', 'old-task-id')""" ,
1826- (1 , zim_schedule_id ),
1827- )
1828- self .wp10db .commit ()
1829-
1830- with self .wp10db .cursor () as cursor :
1831- cursor .execute (
1832- """INSERT INTO selections
1833- (s_id, s_builder_id, s_updated_at, s_content_type, s_version, s_object_key, s_article_count)
1834- VALUES (%s, %s, '20250103000000', 'text/tab-separated-values', 2, 'new.tsv', 100)""" ,
1835- (2 , self .builder .b_id ),
1836- )
1837- cursor .execute (
1838- "UPDATE builders SET b_current_version = 2 WHERE b_id = %s" ,
1839- (self .builder .b_id ,),
1840- )
1841- self .wp10db .commit ()
1850+ zim_schedule_id = self ._setup_failed_zim_regeneration_scenario (
1851+ create_new_selection = True
1852+ )
18421853
18431854 mock_request_zimfarm_task .return_value = "new-task-id"
18441855
@@ -1850,11 +1861,11 @@ def test_regenerate_zim_updates_old_task_when_selection_version_changed(
18501861 with self .wp10db .cursor () as cursor :
18511862 cursor .execute ("SELECT COUNT(*) as count FROM zim_tasks" )
18521863 count = cursor .fetchone ()["count" ]
1853- self .assertEqual (1 , count )
1854-
18551864 cursor .execute ("SELECT z_selection_id FROM zim_tasks" )
18561865 row = cursor .fetchone ()
1857- self .assertEqual (b"2" , row ["z_selection_id" ])
1866+
1867+ self .assertEqual (1 , count )
1868+ self .assertEqual (b"2" , row ["z_selection_id" ])
18581869
18591870 @patch ("wp1.logic.builder.zimfarm.request_zimfarm_task" )
18601871 @patch (
@@ -1867,22 +1878,10 @@ def test_regenerate_zim_saves_new_task_id(
18671878 test that new task_id from Zimfarm is saved correctly.
18681879 """
18691880 self ._insert_builder ()
1870- self ._insert_selection (
1871- 1 , "text/tab-separated-values" , builder_id = self . builder . b_id
1881+ zim_schedule_id = self ._setup_failed_zim_regeneration_scenario (
1882+ old_task_id = "task_v1"
18721883 )
18731884
1874- zim_schedule_id = b"schedule-123"
1875- self ._insert_zim_schedule (zim_schedule_id , self .builder .b_id )
1876-
1877- with self .wp10db .cursor () as cursor :
1878- cursor .execute (
1879- """INSERT INTO zim_tasks
1880- (z_selection_id, z_zim_schedule_id, z_status, z_task_id)
1881- VALUES (%s, %s, 'FAILED', 'task_v1')""" ,
1882- (1 , zim_schedule_id ),
1883- )
1884- self .wp10db .commit ()
1885-
18861885 mock_request_zimfarm_task .return_value = "task_v2"
18871886
18881887 redis = MagicMock ()
@@ -1897,7 +1896,8 @@ def test_regenerate_zim_saves_new_task_id(
18971896 with self .wp10db .cursor () as cursor :
18981897 cursor .execute ("SELECT z_task_id FROM zim_tasks WHERE z_selection_id = 1" )
18991898 row = cursor .fetchone ()
1900- self .assertEqual (b"task_v2" , row ["z_task_id" ])
1899+
1900+ self .assertEqual (b"task_v2" , row ["z_task_id" ])
19011901
19021902 @patch ("wp1.logic.builder.zimfarm.request_zimfarm_task" )
19031903 @patch (
@@ -1909,51 +1909,19 @@ def test_regenerate_zim_updates_b_selection_zim_version(
19091909 """
19101910 test that b_selection_zim_version is updated for downloads to work.
19111911 """
1912- self ._insert_builder (zim_version = 1 ) # startts with version 1
1913-
1914- # selection v1
1915- with self .wp10db .cursor () as cursor :
1916- cursor .execute (
1917- """INSERT INTO selections
1918- (s_id, s_builder_id, s_updated_at, s_content_type, s_version, s_object_key, s_article_count)
1919- VALUES (%s, %s, '20250102000000', 'text/tab-separated-values', 1, 'old.tsv', 100)""" ,
1920- (1 , self .builder .b_id ),
1921- )
1922- self .wp10db .commit ()
1923-
1924- zim_schedule_id = b"schedule-123"
1925- self ._insert_zim_schedule (zim_schedule_id , self .builder .b_id )
1926-
1927- with self .wp10db .cursor () as cursor :
1928- cursor .execute (
1929- """INSERT INTO zim_tasks
1930- (z_selection_id, z_zim_schedule_id, z_status, z_task_id)
1931- VALUES (%s, %s, 'FAILED', 'old-task')""" ,
1932- (1 , zim_schedule_id ),
1933- )
1934- self .wp10db .commit ()
1935-
1936- # Selection v2
1937- with self .wp10db .cursor () as cursor :
1938- cursor .execute (
1939- """INSERT INTO selections
1940- (s_id, s_builder_id, s_updated_at, s_content_type, s_version, s_object_key, s_article_count)
1941- VALUES (%s, %s, '20250103000000', 'text/tab-separated-values', 2, 'new.tsv', 100)""" ,
1942- (2 , self .builder .b_id ),
1943- )
1944- cursor .execute (
1945- "UPDATE builders SET b_current_version = 2 WHERE b_id = %s" ,
1946- (self .builder .b_id ,),
1947- )
1948- self .wp10db .commit ()
1912+ self ._insert_builder (zim_version = 1 )
1913+ zim_schedule_id = self ._setup_failed_zim_regeneration_scenario (
1914+ old_task_id = "old-task" , create_new_selection = True
1915+ )
19491916
19501917 with self .wp10db .cursor () as cursor :
19511918 cursor .execute (
19521919 "SELECT b_selection_zim_version FROM builders WHERE b_id = %s" ,
19531920 (self .builder .b_id ,),
19541921 )
19551922 version_before = cursor .fetchone ()["b_selection_zim_version" ]
1956- self .assertEqual (1 , version_before )
1923+
1924+ self .assertEqual (1 , version_before )
19571925
19581926 mock_request_zimfarm_task .return_value = "task_v2"
19591927
@@ -1968,7 +1936,8 @@ def test_regenerate_zim_updates_b_selection_zim_version(
19681936 (self .builder .b_id ,),
19691937 )
19701938 version_after = cursor .fetchone ()["b_selection_zim_version" ]
1971- self .assertEqual (2 , version_after )
1939+
1940+ self .assertEqual (2 , version_after )
19721941
19731942 @patch ("wp1.logic.builder.zimfarm.zim_file_url_for_task_id" )
19741943 def test_download_url_after_regeneration (self , mock_zim_file_url ):
@@ -1978,6 +1947,7 @@ def test_download_url_after_regeneration(self, mock_zim_file_url):
19781947 self ._insert_builder ()
19791948
19801949 with self .wp10db .cursor () as cursor :
1950+ # Selection v1 with failed ZIM
19811951 cursor .execute (
19821952 """INSERT INTO selections
19831953 (s_id, s_builder_id, s_updated_at, s_content_type, s_version, s_object_key, s_article_count)
@@ -1990,9 +1960,7 @@ def test_download_url_after_regeneration(self, mock_zim_file_url):
19901960 VALUES (%s, 'FAILED', 'old-failed-task')""" ,
19911961 (1 ,),
19921962 )
1993- self .wp10db .commit ()
1994-
1995- with self .wp10db .cursor () as cursor :
1963+ # Selection v2 with successful ZIM
19961964 cursor .execute (
19971965 """INSERT INTO selections
19981966 (s_id, s_builder_id, s_updated_at, s_content_type, s_version, s_object_key, s_article_count)
0 commit comments