Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abc3 committed Sep 27, 2023
1 parent 3f10ff7 commit 83fea47
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 236 deletions.
6 changes: 2 additions & 4 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -436,15 +436,13 @@ defmodule Supavisor.ClientHandler do
{:cluster, {list2str(rest2), alias_or_tenant}}

{a, rest2} ->
{:single, {list2str([a | rest2]), alias_or_tenant}}
{:single, {list2str(Enum.reverse([a | rest2])), alias_or_tenant}}
end
end
end

@spec list2str([String.t()]) :: String.t()
defp list2str(list) do
Enum.join(list, ".")
end
defp list2str(list), do: Enum.join(list, ".")

def decode_startup_packet(<<len::integer-32, _protocol::binary-4, rest::binary>>) do
with {:ok, payload} <- decode_startup_packet_payload(rest) do
Expand Down
2 changes: 1 addition & 1 deletion lib/supavisor/monitoring/tenant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Supavisor.PromEx.Plugins.Tenant do

alias Supavisor, as: S

@tags [:tenant, :user, :mode]
@tags [:tenant, :user, :mode, :type]

@impl true
def polling_metrics(opts) do
Expand Down
2 changes: 1 addition & 1 deletion test/integration/proxy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ defmodule Supavisor.Integration.ProxyTest do
:timer.sleep(500)

[{_, client_pid, _}] =
Supavisor.get_local_manager({"proxy_tenant", "transaction", :transaction})
Supavisor.get_local_manager({{:single, "proxy_tenant"}, "transaction", :transaction})
|> :sys.get_state()
|> then(& &1[:tid])
|> :ets.tab2list()
Expand Down
2 changes: 1 addition & 1 deletion test/supavisor/client_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule Supavisor.ClientHandlerTest do
end

test "consist cluster" do
username = "username"
username = "some.user.cluster.alias"
{t, {u, a}} = ClientHandler.parse_user_info(username)
assert {t, {u, a}} == {:cluster, {"some.user", "alias"}}
end
Expand Down
3 changes: 2 additions & 1 deletion test/supavisor/db_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ defmodule Supavisor.DbHandlerTest do
tenant: "test_tenant",
user_alias: "test_user_alias",
user: "user",
mode: :transaction
mode: :transaction,
replica_type: :single
}

{:ok, :connect, data, {_, next_event, _}} = Db.init(args)
Expand Down
4 changes: 2 additions & 2 deletions test/supavisor/prom_ex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ defmodule Supavisor.PromExTest do
Process.sleep(500)
metrics = PromEx.get_metrics()
assert metrics =~ "tenant=\"#{@tenant}\""
DynamicSupervisor.stop(proxy, user)
GenServer.stop(proxy)
Process.sleep(500)
Supavisor.stop({@tenant, user, :transaction})
Supavisor.stop({{:single, @tenant}, "postgres", :transaction})
Process.sleep(500)
refute PromEx.get_metrics() =~ "tenant=\"#{@tenant}\""
end
Expand Down
2 changes: 1 addition & 1 deletion test/supavisor/syn_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Supavisor.SynHandlerTest do
require Logger
alias Ecto.Adapters.SQL.Sandbox

@id {"syn_tenant", "postgres", :session}
@id {{:single, "syn_tenant"}, "postgres", :session}

test "resolving conflict" do
node2 = :"[email protected]"
Expand Down
138 changes: 12 additions & 126 deletions test/supavisor/tenants_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,70 +70,12 @@ defmodule Supavisor.TenantsTest do
assert %Ecto.Changeset{} = Tenants.change_tenant(tenant)
end

test "get_user/3" do
test "get_user/4" do
_tenant = tenant_fixture()
assert {:error, :not_found} = Tenants.get_user("no_user", "no_tenant", "")
assert {:ok, %{tenant: _, user: _}} = Tenants.get_user("postgres", "dev_tenant", "")
end
end

describe "clusters" do
alias Supavisor.Tenants.Cluster

import Supavisor.TenantsFixtures

@invalid_attrs %{tenant_external_id: nil, type: nil}

test "list_clusters/0 returns all clusters" do
cluster = cluster_fixture()
assert Tenants.list_clusters() == [cluster]
end

test "get_cluster!/1 returns the cluster with given id" do
cluster = cluster_fixture()
assert Tenants.get_cluster!(cluster.id) == cluster
end

test "create_cluster/1 with valid data creates a cluster" do
valid_attrs = %{tenant_external_id: "some tenant_external_id", type: "some type"}

assert {:ok, %Cluster{} = cluster} = Tenants.create_cluster(valid_attrs)
assert cluster.tenant_external_id == "some tenant_external_id"
assert cluster.type == "some type"
end

test "create_cluster/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Tenants.create_cluster(@invalid_attrs)
end

test "update_cluster/2 with valid data updates the cluster" do
cluster = cluster_fixture()

update_attrs = %{
tenant_external_id: "some updated tenant_external_id",
type: "some updated type"
}

assert {:ok, %Cluster{} = cluster} = Tenants.update_cluster(cluster, update_attrs)
assert cluster.tenant_external_id == "some updated tenant_external_id"
assert cluster.type == "some updated type"
end

test "update_cluster/2 with invalid data returns error changeset" do
cluster = cluster_fixture()
assert {:error, %Ecto.Changeset{}} = Tenants.update_cluster(cluster, @invalid_attrs)
assert cluster == Tenants.get_cluster!(cluster.id)
end
assert {:error, :not_found} = Tenants.get_user(:single, "no_user", "no_tenant", "")

test "delete_cluster/1 deletes the cluster" do
cluster = cluster_fixture()
assert {:ok, %Cluster{}} = Tenants.delete_cluster(cluster)
assert_raise Ecto.NoResultsError, fn -> Tenants.get_cluster!(cluster.id) end
end

test "change_cluster/1 returns a cluster changeset" do
cluster = cluster_fixture()
assert %Ecto.Changeset{} = Tenants.change_cluster(cluster)
assert {:ok, %{tenant: _, user: _}} =
Tenants.get_user(:single, "postgres", "dev_tenant", "")
end
end

Expand All @@ -142,22 +84,21 @@ defmodule Supavisor.TenantsTest do

import Supavisor.TenantsFixtures

@invalid_attrs %{active: nil}
@invalid_attrs %{active: nil, alias: nil}
@valid_attrs %{active: true, alias: "some_alias"}

test "list_clusters/0 returns all clusters" do
cluster = cluster_fixture()
assert Tenants.list_clusters() == [cluster]
assert Tenants.list_clusters() |> Repo.preload(:cluster_tenants) == [cluster]
end

test "get_cluster!/1 returns the cluster with given id" do
cluster = cluster_fixture()
assert Tenants.get_cluster!(cluster.id) == cluster
assert Tenants.get_cluster!(cluster.id) |> Repo.preload(:cluster_tenants) == cluster
end

test "create_cluster/1 with valid data creates a cluster" do
valid_attrs = %{active: true}

assert {:ok, %Cluster{} = cluster} = Tenants.create_cluster(valid_attrs)
assert {:ok, %Cluster{} = cluster} = Tenants.create_cluster(@valid_attrs)
assert cluster.active == true
end

Expand All @@ -167,16 +108,15 @@ defmodule Supavisor.TenantsTest do

test "update_cluster/2 with valid data updates the cluster" do
cluster = cluster_fixture()
update_attrs = %{active: false}

assert {:ok, %Cluster{} = cluster} = Tenants.update_cluster(cluster, update_attrs)
assert cluster.active == false
assert {:ok, %Cluster{} = cluster} = Tenants.update_cluster(cluster, @valid_attrs)
assert cluster.active == true
end

test "update_cluster/2 with invalid data returns error changeset" do
cluster = cluster_fixture()
assert {:error, %Ecto.Changeset{}} = Tenants.update_cluster(cluster, @invalid_attrs)
assert cluster == Tenants.get_cluster!(cluster.id)
assert cluster == Tenants.get_cluster!(cluster.id) |> Repo.preload(:cluster_tenants)
end

test "delete_cluster/1 deletes the cluster" do
Expand All @@ -190,58 +130,4 @@ defmodule Supavisor.TenantsTest do
assert %Ecto.Changeset{} = Tenants.change_cluster(cluster)
end
end

describe "cluster_tenants" do
alias Supavisor.Tenants.ClusterTenants

import Supavisor.TenantsFixtures

@invalid_attrs %{active: nil}

test "list_cluster_tenants/0 returns all cluster_tenants" do
cluster_tenants = cluster_tenants_fixture()
assert Tenants.list_cluster_tenants() == [cluster_tenants]
end

test "get_cluster_tenants!/1 returns the cluster_tenants with given id" do
cluster_tenants = cluster_tenants_fixture()
assert Tenants.get_cluster_tenants!(cluster_tenants.id) == cluster_tenants
end

test "create_cluster_tenants/1 with valid data creates a cluster_tenants" do
valid_attrs = %{active: true}

assert {:ok, %ClusterTenants{} = cluster_tenants} = Tenants.create_cluster_tenants(valid_attrs)
assert cluster_tenants.active == true
end

test "create_cluster_tenants/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Tenants.create_cluster_tenants(@invalid_attrs)
end

test "update_cluster_tenants/2 with valid data updates the cluster_tenants" do
cluster_tenants = cluster_tenants_fixture()
update_attrs = %{active: false}

assert {:ok, %ClusterTenants{} = cluster_tenants} = Tenants.update_cluster_tenants(cluster_tenants, update_attrs)
assert cluster_tenants.active == false
end

test "update_cluster_tenants/2 with invalid data returns error changeset" do
cluster_tenants = cluster_tenants_fixture()
assert {:error, %Ecto.Changeset{}} = Tenants.update_cluster_tenants(cluster_tenants, @invalid_attrs)
assert cluster_tenants == Tenants.get_cluster_tenants!(cluster_tenants.id)
end

test "delete_cluster_tenants/1 deletes the cluster_tenants" do
cluster_tenants = cluster_tenants_fixture()
assert {:ok, %ClusterTenants{}} = Tenants.delete_cluster_tenants(cluster_tenants)
assert_raise Ecto.NoResultsError, fn -> Tenants.get_cluster_tenants!(cluster_tenants.id) end
end

test "change_cluster_tenants/1 returns a cluster_tenants changeset" do
cluster_tenants = cluster_tenants_fixture()
assert %Ecto.Changeset{} = Tenants.change_cluster_tenants(cluster_tenants)
end
end
end
84 changes: 0 additions & 84 deletions test/supavisor_web/controllers/cluster_tenants_controller_test.exs

This file was deleted.

25 changes: 10 additions & 15 deletions test/support/fixtures/tenants_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,19 @@ defmodule Supavisor.TenantsFixtures do
{:ok, cluster} =
attrs
|> Enum.into(%{
active: true
active: true,
alias: "some_alias",
cluster_tenants: [
%{
type: "write",
cluster_alias: "some_alias",
tenant_external_id: "proxy_tenant",
active: true
}
]
})
|> Supavisor.Tenants.create_cluster()

cluster
end

@doc """
Generate a cluster_tenants.
"""
def cluster_tenants_fixture(attrs \\ %{}) do
{:ok, cluster_tenants} =
attrs
|> Enum.into(%{
active: true
})
|> Supavisor.Tenants.create_cluster_tenants()

cluster_tenants
end
end

0 comments on commit 83fea47

Please sign in to comment.