igalic
Hi folks,
I’m trying to transform our tests for AuthToken (a wrapper around JOSE) to ExUnitProperties. But I’m kinda failing at the most basic task of transforming a simple function into a generator. What I’ve done is this:
defp stream_authtoken_keys(_seed, _range) do
Stream.repeatedly(&AuthToken.generate_key/0)
end
defp gen_authtoken_key() do
%StreamData{generator: &stream_authtoken_keys/2}
end
However, when trying to use it…
describe "keys" do
property "generate_key/0 returns a valid AES128 key" do
check all authtoken_key <- gen_authtoken_key() do
{:ok, key} = authtoken_key
assert byte_size(key) == 16
end
end
end
I get the following error:
1) property keys generate_key/0 returns a valid AES128 key (AuthTokenTest)
test/authtoken_test.exs:23
** (MatchError) no match of right hand side value: #Function<51.91433161/2 in Stream.repeatedly/1>
code: check all authtoken_key <- gen_authtoken_key() do
stacktrace:
(stream_data) lib/stream_data.ex:203: StreamData.bind_filter/5
(stream_data) lib/stream_data.ex:346: anonymous fn/5 in StreamData.bind_filter/3
(stream_data) lib/stream_data.ex:203: StreamData.check_all/6
test/authtoken_test.exs:24: (test)
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
DanCouper
Just as simple as
authtoken_keyrather thankey?igalic
After posting this in elixir (on irc & discord) I got a very decent reply that works out pretty fine, so I’m gonna close this topic for now with it!
First off, trying to generate
%StreamData{}myself, is kinda bad form, since it’s basically a private struct…We looked for alternatives, and found
StreamData.constant/0. However, on its own, it always generates the same value.Which makes a certain amount of sense when you think about how it’s called:
The clue came from @michalmuskala:
and the code we now have from @ericmj
what we’re doing here is using
StreamData.constant/1as a sort of clock generator, that drives the call ofAuthToken.generate_key/0… and it’s nice start, but we’re needlessly generating alist_ofwhich we thenmapSo let’s update this with all responses below!
This is much cleaner! Since we use
bind/2, which is essentially made for this… better see what’s going on, we can use pipes to extract the flow:We feed a stream of
:unusedintogen_tickerlambda, just so it will be constantly called. Since our call toAuthToken.generate_key/0is wrapped in said lambda, it will be executed on every call, rather than onge_ticker’s declaration. Finally, we pass the so generated%StreamData{}intounshrinkable/0(because it is) before returning it.igalic
i think that was just an awkward copy/paste error… and i should fix this in the original post
igalic
was missing in my paste (not the error causing code)
ericmj
I found a (maybe) cleaner way of doing it that doesn’t involve generating a list:
I haven’t tested this so I can’t verify that it works.
EDIT: @whatyouhide has a more concise solution below: How to create a custom StreamData Generator - #12 by whatyouhide
LostKobrakai
It might also be good to make the generator unshrinkable, because shrinking doesn’t seem to make sense in that context.
igalic
I really couldn’t figure this out from the documentation…
maybe we should add something to the docs that helps people figure out how to create custom generators
@michalmuskala suggested to have
StreamData.repeatedly/1. I was thinking maybe a better way would be to have a way of converting aStreamto aStreamData.LostKobrakai
I found it quite easy to build more complex generators out of the basic generators already in the streamdata module, but I’ve to say that your usecase really doesn’t seem very well supported by the current functions.
anthonator
@igalic’s approach only seems to work if you use the generator in a
check. If you try to use it in agenyou run into a the issue of the value being considered a constant so it’s the same on each run through. Any way around this?anthonator
I was able to get this working. Turned out
checkwas swallowing another error.