@@ -2916,3 +2916,59 @@ def test_get_ldap_parameters(harness):
29162916        harness .charm .get_ldap_parameters ()
29172917        _get_relation_data .assert_called_once ()
29182918        _get_relation_data .reset_mock ()
2919+ 
2920+ 
2921+ def  test_relations_user_databases_map (harness ):
2922+     with  (
2923+         patch ("charm.PostgresqlOperatorCharm.postgresql" ) as  _postgresql ,
2924+         patch ("charm.Patroni.member_started" , new_callable = PropertyMock ) as  _member_started ,
2925+         patch (
2926+             "charm.PostgresqlOperatorCharm.is_cluster_initialised" , new_callable = PropertyMock 
2927+         ) as  _is_cluster_initialised ,
2928+     ):
2929+         # Initial empty results from the functions used in the property that's being tested. 
2930+         _postgresql .list_users_from_relation .return_value  =  set ()
2931+         _postgresql .list_accessible_databases_for_user .return_value  =  set ()
2932+         _postgresql .list_access_groups .return_value  =  {
2933+             "identity_access" ,
2934+             "internal_access" ,
2935+             "relation_access" ,
2936+         }
2937+ 
2938+         # Test when the cluster isn't initialised yet. 
2939+         _is_cluster_initialised .return_value  =  False 
2940+         _member_started .return_value  =  True 
2941+         assert  harness .charm .relations_user_databases_map  ==  {
2942+             "operator" : "all" ,
2943+             "replication" : "all" ,
2944+             "rewind" : "all" ,
2945+         }
2946+ 
2947+         # Test when the cluster is initialised but the cluster member hasn't started yet. 
2948+         _is_cluster_initialised .return_value  =  True 
2949+         _member_started .return_value  =  False 
2950+         assert  harness .charm .relations_user_databases_map  ==  {
2951+             "operator" : "all" ,
2952+             "replication" : "all" ,
2953+             "rewind" : "all" ,
2954+         }
2955+ 
2956+         # Test when there are no relation users in the database. 
2957+         _member_started .return_value  =  True 
2958+         assert  harness .charm .relations_user_databases_map  ==  {}
2959+ 
2960+         # Test when there are relation users in the database. 
2961+         _postgresql .list_users_from_relation .return_value  =  {"user1" , "user2" }
2962+         _postgresql .list_accessible_databases_for_user .side_effect  =  [{"db1" , "db2" }, {"db3" }]
2963+         assert  harness .charm .relations_user_databases_map  ==  {"user1" : "db1,db2" , "user2" : "db3" }
2964+ 
2965+         # Test when the access groups where not created yet. 
2966+         _postgresql .list_accessible_databases_for_user .side_effect  =  [{"db1" , "db2" }, {"db3" }]
2967+         _postgresql .list_access_groups .return_value  =  set ()
2968+         assert  harness .charm .relations_user_databases_map  ==  {
2969+             "user1" : "db1,db2" ,
2970+             "user2" : "db3" ,
2971+             "operator" : "all" ,
2972+             "replication" : "all" ,
2973+             "rewind" : "all" ,
2974+         }
0 commit comments