|
| 1 | +defmodule Together.Test.Fixtures do |
| 2 | + @moduledoc """ |
| 3 | + Helpers for reading and using JSON test fixtures |
| 4 | +
|
| 5 | + This module provides functions to read JSON fixtures from the `fixture` directory, decode them, |
| 6 | + and randomize any IDs present to avoid database deadlocks in async tests. Randomized IDs are |
| 7 | + consistent within the same test process, even across multiple fixture files. |
| 8 | +
|
| 9 | + Randomization is done with the following rules: |
| 10 | +
|
| 11 | + * The key must be named `id`, end with `_id`, or be specified in the `:keys` option. |
| 12 | + * Values that are `null` are left unchanged. |
| 13 | + * Numeric IDs are replaced with a unique positive integer using `System.unique_integer/1`. |
| 14 | + * UUIDs are replaced with a new UUID using `Ecto.UUID.generate/0`. |
| 15 | + * Other values are replaced with a string prefixed with `gen_` and a new UUID. |
| 16 | +
|
| 17 | + ## Configuration |
| 18 | +
|
| 19 | + To reduce boilerplate, you can set the base path for fixtures in your `config/config.exs`: |
| 20 | +
|
| 21 | + config :together, fixture_path: "test/fixtures" |
| 22 | +
|
| 23 | + ## Example |
| 24 | +
|
| 25 | + defmodule MyApp.Test.MyTest do |
| 26 | + use ExUnit.Case, async: true |
| 27 | + alias Together.Test.Fixtures |
| 28 | +
|
| 29 | + test "example test" do |
| 30 | + fixture = Fixtures.load("my_fixture.json") |
| 31 | + # ... |
| 32 | + end |
| 33 | + end |
| 34 | + """ |
| 35 | + |
| 36 | + @id_map_dictionary_key :tg_test_fixture_id_map |
| 37 | + @uuid_re ~r/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/ |
| 38 | + |
| 39 | + @doc """ |
| 40 | + Read and decode a JSON fixture from the given path, then randomize any IDs present |
| 41 | +
|
| 42 | + Matching IDs within the same file will continue to match with different values. Doing this helps |
| 43 | + to prevent database deadlocks that can occur when async tests use fixtures with the same IDs. |
| 44 | +
|
| 45 | + ## Options |
| 46 | +
|
| 47 | + * `keys`: List of additional keys to randomize. By default, only keys named `id` and those |
| 48 | + ending with `_id` will be randomized. |
| 49 | +
|
| 50 | + """ |
| 51 | + @spec load(String.t(), keyword) :: any |
| 52 | + def load(path, opts \\ []) do |
| 53 | + base_path = Application.get_env(:together, :fixture_path, "") |
| 54 | + |
| 55 | + Path.join(base_path, path) |
| 56 | + |> File.read!() |
| 57 | + |> JSON.decode!() |
| 58 | + |> randomize(opts[:keys] || []) |
| 59 | + end |
| 60 | + |
| 61 | + @doc false |
| 62 | + @spec randomize(any, [String.t()]) :: any |
| 63 | + def randomize(value, extra_keys) do |
| 64 | + id_map = Process.get(@id_map_dictionary_key, %{}) |
| 65 | + {id_map, value} = randomize(id_map, value, extra_keys) |
| 66 | + Process.put(@id_map_dictionary_key, id_map) |
| 67 | + |
| 68 | + value |
| 69 | + end |
| 70 | + |
| 71 | + @spec randomize(map, any, [String.t()]) :: {map, any} |
| 72 | + defp randomize(id_map, map_value, extra_keys) when is_map(map_value) do |
| 73 | + Enum.reduce(map_value, {id_map, %{}}, fn |
| 74 | + {key, value}, {id_map, modified_map} -> |
| 75 | + if key == "id" or String.ends_with?(key, "_id") or key in extra_keys do |
| 76 | + cond do |
| 77 | + is_nil(value) -> |
| 78 | + {id_map, modified_map} |
| 79 | + |
| 80 | + is_integer(value) -> |
| 81 | + new_id = Map.get_lazy(id_map, value, fn -> System.unique_integer([:positive]) end) |
| 82 | + {Map.put(id_map, value, new_id), Map.put(modified_map, key, new_id)} |
| 83 | + |
| 84 | + is_binary(value) -> |
| 85 | + case Regex.scan(@uuid_re, value) do |
| 86 | + [] -> |
| 87 | + new_id = Map.get_lazy(id_map, value, fn -> "gen_" <> Ecto.UUID.generate() end) |
| 88 | + {Map.put(id_map, value, new_id), Map.put(modified_map, key, new_id)} |
| 89 | + |
| 90 | + matches -> |
| 91 | + {id_map, modified_value} = |
| 92 | + Enum.reduce(matches, {id_map, value}, fn [match], {id_map, modified_value} -> |
| 93 | + new_id = Map.get_lazy(id_map, match, fn -> Ecto.UUID.generate() end) |
| 94 | + |
| 95 | + {Map.put(id_map, match, new_id), |
| 96 | + String.replace(modified_value, match, new_id)} |
| 97 | + end) |
| 98 | + |
| 99 | + {id_map, Map.put(modified_map, key, modified_value)} |
| 100 | + end |
| 101 | + |
| 102 | + :else -> |
| 103 | + {id_map, modified_map} |
| 104 | + end |
| 105 | + else |
| 106 | + {id_map, modified_value} = randomize(id_map, value, extra_keys) |
| 107 | + {id_map, Map.put(modified_map, key, modified_value)} |
| 108 | + end |
| 109 | + end) |
| 110 | + end |
| 111 | + |
| 112 | + defp randomize(id_map, list_value, extra_keys) when is_list(list_value) do |
| 113 | + {id_map, list_value} = |
| 114 | + Enum.reduce(list_value, {id_map, []}, fn list_element, {id_map, modified_list} -> |
| 115 | + {id_map, modified_list_element} = randomize(id_map, list_element, extra_keys) |
| 116 | + {id_map, [modified_list_element | modified_list]} |
| 117 | + end) |
| 118 | + |
| 119 | + {id_map, Enum.reverse(list_value)} |
| 120 | + end |
| 121 | + |
| 122 | + defp randomize(id_map, value, _extra_keys), do: {id_map, value} |
| 123 | +end |
0 commit comments