defmodule Growth.Calc.AgeTest do
  @moduledoc false

  use ExUnit.Case, async: true

  alias Growth.Calc.Age

  describe "calculate/2" do
    test "consider today as date of measurement" do
      date_of_birth = Date.add(Date.utc_today(), -7)
      assert 7 == Age.calculate(:day, date_of_birth)
    end
  end

  describe "calculate/3" do
    test "calculate age in days" do
      date_of_measurement = ~D[2024-06-09]
      days_of_birth = [{-1, 1}, {-7, 7}, {-30, 30}]

      Enum.map(days_of_birth, fn {days_ago, expected_age} ->
        date_of_birth = Date.add(date_of_measurement, days_ago)
        assert expected_age == Age.calculate(:day, date_of_birth, date_of_measurement)
      end)
    end

    test "calculate age in completed weeks" do
      date_of_measurement = ~D[2024-06-09]
      days_of_birth = [{-1, 0}, {-7, 1}, {-16, 2}, {-30, 4}]

      Enum.map(days_of_birth, fn {days_ago, expected_age} ->
        date_of_birth = Date.add(date_of_measurement, days_ago)
        assert expected_age == Age.calculate(:week, date_of_birth, date_of_measurement)
      end)
    end

    test "calculate age in completed months" do
      date_of_measurement = ~D[2024-06-09]
      days_of_birth = [{-1, 0}, {-16, 0}, {-31, 1}, {-70, 2}, {-93, 3}]

      Enum.map(days_of_birth, fn {days_ago, expected_age} ->
        date_of_birth = Date.add(date_of_measurement, days_ago)
        assert expected_age == Age.calculate(:month, date_of_birth, date_of_measurement)
      end)
    end
  end
end
