zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

type.test.exs (5408B)


      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.RecipeFlow.Type do
     19 use ZenflowsTest.Help.AbsinCase, async: true
     20 
     21 setup do
     22 	%{
     23 		params: %{
     24 			"note" => Factory.str("some note"),
     25 			"action" => Factory.build(:action_id),
     26 			"recipeInputOf" => Factory.insert!(:recipe_process).id,
     27 			"recipeOutputOf" => Factory.insert!(:recipe_process).id,
     28 			"recipeFlowResource" => Factory.insert!(:recipe_resource).id,
     29 			"resourceQuantity" => %{
     30 				"hasUnit" => Factory.insert!(:unit).id,
     31 				"hasNumericalValue" => Factory.decimal(),
     32 			},
     33 			"effortQuantity" => %{
     34 				"hasUnit" => Factory.insert!(:unit).id,
     35 				"hasNumericalValue" => Factory.decimal(),
     36 			},
     37 			"recipeClauseOf" => Factory.insert!(:recipe_exchange).id,
     38 		},
     39 		inserted: Factory.insert!(:recipe_flow),
     40 	}
     41 end
     42 
     43 @frag """
     44 fragment recipeFlow on RecipeFlow {
     45 	id
     46 	note
     47 	action {id}
     48 	resourceQuantity {
     49 		hasUnit {id}
     50 		hasNumericalValue
     51 	}
     52 	effortQuantity {
     53 		hasUnit {id}
     54 		hasNumericalValue
     55 	}
     56 	recipeFlowResource {id}
     57 	recipeInputOf {id}
     58 	recipeOutputOf {id}
     59 	recipeClauseOf {id}
     60 }
     61 """
     62 
     63 describe "Query" do
     64 	test "recipeFlow", %{inserted: new} do
     65 		assert %{data: %{"recipeFlow" => data}} =
     66 			run!("""
     67 				#{@frag}
     68 				query ($id: ID!) {
     69 					recipeFlow(id: $id) {...recipeFlow}
     70 				}
     71 			""", vars: %{"id" => new.id})
     72 
     73 		assert data["id"] == new.id
     74 		assert data["note"] == new.note
     75 		assert data["action"]["id"] == new.action_id
     76 		assert data["resourceQuantity"]["hasUnit"]["id"] == new.resource_quantity_has_unit_id
     77 		assert Decimal.eq?(data["resourceQuantity"]["hasNumericalValue"], new.resource_quantity_has_numerical_value)
     78 		assert data["effortQuantity"]["hasUnit"]["id"] == new.effort_quantity_has_unit_id
     79 		assert Decimal.eq?(data["effortQuantity"]["hasNumericalValue"], new.effort_quantity_has_numerical_value)
     80 		assert data["recipeFlowResource"]["id"] == new.recipe_flow_resource_id
     81 		assert data["recipeInputOf"]["id"] == new.recipe_input_of_id
     82 		assert data["recipeOutputOf"]["id"] == new.recipe_output_of_id
     83 		assert data["recipeClauseOf"]["id"] == new.recipe_clause_of_id
     84 	end
     85 end
     86 
     87 describe "Mutation" do
     88 	test "createRecipeFlow", %{params: params} do
     89 		assert %{data: %{"createRecipeFlow" => %{"recipeFlow" => data}}} =
     90 			run!("""
     91 				#{@frag}
     92 				mutation ($recipeFlow: RecipeFlowCreateParams!) {
     93 					createRecipeFlow(recipeFlow: $recipeFlow) {
     94 						recipeFlow {...recipeFlow}
     95 					}
     96 				}
     97 			""", vars: %{"recipeFlow" => params})
     98 
     99 		assert {:ok, _} = Zenflows.DB.ID.cast(data["id"])
    100 		assert data["note"] == params["note"]
    101 		assert data["action"]["id"] == params["action"]
    102 		assert data["resourceQuantity"]["hasUnit"]["id"] == params["resourceQuantity"]["hasUnit"]
    103 		assert data["resourceQuantity"]["hasNumericalValue"] == params["resourceQuantity"]["hasNumericalValue"]
    104 		assert data["effortQuantity"]["hasUnit"]["id"] == params["effortQuantity"]["hasUnit"]
    105 		assert data["effortQuantity"]["hasNumericalValue"] == params["effortQuantity"]["hasNumericalValue"]
    106 		assert data["recipeFlowResource"]["id"] == params["recipeFlowResource"]
    107 		assert data["recipeInputOf"]["id"] == params["recipeInputOf"]
    108 		assert data["recipeOutputOf"]["id"] == params["recipeOutputOf"]
    109 		assert data["recipeClauseOf"]["id"] == params["recipeClauseOf"]
    110 	end
    111 
    112 	test "updateRecipeFlow", %{params: params, inserted: old} do
    113 		assert %{data: %{"updateRecipeFlow" => %{"recipeFlow" => data}}} =
    114 			run!("""
    115 				#{@frag}
    116 				mutation ($recipeFlow: RecipeFlowUpdateParams!) {
    117 					updateRecipeFlow(recipeFlow: $recipeFlow) {
    118 						recipeFlow {...recipeFlow}
    119 					}
    120 				}
    121 			""", vars: %{"recipeFlow" => Map.put(params, "id", old.id)})
    122 
    123 		assert data["id"] == old.id
    124 		assert data["note"] == params["note"]
    125 		assert data["action"]["id"] == params["action"]
    126 		assert data["resourceQuantity"]["hasUnit"]["id"] == params["resourceQuantity"]["hasUnit"]
    127 		assert data["resourceQuantity"]["hasNumericalValue"] == params["resourceQuantity"]["hasNumericalValue"]
    128 		assert data["effortQuantity"]["hasUnit"]["id"] == params["effortQuantity"]["hasUnit"]
    129 		assert data["effortQuantity"]["hasNumericalValue"] == params["effortQuantity"]["hasNumericalValue"]
    130 		assert data["recipeFlowResource"]["id"] == params["recipeFlowResource"]
    131 		assert data["recipeInputOf"]["id"] == params["recipeInputOf"]
    132 		assert data["recipeOutputOf"]["id"] == params["recipeOutputOf"]
    133 		assert data["recipeClauseOf"]["id"] == params["recipeClauseOf"]
    134 	end
    135 
    136 	test "deleteRecipeFlow", %{inserted: %{id: id}} do
    137 		assert %{data: %{"deleteRecipeFlow" => true}} =
    138 			run!("""
    139 				mutation ($id: ID!) {
    140 					deleteRecipeFlow(id: $id)
    141 				}
    142 			""", vars: %{"id" => id})
    143 	end
    144 end
    145 end