Attached.Test (Attached v0.1.0)

Copy Markdown View Source

Test helpers for attached.

Use in test/support/ only — these bypass the LiveView upload flow and assume you control the input file. Production code should go through changesets (Attached.Ecto.Changeset.put_attached/3).

Summary

Functions

Attaches upload to record.field and returns the updated record.

Configures the Disk storage backend against a unique tmp directory and registers an at_exit cleanup. Returns the storage root.

Functions

attach!(record, field, upload)

Attaches upload to record.field and returns the updated record.

upload accepts:

  • a file path (binary) — filename and content type are inferred via Path.basename/1 and MIME.from_path/1
  • a %Plug.Upload{} or any map with :path (and optionally :filename, :content_type)
  • an existing %Attached.Originals.Original{} — re-attached without storage I/O

The schema's resolved FK column is read from __attached_config__/1, so per-field :foreign_key overrides and the global :default_foreign_key_suffix config are both honored.

Example

user = insert(:user) |> Attached.Test.attach!(:avatar, "test/support/fixtures/red.png")

setup_storage!(opts \\ [])

Configures the Disk storage backend against a unique tmp directory and registers an at_exit cleanup. Returns the storage root.

Call once from test_helper.exs:

Attached.Test.setup_storage!()
ExUnit.start()

One directory is shared across the whole test run — async tests don't need per-test isolation because original keys are already unique. The directory is removed when the BEAM exits cleanly; if a test crashes the runner, leftover dirs in /tmp/attached-test-* are safe to delete.

Options

  • :base_url — public base URL for served files (default "/attachments")
  • :root — override the generated tmp path (mainly useful when you want a stable, gitignored path like test/tmp/storage)