1.9 KiB · text History e569b6d
defmodule CartosnapApp.PhotosFixtures do
  @moduledoc "Photos and trips for tests, inserted directly (no upload, no storage)."

  alias CartosnapApp.Trips
  alias CartosnapApp.Photos.Photo
  alias CartosnapApp.Repo

  @doc """
  A processed photo belonging to `scope`'s user. Pass `lat:`/`lon:` to give
  it a position, `processed: false` to leave its variants unmade, and any
  of the camera details (`camera:`, `f_number:`, ...) or its shape
  (`kind:`, `pano:`, `width:`, `height:`) to set them.
  """
  def photo_fixture(scope, attrs \\ %{}) do
    attrs = Map.new(attrs)
    id = Ecto.UUID.generate()
    user_id = scope.user.id
    base = "#{user_id}/#{id}"

    point =
      case attrs do
        %{lat: lat, lon: lon} -> Photo.point(lat, lon)
        _ -> nil
      end

    variants =
      if Map.get(attrs, :processed, true),
        do: %{
          url_full: "#{base}/full.jpg",
          url_preview: "#{base}/prev.jpg",
          url_thumb: "#{base}/thumb.jpg"
        },
        else: %{}

    %Photo{
      id: id,
      user: user_id,
      filename: Map.get(attrs, :filename, "IMG_#{System.unique_integer([:positive])}.jpg"),
      name: Map.get(attrs, :name),
      date: Map.get(attrs, :date, DateTime.utc_now(:second)),
      promoted: Map.get(attrs, :promoted, false),
      exif: Map.get(attrs, :exif, %{}),
      blurhash: "LEHV6nWB2yk8pyo0adR*.7kCMdnj",
      url_original: "#{base}/orig.jpg",
      geo_point: point
    }
    |> Map.merge(variants)
    |> Map.merge(Map.take(attrs, CartosnapApp.Photos.Facets.keys()))
    |> Map.merge(Map.take(attrs, [:width, :height, :kind, :pano]))
    |> Repo.insert!()
  end

  def trip_fixture(scope, attrs \\ %{}) do
    {:ok, trip} =
      Trips.create_trip(
        scope,
        Enum.into(attrs, %{
          name: "Trip #{System.unique_integer([:positive])}",
          date_start: ~D[2025-06-01],
          date_end: ~D[2025-06-10]
        })
      )

    trip
  end
end