Skip to content

Commit

Permalink
Added a task model with a phx generator
Browse files Browse the repository at this point in the history
  • Loading branch information
kieraneglin committed Jan 25, 2024
1 parent cdd1926 commit ea6b032
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/pinchflat/tasks.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule Pinchflat.Tasks do
@moduledoc """
The Tasks context.
"""

import Ecto.Query, warn: false
alias Pinchflat.Repo

alias Pinchflat.Tasks.Task

@doc """
Returns the list of tasks. Returns [%Task{}, ...]
"""
def list_tasks do
Repo.all(Task)
end

@doc """
Gets a single task.
Returns %Task{}. Raises `Ecto.NoResultsError` if the Task does not exist.
"""
def get_task!(id), do: Repo.get!(Task, id)

@doc """
Creates a task. Returns {:ok, %Task{}} | {:error, %Ecto.Changeset{}}.
"""
def create_task(attrs \\ %{}) do
%Task{}
|> Task.changeset(attrs)
|> Repo.insert()
end

@doc """
Deletes a task. Returns {:ok, %Task{}} | {:error, %Ecto.Changeset{}}.
"""
def delete_task(%Task{} = task) do
Repo.delete(task)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking task changes.
"""
def change_task(%Task{} = task, attrs \\ %{}) do
Task.changeset(task, attrs)
end
end
24 changes: 24 additions & 0 deletions lib/pinchflat/tasks/task.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Pinchflat.Tasks.Task do
@moduledoc """
The Task schema.
"""

use Ecto.Schema
import Ecto.Changeset

alias Pinchflat.MediaSource.Channel

schema "tasks" do
belongs_to :job, Oban.Job
belongs_to :channel, Channel

timestamps(type: :utc_datetime)
end

@doc false
def changeset(task, attrs) do
task
|> cast(attrs, [:job_id, :channel_id])
|> validate_required([:job_id])
end
end
15 changes: 15 additions & 0 deletions priv/repo/migrations/20240125212753_create_tasks.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Pinchflat.Repo.Migrations.CreateTasks do
use Ecto.Migration

def change do
create table(:tasks) do
add :job_id, references(:oban_jobs, on_delete: :delete_all), null: false
add :channel_id, references(:channels, on_delete: :delete_all), null: true

timestamps(type: :utc_datetime)
end

create index(:tasks, [:job_id])
create index(:tasks, [:channel_id])
end
end
51 changes: 51 additions & 0 deletions test/pinchflat/tasks_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
defmodule Pinchflat.TasksTest do
use Pinchflat.DataCase
import Pinchflat.JobFixtures
import Pinchflat.TasksFixtures

alias Pinchflat.Tasks
alias Pinchflat.Tasks.Task

@invalid_attrs %{job_id: nil}

describe "list_tasks/0" do
test "it returns all tasks" do
task = task_fixture()
assert Tasks.list_tasks() == [task]
end
end

describe "get_task!/1" do
test "it returns the task with given id" do
task = task_fixture()
assert Tasks.get_task!(task.id) == task
end
end

describe "create_task/1" do
test "creation with valid data creates a task" do
valid_attrs = %{job_id: job_fixture().id}

assert {:ok, %Task{} = _task} = Tasks.create_task(valid_attrs)
end

test "creation with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Tasks.create_task(@invalid_attrs)
end
end

describe "delete_task/1" do
test "deletion deletes the task" do
task = task_fixture()
assert {:ok, %Task{}} = Tasks.delete_task(task)
assert_raise Ecto.NoResultsError, fn -> Tasks.get_task!(task.id) end
end
end

describe "change_task/1" do
test "it returns a task changeset" do
task = task_fixture()
assert %Ecto.Changeset{} = Tasks.change_task(task)
end
end
end
19 changes: 19 additions & 0 deletions test/support/fixtures/job_fixtures.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Pinchflat.JobFixtures do
@moduledoc false

defmodule TestJobWorker do
@moduledoc false
use Oban.Worker, queue: :default

@impl Oban.Worker
def perform(%Oban.Job{}) do
:ok
end
end

def job_fixture() do
{:ok, job} = Oban.insert(TestJobWorker.new(%{}))

job
end
end
24 changes: 24 additions & 0 deletions test/support/fixtures/tasks_fixtures.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Pinchflat.TasksFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `Pinchflat.Tasks` context.
"""

alias Pinchflat.JobFixtures
alias Pinchflat.MediaSourceFixtures

@doc """
Generate a task.
"""
def task_fixture(attrs \\ %{}) do
{:ok, task} =
attrs
|> Enum.into(%{
channel_id: MediaSourceFixtures.channel_fixture().id,
job_id: JobFixtures.job_fixture().id
})
|> Pinchflat.Tasks.create_task()

task
end
end

0 comments on commit ea6b032

Please sign in to comment.