Skip to content

Commit

Permalink
Clean up all old tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Jun 19, 2024
1 parent 34796cd commit b3dd153
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 27 deletions.
31 changes: 17 additions & 14 deletions lib/polar/streams/version/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ defmodule Polar.Streams.Version.Manager do
basic_setting = Globals.get("basic")
bot = Polar.Accounts.Automation.get_bot!()

from(
v in Version,
where:
v.product_id == ^version.product_id and
v.current_state == ^"active",
offset: ^basic_setting.versions_per_product,
order_by: [desc: :inserted_at]
)
|> Repo.all()
|> Enum.map(fn v ->
Eventful.Transit.perform(v, bot, "deactivate",
comment: "New version activated.",
parameters: %{"event_id" => event.id}
results =
from(
v in Version,
where:
v.product_id == ^version.product_id and
v.current_state == ^"active",
offset: ^basic_setting.versions_per_product,
order_by: [desc: :inserted_at]
)
end)
|> Repo.all()
|> Enum.map(fn v ->
Eventful.Transit.perform(v, bot, "deactivate",
comment: "New version activated.",
parameters: %{"event_id" => event.id}
)
end)

{:ok, results}
end
end
46 changes: 39 additions & 7 deletions test/polar/streams/version/manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,47 +36,79 @@ defmodule Polar.Streams.Version.ManagerTest do
end

describe "deactivate old version on create" do
setup %{product: product} do
setup %{product: product, bot: bot} do
{:ok, version} =
Streams.create_version(product, valid_version_attributes(2))

%{existing_version: version}
{:ok, %{resource: testing_version}} =
Eventful.Transit.perform(version, bot, "test")

{:ok, %{resource: active_version}} =
Eventful.Transit.perform(testing_version, bot, "activate")

%{existing_version: active_version}
end

test "activating a new version deactivates old versions", %{
product: product,
bot: bot,
existing_version: existing_version
} do
assert {:ok, _version} =
assert {:ok, version} =
Streams.create_version(product, valid_version_attributes(3))

{:ok, %{resource: testing_version}} =
Eventful.Transit.perform(version, bot, "test")

{:ok, %{resource: _active_version}} =
Eventful.Transit.perform(testing_version, bot, "activate")

existing_version = Repo.reload(existing_version)

assert existing_version.current_state == "inactive"
end
end

describe "keep 2 previous version" do
setup %{product: product} do
setup %{product: product, bot: bot} do
Polar.Globals.save("basic", %{versions_per_product: 2})

{:ok, version3} =
Streams.create_version(product, valid_version_attributes(3))

{:ok, %{resource: testing_version3}} =
Eventful.Transit.perform(version3, bot, "test")

{:ok, %{resource: active_version3}} =
Eventful.Transit.perform(testing_version3, bot, "activate")

{:ok, version4} =
Streams.create_version(product, valid_version_attributes(4))

%{existing_version: version4, to_be_inactive: version3}
{:ok, %{resource: testing_version4}} =
Eventful.Transit.perform(version4, bot, "test")

{:ok, %{resource: active_version4}} =
Eventful.Transit.perform(testing_version4, bot, "activate")

%{existing_version: active_version4, to_be_inactive: active_version3}
end

test "create new version deactivate version 3", %{
test "create and activate new version deactivate version 3", %{
product: product,
bot: bot,
existing_version: existing_version,
to_be_inactive: to_be_inactive
} do
assert {:ok, _version} =
assert {:ok, version} =
Streams.create_version(product, valid_version_attributes(5))

{:ok, %{resource: testing_version}} =
Eventful.Transit.perform(version, bot, "test")

{:ok, %{resource: _active_version}} =
Eventful.Transit.perform(testing_version, bot, "activate")

to_be_inactive = Repo.reload(to_be_inactive)

assert to_be_inactive.current_state == "inactive"
Expand Down
9 changes: 7 additions & 2 deletions test/polar/streams/version/transitions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Polar.Streams.Version.TransitionsTest do

password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})
bot = bot_fixture(%{password: password})

{:ok, product} =
Streams.create_product(%{
Expand Down Expand Up @@ -47,7 +47,12 @@ defmodule Polar.Streams.Version.TransitionsTest do
]
})

{:ok, user: user, version: version}
{:ok, %{resource: testing_version}} = Eventful.Transit.perform(version, bot, "test")

{:ok, %{resource: active_version}} =
Eventful.Transit.perform(testing_version, bot, "activate")

{:ok, user: user, version: active_version}
end

describe "deactivate" do
Expand Down
9 changes: 7 additions & 2 deletions test/polar_web/controllers/stream_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule PolarWeb.StreamControllerTest do

password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})
bot = bot_fixture(%{password: password})

{:ok, space} = Accounts.create_space(user, %{name: "some-test-123"})

Expand All @@ -36,7 +36,7 @@ defmodule PolarWeb.StreamControllerTest do
}
})

{:ok, _version} =
{:ok, version} =
Streams.create_version(product, %{
serial: "20240209_13:00",
items: [
Expand All @@ -57,6 +57,11 @@ defmodule PolarWeb.StreamControllerTest do
]
})

{:ok, %{resource: testing_version}} = Eventful.Transit.perform(version, bot, "test")

{:ok, %{resource: _active_version}} =
Eventful.Transit.perform(testing_version, bot, "activate")

{:ok, product: product, credential: credential}
end

Expand Down
10 changes: 8 additions & 2 deletions test/polar_web/live/root_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule PolarWeb.RootLiveTest do

password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})
bot = bot_fixture(%{password: password})

{:ok, space} = Accounts.create_space(user, %{name: "some-test-123"})

Expand Down Expand Up @@ -58,7 +58,7 @@ defmodule PolarWeb.RootLiveTest do
]
})

{:ok, _version} =
{:ok, version} =
Streams.create_version(product, %{
serial: "20240209-13",
items: [
Expand All @@ -79,6 +79,12 @@ defmodule PolarWeb.RootLiveTest do
]
})

{:ok, %{resource: testing_version}} =
Eventful.Transit.perform(version, bot, "test")

{:ok, %{resource: _active_version}} =
Eventful.Transit.perform(testing_version, bot, "activate")

{:ok, product: product, credential: credential}
end

Expand Down

0 comments on commit b3dd153

Please sign in to comment.