Morphix 0.6.0 released, now with atoms to strings! https://hex.pm/packages/morphix/0.6.0

Morphix, a small library for working with Tuples and Lists, has a new release this week, adding stringmorphification (converting atom keys to map keys) to the list of available functions. Many thanks for the pull request from github/bryanhuntesl.

https://hex.pm/packages/morphix/0.6.0

MecksUnit: Elegantly mock module functions in (async) ExUnit tests

It is a well-know topic within the Elixir community: “To mock or not to mock? :)”

Every alchemist probably has his / her own opinion concerning this topic. José Valim and Plataformatec has published the Hex package Mox which complies with his article on mocking in Elixir.

Personally, I’m not convinced in having to change the code “in service of” testing certain modules. Why would one add abstraction to code of which its purpose isn’t supposed to be interchangeable (with mock modules for instance)?

After some Googling, I found Espec of which I thought that that’s a little bit too much. Finally, I found Mock which could have done the job. But there are two downsides:

  1. You cannot use async: true
  2. Defining the mock functions could have been done in a more readable way

Based on that, I decided to write MecksUnit which solves just that. An example:

# (in test/test_helper.exs)

ExUnit.start()
MecksUnit.mock()

# (in test/mecks_unit_test.exs)

defmodule Foo do
  def trim(string) do
    String.trim(string)
  end
end

defmodule MecksUnitTest do
  use ExUnit.Case, async: true
  use MecksUnit.Case

  defmock String do
    def trim("  Paul  "), do: "Engel"
    def trim("  Foo  ", "!"), do: "Bar"
    def trim(_, "!"), do: {:passthrough, ["  Surprise!  !!!!", "!"]}
    def trim(_, _), do: :passthrough
  end

  defmock List do
    def wrap(:foo), do: [1, 2, 3, 4]
  end

  mocked_test "using mocked module functions" do
    task =
      Task.async(fn ->
        assert "Engel" == String.trim("  Paul  ")
        assert "Engel" == Foo.trim("  Paul  ")
        assert "Bar" == String.trim("  Foo  ", "!")
        assert "  Surprise!  " == String.trim("  Paul  ", "!")
        assert "MecksUnit" == String.trim("  MecksUnit  ")
        assert "Paul Engel" == String.trim("  Paul Engel  ", " ")
        assert [1, 2, 3, 4] == List.wrap(:foo)
        assert [] == List.wrap(nil)
        assert [:bar] == List.wrap(:bar)
        assert [:foo, :bar] == List.wrap([:foo, :bar])
      end)

    Task.await(task)
  end

  test "using the original module functions" do
    task =
      Task.async(fn ->
        assert "Paul" == String.trim("  Paul  ")
        assert "Paul" == Foo.trim("  Paul  ")
        assert "  Foo  " == String.trim("  Foo  ", "!")
        assert "  Paul  " == String.trim("  Paul  ", "!")
        assert "MecksUnit" == String.trim("  MecksUnit  ")
        assert "Paul Engel" == String.trim("  Paul Engel  ", " ")
        assert [:foo] == List.wrap(:foo)
        assert [] == List.wrap(nil)
        assert [:bar] == List.wrap(:bar)
        assert [:foo, :bar] == List.wrap([:foo, :bar])
      end)

    Task.await(task)
  end
end

Mocking module functions is pretty straightforward and done as follows:

  1. Add use MecksUnit.Case at the beginning of your test file
  2. Use defmock as if you would define the original module with defmodule containing mocked functions
  3. Use mocked_test as if you would define a normal ExUnit test after having defined all the required mock modules
  4. Add MecksUnit.mock() in your test/test_helper.exs file

The defined mock modules only apply to the first mocked_test encountered. So they are isolated (despite of :meck having an unfortunate global effect) as MecksUnit takes care of it. Also, non-matching function heads within the mock module will result in invoking the original module function as well. And last but not least: you can just run the tests asynchronously.

Enjoy using MecksUnit (if you prefer unobtrusive mocking). A Github star is very welcome, haha ;)

Capturing Functions and Expressions in Elixir

In this post, we’re going to look at capturing functions and expressions, which can make Elixir code more concise.

https://davidtang.io/2019/01/10/capturing-functions-and-expressions-in-elixir.html

Ecto and Binary IDs Generated By PostgreSQL

As well as having Ecto use UUIDs as primary keys it is also possible for Ecto to configure PostgreSQL so that it generate the UUIDs:

http://www.swwritings.com/post/2019-01-11-ecto-and-binary-ids-generated-by-postgresql

Send smart emails with ExCampaignMonitor

New version of ExCampaignMonitor (0.9.0) to start working through the transactional API: https://hexdocs.pm/ex_campaign_monitor/0.9.0/ExCampaignMonitor.html#send_smart_email/2

Using configuration to swap modules for mocks in your Elixir tests.

When you have a service module that does something with a third party using a mock module can prove to be a great tool for testing when you don’t want to depend on that external service. In my post I show you how to do that without dependency injection but rather with elixir’s application configuration.

https://polymorphic.productions/posts/using-configuration-to-swap-modules-for-mocks-in-your-elixir-tests

First month of functional programming in Elixir

Thoughts from one month of functional programming in Elixir

Elixir has an impressive feature set and a fast-growing community. Coming from an object-oriented background using mostly Java and Javascript, Elixir syntax and structure is both familiar and not.

Processing Large CSV files with Elixir Streams

Processing Large CSV files with Elixir Streams

Elixir Streams are a powerful and elegant way to process large CSV files. In this article I compare the greedy and the lazy approach with memory and cpu benchmarking.

No Touching released

no_touching provides a way to check if your code is calling private functions from other libraries/dependencies! Highly configurable, extremely fast, and a really nice check to add to CI.

https://hex.pm/packages/no_touching

How to perform Hot Code Swapping using Distillery— #2 — A (Live Demo) GenServer State update.

https://medium.com/@blackode/how-to-perform-hot-code-swapping-using-distillery-2-a-live-demo-genserver-state-update-53d8344d9147

This article will guide you to perform hot code swapping in Elixir using the Elixir package distillery-2.0 (at the moment of writing this article).

A live demo on updating GenServer state will stand out as a good example.

WaspVM 0.8.0 released

Complete instruction set support, 67% increase in efficiency over previous version, and more. View the release notes here

Phoenix Contexts

In the latest ElixirCast episode we’ll look at Phoenix contexts. This is a great episode for anyone new to Phoenix or wanting to see how contexts work.

https://elixircasts.io/phoenix-contexts

Phoenix 1.4 Chat Server: Part 4

Phoenix 1.4 Chat Server, Part 4.

This episode covers building an Auth Plug. It will let us restrict any action in any controller to only logged-in users or to only admins. We also set up a special function plug for the user controller for restricting actions to the correct user. E.g. users can edit their own accounts but not each others.

Message passing for actors and humans

Slides and Video for my CodeMesh talk this year.

http://crowdhailer.me/2018-11-08/message-passing-for-actors-and-humans/

Inject from your controller to decouple your contexts in Phoenix.

In this post I will show you a simple pattern to decouple your Phoenix context so that you can specify from your controller how you want to preload your Ecto.Query.

https://polymorphic.productions/posts/inject-from-your-controller-to-decouple-your-contexts-in-phoenix

Hex Bob Package Status Page

I’ve created a small page listing all available precompiled packages of hex bob. Added availability checks given the recent issues with updates.

https://bobs-list.kobrakai.de/

Ecto and Binary IDs

Using UUIDs as primary keys is simple with migration_primary_key and a simple change to the schema:

https://www.annkissam.com/elixir/alembic/posts/2019/01/04/ecto-and-binary-ids.html

How We Used Elixir's GenServers + Dynamic Supervisors to Build Concurrent, Fault Tolerant Workflows

At The Flatiron School, we leveraged Elixir’s GenServers and Dynamic Supervisors to “deploy” large amounts of content to our students faster and less error-prone than ever before. https://www.thegreatcodeadventure.com/how-we-used-elixirs-genservers-dynamic-supervisors-to-build-concurrent-fault-tolerant-workflows/

hackney version 1.15.0 has been released

just released hackney version 1.15.0 an HTTP client library. This release add a performance improvement to the pool and an addition to multipart forms handling.

site: https://github.com/benoitc/hackney news: https://github.com/benoitc/hackney/blob/master/NEWS.md#1150—2019-01-04 …

Gitlab-ing Phoenix — (Elixir and others) Aka GitOps

https://medium.com/blackode/gitlab-ing-phoenix-elixir-and-others-aka-gitops-50e291ce5d85

This article is all about Automating Phoenix Development & Deployment using Gitlab CI/CD.

Previous page Next page