Skip to content

Commit

Permalink
Refactor view, test and factory
Browse files Browse the repository at this point in the history
  • Loading branch information
EMaksy committed Mar 20, 2024
1 parent 312a52d commit ea5233b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lib/wanda_web/schemas/v3/catalog/check.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ defmodule WandaWeb.Schemas.V3.Catalog.Check do
"Message returned when the check return value is warning. Only available for `expect_enum` expectations"
}
},
required: [:name, :type, :expression, :failure_message, :warning_message]
required: [:name, :type, :expression, :failure_message]
}
},
when: %Schema{
Expand Down
9 changes: 1 addition & 8 deletions lib/wanda_web/views/v1/catalog_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ defmodule WandaWeb.V1.CatalogView do
%{type: :expect_enum} = expectation -> Map.put(expectation, :type, :unknown)
expectation -> expectation
end)
|> Enum.map(fn %{
name: name,
type: type,
expression: expression,
failure_message: failure_message
} ->
%{name: name, type: type, expression: expression, failure_message: failure_message}
end)
|> Enum.map(&Map.drop(Map.from_struct(&1), [:warning_message]))

%{check | expectations: adapted_expectations}
end
Expand Down
10 changes: 6 additions & 4 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ defmodule Wanda.Factory do
description: Faker.Lorem.sentence(),
remediation: Faker.Lorem.sentence(),
metadata: %{
target_type: Faker.StarWars.character(),
cluster_type: Faker.Lorem.sentence(),
target_type: Enum.random(["cluster", "host"]),
cluster_type: Enum.random(["ASCS_ERS", "HANA_SCALE_UP"]),
provider:
Enum.take_random(["azure", "nutanix", "kvm", "vmware, gcp, aws"], Enum.random(0..6))
Enum.take_random(["azure", "nutanix", "kvm", "vmware, gcp, aws"], Enum.random(1..6))
},
severity: Enum.random([:critical, :warning, :passing]),
facts: build_list(10, :catalog_fact),
Expand Down Expand Up @@ -68,7 +68,9 @@ defmodule Wanda.Factory do
%Catalog.Expectation{
name: Faker.StarWars.character(),
type: Enum.random([:expect, :expect_same, :expect_enum]),
expression: Faker.StarWars.quote()
expression: Faker.StarWars.quote(),
failure_message: Faker.Lorem.sentence(),
warning_message: Faker.Lorem.sentence()
}
end

Expand Down
9 changes: 6 additions & 3 deletions test/wanda/executions/evaluation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,8 @@ defmodule Wanda.Executions.EvaluationTest do
if facts.#{fact_name} == "unknown" {
"unknown"
}
"""
""",
failure_message: nil
)

[%Catalog.Check{id: check_id}] =
Expand Down Expand Up @@ -1761,12 +1762,14 @@ defmodule Wanda.Executions.EvaluationTest do
build(:catalog_expectation,
name: expectation_name,
type: :expect_same,
expression: "facts.#{fact_name}"
expression: "facts.#{fact_name}",
failure_message: nil
),
build(:catalog_expectation,
name: another_expectation_name,
type: :expect_same,
expression: "facts.#{another_fact_name} == \"#{another_fact_value}\""
expression: "facts.#{another_fact_name} == \"#{another_fact_value}\"",
failure_message: nil
)
]
)
Expand Down
48 changes: 24 additions & 24 deletions test/wanda_web/views/v1/catalog_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule WandaWeb.V1.CatalogViewTest do
import Phoenix.View
import Wanda.Factory

alias Wanda.Catalog.Check
alias WandaWeb.V1.CatalogView

describe "CatalogView" do
Expand All @@ -13,39 +14,27 @@ defmodule WandaWeb.V1.CatalogViewTest do
build(:check, expectations: build_list(2, :catalog_expectation, type: :expect_same))
]

updated_checks =
Enum.map(checks, fn check ->
updated_expectations =
Enum.map(check.expectations, fn %{
name: name,
type: type,
expression: expression,
failure_message: failure_message
} ->
%{name: name, type: type, expression: expression, failure_message: failure_message}
end)

%{check | expectations: updated_expectations}
adapted_checks =
Enum.map(checks, fn %Check{expectations: expectations} = check ->
%{
check
| expectations:
Enum.map(expectations, fn expectation ->
expectation
|> Map.from_struct()
|> Map.drop([:warning_message])
end)
}
end)

rendered_catalog = render(CatalogView, "catalog.json", catalog: checks)

assert %{
items: ^updated_checks
items: ^adapted_checks
} = rendered_catalog
end
end

test "renders catalog.json like expected in the v1 schema" do
checks = [
build(:check, expectations: build_list(2, :catalog_expectation, type: :expect)),
build(:check, expectations: build_list(2, :catalog_expectation, type: :expect_same))
]

rendered_catalog = render(CatalogView, "catalog.json", catalog: checks)
refute Access.get(rendered_catalog, :warning_message)
end

describe "adapt to V1 version" do
test "should remove checks with expect_enum expectations" do
checks =
Expand All @@ -63,5 +52,16 @@ defmodule WandaWeb.V1.CatalogViewTest do
]
} = render(CatalogView, "catalog.json", catalog: checks)
end

test "should remove warning_message in check expectations" do
checks = build_list(1, :check)

%{
items: [%Check{expectations: [expectation | _rest_expectations]}]
} =
render(CatalogView, "catalog.json", catalog: checks)

refute Map.has_key?(expectation, :warning_message)
end
end
end

0 comments on commit ea5233b

Please sign in to comment.