@@ -2545,3 +2545,96 @@ def test_historic_to_historic(self):
2545
2545
)[0 ]
2546
2546
pt1i = pt1h .instance
2547
2547
self .assertEqual (pt1i .organization .name , "original" )
2548
+
2549
+ def test_non_historic_to_historic_prefetch (self ):
2550
+ org1 = TestOrganizationWithHistory .objects .create (name = "org1" )
2551
+ org2 = TestOrganizationWithHistory .objects .create (name = "org2" )
2552
+
2553
+ p1 = TestParticipantToHistoricOrganization .objects .create (
2554
+ name = "p1" , organization = org1
2555
+ )
2556
+ p2 = TestParticipantToHistoricOrganization .objects .create (
2557
+ name = "p2" , organization = org1
2558
+ )
2559
+ p3 = TestParticipantToHistoricOrganization .objects .create (
2560
+ name = "p3" , organization = org2
2561
+ )
2562
+ p4 = TestParticipantToHistoricOrganization .objects .create (
2563
+ name = "p4" , organization = org2
2564
+ )
2565
+
2566
+ with self .assertNumQueries (2 ):
2567
+ record1 , record2 = TestOrganizationWithHistory .objects .prefetch_related (
2568
+ "participants"
2569
+ ).all ()
2570
+
2571
+ self .assertListEqual (
2572
+ [p .name for p in record1 .participants .all ()],
2573
+ [p1 .name , p2 .name ],
2574
+ )
2575
+ self .assertListEqual (
2576
+ [p .name for p in record2 .participants .all ()],
2577
+ [p3 .name , p4 .name ],
2578
+ )
2579
+
2580
+ def test_historic_to_non_historic_prefetch (self ):
2581
+ org1 = TestOrganization .objects .create (name = "org1" )
2582
+ org2 = TestOrganization .objects .create (name = "org2" )
2583
+
2584
+ p1 = TestHistoricParticipantToOrganization .objects .create (
2585
+ name = "p1" , organization = org1
2586
+ )
2587
+ p2 = TestHistoricParticipantToOrganization .objects .create (
2588
+ name = "p2" , organization = org1
2589
+ )
2590
+ p3 = TestHistoricParticipantToOrganization .objects .create (
2591
+ name = "p3" , organization = org2
2592
+ )
2593
+ p4 = TestHistoricParticipantToOrganization .objects .create (
2594
+ name = "p4" , organization = org2
2595
+ )
2596
+
2597
+ with self .assertNumQueries (2 ):
2598
+ record1 , record2 = TestOrganization .objects .prefetch_related (
2599
+ "participants"
2600
+ ).all ()
2601
+
2602
+ self .assertListEqual (
2603
+ [p .name for p in record1 .participants .all ()],
2604
+ [p1 .name , p2 .name ],
2605
+ )
2606
+ self .assertListEqual (
2607
+ [p .name for p in record2 .participants .all ()],
2608
+ [p3 .name , p4 .name ],
2609
+ )
2610
+
2611
+ def test_historic_to_historic_prefetch (self ):
2612
+ org1 = TestOrganizationWithHistory .objects .create (name = "org1" )
2613
+ org2 = TestOrganizationWithHistory .objects .create (name = "org2" )
2614
+
2615
+ p1 = TestHistoricParticipanToHistoricOrganization .objects .create (
2616
+ name = "p1" , organization = org1
2617
+ )
2618
+ p2 = TestHistoricParticipanToHistoricOrganization .objects .create (
2619
+ name = "p2" , organization = org1
2620
+ )
2621
+ p3 = TestHistoricParticipanToHistoricOrganization .objects .create (
2622
+ name = "p3" , organization = org2
2623
+ )
2624
+ p4 = TestHistoricParticipanToHistoricOrganization .objects .create (
2625
+ name = "p4" , organization = org2
2626
+ )
2627
+
2628
+ with self .assertNumQueries (2 ):
2629
+ record1 , record2 = TestOrganizationWithHistory .objects .prefetch_related (
2630
+ "historic_participants"
2631
+ ).all ()
2632
+
2633
+ self .assertListEqual (
2634
+ [p .name for p in record1 .historic_participants .all ()],
2635
+ [p1 .name , p2 .name ],
2636
+ )
2637
+ self .assertListEqual (
2638
+ [p .name for p in record2 .historic_participants .all ()],
2639
+ [p3 .name , p4 .name ],
2640
+ )
0 commit comments