Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Add tests for StorageQuery.get_main_storage[_id]/1
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro committed Oct 15, 2017
1 parent a73d093 commit f29882d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
13 changes: 9 additions & 4 deletions lib/event/loggable/flow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ defmodule Helix.Event.Loggable.Flow do

unquote(block)

# Declaring the typespec for `generate/1` here, since the Event may
# implement multiple `log/2` patterns, but they all share the same
# signature
@spec generate(unquote(__CALLER__.module).t) ::
[Helix.Event.Loggable.Flow.log_entry]

# Fallback
def generate(_),
do: []
Expand All @@ -56,13 +62,10 @@ defmodule Helix.Event.Loggable.Flow do
defined, in which case the event will be pattern-matched against them.
"""
defmacro log(query, do: block) do
module = MacroUtils.remove_protocol_namespace(__CALLER__.module, __MODULE__)
query = replace_module(query, module)
query = replace_module(query, __CALLER__.module)

quote do

@spec generate(unquote(module).t) ::
[Helix.Event.Loggable.Flow.log_entry]
def generate(unquote(query)) do
unquote(block)
end
Expand All @@ -84,6 +87,8 @@ defmodule Helix.Event.Loggable.Flow do
and mess everything up, Helix won't compile.
"""
defp replace_module(query, caller) do
caller = MacroUtils.remove_protocol_namespace(caller, Helix.Event.Loggable)

{a, s, t} = query

# Verifies whether it's a pattern match (`var = :something`)
Expand Down
22 changes: 18 additions & 4 deletions lib/software/query/storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,26 @@ defmodule Helix.Software.Query.Storage do
defdelegate get_storage_drives(storage),
to: StorageDriveInternal

# TODO Test
@spec get_main_storage(Server.idt) ::
Storage.t
@doc """
Returns the "main" storage of a server, which, for the time being, is the only
storage a server may have.
"""
def get_main_storage(server) do
server
|> get_main_storage_id()
|> fetch()
end

@spec get_main_storage_id(Server.idt) ::
Storage.id
def get_main_storage(server = %Server{}),
do: get_main_storage(server.server_id)
def get_main_storage(server_id = %Server.ID{}) do
@doc """
Identical to `get_main_storage`, but only returns the server's storage id.
"""
def get_main_storage_id(server = %Server{}),
do: get_main_storage_id(server.server_id)
def get_main_storage_id(server_id = %Server.ID{}) do
server_id
|> CacheQuery.from_server_get_storages!()
|> List.first()
Expand Down
24 changes: 24 additions & 0 deletions test/software/query/storage_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule Helix.Software.Query.StorageTest do

alias Helix.Software.Query.Storage, as: StorageQuery

alias Helix.Test.Server.Setup, as: ServerSetup
alias Helix.Test.Software.Helper, as: SoftwareHelper
alias Helix.Test.Software.Setup, as: SoftwareSetup

describe "storage_contents/1" do
Expand Down Expand Up @@ -42,4 +44,26 @@ defmodule Helix.Software.Query.StorageTest do
refute Enum.empty?(StorageQuery.files_on_storage(storage))
end
end

describe "get_main_storage_id/1" do
test "returns the 'main' storage id of the server" do
{server, _} = ServerSetup.server()

storage = SoftwareHelper.get_storage(server)
main_storage_id = StorageQuery.get_main_storage_id(server)

assert storage.storage_id == main_storage_id
end
end

describe "get_main_storage/1" do
test "returns the 'main' storage of the server" do
{server, _} = ServerSetup.server()

storage = SoftwareHelper.get_storage(server)
main_storage = StorageQuery.get_main_storage(server)

assert storage == main_storage
end
end
end
1 change: 0 additions & 1 deletion test/support/event/setup/software.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ defmodule Helix.Test.Event.Setup.Software do
event
end


@doc """
Generates a FileDownloadFailed event with real data.
"""
Expand Down

0 comments on commit f29882d

Please sign in to comment.