Skip to content

Commit a40c4a1

Browse files
value + tests
1 parent f23fb79 commit a40c4a1

File tree

2 files changed

+45
-93
lines changed

2 files changed

+45
-93
lines changed

lib/astarte/core/generators/mapping/value.ex

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@ defmodule Astarte.Core.Generators.Mapping.Value do
2424

2525
alias Astarte.Core.Interface
2626
alias Astarte.Core.Mapping
27-
alias Astarte.Core.Mapping.ValueType
2827

2928
alias Astarte.Core.Generators.Mapping.ValueType, as: ValueTypeGenerator
3029

31-
alias Astarte.Common.Generators.DateTime, as: DateTimeGenerator
32-
alias Astarte.Common.Generators.Timestamp, as: TimestampGenerator
33-
3430
@doc """
3531
Generates a valid value based on interface
3632
"""
@@ -76,11 +72,6 @@ defmodule Astarte.Core.Generators.Mapping.Value do
7672
|> map(&Enum.join(&1, "/"))
7773
end
7874

79-
defp endpoint_base(:individual, endpoint), do: endpoint
80-
81-
defp endpoint_base(:object, endpoint),
82-
do: endpoint_manipulate(endpoint, fn list -> Enum.drop(list, -1) end)
83-
8475
defp endpoint_postfix(endpoint), do: Regex.replace(~r/.*\//, endpoint, "")
8576

8677
defp build_value(value_type), do: ValueTypeGenerator.value_from_type(value_type)
@@ -93,10 +84,32 @@ defmodule Astarte.Core.Generators.Mapping.Value do
9384
end
9485
end
9586

96-
defp endpoint_manipulate(endpoint, manipulate_fn) do
97-
endpoint
98-
|> String.split("/")
99-
|> manipulate_fn.()
100-
|> Enum.join("/")
101-
end
87+
@doc """
88+
Returns true if `path` matches `endpoint` according to the given `aggregation`.
89+
TODO: move to astarte_core
90+
"""
91+
@spec path_matches_endpoint?(:individual | :object, String.t(), String.t()) :: boolean()
92+
def path_matches_endpoint?(:individual, endpoint, path),
93+
do:
94+
path_matches_endpoint?(
95+
endpoint |> Mapping.normalize_endpoint() |> String.split("/"),
96+
path |> String.split("/")
97+
)
98+
99+
def path_matches_endpoint?(:object, endpoint, path),
100+
do:
101+
path_matches_endpoint?(
102+
endpoint |> Mapping.normalize_endpoint() |> String.split("/") |> Enum.drop(-1),
103+
path |> String.split("/")
104+
)
105+
106+
defp path_matches_endpoint?([], []), do: true
107+
108+
defp path_matches_endpoint?(["" | endpoints], [_ | paths]),
109+
do: path_matches_endpoint?(endpoints, paths)
110+
111+
defp path_matches_endpoint?([same | endpoints], [same | paths]),
112+
do: path_matches_endpoint?(endpoints, paths)
113+
114+
defp path_matches_endpoint?(_, _), do: false
102115
end

test/astarte/core/generators/mapping/value_test.exs

Lines changed: 17 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ defmodule Astarte.Core.Generators.Mapping.ValueTest do
2222

2323
alias Astarte.Core.Interface
2424
alias Astarte.Core.Mapping
25-
alias Astarte.Core.Mapping.EndpointsAutomaton
2625

2726
alias Astarte.Core.Generators.Interface, as: InterfaceGenerator
28-
alias Astarte.Core.Generators.Mapping, as: MappingGenerator
2927
alias Astarte.Core.Generators.Mapping.Value, as: ValueGenerator
3028

3129
@moduletag :value
@@ -121,98 +119,39 @@ defmodule Astarte.Core.Generators.Mapping.ValueTest do
121119
@describetag :success
122120
@describetag :ut
123121

124-
property "generates value based on interface" do
122+
property "generates value based on interface (gen)" do
125123
gen = InterfaceGenerator.interface() |> ValueGenerator.value()
126124

127125
check all value <- gen do
128126
assert %{path: _path, value: _value} = value
129127
end
130128
end
131129

132-
@tag :temp
133-
property "generates values based on aggregation :object must have mapping endpoint = base_endpoint + postfix" do
134-
gen all %Interface{mappings: mappings} = interface <-
135-
InterfaceGenerator.interface(aggregation: :object),
136-
%{path: path, value: value} <- ValueGenerator.value(interface) do
137-
IO.inspect("Ciao")
130+
property "generates value based on interface (struct)" do
131+
check all interface <- InterfaceGenerator.interface(),
132+
value <- ValueGenerator.value(interface) do
133+
assert %{path: _path, value: _value} = value
134+
end
135+
end
138136

139-
for %Mapping{endpoint: endpoint} <- mappings do
140-
IO.inspect(endpoint)
141-
end
137+
property "generates value must have mapping path matches endpoint" do
138+
check all %Interface{mappings: mappings, aggregation: aggregation} = interface <-
139+
InterfaceGenerator.interface(),
140+
%{path: path, value: _value} <- ValueGenerator.value(interface) do
141+
assert Enum.any?(mappings, fn %Mapping{endpoint: endpoint} ->
142+
ValueGenerator.path_matches_endpoint?(aggregation, endpoint, path)
143+
end)
142144
end
143145
end
144146

145147
property "generates values based on aggregation :object must have postfix different from other fields" do
146148
gen = InterfaceGenerator.interface(aggregation: :object) |> ValueGenerator.value()
147149

148150
check all %{path: path, value: value} <- gen do
149-
endpoint_postfix = path |> String.split("/") |> List.last()
150-
151-
for {postfix, _} <- value do
152-
refute postfix == endpoint_postfix
153-
end
151+
last_seg = path |> String.split("/") |> List.last()
152+
keys = Map.keys(value)
153+
refute last_seg in keys
154154
end
155155
end
156156
end
157-
158-
# @doc false
159-
# describe "value generator" do
160-
# @describetag :success
161-
# @describetag :ut
162-
163-
# property "generates value based on interface" do
164-
# check all value <- InterfaceGenerator.interface() |> ValueGenerator.value() do
165-
# assert %{path: _path, value: _value} = value
166-
# end
167-
# end
168-
169-
# property "generates valid value based on aggregation" do
170-
# check all aggregation <- one_of([:individual, :object]),
171-
# value <-
172-
# InterfaceGenerator.interface(aggregation: aggregation)
173-
# |> ValueGenerator.value() do
174-
# assert valid?(aggregation, value)
175-
# end
176-
# end
177-
178-
# property "generates values for the correct endpoint for :individual interfaces" do
179-
# check all interface <- InterfaceGenerator.interface(aggregation: :individual),
180-
# %{path: path, value: value} <- ValueGenerator.value(interface) do
181-
# assert valid_value_for_path?(interface, path, value)
182-
# end
183-
# end
184-
185-
# # property "check if path matches at least one endpoint considering aggregation" do
186-
# # check all aggregation <- one_of([:individual, :object]),
187-
# # interface_type <- InterfaceGenerator.type(),
188-
# # mappings <-
189-
# # MappingGenerator.mapping(interface_type: interface_type)
190-
# # |> list_of(min_length: 1, max_length: 10),
191-
# # %{path: path} <-
192-
# # InterfaceGenerator.interface(
193-
# # type: interface_type,
194-
# # aggregation: aggregation,
195-
# # mappings: mappings
196-
# # )
197-
# # |> ValueGenerator.value() do
198-
# # assert Enum.any?(mappings, fn %Mapping{endpoint: endpoint} ->
199-
# # path_matches_endpoint?(aggregation, path, endpoint)
200-
# # end)
201-
# # end
202-
# # end
203-
204-
# property "check field is present in object field (aggregation :object)" do
205-
# check all %{mappings: mappings} = interface <-
206-
# InterfaceGenerator.interface(aggregation: :object),
207-
# %{value: value} <- ValueGenerator.value(interface),
208-
# endpoints =
209-
# mappings
210-
# |> Enum.map(fn %Mapping{endpoint: endpoint} ->
211-
# Regex.replace(~r"^.*/", endpoint, "")
212-
# end),
213-
# fields = value |> Enum.map(fn {field, _} -> field end) do
214-
# assert Enum.all?(fields, &(&1 in endpoints))
215-
# end
216-
# end
217-
# end
218157
end

0 commit comments

Comments
 (0)