D4no0
I have currently some functionality that fetches/stores some data in redis using redix. From what I see when it comes to testing, there is nothing specified in the documentation, so I guess the most straightforward way would be to mock it.
Instead of mocking, I was wondering if it would be possible to take the approach the Ecto took, using the real database but being able to test in isolation. It does seem that redis supports concept of transaction, even though not sure how much of it overlaps with how it works in DB engines like Postgres.
What are your thoughts on this?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mudasobwa
If I had a logic that requires a real
redisinstance to test (e. g. pubsub subscription, polling from the other place, or like,) I’d definitely supply a PR torediximplementing the testing framework.I never had such a condition (e. g. I used
redisas a pure kv-store,) and since I trust in Andrea’s coding skills, I mock it completely.D4no0
That is true, this is my use-case too. I am not very familiar with redis, this is why I would like the calls to go to a real instance, just in case some of my assumptions when it comes to mocking are wrong, I would rather spend my time on a better testing setup than debugging that on deployed thing.
mudasobwa
What an original approach!
Well, then you might want to open the issue to:
Redix.BehaviourRediximplement itRedix.Testimplementing this same behaviour and mimickingRedixby optionally wrapping each and every call totransaction_pipeline/3and telemetrying itSince
redisdoesn’t have a rollback mechanism at all, you’ll need toWATCHthe data and/or implement the rollback manually. Here is a good blog about why You Don’t Need Transaction Rollbacks in Redis.You anyway would need to implement all the above, hence my proposal to do a PR.
D4no0
I won’t have time to do this now, as I will need some time to understand how test setups work and how to do the rollbacks correctly (so it would work with async tests too), but I would be most certainly interested in adding the feature as soon as I get a little bit more time as there is a lot of redis usage at the current projects I work on.
I wonder if it makes sense to make the implementation more abstract, as I will be most probably be interested in doing this with MongoDB tests too and I bet there is no way to do that ATM.
mudasobwa
Should not we summon @whatyouhide here?
I am thinking about something that would make it possible to write tests like
rkallos
The approach I took to testing an application that used Redis was to start multiple Redis instances.
I used an Agent to pass out unused ports to tests, and would start a Redis server listening on that unused port using System.cmd/3, then pass the port in the ExUnit context, which would find its way into the app configuration. That way, each test that required Redis could have its own unique instance, which meant tests could be run concurrently.
While this isn’t very efficient, it works well enough during local development and in CI that it isn’t enough of an issue to replace at the moment.
Here’s the module I wrote for starting Redis instances:
and port_wrapper.sh:
sorentwo
You don’t necessarily need multiple Redis instances. Every instance has 16 “databases” by default, addressable from 0 through 15. The data in each of those databases is completely isolated, and you’re actually able to selectively flush a single db.
You can use separate database instances for each test case (anything that is testing for the presence of specific values) and then call
flushdbafterwards to clear only that database.whatyouhide
I don’t think Redix itself should change to accommodate testing here. Redix is concerned with talking to the Redis database, and it gives you enough tools that you can build testing utilities on top of it. The databases examples that @sorentwo mentioned is already a really good way to go with at least 16 parallel tests using Redis. Multiple instances also work—Redis is really cheap to run.
Redis itself doesn’t support complex transactions the way relational DBs (Postgres and co) that Ecto supports do, so implementing a sandbox here would not really be possible.
In case mocks are your choice, I don’t think Redix should declare and/or implement behaviours for that either. Redix’s API is pretty slim and chances are you’re not using all of it, so declaring the interface you need to talk to Redis for your use case and then declaring mocks for that seems like a good approach to me.