@@ -3607,3 +3607,113 @@ func intToIPv4(n uint32) net.IP {
3607
3607
binary .BigEndian .PutUint32 (ip , n )
3608
3608
return ip
3609
3609
}
3610
+
3611
+ func TestSqlStore_GetPeersByGroupIDs (t * testing.T ) {
3612
+ accountID := "bf1c8084-ba50-4ce7-9439-34653001fc3b"
3613
+
3614
+ group1ID := "test-group-1"
3615
+ group2ID := "test-group-2"
3616
+ emptyGroupID := "empty-group"
3617
+
3618
+ peer1 := "cfefqs706sqkneg59g4g"
3619
+ peer2 := "cfeg6sf06sqkneg59g50"
3620
+
3621
+ tests := []struct {
3622
+ name string
3623
+ groupIDs []string
3624
+ expectedPeers []string
3625
+ expectedCount int
3626
+ }{
3627
+ {
3628
+ name : "retrieve peers from single group with multiple peers" ,
3629
+ groupIDs : []string {group1ID },
3630
+ expectedPeers : []string {peer1 , peer2 },
3631
+ expectedCount : 2 ,
3632
+ },
3633
+ {
3634
+ name : "retrieve peers from single group with one peer" ,
3635
+ groupIDs : []string {group2ID },
3636
+ expectedPeers : []string {peer1 },
3637
+ expectedCount : 1 ,
3638
+ },
3639
+ {
3640
+ name : "retrieve peers from multiple groups (with overlap)" ,
3641
+ groupIDs : []string {group1ID , group2ID },
3642
+ expectedPeers : []string {peer1 , peer2 }, // should deduplicate
3643
+ expectedCount : 2 ,
3644
+ },
3645
+ {
3646
+ name : "retrieve peers from existing 'All' group" ,
3647
+ groupIDs : []string {"cfefqs706sqkneg59g3g" }, // All group from test data
3648
+ expectedPeers : []string {peer1 , peer2 },
3649
+ expectedCount : 2 ,
3650
+ },
3651
+ {
3652
+ name : "retrieve peers from empty group" ,
3653
+ groupIDs : []string {emptyGroupID },
3654
+ expectedPeers : []string {},
3655
+ expectedCount : 0 ,
3656
+ },
3657
+ {
3658
+ name : "retrieve peers from non-existing group" ,
3659
+ groupIDs : []string {"non-existing-group" },
3660
+ expectedPeers : []string {},
3661
+ expectedCount : 0 ,
3662
+ },
3663
+ {
3664
+ name : "empty group IDs list" ,
3665
+ groupIDs : []string {},
3666
+ expectedPeers : []string {},
3667
+ expectedCount : 0 ,
3668
+ },
3669
+ {
3670
+ name : "mix of existing and non-existing groups" ,
3671
+ groupIDs : []string {group1ID , "non-existing-group" },
3672
+ expectedPeers : []string {peer1 , peer2 },
3673
+ expectedCount : 2 ,
3674
+ },
3675
+ }
3676
+
3677
+ for _ , tt := range tests {
3678
+ t .Run (tt .name , func (t * testing.T ) {
3679
+ store , cleanup , err := NewTestStoreFromSQL (context .Background (), "../testdata/store_policy_migrate.sql" , t .TempDir ())
3680
+ t .Cleanup (cleanup )
3681
+ require .NoError (t , err )
3682
+
3683
+ ctx := context .Background ()
3684
+
3685
+ groups := []* types.Group {
3686
+ {
3687
+ ID : group1ID ,
3688
+ AccountID : accountID ,
3689
+ },
3690
+ {
3691
+ ID : group2ID ,
3692
+ AccountID : accountID ,
3693
+ },
3694
+ }
3695
+ require .NoError (t , store .CreateGroups (ctx , accountID , groups ))
3696
+
3697
+ require .NoError (t , store .AddPeerToGroup (ctx , accountID , peer1 , group1ID ))
3698
+ require .NoError (t , store .AddPeerToGroup (ctx , accountID , peer2 , group1ID ))
3699
+ require .NoError (t , store .AddPeerToGroup (ctx , accountID , peer1 , group2ID ))
3700
+
3701
+ peers , err := store .GetPeersByGroupIDs (ctx , accountID , tt .groupIDs )
3702
+ require .NoError (t , err )
3703
+ require .Len (t , peers , tt .expectedCount )
3704
+
3705
+ if tt .expectedCount > 0 {
3706
+ actualPeerIDs := make ([]string , len (peers ))
3707
+ for i , peer := range peers {
3708
+ actualPeerIDs [i ] = peer .ID
3709
+ }
3710
+ assert .ElementsMatch (t , tt .expectedPeers , actualPeerIDs )
3711
+
3712
+ // Verify all returned peers belong to the correct account
3713
+ for _ , peer := range peers {
3714
+ assert .Equal (t , accountID , peer .AccountID )
3715
+ }
3716
+ }
3717
+ })
3718
+ }
3719
+ }
0 commit comments