defmodule Lightning.DataCase do
  @moduledoc """
  This module defines the setup for tests requiring
  access to the application's data layer.

  You may define functions here to be used as helpers in
  your tests.

  Finally, if the test case interacts with the database,
  we enable the SQL sandbox, so changes done to the database
  are reverted at the end of every test. If you are using
  PostgreSQL, you can even run database tests asynchronously
  by setting `use Lightning.DataCase, async: true`, although
  this option is not recommended for other databases.
  """

  use ExUnit.CaseTemplate

  using do
    quote do
      alias Lightning.Repo

      import Ecto
      import Ecto.Changeset
      import Ecto.Query

      import Lightning.Factories
      import Lightning.ModelHelpers
      import Lightning.TestUtils

      import Lightning.DataCase

      use Oban.Testing, repo: Lightning.Repo
    end
  end

  setup tags do
    Mox.stub_with(Lightning.MockConfig, Lightning.Config.API)

    Mox.stub_with(LightningMock, Lightning.API)

    # Default to Hackney adapter so that Bypass dependent tests continue working
    Mox.stub_with(Lightning.Tesla.Mock, Tesla.Adapter.Hackney)

    Mox.stub_with(
      Lightning.Extensions.MockUsageLimiter,
      Lightning.Extensions.UsageLimiter
    )

    Mox.stub_with(
      Lightning.Extensions.MockAccountHook,
      Lightning.Extensions.AccountHook
    )

    Mox.stub_with(
      Lightning.Extensions.MockCollectionHook,
      Lightning.Extensions.CollectionHook
    )

    Mox.stub_with(
      Lightning.Extensions.MockProjectHook,
      Lightning.Extensions.ProjectHook
    )

    pid =
      Ecto.Adapters.SQL.Sandbox.start_owner!(Lightning.Repo,
        shared: not tags[:async]
      )

    on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
  end
end
