How to create a negative test for a verifier

Hi, I'm working on adding some verifiers to the transformer for the Ash Neo4j DataLayer, and I've got a simple Verifier that checks neo4j convention on the resource label Here is the verifier:
defmodule AshNeo4j.Verifiers.VerifyLabelCamelCase do
@moduledoc "Verifies that the label is camelcase"
use Spark.Dsl.Verifier

alias Spark.Dsl.Verifier
alias Spark.Error.DslError
@regex ~r/^[A-Z][a-zA-Z]*$/

@impl true
def verify(dsl) do
IO.inspect(dsl, label: :verify_label_camel_case_dsl)
resource = Verifier.get_persisted(dsl, :module) |> IO.inspect(label: :resource)
neo4j = Map.get(dsl, [:neo4j])
label = Keyword.get(neo4j.opts, :label, nil) |> IO.inspect(label: :label)

cond do
label == nil ->
{:error,
DslError.exception(
module: resource,
message: "Label missing"
)}

Regex.match?(@regex, Atom.to_string(label)) ->
:ok

true ->
{:error,
DslError.exception(
module: resource,
message: "Label must be CamelCase"
)}
end
end
end
defmodule AshNeo4j.Verifiers.VerifyLabelCamelCase do
@moduledoc "Verifies that the label is camelcase"
use Spark.Dsl.Verifier

alias Spark.Dsl.Verifier
alias Spark.Error.DslError
@regex ~r/^[A-Z][a-zA-Z]*$/

@impl true
def verify(dsl) do
IO.inspect(dsl, label: :verify_label_camel_case_dsl)
resource = Verifier.get_persisted(dsl, :module) |> IO.inspect(label: :resource)
neo4j = Map.get(dsl, [:neo4j])
label = Keyword.get(neo4j.opts, :label, nil) |> IO.inspect(label: :label)

cond do
label == nil ->
{:error,
DslError.exception(
module: resource,
message: "Label missing"
)}

Regex.match?(@regex, Atom.to_string(label)) ->
:ok

true ->
{:error,
DslError.exception(
module: resource,
message: "Label must be CamelCase"
)}
end
end
end
Here is an invalid resource, the neo4j (node) label for the InvalidLabel resource should be :Comment not :comment
defmodule InvalidLabel do
@moduledoc false
use Ash.Resource,
domain: AshNeo4j.Test.Domain,
data_layer: AshNeo4j.DataLayer

neo4j do
label :comment
store [:title]
translate id: :uuid
end

attributes do
uuid_primary_key :id
attribute :title, :string, public?: true
end
end
defmodule InvalidLabel do
@moduledoc false
use Ash.Resource,
domain: AshNeo4j.Test.Domain,
data_layer: AshNeo4j.DataLayer

neo4j do
label :comment
store [:title]
translate id: :uuid
end

attributes do
uuid_primary_key :id
attribute :title, :string, public?: true
end
end
I'm at a loss how to write a negative test, as the module simply doesn't compile. Ideally I'd like to delay compilation until in the unit test, and pass the test if it errors. Help appreciated.
Solution:
Define the module inside the test block
Jump to solution
3 Replies
Solution
ZachDaniel
ZachDaniel2w ago
Define the module inside the test block
ZachDaniel
ZachDaniel2w ago
And wrap that in assert_raise

Did you find this page helpful?