From ea5233b6cd54ff4e80a1835eb1dbfa187add0769 Mon Sep 17 00:00:00 2001 From: EMaksy Date: Tue, 19 Mar 2024 11:05:09 +0100 Subject: [PATCH] Refactor view, test and factory --- lib/wanda_web/schemas/v3/catalog/check.ex | 2 +- lib/wanda_web/views/v1/catalog_view.ex | 9 +--- test/support/factory.ex | 10 ++-- test/wanda/executions/evaluation_test.exs | 9 ++-- test/wanda_web/views/v1/catalog_view_test.exs | 48 +++++++++---------- 5 files changed, 38 insertions(+), 40 deletions(-) diff --git a/lib/wanda_web/schemas/v3/catalog/check.ex b/lib/wanda_web/schemas/v3/catalog/check.ex index 45fe91c3..a9f67f91 100644 --- a/lib/wanda_web/schemas/v3/catalog/check.ex +++ b/lib/wanda_web/schemas/v3/catalog/check.ex @@ -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{ diff --git a/lib/wanda_web/views/v1/catalog_view.ex b/lib/wanda_web/views/v1/catalog_view.ex index 30ecba2b..85ede63e 100644 --- a/lib/wanda_web/views/v1/catalog_view.ex +++ b/lib/wanda_web/views/v1/catalog_view.ex @@ -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 diff --git a/test/support/factory.ex b/test/support/factory.ex index 597a7b0e..aab526ef 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -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), @@ -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 diff --git a/test/wanda/executions/evaluation_test.exs b/test/wanda/executions/evaluation_test.exs index a9c95c35..2b9880e3 100644 --- a/test/wanda/executions/evaluation_test.exs +++ b/test/wanda/executions/evaluation_test.exs @@ -839,7 +839,8 @@ defmodule Wanda.Executions.EvaluationTest do if facts.#{fact_name} == "unknown" { "unknown" } - """ + """, + failure_message: nil ) [%Catalog.Check{id: check_id}] = @@ -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 ) ] ) diff --git a/test/wanda_web/views/v1/catalog_view_test.exs b/test/wanda_web/views/v1/catalog_view_test.exs index 3f2e21b2..94956c40 100644 --- a/test/wanda_web/views/v1/catalog_view_test.exs +++ b/test/wanda_web/views/v1/catalog_view_test.exs @@ -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 @@ -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 = @@ -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