-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AppEngine: Implement property-based tests
Implement property-based tests for `cast_value` functions. Signed-off-by: Ismet Softic <ismet.softic@secomind.com>
- Loading branch information
Showing
3 changed files
with
330 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
apps/astarte_appengine_api/test/support/generators/interface-value.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# | ||
# This file is part of Astarte. | ||
# | ||
# Copyright 2024 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
defmodule Astarte.AppEngine.API.InterfaceValueTestGenerator do | ||
use ExUnitProperties | ||
|
||
def gen_datetime() do | ||
StreamData.fixed_map(%{ | ||
year: StreamData.integer(2020..2030), | ||
month: StreamData.integer(1..12), | ||
day: StreamData.integer(1..28), | ||
hour: StreamData.integer(0..23), | ||
minute: StreamData.integer(0..59), | ||
second: StreamData.integer(0..59), | ||
millisecond: StreamData.integer(0..999) | ||
}) | ||
|> StreamData.map(fn %{ | ||
year: y, | ||
month: mo, | ||
day: d, | ||
hour: h, | ||
minute: mi, | ||
second: s, | ||
millisecond: ms | ||
} -> | ||
{:ok, date} = Date.new(y, mo, d) | ||
{:ok, time} = Time.new(h, mi, s, {ms, 3}) | ||
{:ok, datetime} = DateTime.new(date, time, "Etc/UTC") | ||
DateTime.to_iso8601(datetime) | ||
end) | ||
end | ||
|
||
def gen_datetime_array() do | ||
StreamData.list_of(gen_datetime(), min_length: 1, max_length: 10) | ||
end | ||
|
||
def gen_invalid_datetime() do | ||
StreamData.string(:printable, min_length: 1, max_length: 30) | ||
|> StreamData.filter(&(!String.match?(&1, ~r/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/))) | ||
end | ||
|
||
def gen_invalid_datetime_array() do | ||
StreamData.list_of(gen_invalid_datetime(), min_length: 1, max_length: 10) | ||
end | ||
|
||
def gen_binaryblob() do | ||
StreamData.binary(min_length: 1, max_length: 10) | ||
|> StreamData.map(&Base.encode64/1) | ||
end | ||
|
||
def gen_binaryblob_array() do | ||
StreamData.list_of(gen_binaryblob(), min_length: 1, max_length: 10) | ||
end | ||
|
||
def gen_invalid_binaryblob() do | ||
StreamData.string(:printable, min_length: 1, max_length: 10) | ||
|> StreamData.filter(&(!String.match?(&1, ~r/^[A-Za-z0-9\/\+\=]{0,2}$/))) | ||
end | ||
|
||
def gen_invalid_binaryblob_array() do | ||
StreamData.list_of(gen_invalid_binaryblob(), min_length: 1, max_length: 10) | ||
end | ||
|
||
def gen_double_array() do | ||
StreamData.list_of(StreamData.float(), min_length: 1, max_length: 10) | ||
end | ||
|
||
def gen_invalid_double_array do | ||
StreamData.list_of(StreamData.string(:printable), min_length: 1) | ||
end | ||
|
||
def gen_longinteger_array() do | ||
StreamData.one_of([ | ||
StreamData.list_of(StreamData.integer(), min_length: 1, max_length: 10), | ||
StreamData.list_of(StreamData.map(StreamData.integer(), &Integer.to_string/1), | ||
min_length: 1, | ||
max_length: 10 | ||
) | ||
]) | ||
end | ||
|
||
def gen_invalid_longinteger() do | ||
StreamData.string(:printable, min_length: 1, max_length: 10) | ||
end | ||
|
||
def gen_invalid_longinteger_array() do | ||
StreamData.list_of(gen_invalid_longinteger(), min_length: 1, max_length: 10) | ||
end | ||
|
||
def gen_string_array() do | ||
StreamData.list_of(StreamData.string(:printable), min_length: 1, max_length: 10) | ||
end | ||
|
||
def gen_integer_array() do | ||
StreamData.list_of(StreamData.integer(), min_length: 1, max_length: 10) | ||
end | ||
|
||
def gen_boolean_array() do | ||
StreamData.list_of(StreamData.boolean(), min_length: 1, max_length: 10) | ||
end | ||
|
||
def gen_object(expected_types) do | ||
StreamData.fixed_map( | ||
Enum.into(expected_types, %{}, fn {key, type} -> | ||
{key, gen_value(type)} | ||
end) | ||
) | ||
end | ||
|
||
defp gen_value(:integer), do: StreamData.integer() | ||
defp gen_value(:string), do: StreamData.string(:printable) | ||
defp gen_value(:longinteger), do: StreamData.integer() | ||
end |