Skip to content

Commit

Permalink
tests and fixes for new_work
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Feb 20, 2024
1 parent 9ad0600 commit c5587dd
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 40 deletions.
4 changes: 4 additions & 0 deletions lib/banchan/works/work_upload.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
defmodule Banchan.Works.WorkUpload do
@moduledoc """
Uploads belonging to Works. For media uploads, we also keep a thumbnailed
preview.
"""
use Ecto.Schema
import Ecto.Changeset

Expand Down
24 changes: 5 additions & 19 deletions lib/banchan/works/works.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ defmodule Banchan.Works do
}
end)

offering = Keyword.get(opts, :offering)
client = Keyword.get(opts, :client)
commission = Keyword.get(opts, :commission)
offering = Keyword.get(opts, :offering)

%Work{
studio_id: studio.id,
client_id: client && client.id,
offering_id: offering && offering.id,
commission_id: commission && commission.id,
uploads: work_uploads
Expand Down Expand Up @@ -185,6 +188,7 @@ defmodule Banchan.Works do
end
end

# credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity
defp filter_current_user(q, opts) do
mature_content_enabled? = Application.get_env(:banchan, :mature_content_enabled?, false)

Expand Down Expand Up @@ -289,24 +293,6 @@ defmodule Banchan.Works do
"""
def get_work!(id), do: Repo.get!(Work, id)

@doc """
Creates a work.
## Examples
iex> create_work(%{field: value})
{:ok, %Work{}}
iex> create_work(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_work(attrs \\ %{}) do
%Work{}
|> Work.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a work.
Expand Down
2 changes: 1 addition & 1 deletion priv/repo/migrations/20240218024033_create_works.exs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ defmodule Banchan.Repo.Migrations.CreateWorks do
end
)

create table(:work_uploads, primary_key: false) do
create table(:work_uploads) do
add :comment, :text
add :work_id, references(:works, on_delete: :delete_all, type: :uuid), null: false
add :upload_id, references(:uploads, on_delete: :delete_all, type: :uuid), null: false
Expand Down
67 changes: 56 additions & 11 deletions test/banchan/works_test.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
defmodule Banchan.WorksTest do
use Banchan.DataCase

alias Banchan.Works
import Banchan.AccountsFixtures
import Banchan.StudiosFixtures
import Banchan.WorksFixtures

describe "works" do
alias Banchan.Works.Work
alias Banchan.Uploads
alias Banchan.Works
alias Banchan.Works.Work

import Banchan.WorksFixtures
setup do
# Explicitly get a connection before each test
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)

Check warning on line 14 in test/banchan/works_test.exs

View workflow job for this annotation

GitHub Actions / Static Code Analysis (1.14.5, 25)

Nested modules could be aliased at the top of the invoking module.
# Setting the shared mode must be done only after checkout
Ecto.Adapters.SQL.Sandbox.mode(Banchan.Repo, :auto)

Check warning on line 16 in test/banchan/works_test.exs

View workflow job for this annotation

GitHub Actions / Static Code Analysis (1.14.5, 25)

Nested modules could be aliased at the top of the invoking module.
end

describe "works" do
@invalid_attrs %{private: nil, description: nil, title: nil, tags: nil, mature: nil}

@tag skip: "TODO"
Expand All @@ -22,8 +31,20 @@ defmodule Banchan.WorksTest do
assert Works.get_work!(work.id) == work
end

@tag skip: "TODO"
test "create_work/1 with valid data creates a work" do
test "new_work/1 with valid data creates a work" do
artist = user_fixture()
client = user_fixture()
studio = studio_fixture([artist])

uploads = [
Uploads.save_file!(
client,
Path.expand("../support/file-types/image/test.png", __DIR__),
"image/png",
"test.png"
)
]

valid_attrs = %{
private: true,
description: "some description",
Expand All @@ -32,17 +53,42 @@ defmodule Banchan.WorksTest do
mature: true
}

assert {:ok, %Work{} = work} = Works.create_work(valid_attrs)
assert {:ok, %Work{} = work} =
Works.new_work(artist, studio, valid_attrs, uploads, client: client)

assert work.private == true
assert work.description == "some description"
assert work.title == "some title"
assert work.tags == ["option1", "option2"]
assert work.mature == true

work = work |> Repo.preload([:client, :studio])

assert work.client.id == client.id
assert work.studio.id == studio.id
end

@tag skip: "TODO"
test "create_work/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Works.create_work(@invalid_attrs)
test "new_work/1 with invalid data returns error changeset" do
artist = user_fixture()
studio = studio_fixture([artist])

uploads = [
Uploads.save_file!(
artist,
Path.expand("../support/file-types/image/test.png", __DIR__),
"image/png",
"test.png"
)
]

assert {:error, %Ecto.Changeset{}} = Works.new_work(artist, studio, @invalid_attrs, uploads)
end

test "new_work/1 requires non-empty uploads" do
artist = user_fixture()
studio = studio_fixture([artist])
uploads = []
assert {:error, :uploads_required} = Works.new_work(artist, studio, @invalid_attrs, uploads)
end

@tag skip: "TODO"
Expand Down Expand Up @@ -72,7 +118,6 @@ defmodule Banchan.WorksTest do
assert work == Works.get_work!(work.id)
end

@tag skip: "TODO"
test "change_work/1 returns a work changeset" do
work = work_fixture()
assert %Ecto.Changeset{} = Works.change_work(work)
Expand Down
29 changes: 20 additions & 9 deletions test/support/fixtures/works_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@ defmodule Banchan.WorksFixtures do
def work_fixture(attrs \\ %{}) do
artist = Map.get(attrs, :artist, user_fixture(%{roles: [:artist]}))
studio = Map.get(attrs, :studio, studio_fixture([artist]))
uploads = Map.get(attrs, :uploads, [
%{name: "test.png", dir: "file-types/images", type: "image/png"},
%{name: "test.jpg", dir: "file-types/images", type: "image/jpg"}
])
uploads = uploads |> Enum.map(fn file ->
Uploads.save_file!(artist, Path.expand(Path.join(["..", file.dir, file.name]), __DIR__), file.type, file.name)
end)

uploads =
Map.get(attrs, :uploads, [
%{name: "test.png", dir: "file-types/image", type: "image/png"},
%{name: "test.jpg", dir: "file-types/image", type: "image/jpg"}
])

uploads =
uploads
|> Enum.map(fn file ->
Uploads.save_file!(
artist,
Path.expand(Path.join(["..", file.dir, file.name]), __DIR__),
file.type,
file.name
)
end)

{:ok, work} =
Banchan.Works.new_work(
Expand All @@ -36,8 +46,9 @@ defmodule Banchan.WorksFixtures do
mature: false
}),
uploads,
offering: Map.get(attrs, :offering),
commission: Map.get(attrs, :commission)
client: Map.get(attrs, :client),
commission: Map.get(attrs, :commission),
offering: Map.get(attrs, :offering)
)

work
Expand Down

0 comments on commit c5587dd

Please sign in to comment.