nallwhy / doumi_test

MIT License
2 stars 0 forks source link

Doumi.Test

Hex Version Hex Docs License Last Updated

Doumi.Test is a helper library that make it easier to test your Elixir codes.

도우미(Doumi) means "helper" in Korean.

Usage

assert_same_values/2

defmodule MyApp.Test do
  use ExUnit.Case
  use Doumi.Test

  test "returns true with the same values" do
    number1 = 1
    number2 = 1.0

    datetime1 = ~U[2023-11-11 00:00:00Z]
    datetime2 = ~U[2023-11-11 00:00:00.000Z]

    decimal1 = Decimal.new("1.1")
    decimal2 = 1.1

    # instead of
    assert number1 == number2
    assert DateTime.compare(datetime1, datetime2) == :eq
    assert Decimal.equal?(decimal1, decimal2)

    # do
    assert_same_values number1, number2
    assert_same_values datetime1, datetime2
    assert_same_values decimal1, decimal2
  end
end

assert_same_fields/3

defmodule MyApp.Test do
  use ExUnit.Case
  use Doumi.Test

  test "returns true with the same fields" do
    attrs = %{
      name: "name",
      age: 30,
      married: false
    }

    assert {:ok, something} = create_something(attrs)

    # instead of
    assert something.name == attrs.name
    assert something.age == attrs.age
    assert something.married == attrs.married

    # do
    assert_same_fields something, attrs, [:name, :age, :married]
  end
end

assert_same_records/2

defmodule MyApp.Test do
  use MyApp.DataCase
  use Doumi.Test

  test "returns true with the same records" do
    something = insert(:something)

    assert {:ok, fetched_something} = fetch_something(something.primary_key0, something.primary_key1)

    # instead of
    assert fetched_something.primary_key0 == something.primary_key0
    assert fetched_something.primary_key1 == something.primary_key1

    # do
    assert_same_records fetched_something, something
  end
end

reload/2, reload!/2

defmodule MyApp.Test do
  use MyApp.DataCase
  use Doumi.Test

  test "reload record" do
    counter = insert(:counter)

    assert :ok = increase_counter_and_no_return(counter)

    updated_counter = reload!(counter)

    assert updated_counter.count == counter.count + 1
  end
end

Installation

The package can be installed by adding doumi_test to your list of dependencies in mix.exs:

def deps do
  [
    {:doumi_test, "~> 0.2.0"}
  ]
end

To configure formatter, add :doumi_test to import_deps of .formatter.exs:

[
  ...
  import_deps: [..., :doumi_test]
]

Doumi*

All Doumi libraries:

Copyright and License

Copyright (c) 2023 Jinkyou Son (Json)

This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.