@@ -393,6 +393,58 @@ def test_copr_get_all_by_commit(clean_before_and_after, multiple_copr_builds):
393393 )
394394
395395
396+ def test_copr_get_all_by_ordering_by_id_desc (clean_before_and_after , pr_project_event_model ):
397+ """
398+ Test that get_all_by() orders by id DESC (latest first), not by build_id DESC.
399+
400+ This is important because build_id is a String, so string ordering would be incorrect.
401+ For example, "9931733" > "10020464" as strings, but 10020464 > 9931733 numerically.
402+ Ordering by id DESC ensures we get the latest build (highest id) first.
403+ """
404+ _ , run_model_1 = SRPMBuildModel .create_with_new_run (project_event_model = pr_project_event_model )
405+ group_1 = CoprBuildGroupModel .create (run_model_1 )
406+ build_1 = CoprBuildTargetModel .create (
407+ build_id = "9931733" , # Lower build_id, but created first (lower id)
408+ project_name = SampleValues .project ,
409+ owner = SampleValues .owner ,
410+ web_url = "https://copr.fedorainfracloud.org/coprs/build/9931733/" ,
411+ target = SampleValues .target ,
412+ status = BuildStatus .success ,
413+ copr_build_group = group_1 ,
414+ )
415+
416+ _ , run_model_2 = SRPMBuildModel .create_with_new_run (project_event_model = pr_project_event_model )
417+ group_2 = CoprBuildGroupModel .create (run_model_2 )
418+ build_2 = CoprBuildTargetModel .create (
419+ build_id = "10020464" , # Higher build_id, created second (higher id)
420+ project_name = SampleValues .project ,
421+ owner = SampleValues .owner ,
422+ web_url = "https://copr.fedorainfracloud.org/coprs/build/10020464/" ,
423+ target = SampleValues .target ,
424+ status = BuildStatus .success ,
425+ copr_build_group = group_2 ,
426+ )
427+
428+ # Verify build_2 has higher id (was created later)
429+ assert build_2 .id > build_1 .id
430+
431+ # Verify that string ordering of build_id would be wrong
432+ assert build_1 .build_id > build_2 .build_id # "9931733" > "10020464" as strings
433+
434+ builds = CoprBuildTargetModel .get_all_by (
435+ commit_sha = SampleValues .commit_sha ,
436+ project_name = SampleValues .project ,
437+ owner = SampleValues .owner ,
438+ target = SampleValues .target ,
439+ status = BuildStatus .success ,
440+ )
441+
442+ # next(iter(builds)) should return the latest build (build_2 with higher id)
443+ latest_build = next (iter (builds ))
444+ assert latest_build .id == build_2 .id
445+ assert latest_build .build_id == "10020464"
446+
447+
396448def test_multiple_pr_models (clean_before_and_after ):
397449 pr1 = PullRequestModel .get_or_create (
398450 pr_id = 1 ,
0 commit comments