Commit dd976d9a authored by Mayel's avatar Mayel

fix inScopeOf, add attr_get_id, more intent testing

parent 3fb0dcf7
Pipeline #215275517 failed with stage
in 1 minute and 4 seconds
......@@ -63,7 +63,7 @@ defmodule CommonsPub.Profiles.Profile do
|> Changeset.validate_required(@required)
|> Changeset.change(
creator_id: creator.id,
geolocation_id: Map.get(attrs, :geolocation), # TODO: validate location
geolocation_id: CommonsPub.Common.attr_get_id(attrs, :geolocation), # TODO: validate location
is_public: true
)
|> common_changeset()
......
......@@ -103,7 +103,7 @@ defmodule CommonsPub.Users.User do
|> change_public()
|> Changeset.change(
# TODO: validate location
geolocation_id: Map.get(attrs, :geolocation)
geolocation_id: CommonsPub.Common.attr_get_id(attrs, :geolocation)
)
end
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Claim.Claims do
import CommonsPub.Common, only: [maybe_put: 3]
import CommonsPub.Common, only: [maybe_put: 3, attr_get_id: 2]
alias CommonsPub.Repo
alias CommonsPub.Users.User
......@@ -51,11 +51,11 @@ defmodule ValueFlows.Claim.Claims do
defp prepare_attrs(attrs) do
attrs
|> maybe_put(:action_id, Map.get(attrs, :action))
|> maybe_put(:action_id, attr_get_id(attrs, :action))
|> maybe_put(:context_id,
attrs |> Map.get(:in_scope_of) |> CommonsPub.Common.maybe(&List.first/1)
)
|> maybe_put(:resource_conforms_to_id, Map.get(attrs, :resource_conforms_to))
|> maybe_put(:triggered_by_id, Map.get(attrs, :triggered_by))
|> maybe_put(:resource_conforms_to_id, attr_get_id(attrs, :resource_conforms_to))
|> maybe_put(:triggered_by_id, attr_get_id(attrs, :triggered_by))
end
end
......@@ -5,8 +5,6 @@ defmodule ValueFlows.Hydration do
UsersResolver
}
alias CommonsPub.Users.User
alias CommonsPub.Communities.Community
alias ValueFlows.Observation.{
Process,
......@@ -74,7 +72,7 @@ defmodule ValueFlows.Hydration do
resolve: &ValueFlows.Util.GraphQL.canonical_url_edge/3
],
in_scope_of: [
resolve: &CommonResolver.context_edge/3
resolve: &ValueFlows.Util.GraphQL.scope_edge/3
],
eligible_location: [
resolve: &ValueFlows.Proposal.GraphQL.eligible_location_edge/3
......@@ -106,7 +104,7 @@ defmodule ValueFlows.Hydration do
resolve: &ValueFlows.Util.GraphQL.at_location_edge/3
],
in_scope_of: [
resolve: &CommonResolver.context_edge/3
resolve: &ValueFlows.Util.GraphQL.scope_edge/3
],
image: [
resolve: &UploadResolver.image_content_edge/3
......@@ -165,7 +163,7 @@ defmodule ValueFlows.Hydration do
resolve: &ValueFlows.Claim.GraphQL.fetch_triggered_by_edge/3,
],
in_scope_of: [
resolve: &CommonResolver.context_edge/3,
resolve: &ValueFlows.Util.GraphQL.scope_edge/3,
],
creator: [
resolve: &UsersResolver.creator_edge/3
......@@ -194,7 +192,7 @@ defmodule ValueFlows.Hydration do
resolve: &ValueFlows.Util.GraphQL.effort_quantity_edge/3
],
in_scope_of: [
resolve: &CommonResolver.context_edge/3
resolve: &ValueFlows.Util.GraphQL.scope_edge/3
],
input_of: [
resolve: &ValueFlows.Observation.EconomicEvent.GraphQL.fetch_input_of_edge/3
......@@ -237,7 +235,7 @@ defmodule ValueFlows.Hydration do
resolve: &ValueFlows.Observation.EconomicResource.GraphQL.fetch_state_edge/3
],
in_scope_of: [
resolve: &CommonResolver.context_edge/3
resolve: &ValueFlows.Util.GraphQL.scope_edge/3
],
classified_as: [
resolve: &ValueFlows.Util.GraphQL.fetch_classifications_edge/3
......@@ -284,7 +282,7 @@ defmodule ValueFlows.Hydration do
resolve: &ValueFlows.Util.GraphQL.canonical_url_edge/3
],
in_scope_of: [
resolve: &CommonResolver.context_edge/3
resolve: &ValueFlows.Util.GraphQL.scope_edge/3
],
classified_as: [
resolve: &ValueFlows.Util.GraphQL.fetch_classifications_edge/3
......@@ -556,15 +554,18 @@ defmodule ValueFlows.Hydration do
:organization | :person
def agent_resolve_type(%{agent_type: :person}, _), do: :person
def agent_resolve_type(%{agent_type: :organization}, _), do: :organization
def agent_resolve_type(%Organisation{}, _), do: :organization
def agent_resolve_type(%CommonsPub.Users.User{}, _), do: :person
def agent_resolve_type(_, _), do: :person
# def agent_resolve_type(%User{}, _), do: :user
# def person_is_type_of(_), do: true
# def organization_is_type_of(_), do: true
# def resolve_context_type(%Organisation{}, _), do: :organisation
def resolve_context_type(%Community{}, _), do: :community
def resolve_context_type(%User{}, _), do: :user
def resolve_context_type(%CommonsPub.Users.User{}, _), do: :person
def resolve_context_type(%Organisation{}, _), do: :organization
def resolve_context_type(%CommonsPub.Communities.Community{}, _), do: :community
def resolve_context_type(%CommonsPub.Collections.Collection{}, _), do: :collection
def resolve_context_type(_, _), do: :agent
def production_flow_item_resolve_type(%EconomicResource{}, _), do: :economic_resource
def production_flow_item_resolve_type(%Process{}, _), do: :process
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Knowledge.ProcessSpecification.ProcessSpecifications do
import CommonsPub.Common, only: [maybe_put: 3]
import CommonsPub.Common, only: [maybe_put: 3, attr_get_id: 2]
alias CommonsPub.{Activities, Common, Feeds, Repo}
alias CommonsPub.GraphQL.{Fields, Page}
......
......@@ -62,7 +62,7 @@ defmodule ValueFlows.Knowledge.ResourceSpecification do
|> Changeset.validate_required(@required)
|> Changeset.change(
creator_id: creator.id,
default_unit_of_effort_id: Map.get(attrs, :default_unit_of_effort),
default_unit_of_effort_id: CommonsPub.Common.attr_get_id(attrs, :default_unit_of_effort),
context_id: context.id,
is_public: true
)
......@@ -78,7 +78,7 @@ defmodule ValueFlows.Knowledge.ResourceSpecification do
|> Changeset.validate_required(@required)
|> Changeset.change(
creator_id: creator.id,
default_unit_of_effort_id: Map.get(attrs, :default_unit_of_effort),
default_unit_of_effort_id: CommonsPub.Common.attr_get_id(attrs, :default_unit_of_effort),
is_public: true
)
|> common_changeset()
......@@ -93,7 +93,7 @@ defmodule ValueFlows.Knowledge.ResourceSpecification do
|> Changeset.cast(attrs, @cast)
|> Changeset.change(
context_id: context.id,
default_unit_of_effort_id: Map.get(attrs, :default_unit_of_effort)
default_unit_of_effort_id: CommonsPub.Common.attr_get_id(attrs, :default_unit_of_effort)
)
|> common_changeset()
end
......@@ -101,7 +101,7 @@ defmodule ValueFlows.Knowledge.ResourceSpecification do
def update_changeset(%ResourceSpecification{} = resource_spec, attrs) do
resource_spec
|> Changeset.cast(attrs, @cast)
|> Changeset.change(default_unit_of_effort_id: Map.get(attrs, :default_unit_of_effort))
|> Changeset.change(default_unit_of_effort_id: CommonsPub.Common.attr_get_id(attrs, :default_unit_of_effort))
|> common_changeset()
end
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Knowledge.ResourceSpecification.ResourceSpecifications do
import CommonsPub.Common, only: [maybe_put: 3]
import CommonsPub.Common, only: [maybe_put: 3, attr_get_id: 2]
alias CommonsPub.{Activities, Common, Feeds, Repo}
alias CommonsPub.GraphQL.{Fields, Page}
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Observation.EconomicEvent.EconomicEvents do
import CommonsPub.Common, only: [maybe_put: 3]
import CommonsPub.Common, only: [maybe_put: 3, attr_get_id: 2]
alias CommonsPub.{Activities, Common, Feeds, Repo}
alias CommonsPub.GraphQL.{Fields, Page}
......@@ -367,19 +367,19 @@ defmodule ValueFlows.Observation.EconomicEvent.EconomicEvents do
defp prepare_attrs(attrs) do
attrs
|> maybe_put(:action_id, Map.get(attrs, :action))
|> maybe_put(:action_id, attr_get_id(attrs, :action))
|> maybe_put(:context_id,
attrs |> Map.get(:in_scope_of) |> CommonsPub.Common.maybe(&List.first/1)
)
|> maybe_put(:provider_id, Map.get(attrs, :provider))
|> maybe_put(:receiver_id, Map.get(attrs, :receiver))
|> maybe_put(:input_of_id, Map.get(attrs, :input_of))
|> maybe_put(:output_of_id, Map.get(attrs, :output_of))
|> maybe_put(:resource_conforms_to_id, Map.get(attrs, :resource_conforms_to))
|> maybe_put(:resource_inventoried_as_id, Map.get(attrs, :resource_inventoried_as))
|> maybe_put(:to_resource_inventoried_as_id, Map.get(attrs, :to_resource_inventoried_as))
|> maybe_put(:triggered_by_id, Map.get(attrs, :triggered_by))
|> maybe_put(:at_location_id, Map.get(attrs, :at_location))
|> maybe_put(:provider_id, attr_get_id(attrs, :provider))
|> maybe_put(:receiver_id, attr_get_id(attrs, :receiver))
|> maybe_put(:input_of_id, attr_get_id(attrs, :input_of))
|> maybe_put(:output_of_id, attr_get_id(attrs, :output_of))
|> maybe_put(:resource_conforms_to_id, attr_get_id(attrs, :resource_conforms_to))
|> maybe_put(:resource_inventoried_as_id, attr_get_id(attrs, :resource_inventoried_as))
|> maybe_put(:to_resource_inventoried_as_id, attr_get_id(attrs, :to_resource_inventoried_as))
|> maybe_put(:triggered_by_id, attr_get_id(attrs, :triggered_by))
|> maybe_put(:at_location_id, attr_get_id(attrs, :at_location))
|> parse_measurement_attrs()
end
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Observation.Process.Processes do
import CommonsPub.Common, only: [maybe_put: 3]
import CommonsPub.Common, only: [maybe_put: 3, attr_get_id: 2]
alias CommonsPub.{Activities, Common, Feeds, Repo}
alias CommonsPub.GraphQL.{Fields, Page}
......@@ -217,7 +217,7 @@ defmodule ValueFlows.Observation.Process.Processes do
defp prepare_attrs(attrs) do
attrs
|> maybe_put(:based_on_id, Map.get(attrs, :based_on))
|> maybe_put(:based_on_id, attr_get_id(attrs, :based_on))
|> maybe_put(:context_id,
attrs |> Map.get(:in_scope_of) |> CommonsPub.Common.maybe(&List.first/1)
)
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Observation.EconomicResource.EconomicResources do
import CommonsPub.Common, only: [maybe_put: 3]
import CommonsPub.Common, only: [maybe_put: 3, attr_get_id: 2]
alias CommonsPub.{Activities, Common, Feeds, Repo}
alias CommonsPub.GraphQL.{Fields, Page}
......@@ -212,15 +212,15 @@ defmodule ValueFlows.Observation.EconomicResource.EconomicResources do
defp prepare_attrs(attrs) do
attrs
|> maybe_put(:primary_accountable_id, Map.get(attrs, :primary_accountable))
|> maybe_put(:primary_accountable_id, attr_get_id(attrs, :primary_accountable))
|> maybe_put(:context_id,
attrs |> Map.get(:in_scope_of) |> CommonsPub.Common.maybe(&List.first/1)
)
|> maybe_put(:current_location_id, Map.get(attrs, :current_location))
|> maybe_put(:conforms_to_id, Map.get(attrs, :conforms_to))
|> maybe_put(:contained_in_id, Map.get(attrs, :contained_in))
|> maybe_put(:unit_of_effort_id, Map.get(attrs, :unit_of_effort))
|> maybe_put(:state_id, Map.get(attrs, :state))
|> maybe_put(:current_location_id, attr_get_id(attrs, :current_location))
|> maybe_put(:conforms_to_id, attr_get_id(attrs, :conforms_to))
|> maybe_put(:contained_in_id, attr_get_id(attrs, :contained_in))
|> maybe_put(:unit_of_effort_id, attr_get_id(attrs, :unit_of_effort))
|> maybe_put(:state_id, attr_get_id(attrs, :state))
|> parse_measurement_attrs()
end
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Planning.Intent.Intents do
import CommonsPub.Common, only: [maybe_put: 3]
import CommonsPub.Common, only: [maybe_put: 3, attr_get_id: 2]
alias CommonsPub.{Activities, Common, Feeds, Repo}
alias CommonsPub.GraphQL.{Fields, Page}
......@@ -195,17 +195,17 @@ defmodule ValueFlows.Planning.Intent.Intents do
defp prepare_attrs(attrs) do
attrs
|> maybe_put(:action_id, Map.get(attrs, :action))
|> maybe_put(:action_id, attr_get_id(attrs, :action))
|> maybe_put(:context_id,
attrs |> Map.get(:in_scope_of) |> CommonsPub.Common.maybe(&List.first/1)
)
|> maybe_put(:at_location_id, Map.get(attrs, :at_location))
|> maybe_put(:provider_id, Map.get(attrs, :provider))
|> maybe_put(:receiver_id, Map.get(attrs, :receiver))
|> maybe_put(:input_of_id, Map.get(attrs, :input_of))
|> maybe_put(:output_of_id, Map.get(attrs, :output_of))
|> maybe_put(:resource_conforms_to_id, Map.get(attrs, :resource_conforms_to))
|> maybe_put(:resource_inventoried_as_id, Map.get(attrs, :resource_inventoried_as))
|> maybe_put(:at_location_id, attr_get_id(attrs, :at_location))
|> maybe_put(:provider_id, attr_get_id(attrs, :provider))
|> maybe_put(:receiver_id, attr_get_id(attrs, :receiver))
|> maybe_put(:input_of_id, attr_get_id(attrs, :input_of))
|> maybe_put(:output_of_id, attr_get_id(attrs, :output_of))
|> maybe_put(:resource_conforms_to_id, attr_get_id(attrs, :resource_conforms_to))
|> maybe_put(:resource_inventoried_as_id, attr_get_id(attrs, :resource_inventoried_as))
|> parse_measurement_attrs()
end
......
# SPDX-License-Identifier: AGPL-3.0-only
defmodule ValueFlows.Proposal.Proposals do
import CommonsPub.Common, only: [maybe_put: 3]
import CommonsPub.Common, only: [maybe_put: 3, attr_get_id: 2]
alias CommonsPub.{Activities, Common, Feeds, Repo}
alias CommonsPub.GraphQL.{Fields, Page}
......@@ -244,6 +244,6 @@ defmodule ValueFlows.Proposal.Proposals do
|> maybe_put(:context_id,
attrs |> Map.get(:in_scope_of) |> CommonsPub.Common.maybe(&List.first/1)
)
|> maybe_put(:eligible_location_id, Map.get(attrs, :eligible_location))
|> maybe_put(:eligible_location_id, attr_get_id(attrs, :eligible_location))
end
end
......@@ -2,7 +2,7 @@
Grouping around something to create a boundary or context, used for documenting,
accounting, planning. Extended from the default of `Person | Organization`
"""
union AccountingScope = Person | Organization | Community | Collection
union AccountingScope = User | Organisation | Person | Organization | Community | Collection
"""
An action verb defining the kind of event, commitment, or intent.
......
......@@ -25,8 +25,13 @@ defmodule ValueFlows.Util.GraphQL do
def canonical_url_edge(obj, _, _),
do: {:ok, CommonsPub.ActivityPub.Utils.get_object_canonical_url(obj)}
def scope_edge(%{context_id: id}, page_opts, info),
do: CommonsPub.Web.GraphQL.CommonResolver.context_edges(%{context_ids: [id]}, page_opts, info)
def scope_edge(_, _, _),
do: {:ok, nil}
def fetch_provider_edge(%{provider_id: id}, _, info) when not is_nil(id) do
# CommonResolver.context_edge(%{context_id: id}, nil, info)
{:ok, ValueFlows.Agent.Agents.agent(id, GraphQL.current_user(info))}
end
......@@ -35,7 +40,6 @@ defmodule ValueFlows.Util.GraphQL do
end
def fetch_receiver_edge(%{receiver_id: id}, _, info) when not is_nil(id) do
# CommonResolver.context_edge(%{context_id: id}, nil, info)
{:ok, ValueFlows.Agent.Agents.agent(id, GraphQL.current_user(info))}
end
......
......@@ -14,7 +14,6 @@ defmodule CommonsPub.Common do
def maybe_get(%{} = map, key, fallback), do: Map.get(map, key, fallback)
def maybe_get(_, _, fallback), do: fallback
@doc "conditionally update a map"
def maybe_put(map, _key, nil), do: map
def maybe_put(map, _key, ""), do: map
......@@ -22,6 +21,7 @@ defmodule CommonsPub.Common do
@doc "Applies change_fn if the first parameter is not nil."
def maybe(nil, _change_fn), do: nil
def maybe(val, change_fn) do
change_fn.(val)
end
......@@ -30,6 +30,7 @@ defmodule CommonsPub.Common do
def maybe_ok_error({:ok, val}, change_fn) do
{:ok, change_fn.(val)}
end
def maybe_ok_error(other, _change_fn), do: other
@doc "Append an item to a list if it is not nil"
......@@ -88,6 +89,18 @@ defmodule CommonsPub.Common do
end
end
def attr_get_id(attrs, field_name) do
if is_map(attrs) and Map.has_key?(attrs, field_name) do
attr = Map.get(attrs, field_name)
if is_map(attr) and Map.has_key?(attr, :id) do
attr.id
else
attr
end
end
end
def maybe_str_to_atom(str) do
try do
String.to_existing_atom(str)
......
......@@ -31,7 +31,7 @@ defmodule CommonsPub.Web.GraphQL.Phase.ExecutionResolution do
defp debug_exception(msg, exception, stacktrace, kind) do
debug_log(msg, exception, stacktrace, kind)
if CommonsPub.Config.get(:env) == :dev or System.get_env("SENTRY_ENV") == "next" do
if CommonsPub.Config.get(:env) == :dev or CommonsPub.Config.get(:env) == :test or System.get_env("SENTRY_ENV") == "next" do
{:error,
msg <>
": " <>
......
......@@ -87,9 +87,7 @@ defmodule Valueflows.Agent.Person.GraphQLTest do
assert_intent(List.first(queried["intents"]))
assert_process(List.first(queried["processes"]))
assert_economic_event(List.first(queried["economicEvents"]))
# assert_economic_resource(List.first(queried["inventoriedEconomicResources"])) #TODO: not sure why nil
assert_economic_resource(List.first(queried["inventoriedEconomicResources"]))
assert_geolocation(queried["primaryLocation"])
end
end
......
......@@ -129,7 +129,7 @@ defmodule ValueFlows.Observation.EconomicEvent.EventsGraphQLTest do
q = economic_event_query(fields: [in_scope_of: [:__typename]])
conn = user_conn(user)
assert fetched = grumble_post_key(q, conn, :economic_event, %{id: event.id})
assert hd(fetched["inScopeOf"])["__typename"] == "User"
assert hd(fetched["inScopeOf"])["__typename"] == "Person"
end
end
......@@ -280,7 +280,7 @@ defmodule ValueFlows.Observation.EconomicEvent.EventsGraphQLTest do
assert event = grumble_post_key(q, conn, :create_economic_event, vars)["economicEvent"]
assert_economic_event(event)
assert hd(event["inScopeOf"])["__typename"] == "User"
assert hd(event["inScopeOf"])["__typename"] == "Person"
end
test "create an economic event with an input and an output" do
......
......@@ -16,7 +16,7 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
import ValueFlows.Test.Faking
alias ValueFlows.Planning.Intent.Intents
@debug false
@debug true
@schema CommonsPub.Web.GraphQL.Schema
describe "intent" do
......@@ -39,15 +39,19 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
parent = fake_user!()
intent = fake_intent!(user, %{
provider: user.id,
at_location: location,
resource_quantity: measure(%{unit_id: unit.id}),
effort_quantity: measure(%{unit_id: unit.id}),
available_quantity: measure(%{unit_id: unit.id}),
in_scope_of: [parent.id]
})
IO.inspect(intent: intent)
proposal = fake_proposal!(user)
some(5, fn -> fake_proposed_intent!(proposal, intent) end)
fake_proposed_intent!(proposal, intent)
assert intent_queried =
CommonsPub.Web.GraphQL.QueryHelper.run_query_id(
......@@ -60,6 +64,13 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
)
assert_intent(intent_queried)
assert_proposal(hd(intent_queried["publishedIn"])["publishedIn"])
assert_geolocation(intent_queried["atLocation"])
assert_agent(intent_queried["provider"])
assert_measure(intent_queried["resourceQuantity"])
assert_measure(intent_queried["availableQuantity"])
assert_measure(intent_queried["effortQuantity"])
assert hd(intent_queried["inScopeOf"])["__typename"] == "Person"
end
test "fails for deleted intent" do
......@@ -118,7 +129,7 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
q = intent_query(fields: [in_scope_of: [:__typename]])
conn = user_conn(user)
assert intent = grumble_post_key(q, conn, :intent, %{id: intent.id})
assert hd(intent["inScopeOf"])["__typename"] == "User"
assert hd(intent["inScopeOf"])["__typename"] == "Person"
end
end
......@@ -211,7 +222,7 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
assert intent = grumble_post_key(q, conn, :create_intent, vars)["intent"]
assert_intent(intent)
assert [context] = intent["inScopeOf"]
assert context["__typename"] == "User"
assert context["__typename"] == "Person"
end
test "creates a new intent with a location" do
......@@ -372,7 +383,7 @@ defmodule ValueFlows.Planning.Intent.GraphQLTest do
assert resp = grumble_post_key(q, conn, :update_intent, vars)["intent"]
assert [context] = resp["inScopeOf"]
assert context["__typename"] == "User"
assert context["__typename"] == "Person"
end
test "updates an existing intent with a location" do
......
......@@ -137,7 +137,7 @@ defmodule ValueFlows.Proposal.GraphQLTest do
q = proposal_query(fields: [in_scope_of: [:__typename]])
conn = user_conn(user)
assert proposal = grumble_post_key(q, conn, :proposal, %{id: proposal.id})
assert hd(proposal["inScopeOf"])["__typename"] == "User"
assert hd(proposal["inScopeOf"])["__typename"] == "Person"
end
end
......@@ -175,7 +175,7 @@ defmodule ValueFlows.Proposal.GraphQLTest do
vars = %{proposal: proposal_input(%{"inScopeOf" => [parent.id]})}
assert proposal = grumble_post_key(q, conn, :create_proposal, vars)["proposal"]
assert_proposal_full(proposal)
assert hd(proposal["inScopeOf"])["__typename"] == "User"
assert hd(proposal["inScopeOf"])["__typename"] == "Person"
end
test "creates a new proposal with an eligible location" do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment