domain.test.exs (3814B)
1 # Zenflows is designed to implement the Valueflows vocabulary, 2 # written and maintained by srfsh <info@dyne.org>. 3 # Copyright (C) 2021-2023 Dyne.org foundation <foundation@dyne.org>. 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU Affero General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU Affero General Public License for more details. 14 # 15 # You should have received a copy of the GNU Affero General Public License 16 # along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 defmodule ZenflowsTest.VF.Scenario.Domain do 19 use ZenflowsTest.Help.EctoCase, async: true 20 21 alias Ecto.Changeset 22 alias Zenflows.VF.{ 23 Scenario, 24 Scenario.Domain, 25 ScenarioDefinition, 26 } 27 28 setup do 29 %{ 30 params: %{ 31 name: Factory.str("name"), 32 note: Factory.str("note"), 33 has_beginning: DateTime.utc_now(), 34 has_end: DateTime.utc_now(), 35 defined_as_id: Factory.insert!(:scenario_definition).id, 36 refinement_of_id: Factory.insert!(:scenario).id, 37 }, 38 inserted: Factory.insert!(:scenario), 39 } 40 end 41 42 describe "one/1" do 43 test "with good id: finds the Scenario", %{inserted: %{id: id}} do 44 assert {:ok, %Scenario{}} = Domain.one(id) 45 end 46 47 test "with bad id: doesn't find the Scenario" do 48 assert {:error, "not found"} = Domain.one(Factory.id()) 49 end 50 end 51 52 describe "create/1" do 53 test "with good params: creates a Scenario", %{params: params} do 54 assert {:ok, %Scenario{} = new} = Domain.create(params) 55 assert new.name == params.name 56 assert new.note == params.note 57 assert new.has_beginning == params.has_beginning 58 assert new.has_end == params.has_end 59 assert new.defined_as_id == params.defined_as_id 60 assert new.refinement_of_id == params.refinement_of_id 61 end 62 63 test "with bad params: doesn't create a Scenario" do 64 assert {:error, %Changeset{}} = Domain.create(%{}) 65 end 66 end 67 68 describe "update/2" do 69 test "with good params: updates the Scenario", %{params: params, inserted: old} do 70 assert {:ok, %Scenario{} = new} = Domain.update(old.id, params) 71 assert new.name == params.name 72 assert new.note == params.note 73 assert new.has_beginning == params.has_beginning 74 assert new.has_end == params.has_end 75 assert new.defined_as_id == params.defined_as_id 76 assert new.refinement_of_id == params.refinement_of_id 77 end 78 79 test "with bad params: doesn't update the Scenario", %{inserted: old} do 80 assert {:ok, %Scenario{} = new} = Domain.update(old.id, %{}) 81 assert new.name == old.name 82 assert new.note == old.note 83 assert new.has_beginning == old.has_beginning 84 assert new.has_end == old.has_end 85 assert new.defined_as_id == old.defined_as_id 86 assert new.refinement_of_id == old.refinement_of_id 87 end 88 end 89 90 describe "delete/1" do 91 test "with good id: deletes the Scenario", %{inserted: %{id: id}} do 92 assert {:ok, %Scenario{id: ^id}} = Domain.delete(id) 93 assert {:error, "not found"} = Domain.one(id) 94 end 95 96 test "with bad id: doesn't delete the Scenario" do 97 assert {:error, "not found"} = Domain.delete(Factory.id()) 98 end 99 end 100 101 describe "preload/2" do 102 test "preloads `:defined_as`", %{inserted: scen} do 103 scen = Domain.preload(scen, :defined_as) 104 assert scen_def = %ScenarioDefinition{} = scen.defined_as 105 assert scen_def.id == scen.defined_as_id 106 end 107 108 test "preloads `:refinement_of`", %{inserted: scen} do 109 scen = Domain.preload(scen, :refinement_of) 110 # since it has 50% chance 111 if scen.refinement_of != nil do 112 assert refin_of = %Scenario{} = scen.refinement_of 113 assert refin_of.id == scen.refinement_of_id 114 end 115 end 116 end 117 end