Skip to content

Commit

Permalink
Add all query tests of event
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed May 24, 2024
1 parent fd40590 commit 4278dc1
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/plugins_management/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ defmodule MishkaInstaller.PluginsManagement.Event do
field(:extension, atom(), enforce: true, derive: "validate(atom)")

# This type can be used when you want to introduce a list of modules that an event depend on them.
field(:depends, list(String.t()), default: [], derive: "validate(not_empty)")
field(:depends, list(String.t()), default: [], derive: "validate(list)")
# This type can be used when you want to introduce an extra data for an event.
field(:extra, list(map()), default: [], derive: "validate(not_empty)")
field(:extra, list(map()), default: [], derive: "validate(list)")
# This type can be used when you want to introduce an event inserted_at unix time(timestamp).
field(:inserted_at, DateTime.t(), auto: {Extra, :get_unix_time})
# This type can be used when you want to introduce an event updated_at unix time(timestamp).
Expand Down
97 changes: 97 additions & 0 deletions test/plugins_management/event_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
defmodule MishkaInstallerTest.PluginsManagement.EventTest do
# Should be tested by mix test --trace --seed 0
use ExUnit.Case, async: false
alias MishkaInstaller.PluginsManagement.Event

###################################################################################
########################## (▰˘◡˘▰) QueryTest (▰˘◡˘▰) ########################
###################################################################################
describe "Event Table CRUD QueryTest ===>" do
test "Create a Plugin record", _context do
{:error, %{message: _msg, fields: [:extension, :event, :name]}} = assert Event.create(%{})

create = fn ->
%{name: MishkaTest.Email, event: "after_login_test", extension: MishkaTest}
|> Event.create()
end

{:ok, %Event{status: :registered, event: "after_login_test", name: MishkaTest.Email}} =
assert create.()

{:error, _error} = assert create.()
end

test "Read a Plugin/Plugins records", _context do
assert Event.read(:all) != []
assert !is_nil(Event.read(name: MishkaTest.Email))
assert MishkaTest = List.first(Event.read(event: "after_login_test")).extension
assert is_nil(Event.read(name: MishkaTest.Email1))
assert [] = Event.read(event: "after_login_test1")
get_data = Event.read(:all) |> List.first()
assert !is_nil(Event.read(get_data.id))
end

test "Update a Plugin record", _context do
get_data = Event.read(:all) |> List.first()
{:ok, struct} = assert Event.update(Map.merge(get_data, %{priority: 50}), get_data.id)
assert struct.priority == 50
get_data1 = Event.read(:all) |> List.first()
assert get_data1.id == get_data.id
{:ok, struct} = assert Event.update(:priority, 67, get_data.id)
assert Event.read(struct.id).priority == 67
end

test "All keys of plugins Record", _context do
assert Event.records() != []
end

test "Unique? plugin Record by name", _context do
assert !Event.unique?(MishkaTest.Email)
assert Event.unique?(MishkaTest.Email1)
end

test "Delete plugin Record", _context do
get_data = Event.read(:all) |> List.first()
{:ok, struct} = assert Event.delete(name: get_data.name)
assert is_nil(Event.read(struct.id))
assert Event.read(:all) == []
end

test "Drop all plugins records" do
create = fn ->
%{name: MishkaTest.Email, event: "after_login_test", extension: MishkaTest}
|> Event.create()
end

{:ok, _struct} = assert create.()
assert Event.read(:all) != []
{:ok, :atomic} = assert Event.drop()
assert Event.read(:all) == []
end

test "Check Hold statuses from plugins records" do
create = fn name, status, deps ->
%{name: name, extension: MishkaTest, depends: deps}
|> Map.put(:event, "after_login_test")
|> Map.put(:status, status)
|> Event.create()
end

{:ok, struct} =
assert create.(MishkaTest.Email, :registered, [
MishkaTest.Email1,
MishkaTest.Email2,
MishkaTest.Email3
])

assert length(Event.hold_statuses(struct.depends)) == 3
{:error, _error} = assert Event.hold_statuses?(struct.depends)

create.(MishkaTest.Email1, :started, [])
create.(MishkaTest.Email2, :started, [])
create.(MishkaTest.Email3, :started, [])
assert length(Event.hold_statuses(struct.depends)) == 0
:ok = assert Event.hold_statuses?(struct.depends)
end
end
end
5 changes: 5 additions & 0 deletions test/plugins_management/hook_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule MishkaInstallerTest.PluginsManagement.HookTest do
# Should be tested by mix test --trace --seed 0
use ExUnit.Case, async: false
# alias alias MishkaInstaller.PluginsManagement.Event
end
20 changes: 20 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ ExUnit.start()

alias MishkaInstaller.ProcessingPipelines.Queue.{Queue, Job}
alias MnesiaAssistant.Table
alias MishkaInstaller.PluginsManagement.Event

MnesiaAssistant.start("#{Mix.env()}", ".mnesia", :mishka_installer_queue)

{0, [], :timer.seconds(15), 3}
Expand All @@ -26,3 +28,21 @@ end
MishkaInstallerTest.Support.MishkaQueue.WorkerOne.worker?()
MishkaInstallerTest.Support.MishkaQueue.WorkerTwo.worker?()
MishkaInstallerTest.Support.MishkaQueue.WorkerThree.worker?()

{0, [Queue], :timer.seconds(15), 3}
|> Table.start_table(
Event,
[
type: :set,
ram_copies: [node()],
attributes: Event.keys(),
index: [:name, :event, :extension],
record_name: Event,
storage_properties: [ets: [{:read_concurrency, true}, {:write_concurrency, true}]]
],
:mishka_installer_event
)
|> case do
{:ok, :create_table, :mishka_installer_event} -> :ok
_ -> :error
end

0 comments on commit 4278dc1

Please sign in to comment.