Kaweeda

Kaweeda

Streamdata: Unique generated data

Hi all, generating a property test and want to make sure that map_a and map_b are not equal, if they are then just pass :ok, and if not then assert.

Here is the code :

property "when a & b are arbitrary small json objects; patch(a, diff(a,b)) == b" do
    primitive = one_of([string(:printable), integer()])

    json_fragment_gen =
      tree(primitive, fn child_gen ->
        StreamData.one_of([
          # A JSON array
          list_of(child_gen),
          # A JSON object. Only supports string keys.
          map_of(string(:printable), child_gen)
        ])
      end)

    # Only valid top-level JSON are objects and arrays
    json_gen = json_fragment_gen |> filter(fn val -> is_map(val) end, 100)

    check all(
            map_a <- json_gen,
            map_b <- json_gen,

            max_runs: 100,
            initial_size: 1,
            max_generation_size: 50
          ) do

        
        assert map_b == js_patch(map_a, JsonDiffEx.diff(map_a, map_b))

So instead of checking before the assertion, is there a way to generate two unique maps that can never be equal to each other or if they are equal to each to just ignore it or pass :ok.

property "when a & b are arbitrary small json objects; patch(a, diff(a,b)) == b" do
    primitive = one_of([string(:printable), integer()])

    json_fragment_gen =
      tree(primitive, fn child_gen ->
        StreamData.one_of([
          # A JSON array
          list_of(child_gen),
          # A JSON object. Only supports string keys.
          map_of(string(:printable), child_gen)
        ])
      end)

    # Only valid top-level JSON are objects and arrays
    json_gen = json_fragment_gen |> filter(fn val -> is_map(val) end, 100)

    check all(
            map_a <- json_gen,
            map_b <- json_gen,

            max_runs: 100,
            initial_size: 1,
            max_generation_size: 50
          ) do

          if map_a == map_b do
            :ok
          else
            assert map_b == js_patch(map_a, JsonDiffEx.diff(map_a, map_b))

Any help would be appreciated, thanks in advance.

First Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@Kaweeda check all takes a filter, you can

    check all(
            map_a <- json_gen,
            map_b <- json_gen,
            map_a != map_b,

Where Next?

Popular in Questions Top

jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement