Skip to content

Commit

Permalink
refactor:
Browse files Browse the repository at this point in the history
tests to accommodate factory  make Events.get_number_of_tickets_available/1 more dynamic
  • Loading branch information
okothkongo committed Nov 14, 2023
1 parent 4f0007e commit be1313c
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 173 deletions.
4 changes: 2 additions & 2 deletions lib/elixir_conf_africa/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ defmodule ElixirConfAfrica.Events do
"""

def get_number_of_tickets_available_for_elixir_conf_2024 do
def get_number_of_tickets_available(name) do
query =
from t in TicketType,
join: e in Event,
on: t.event_id == e.id and e.name == "ElixirConf Africa 2024",
on: t.event_id == e.id and e.name == ^name,
select: sum(t.number)

Repo.one(query)
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir_conf_africa_web/live/home_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ElixirConfAfricaWeb.HomeLive.Index do

def mount(_params, _session, socket) do
elixir_conf_africa_event = Events.get_elixir_conf_event_and_ticket_types()
availabe_tickets = Events.get_number_of_tickets_available_for_elixir_conf_2024()
availabe_tickets = Events.get_number_of_tickets_available("ElixirConf Africa 2024")

{:ok,
socket
Expand Down
58 changes: 25 additions & 33 deletions test/elixir_conf_africa/events_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ defmodule ElixirConfAfrica.EventsTest do
describe "events" do
alias ElixirConfAfrica.Events.Event

import ElixirConfAfrica.EventsFixtures

import ElixirConfAfrica.Factory

@invalid_attrs %{
Expand All @@ -18,42 +16,41 @@ defmodule ElixirConfAfrica.EventsTest do
start_date: nil,
end_date: nil
}
setup do
event = insert!(:elixir_conf_event)
[event: event]
end

test "list_events/0 returns all events" do
event = event_fixture()
test "list_events/0 returns all events", %{event: event} do
assert Events.list_events() == [event]
end

test "get_elixir_conf_event /0 returns the elixir conf event" do
event =
insert!(:elixir_conf_event)

assert Events.get_elixir_conf_event().name == event.name
test "get_elixir_conf_event /0 returns the elixir conf event", %{event: event} do
assert %{name: name} = Events.get_elixir_conf_event()
assert name == event.name
end

test "get_event!/1 returns the event with given id" do
event = event_fixture()
test "get_event!/1 returns the event with given id", %{event: event} do
assert Events.get_event!(event.id) == event
end

test "get_elixir_conf_event_and_ticket_types/0 returns the elixir conf event with ticket types" do
event =
insert!(:elixir_conf_event)
test "get_elixir_conf_event_and_ticket_types/0 returns the elixir conf event with ticket types",
%{event: event} do
ticket_type = insert!(:elixir_conf_ticket_type, event_id: event.id)

_ticket_type =
insert!(:elixir_conf_ticket_type, event_id: event.id)
assert %{ticket_types: [fetched_ticket_type]} =
Events.get_elixir_conf_event_and_ticket_types()

assert Events.get_elixir_conf_event_and_ticket_types().ticket_types != []
assert fetched_ticket_type.id == ticket_type.id
end

test "get_all_available_tickets/0 returns the number of available tickets" do
event =
insert!(:elixir_conf_event)
test "get_all_available_tickets/0 returns the number of available tickets", %{event: event} do
ticket_type = insert!(:elixir_conf_ticket_type, event_id: event.id)
ticket_type1 = insert!(:elixir_conf_ticket_type, event_id: event.id)
totat_number_of_tickets = ticket_type.number + ticket_type1.number

ticket_type =
insert!(:elixir_conf_ticket_type, event_id: event.id)

assert Events.get_number_of_tickets_available_for_elixir_conf_2024() == ticket_type.number
assert Events.get_number_of_tickets_available("ElixirConf Africa 2024") ==
totat_number_of_tickets
end

test "create_event/1 with valid data creates a event" do
Expand All @@ -79,9 +76,7 @@ defmodule ElixirConfAfrica.EventsTest do
assert {:error, %Ecto.Changeset{}} = Events.create_event(@invalid_attrs)
end

test "update_event/2 with valid data updates the event" do
event = event_fixture()

test "update_event/2 with valid data updates the event", %{event: event} do
update_attrs = %{
name: "some updated name",
description: "some updated description",
Expand All @@ -100,20 +95,17 @@ defmodule ElixirConfAfrica.EventsTest do
assert event.end_date == ~N[2023-10-06 06:18:00]
end

test "update_event/2 with invalid data returns error changeset" do
event = event_fixture()
test "update_event/2 with invalid data returns error changeset", %{event: event} do
assert {:error, %Ecto.Changeset{}} = Events.update_event(event, @invalid_attrs)
assert event == Events.get_event!(event.id)
end

test "delete_event/1 deletes the event" do
event = event_fixture()
test "delete_event/1 deletes the event", %{event: event} do
assert {:ok, %Event{}} = Events.delete_event(event)
assert_raise Ecto.NoResultsError, fn -> Events.get_event!(event.id) end
end

test "change_event/1 returns a event changeset" do
event = event_fixture()
test "change_event/1 returns a event changeset", %{event: event} do
assert %Ecto.Changeset{} = Events.change_event(event)
end
end
Expand Down
54 changes: 23 additions & 31 deletions test/elixir_conf_africa/ticket_types_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@ defmodule ElixirConfAfrica.TicketTypesTest do
describe "ticket_types" do
alias ElixirConfAfrica.TicketTypes.TicketType

import ElixirConfAfrica.TicketTypesFixtures
import ElixirConfAfrica.EventsFixtures

import ElixirConfAfrica.Factory
@invalid_attrs %{name: nil, description: nil, price: nil, number: nil}

test "list_ticket_types/0 returns all ticket_types" do
event = event_fixture()
ticket_type = ticket_type_fixture(%{event_id: event.id})
assert TicketTypes.list_ticket_types() == [ticket_type]
setup do
event = insert!(:elixir_conf_event)
ticket_type = insert!(:elixir_conf_ticket_type, event_id: event.id)
%{ticket_type: ticket_type, event: event}
end

test "get_ticket_type!/1 returns the ticket_type with given id" do
event = event_fixture()
ticket_type = ticket_type_fixture(%{event_id: event.id})

assert TicketTypes.get_ticket_type!(ticket_type.id) == ticket_type
test "list_ticket_types/0 returns all ticket_types", %{ticket_type: ticket_type} do
assert [fetched_ticket_type] = TicketTypes.list_ticket_types()
assert ticket_type.id == fetched_ticket_type.id
end

test "create_ticket_type/1 with valid data creates a ticket_type" do
event = event_fixture()
test "get_ticket_type!/1 returns the ticket_type with given id", %{ticket_type: ticket_type} do
assert fetched_ticket_type = TicketTypes.get_ticket_type!(ticket_type.id)
assert ticket_type.id == fetched_ticket_type.id
end

test "create_ticket_type/1 with valid data creates a ticket_type", %{event: event} do
valid_attrs = %{
event_id: event.id,
name: "some name",
Expand All @@ -45,10 +43,9 @@ defmodule ElixirConfAfrica.TicketTypesTest do
assert {:error, %Ecto.Changeset{}} = TicketTypes.create_ticket_type(@invalid_attrs)
end

test "update_ticket_type/2 with valid data updates the ticket_type" do
event = event_fixture()
ticket_type = ticket_type_fixture(%{event_id: event.id})

test "update_ticket_type/2 with valid data updates the ticket_type", %{
ticket_type: ticket_type
} do
update_attrs = %{
name: "some updated name",
description: "some updated description",
Expand All @@ -65,27 +62,22 @@ defmodule ElixirConfAfrica.TicketTypesTest do
assert ticket_type.number == 579
end

test "update_ticket_type/2 with invalid data returns error changeset" do
event = event_fixture()
ticket_type = ticket_type_fixture(%{event_id: event.id})

test "update_ticket_type/2 with invalid data returns error changeset", %{
ticket_type: ticket_type
} do
assert {:error, %Ecto.Changeset{}} =
TicketTypes.update_ticket_type(ticket_type, @invalid_attrs)

assert ticket_type == TicketTypes.get_ticket_type!(ticket_type.id)
assert fetched_ticket_type = TicketTypes.get_ticket_type!(ticket_type.id)
assert ticket_type.updated_at == fetched_ticket_type.updated_at
end

test "delete_ticket_type/1 deletes the ticket_type" do
event = event_fixture()
ticket_type = ticket_type_fixture(%{event_id: event.id})

test "delete_ticket_type/1 deletes the ticket_type", %{ticket_type: ticket_type} do
assert {:ok, %TicketType{}} = TicketTypes.delete_ticket_type(ticket_type)
assert_raise Ecto.NoResultsError, fn -> TicketTypes.get_ticket_type!(ticket_type.id) end
end

test "change_ticket_type/1 returns a ticket_type changeset" do
event = event_fixture()
ticket_type = ticket_type_fixture(%{event_id: event.id})
test "change_ticket_type/1 returns a ticket_type changeset", %{ticket_type: ticket_type} do
assert %Ecto.Changeset{} = TicketTypes.change_ticket_type(ticket_type)
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/elixir_conf_africa_web/live/event_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule ElixirConfAfricaWeb.EventLiveTest do
use ElixirConfAfricaWeb.ConnCase

import Phoenix.LiveViewTest
import ElixirConfAfrica.EventsFixtures
import ElixirConfAfrica.Factory

@create_attrs %{
name: "some name",
Expand Down Expand Up @@ -30,7 +30,7 @@ defmodule ElixirConfAfricaWeb.EventLiveTest do
}

defp create_event(_) do
event = event_fixture()
event = insert!(:elixir_conf_event)
%{event: event}
end

Expand Down
19 changes: 6 additions & 13 deletions test/elixir_conf_africa_web/live/ticket_type_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ defmodule ElixirConfAfricaWeb.TicketTypeLiveTest do
use ElixirConfAfricaWeb.ConnCase

import Phoenix.LiveViewTest
import ElixirConfAfrica.TicketTypesFixtures
import ElixirConfAfrica.EventsFixtures
import ElixirConfAfrica.Factory

@create_attrs %{
name: "some name",
Expand All @@ -19,25 +18,21 @@ defmodule ElixirConfAfricaWeb.TicketTypeLiveTest do
}
@invalid_attrs %{name: nil, description: nil, price: nil, number: nil}

defp create_ticket_type(_) do
event = event_fixture()
ticket_type = ticket_type_fixture(event_id: event.id)
%{ticket_type: ticket_type}
setup do
event = insert!(:elixir_conf_event)
ticket_type = insert!(:elixir_conf_ticket_type, event_id: event.id)
%{ticket_type: ticket_type, event: event}
end

describe "Index" do
setup [:create_ticket_type]

test "lists all ticket_types", %{conn: conn, ticket_type: ticket_type} do
{:ok, _index_live, html} = live(conn, ~p"/ticket_types")

assert html =~ "Listing Ticket types"
assert html =~ ticket_type.name
end

test "saves new ticket_type", %{conn: conn} do
event = event_fixture()

test "saves new ticket_type", %{conn: conn, event: event} do
{:ok, index_live, _html} = live(conn, ~p"/ticket_types")

assert index_live |> element("a", "New Ticket type") |> render_click() =~
Expand Down Expand Up @@ -97,8 +92,6 @@ defmodule ElixirConfAfricaWeb.TicketTypeLiveTest do
end

describe "Show" do
setup [:create_ticket_type]

test "displays ticket_type", %{conn: conn, ticket_type: ticket_type} do
{:ok, _show_live, html} = live(conn, ~p"/ticket_types/#{ticket_type}")

Expand Down
68 changes: 0 additions & 68 deletions test/support/fixtures/events_fixtures.ex

This file was deleted.

23 changes: 0 additions & 23 deletions test/support/fixtures/ticket_types_fixtures.ex

This file was deleted.

0 comments on commit be1313c

Please sign in to comment.