Skip to content

Commit

Permalink
Add entires test for all plugins of an event
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed May 27, 2024
1 parent e98e4af commit 4b91ae9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
23 changes: 12 additions & 11 deletions lib/plugins_management/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,19 @@ defmodule MishkaInstaller.PluginsManagement.Event do
{:error, error}

data ->
data
|> Enum.each(fn pl_item ->
with :ok <- plugin_status(pl_item.status),
:ok <- hold_statuses?(pl_item.depends),
{:ok, db_plg} <- update(:status, :started, pl_item.id),
:ok <- MishkaInstaller.broadcast("event", :start, pl_item, true) do
{:ok, :stop, db_plg}
end
end)

sorted_plugins =
Enum.reject(data, &(&1.status in [:stopped, :held]))
Enum.map(data, fn pl_item ->
output =
with :ok <- plugin_status(pl_item.status),
:ok <- hold_statuses?(pl_item.depends),
{:ok, db_plg} <- update(:status, :started, pl_item.id),
:ok <- MishkaInstaller.broadcast("event", :start, pl_item, true) do
db_plg
end

output
end)
|> Enum.reject(&(is_tuple(&1) and elem(&1, 0) == :error))
|> Enum.sort_by(&{&1.priority, &1.name})

PluginModuleStateEvent.purge_create(sorted_plugins, event)
Expand Down
1 change: 0 additions & 1 deletion lib/plugins_management/hook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ defmodule MishkaInstaller.PluginsManagement.Hook do
def start_helper(module, state, reg_db_plg) do
case module.start(true) do
{:ok, :start, st_db_plg} ->
IO.inspect("hiii-=-=-==-=-=->")
Keyword.merge(state, status: st_db_plg.status, depends: st_db_plg.depends)

{:error, [%{field: :event, action: :compile}]} ->
Expand Down
32 changes: 31 additions & 1 deletion test/plugins_management/event_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule MishkaInstallerTest.PluginsManagement.EventTest do
use ExUnit.Case, async: false
alias MishkaInstaller.PluginsManagement.Event
alias MishkaInstallerTest.Support.MishkaPlugin.RegisterEmailSender

alias MishkaInstaller.Helper.PluginModuleStateEvent
###################################################################################
########################## (▰˘◡˘▰) QueryTest (▰˘◡˘▰) ########################
###################################################################################
Expand Down Expand Up @@ -166,6 +166,36 @@ defmodule MishkaInstallerTest.PluginsManagement.EventTest do
{:error, _error} = assert Event.get(name: RegisterEmailSender)
end

test "Start all plugins of an event" do
Event.drop()

create = fn name, status, deps, event, priority ->
%{name: name, extension: MishkaTest, depends: deps, priority: priority}
|> Map.put(:event, event)
|> Map.put(:status, status)
|> Event.create()
end

deps = [MishkaTest.Email1, MishkaTest.Email2, MishkaTest.Email3]

create.(MishkaTest.Email, :registered, deps, "after_login_test", 100)
create.(MishkaTest.Email1, :started, [], "after_login_test", 60)
create.(MishkaTest.Email2, :started, [], "after_login_test", 50)
create.(MishkaTest.Email3, :started, [], "before_login_test", 10)

Event.start("after_login_test")
module = PluginModuleStateEvent.module_event_name("after_login_test")
assert module.initialize?()

module = PluginModuleStateEvent.module_event_name("before_login_test")

assert_raise UndefinedFunctionError, fn ->
assert module.initialize?()
end

Event.drop()
end

test "Backup all plugins" do
end

Expand Down

0 comments on commit 4b91ae9

Please sign in to comment.