1111from diracx .core .models import (
1212 PilotFieldsMapping ,
1313 PilotStatus ,
14- ScalarSearchOperator ,
15- ScalarSearchSpec ,
16- VectorSearchOperator ,
17- VectorSearchSpec ,
1814)
19-
2015from diracx .db .sql .pilots .db import PilotAgentsDB
2116
2217from .util import (
@@ -40,117 +35,6 @@ async def pilot_db(tmp_path):
4035 yield agents_db
4136
4237
43- async def get_pilot_jobs_ids_by_pilot_id (
44- pilot_db : PilotAgentsDB , pilot_id : int
45- ) -> list [int ]:
46- _ , jobs = await pilot_db .search_pilot_to_job_mapping (
47- parameters = ["JobID" ],
48- search = [
49- ScalarSearchSpec (
50- parameter = "PilotID" ,
51- operator = ScalarSearchOperator .EQUAL ,
52- value = pilot_id ,
53- )
54- ],
55- sorts = [],
56- distinct = True ,
57- per_page = 10000 ,
58- )
59-
60- return [job ["JobID" ] for job in jobs ]
61-
62-
63- async def get_pilots_by_stamp_bulk (
64- pilot_db : PilotAgentsDB , pilot_stamps : list [str ], parameters : list [str ] = []
65- ) -> list [dict [Any , Any ]]:
66- _ , pilots = await pilot_db .search_pilots (
67- parameters = parameters ,
68- search = [
69- VectorSearchSpec (
70- parameter = "PilotStamp" ,
71- operator = VectorSearchOperator .IN ,
72- values = pilot_stamps ,
73- )
74- ],
75- sorts = [],
76- distinct = True ,
77- per_page = 1000 ,
78- )
79-
80- # Custom handling, to see which pilot_stamp does not exist (if so, say which one)
81- found_keys = {row ["PilotStamp" ] for row in pilots }
82- missing = set (pilot_stamps ) - found_keys
83-
84- if missing :
85- raise PilotNotFoundError (
86- data = {"pilot_stamp" : str (missing )},
87- detail = str (missing ),
88- non_existing_pilots = missing ,
89- )
90-
91- return pilots
92-
93-
94- @pytest .fixture
95- async def add_stamps (pilot_db ):
96- async def _add_stamps (start_n = 0 ):
97- async with pilot_db as db :
98- # Add pilots
99- refs = [f"ref_{ i } " for i in range (start_n , start_n + N )]
100- stamps = [f"stamp_{ i } " for i in range (start_n , start_n + N )]
101- pilot_references = dict (zip (stamps , refs ))
102-
103- vo = MAIN_VO
104-
105- await db .add_pilots_bulk (
106- stamps , vo , grid_type = "DIRAC" , pilot_references = pilot_references
107- )
108-
109- pilots = await get_pilots_by_stamp_bulk (db , stamps )
110-
111- return pilots
112-
113- return _add_stamps
114-
115-
116- @pytest .fixture
117- async def create_old_pilots_environment (pilot_db , create_timed_pilots ):
118- non_aborted_recent = await create_timed_pilots (
119- datetime (2025 , 1 , 1 , tzinfo = timezone .utc ), False , N
120- )
121- aborted_recent = await create_timed_pilots (
122- datetime (2025 , 1 , 1 , tzinfo = timezone .utc ), True , 2 * N
123- )
124-
125- aborted_very_old = await create_timed_pilots (
126- datetime (2003 , 3 , 10 , tzinfo = timezone .utc ), True , 3 * N
127- )
128- non_aborted_very_old = await create_timed_pilots (
129- datetime (2003 , 3 , 10 , tzinfo = timezone .utc ), False , 4 * N
130- )
131-
132- pilot_number = 4 * N
133-
134- assert pilot_number == (
135- len (non_aborted_recent )
136- + len (aborted_recent )
137- + len (aborted_very_old )
138- + len (non_aborted_very_old )
139- )
140-
141- # Phase 0. Verify that we have the right environment
142- async with pilot_db as pilot_db :
143- # Ensure that we can get every pilot (only get first of each group)
144- await get_pilots_by_stamp_bulk (pilot_db , [non_aborted_recent [0 ]["PilotStamp" ]])
145- await get_pilots_by_stamp_bulk (pilot_db , [aborted_recent [0 ]["PilotStamp" ]])
146- await get_pilots_by_stamp_bulk (pilot_db , [aborted_very_old [0 ]["PilotStamp" ]])
147- await get_pilots_by_stamp_bulk (
148- pilot_db , [non_aborted_very_old [0 ]["PilotStamp" ]]
149- )
150-
151- return non_aborted_recent , aborted_recent , non_aborted_very_old , aborted_very_old
152-
153-
15438@pytest .mark .asyncio
15539async def test_insert_and_select (pilot_db : PilotAgentsDB ):
15640 async with pilot_db as pilot_db :
0 commit comments