99import simcore_postgres_database .cli
1010import sqlalchemy as sa
1111import sqlalchemy .engine
12+ import sqlalchemy .exc
1213from faker import Faker
1314from pytest_simcore .helpers import postgres_tools
14- from pytest_simcore .helpers .faker_factories import random_project
15+ from pytest_simcore .helpers .faker_factories import random_project , random_user
1516from simcore_postgres_database .models .projects import projects
1617from simcore_postgres_database .models .projects_to_jobs import projects_to_jobs
17- from sqlalchemy . dialects . postgresql import insert
18+ from simcore_postgres_database . models . users import users
1819
1920
2021@pytest .fixture
@@ -55,7 +56,24 @@ def test_populate_projects_to_jobs_during_migration(
5556 simcore_postgres_database .cli .upgrade .callback ("8403acca8759" )
5657
5758 with sync_engine .connect () as conn :
58- sample_projects = [
59+
60+ # Ensure the projects_to_jobs table does NOT exist yet
61+ with pytest .raises (sqlalchemy .exc .ProgrammingError ) as exc_info :
62+ conn .execute (
63+ sa .select (sa .func .count ()).select_from (projects_to_jobs )
64+ ).scalar ()
65+ assert "psycopg2.errors.UndefinedTable" in f"{ exc_info .value } "
66+
67+ # INSERT data (emulates data in-place)
68+ user_data = random_user (
69+ faker , name = "test_populate_projects_to_jobs_during_migration"
70+ )
71+ user_id = conn .execute (
72+ sa .insert (users ).values (user_data ).returning (users .c .id )
73+ ).scalar ()
74+
75+ SPACES = " " * 3
76+ projects_data = [
5977 random_project (
6078 faker ,
6179 uuid = "cd03450c-4c17-4c2c-85fd-0d951d7dcd5a" ,
@@ -70,36 +88,42 @@ def test_populate_projects_to_jobs_during_migration(
7088 }
7189 """
7290 ),
91+ prj_owner = user_id ,
7392 ),
7493 random_project (
7594 faker ,
7695 uuid = "bf204942-007b-11ef-befd-0242ac114f07" ,
77- name = "studies/4b7a704a-007a-11ef-befd-0242ac114f07/jobs/bf204942-007b-11ef-befd-0242ac114f07" ,
96+ name = f "studies/4b7a704a-007a-11ef-befd-0242ac114f07/jobs/bf204942-007b-11ef-befd-0242ac114f07{ SPACES } " ,
7897 description = "Valid project 2" ,
98+ prj_owner = user_id ,
7999 ),
80100 random_project (
81101 faker ,
82102 uuid = "33333333-3333-3333-3333-333333333333" ,
83103 name = "invalid/project/name" ,
84104 description = "Invalid project" ,
105+ prj_owner = user_id ,
85106 ),
86107 ]
87- conn .execute (insert (projects ).values (sample_projects ))
108+ for prj in projects_data :
109+ conn .execute (sa .insert (projects ).values (prj ))
88110
89- # Run upgrade to head! to populate
111+ # MIGRATE UPGRADE: this should populate
90112 simcore_postgres_database .cli .upgrade .callback ("head" )
91113
92114 with sync_engine .connect () as conn :
93115 # Query the projects_to_jobs table
94- result = conn .execute (sa .select (projects_to_jobs )).fetchall ()
116+ result = conn .execute (
117+ sa .select (projects_to_jobs .c .project_uuid , projects_to_jobs .c .job_name )
118+ ).fetchall ()
95119
96120 # Assert only valid projects are added
97121 assert len (result ) == 2
98- assert {
99- "project_uuid" : " cd03450c-4c17-4c2c-85fd-0d951d7dcd5a" ,
100- "job_name" : " solvers/simcore%2Fservices%2Fcomp%2Fitis%2Fsleeper/releases/2.2.1/jobs/cd03450c-4c17-4c2c-85fd-0d951d7dcd5a" ,
101- } in result
102- assert {
103- "project_uuid" : " bf204942-007b-11ef-befd-0242ac114f07" ,
104- "job_name" : " studies/4b7a704a-007a-11ef-befd-0242ac114f07/jobs/bf204942-007b-11ef-befd-0242ac114f07" ,
105- } in result
122+ assert (
123+ "cd03450c-4c17-4c2c-85fd-0d951d7dcd5a" ,
124+ "solvers/simcore%2Fservices%2Fcomp%2Fitis%2Fsleeper/releases/2.2.1/jobs/cd03450c-4c17-4c2c-85fd-0d951d7dcd5a" ,
125+ ) in result
126+ assert (
127+ "bf204942-007b-11ef-befd-0242ac114f07" ,
128+ "studies/4b7a704a-007a-11ef-befd-0242ac114f07/jobs/bf204942-007b-11ef-befd-0242ac114f07" ,
129+ ) in result
0 commit comments