Skip to content

Commit

Permalink
Top-level Publish tests
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryape committed Aug 5, 2024
1 parent 8d81ca7 commit 3a2ea18
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 67 deletions.
68 changes: 18 additions & 50 deletions lib/smee/publish.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ defmodule Smee.Publish do
alias Smee.Publish.Markdown

Check warning on line 76 in lib/smee/publish.ex

View workflow job for this annotation

GitHub Actions / Build and test

unused alias Markdown
alias Smee.Publish.Csv

Check warning on line 77 in lib/smee/publish.ex

View workflow job for this annotation

GitHub Actions / Build and test

unused alias Csv
alias Smee.Publish.SamlXml
alias Smee.Publish.FrontendUtils

use Smee.Publish.LegacyCompatibility

Expand All @@ -85,21 +86,9 @@ defmodule Smee.Publish do
"""
@spec formats() :: list(atom())
def formats() do
[
:csv,
:disco,
:index,
:markdown,
:saml,
:thiss,
:udest,
:udisco
]
FrontendUtils.formats()
end

@default_options [format: :saml, lang: "en", id_type: :hash, to: "published", labels: false]
@allowed_options Keyword.keys(@default_options) ++ [:valid_until, :filename]

@doc """
Estimates the size (in bytes) of an aggregated published file or stream in the selected format (defaulting to SAML2
metadata).
Expand All @@ -112,8 +101,8 @@ defmodule Smee.Publish do
"""
@spec eslength(entities :: Enumerable.t(), options :: keyword()) :: integer()
def eslength(entities, options \\ []) do
options = prepare_options(options)
apply(select_backend(options), :eslength, [entities, options])
options = FrontendUtils.prepare_options(options)
apply(FrontendUtils.select_backend(options), :eslength, [entities, options])
end

@doc """
Expand All @@ -127,8 +116,8 @@ defmodule Smee.Publish do
"""
@spec aggregate(entities :: Enumerable.t(), options :: keyword()) :: binary()
def aggregate(entities, options \\ []) do
options = prepare_options(options)
apply(select_backend(options), :aggregate, [entities, options])
options = FrontendUtils.prepare_options(options)
apply(FrontendUtils.select_backend(options), :aggregate, [entities, options])
end

@doc """
Expand All @@ -143,8 +132,8 @@ defmodule Smee.Publish do
"""
@spec aggregate_stream(entities :: Enumerable.t(), options :: keyword()) :: Enumerable.t(binary())
def aggregate_stream(entities, options \\ []) do
options = prepare_options(options)
apply(select_backend(options), :aggregate_stream, [entities, options])
options = FrontendUtils.prepare_options(options)
apply(FrontendUtils.select_backend(options), :aggregate_stream, [entities, options])
end

@doc """
Expand All @@ -159,8 +148,8 @@ defmodule Smee.Publish do
"""
@spec items(entities :: Enumerable.t(), options :: keyword()) :: Map.t(tuple())
def items(entities, options \\ []) do
options = prepare_options(options)
apply(select_backend(options), :items, [entities, options])
options = FrontendUtils.prepare_options(options)
apply(FrontendUtils.select_backend(options), :items, [entities, options])
end

@doc """
Expand All @@ -175,8 +164,8 @@ defmodule Smee.Publish do
"""
@spec items_stream(entities :: Enumerable.t(), options :: keyword()) :: Enumerable.t(tuple())
def items_stream(entities, options \\ []) do
options = prepare_options(options)
apply(select_backend(options), :items_stream, [entities, options])
options = FrontendUtils.prepare_options(options)
apply(FrontendUtils.select_backend(options), :items_stream, [entities, options])
end

@doc """
Expand All @@ -190,8 +179,8 @@ defmodule Smee.Publish do
"""
@spec raw_stream(entities :: Enumerable.t(), options :: keyword()) :: Enumerable.t(tuple())
def raw_stream(entities, options \\ []) do
options = prepare_options(options)
apply(select_backend(options), :raw_stream, [entities, options])
options = FrontendUtils.prepare_options(options)
apply(FrontendUtils.select_backend(options), :raw_stream, [entities, options])
end

@doc """
Expand All @@ -203,8 +192,8 @@ defmodule Smee.Publish do
"""
@spec write_aggregate(entities :: Enumerable.t(), options :: keyword()) :: binary()
def write_aggregate(entities, options \\ []) do
options = prepare_options(options)
apply(select_backend(options), :write_aggregate, [entities, options])
options = FrontendUtils.prepare_options(options)
apply(FrontendUtils.select_backend(options), :write_aggregate, [entities, options])
end

@doc """
Expand All @@ -221,31 +210,10 @@ defmodule Smee.Publish do
"""
@spec write_items(entities :: Enumerable.t(), options :: keyword()) :: list()
def write_items(entities, options \\ []) do
options = prepare_options(options)
apply(select_backend(options), :write_items, [entities, options])
options = FrontendUtils.prepare_options(options)
apply(FrontendUtils.select_backend(options), :write_items, [entities, options])
end

################################################################################

defp prepare_options(options) do
Keyword.merge(@default_options, options)
|> Keyword.take(@allowed_options)
end

defp select_backend(options) do
case options[:format] do
:csv -> Csv
:disco -> Disco
:index -> Index
:markdown -> Markdown
:metadata -> SamlXml
:saml -> SamlXml
:thiss -> Thiss
:udest -> Udest
:udisco -> Udisco
nil -> SamlXml
_ -> raise "Unknown publishing format '#{options[:format]} - known formats include #{formats()}'"
end
end

end
42 changes: 42 additions & 0 deletions lib/smee/publish/frontend_utils.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule Smee.Publish.FrontendUtils do

@default_options [format: :saml, lang: "en", id_type: :hash, to: "published", labels: false]
@allowed_options Keyword.keys(@default_options) ++ [:valid_until, :filename]

@spec formats() :: list(atom())
def formats() do
[
:csv,
:disco,
:index,
:markdown,
:saml,
:thiss,
:udest,
:udisco
]
end

def prepare_options(options) do
Keyword.merge(@default_options, options)
|> Keyword.take(@allowed_options)
end

def select_backend(options) do
case options[:format] do
:csv -> Smee.Publish.Csv
:disco -> Smee.Publish.Disco
:index -> Smee.Publish.Index
:markdown -> Smee.Publish.Markdown
:metadata -> Smee.Publish.SamlXml
:saml -> Smee.Publish.SamlXml
:thiss -> Smee.Publish.Thiss
:udest -> Smee.Publish.Udest
:udisco -> Smee.Publish.Udisco
:default -> Smee.Publish.SamlXml
nil -> Smee.Publish.SamlXml
_ -> raise "Unknown publishing format ':#{options[:format]}' - known formats include #{Enum.join(formats(), ", :")}"
end
end

end
81 changes: 81 additions & 0 deletions test/smee/smee_publish_frontend_utils_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
defmodule SmeePublishFrontendUtilsTest do
use ExUnit.Case

alias Smee.Publish.FrontendUtils
alias Smee.Source
#alias Smee.Metadata
#alias Smee.Lint
#alias Smee.XmlMunger

@valid_metadata Source.new("test/support/static/aggregate.xml")
|> Smee.fetch!()

describe "formats/0" do

test "returns a list of supported publishing formats" do
assert [:csv, :disco, :index, :markdown, :saml, :thiss, :udest, :udisco] = FrontendUtils.formats()
end

test "does not return a list containing private formats" do
refute Enum.member?(FrontendUtils.formats(), [:progress])
refute Enum.member?(FrontendUtils.formats(), [:string])
refute Enum.member?(FrontendUtils.formats(), [:null])
end

end

describe "prepare_options/1" do

test "user options have a format of :saml by default" do
assert :saml = Keyword.get(FrontendUtils.prepare_options([]), :format)
end

test "user options have a lang of 'en' by default" do
assert "en" = Keyword.get(FrontendUtils.prepare_options([]), :lang)
end

test "user options have an id_type of :hash by default" do
assert :hash = Keyword.get(FrontendUtils.prepare_options([]), :id_type)
end

test "user options have a default output path of './published' by default" do
assert "published" = Keyword.get(FrontendUtils.prepare_options([]), :to)
end

test "user options have index labels turned off by default" do
refute Keyword.get(FrontendUtils.prepare_options([]), :labels)
end

test "unknown option keys do not pass through" do
refute Keyword.get(FrontendUtils.prepare_options([banana: "icecream"]), :banana)
end

end

describe "select_backend/1" do

test "known, supported format types return a Publish module name" do
assert Smee.Publish.Csv = FrontendUtils.select_backend([format: :csv])
assert Smee.Publish.Disco = FrontendUtils.select_backend([format: :disco])
assert Smee.Publish.Index = FrontendUtils.select_backend([format: :index])
assert Smee.Publish.Markdown = FrontendUtils.select_backend([format: :markdown])
assert Smee.Publish.SamlXml = FrontendUtils.select_backend([format: :saml])
assert Smee.Publish.Thiss = FrontendUtils.select_backend([format: :thiss])
assert Smee.Publish.Udest = FrontendUtils.select_backend([format: :udest])
assert Smee.Publish.Udisco = FrontendUtils.select_backend([format: :udisco])
end

test "there are various legacy aliases for the default format, SAML" do
assert Smee.Publish.SamlXml = FrontendUtils.select_backend([format: :metadata])
assert Smee.Publish.SamlXml = FrontendUtils.select_backend([format: :nil])
assert Smee.Publish.SamlXml = FrontendUtils.select_backend([format: :default])

end

test "Unknown format types cause an exception" do
assert_raise RuntimeError, fn -> FrontendUtils.select_backend([format: :msword]) end
end

end

end
Loading

0 comments on commit 3a2ea18

Please sign in to comment.