1.7 KiB · text History bbb2a48
defmodule Vigil.ReleasesFixtures do
  @moduledoc "Releases, and the issues and artifact bundles release pages show, for tests."

  alias Vigil.Artifacts.ArtifactBundle
  alias Vigil.Issues.Issue
  alias Vigil.Releases
  alias Vigil.Repo

  @doc "Release `version` of `scope`'s organization, linked to `projects`."
  def release_fixture(scope, version, projects, attrs \\ %{}) do
    attrs =
      Map.merge(%{"version" => version, "projects" => Enum.map(projects, & &1.slug)}, attrs)

    {:ok, release, _} = Releases.create_release(scope, attrs)
    release
  end

  @doc "An issue of `project`, as event processing would leave it."
  def issue_fixture(project, attrs \\ []) do
    now = DateTime.utc_now()

    Repo.insert!(
      struct(
        %Issue{
          project_id: project.id,
          organization_id: project.organization_id,
          short_id: System.unique_integer([:positive]),
          type: :error,
          title: "boom #{System.unique_integer([:positive])}",
          first_seen: now,
          last_seen: now
        },
        attrs
      )
    )
  end

  @doc "An (assembled) artifact bundle for `release`, linked to `project`."
  def artifact_bundle_fixture(project, release, attrs \\ []) do
    bundle =
      Repo.insert!(
        struct(
          %ArtifactBundle{
            organization_id: project.organization_id,
            checksum: :crypto.strong_rand_bytes(20) |> Base.encode16(case: :lower),
            release: release,
            artifact_count: 2,
            size: 2048
          },
          attrs
        )
      )

    Repo.insert_all("artifact_bundle_projects", [
      %{
        artifact_bundle_id: Ecto.UUID.dump!(bundle.id),
        project_id: Ecto.UUID.dump!(project.id)
      }
    ])

    bundle
  end
end