dimitarvp
StreamData: generating valid tri-tuple date
Hello.
I would like to generate a valid tri-tuple date with StreamData. After some digging and experimentation I arrived at this working code:
defmodule SomeFactory do
def valid_date?({_y, _m, _d} = date_tuple) do
case Date.from_erl(date_tuple) do
{:ok, _date} -> true
_ -> false
end
end
def date_maybe_invalid_tuple() do
gen all y <- integer(0..99),
m <- integer(1..12),
d <- integer(1..31) do
{y, m, d}
end
end
def date_valid_tuple() do
StreamData.filter(date_maybe_invalid_tuple(), &valid_date?/1)
end
end
Running this gave me a reasonable certainty that the filter indeed stops invalid dates like 2018-02-30 (it returns the empty list as I expected):
Enum.take(SomeFactory.date_valid_tuple(), 1_000_000) |> Enum.reject(&SomeFactory.valid_date?/1)
So, am I doing this right? And is there a way to do the above with only one function?
Marked As Solved
mudasobwa
Creator of Cure
Looks perfectly legit.
The only thing is you are kinda abusing case; use Kernel.match?/2 instead:
def valid_date?({_y, _m, _d} = date_tuple) do
match?({:ok, _date}, Date.from_erl(date_tuple))
end
Also, you might put this guard directly into the generator as shown here:
gen all y <- integer(0..99),
m <- integer(1..12),
d <- integer(1..31),
match?({:ok, _date}, Date.from_erl({y, m, d})) do
2
Popular in Questions
can someone please explain to me how Enum.reduce works with maps
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New







