ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - campezzi/ignorant: Simplify comparison of Elixir data structures by ensuring fields are present but ignoring their values.
Simplify comparison of Elixir data structures by ensuring fields are present but ignoring their values. - campezzi/ignorant
Visit Site

GitHub - campezzi/ignorant: Simplify comparison of Elixir data structures by ensuring fields are present but ignoring their values.

GitHub - campezzi/ignorant: Simplify comparison of Elixir data structures by ensuring fields are present but ignoring their values.

Ignorant

Because sometimes ignorance is bliss.

Ignorant defines a protocol for data structures that may selectively ignore specific values they contain, usually to simplify partial comparison in tests.

Motivation

Sometimes it's useful to compare data structures in tests while ignoring pieces of data that are not deterministic - auto-generated primary keys, calculated timestamps, and so on. Ignoring certain bits ensures the shape of the data is correct when it's not possible to enforce its values.

Example

An implementation for Map is provided with that package for the purpose described above. Suppose you have an API endpoint that fetches a record from the database and returns JSON data which translates to this response map:

%{
  id: 37,
  name: "Jim"
}

Now let's say you want to write a test to ensure that it returns the right data. Problem is, the id field is auto-generated and therefore has no deterministic value between test runs. You could use pattern matching...

assert %{id: _, name: "Jim"} = response

...but you can't assign the value on the left side to a variable (or module attribute) and reuse it, and if the match fails you don't get a nice diff message. That's where Ignorant comes in:

assert %{id: :ignored, name: "Jim"} == Ignorant.ignore(response, [:id])

The left side is now a proper value that can be put in a variable or attribute and reused. Also note the == (equality) assertion instead of = (match) assertion. That gives us a pretty diff that highlights exactly what doesn't look the same on both sides, and is also stricter (which is a good idea in tests).

It quickly becomes annoying and error-prone to tag things as :ignored in one side of the comparison and include the corresponding field on the second parameter in the call to ignore/2 on the other side, so we can clean that up by using merge_ignored/2:

expected = %{id: :ignored, name: "Jim"}
assert expected == Ignorant.merge_ignored(response, expected)

merge_ignored/2 will walk through the expected map extracting all fields tagged as :ignored, and then apply those to response while keeping all other values intact. The resulting map should be equal to expected. If not, you'll get a pretty diff explaining what's different.

Installation

  1. Add ignorant to your list of dependencies in mix.exs:
def deps do
  [{:ignorant, "~> 0.1.0"}]
end
  1. Ensure ignorant is started before your application:
def application do
  [applications: [:ignorant]]
end

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory