@@ -16,11 +16,77 @@ defmodule Supavisor.TenantsTest do
1616 allow_list: [ "foo" , "bar" ]
1717 }
1818
19+ test "list_tenants/0 returns all tenants" do
20+ tenants = Tenants . list_tenants ( )
21+
22+ assert Enum . all? ( 1 .. 10 , fn i ->
23+ "cluster_pool_tenant_#{ i } " in Enum . map ( tenants , & & 1 . external_id )
24+ end )
25+ end
26+
1927 test "get_tenant!/1 returns the tenant with given id" do
2028 tenant = tenant_fixture ( )
2129 assert Tenants . get_tenant! ( tenant . id ) |> Repo . preload ( :users ) == tenant
2230 end
2331
32+ test "get_tenant_by_external_id/1 returns the tenant with given external_id" do
33+ tenant = tenant_fixture ( )
34+ assert Tenants . get_tenant_by_external_id ( tenant . external_id ) == tenant
35+ end
36+
37+ test "get_cluster_by_alias/1 returns the cluster with given alias" do
38+ cluster = cluster_fixture ( )
39+ assert Tenants . get_cluster_by_alias ( cluster . alias ) == cluster
40+ end
41+
42+ test "get_tenant_cache/2 returns a tenant from cache" do
43+ tenant = tenant_fixture ( % { external_id: "cache_tenant" , sni_hostname: "cache.example.com" } )
44+
45+ Cachex . put (
46+ Supavisor.Cache ,
47+ { :tenant_cache , "cache_tenant" , "cache.example.com" } ,
48+ { :cached , tenant }
49+ )
50+
51+ assert Tenants . get_tenant_cache ( "cache_tenant" , "cache.example.com" ) |> Repo . preload ( :users ) ==
52+ tenant
53+ end
54+
55+ test "get_tenant_cache/2 fetches and caches a tenant after it expires" do
56+ tenant = tenant_fixture ( % { external_id: "cache_tenant" , sni_hostname: "cache.example.com" } )
57+
58+ Cachex . put ( Supavisor.Cache , { :tenant_cache , "cache_tenant" , "cache.example.com" } , tenant ,
59+ ttl: 10
60+ )
61+
62+ Process . sleep ( 50 )
63+
64+ assert Tenants . get_tenant_cache ( "cache_tenant" , "cache.example.com" ) |> Repo . preload ( :users ) ==
65+ tenant
66+ end
67+
68+ test "get_tenant/2 returns the tenant with given external_id and sni_hostname" do
69+ tenant = tenant_fixture ( % { external_id: "sni_tenant" , sni_hostname: "sni.example.com" } )
70+
71+ assert Tenants . get_tenant ( "sni_tenant" , "sni.example.com" ) |> Repo . preload ( :users ) ==
72+ tenant
73+ end
74+
75+ test "get_tenant/2 returns the tenant with sni_hostname when external_id is nil" do
76+ tenant = tenant_fixture ( % { sni_hostname: "sni.example.com" } )
77+
78+ assert Tenants . get_tenant ( nil , "sni.example.com" ) |> Repo . preload ( :users ) ==
79+ tenant
80+ end
81+
82+ test "get_tenant/2 when both provided external_id and sni are nil" do
83+ assert Tenants . get_tenant ( nil , nil ) == nil
84+ end
85+
86+ test "get_tenant/2 returns nil if tenant is not found" do
87+ assert Tenants . get_tenant ( "no_tenant" , "no_host" ) == nil
88+ end
89+
2490 test "create_tenant/1 with valid data creates a tenant" do
2591 user_valid_attrs = % {
2692 "db_user" => "some db_user" ,
@@ -80,6 +146,32 @@ defmodule Supavisor.TenantsTest do
80146 assert { :ok , % { tenant: _ , user: _ } } =
81147 Tenants . get_user ( :single , "postgres" , "dev_tenant" , "" )
82148 end
149+
150+ test "update_tenant_ps/2 updates the tenant's default_parameter_status" do
151+ _tenant = tenant_fixture ( )
152+ default_parameter_status = % { "server_version" => "17.0" }
153+
154+ assert { :ok , % Tenant { default_parameter_status: ^ default_parameter_status } } =
155+ Tenants . update_tenant_ps ( "dev_tenant" , default_parameter_status )
156+ end
157+
158+ test "delete_tenant_by_external_id/1 returns true tenant is found and deleted by a given external_id" do
159+ tenant = tenant_fixture ( )
160+ assert Tenants . delete_tenant_by_external_id ( tenant . external_id ) == true
161+ end
162+
163+ test "delete_tenant_by_external_id/1 returns false if no tenant is found from a given external_id" do
164+ assert Tenants . delete_tenant_by_external_id ( "dev_tenant" ) == false
165+ end
166+
167+ test "delete_cluster_by_alias/1 returns true if cluster is found and deleted by a given alias" do
168+ cluster = cluster_fixture ( )
169+ assert Tenants . delete_cluster_by_alias ( cluster . alias ) == true
170+ end
171+
172+ test "delete_cluster_by_alias/1 returns false if no cluster is found from a given alias" do
173+ assert Tenants . delete_cluster_by_alias ( "some_alias" ) == false
174+ end
83175 end
84176
85177 describe "clusters" do
0 commit comments