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

Tee
can someone please explain to me how Enum.reduce works with maps
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
pmjoe
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
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

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
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 41539 114
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
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
hariharasudhan94
Lets say i have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; "XXX...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement