Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for extra event functions #35

Merged
merged 27 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a05a90f
Added tests for extra event functions
MICHAELMUNAVU83 Oct 26, 2023
945f0d2
added formatting to files
MICHAELMUNAVU83 Oct 26, 2023
efe5121
Used test libraries
MICHAELMUNAVU83 Oct 30, 2023
aade95b
Fixed spec error
MICHAELMUNAVU83 Oct 30, 2023
e39bc06
Fixed get elixir conf function
MICHAELMUNAVU83 Oct 30, 2023
4739798
Added factories
MICHAELMUNAVU83 Oct 30, 2023
e5a2187
Added factories
MICHAELMUNAVU83 Oct 30, 2023
e27e8ad
Added module doc false
MICHAELMUNAVU83 Oct 30, 2023
4f0007e
Renamed get all available tickets and removed dead code
MICHAELMUNAVU83 Oct 31, 2023
67ddc23
Added tests for extra event functions
MICHAELMUNAVU83 Oct 26, 2023
aced203
added formatting to files
MICHAELMUNAVU83 Oct 26, 2023
5288196
Used test libraries
MICHAELMUNAVU83 Oct 30, 2023
e04dd7c
Fixed spec error
MICHAELMUNAVU83 Oct 30, 2023
951f074
Fixed get elixir conf function
MICHAELMUNAVU83 Oct 30, 2023
a5032a4
Added factories
MICHAELMUNAVU83 Oct 30, 2023
15531cd
Added factories
MICHAELMUNAVU83 Oct 30, 2023
4977205
Added module doc false
MICHAELMUNAVU83 Oct 30, 2023
8d9f09b
Renamed get all available tickets and removed dead code
MICHAELMUNAVU83 Oct 31, 2023
be7f141
refactor:
okothkongo Nov 14, 2023
1c1b516
fix lints
okothkongo Nov 14, 2023
e2a55e0
Merge branch 'testing-event-functions' of https://github.com/beamkeny…
MICHAELMUNAVU83 Nov 14, 2023
6daa68e
fixed linters errors
MICHAELMUNAVU83 Nov 14, 2023
4dfbf7c
Removed static id of 1 from event map
MICHAELMUNAVU83 Nov 14, 2023
eb4ea01
refactor:
okothkongo Nov 14, 2023
be0b057
ensure correct specs
okothkongo Nov 15, 2023
1df6440
add removed func
okothkongo Nov 15, 2023
207c6a6
be more stricter
okothkongo Nov 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
#
## Readability Checks
#
{Credo.Check.Readability.AliasOrder, false},
{Credo.Check.Readability.AliasOrder, []},
{Credo.Check.Readability.FunctionNames, []},
{Credo.Check.Readability.LargeNumbers, []},
{Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]},
Expand Down
2 changes: 1 addition & 1 deletion coveralls.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{

"coverage_options": {
"minimum_coverage": 69.2
"minimum_coverage": 89.5
},
"skip_files": ["lib/elixir_conf_africa_web/components/core_components.ex"]
}
33 changes: 24 additions & 9 deletions lib/elixir_conf_africa/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ defmodule ElixirConfAfrica.Events do
"""

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

alias ElixirConfAfrica.Events.Event
alias ElixirConfAfrica.Repo
alias ElixirConfAfrica.TicketTypes.TicketType

@doc """
Expand All @@ -18,24 +18,34 @@ defmodule ElixirConfAfrica.Events do
[%Event{}, ...]

"""
@spec list_events() :: list()
def list_events do
Repo.all(from e in Event, order_by: [desc: e.id])
end

def get_elixir_conf_event_and_ticket_types do
get_elixir_conf_event()
|> Repo.preload(:ticket_types)
end
@doc """
Returns the elixir conf event together with all its ticket types
"""
@spec get_event_with_ticket_types_by_event_name(String.t()) :: Event.t()
def get_event_with_ticket_types_by_event_name(event_name) do
query =
from event in Event,
join: ticket_types in assoc(event, :ticket_types),
where: event.name == ^event_name,
preload: [ticket_types: ticket_types]

defp get_elixir_conf_event do
Repo.get_by(Event, name: "ElixirConf Africa 2024")
Repo.one(query)
end

def get_all_available_tickets do
@doc """
Get totals number of available tickets for a given event
"""
@spec get_total_number_of_available_tickets(String.t()) :: Event.t()
def get_total_number_of_available_tickets(event_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 == ^event_name,
select: sum(t.number)

Repo.one(query)
Expand All @@ -55,6 +65,7 @@ defmodule ElixirConfAfrica.Events do
** (Ecto.NoResultsError)

"""
@spec get_event!(non_neg_integer()) :: Event.t() | Ecto.NoResultsError
def get_event!(id), do: Repo.get!(Event, id)

@doc """
Expand All @@ -69,6 +80,7 @@ defmodule ElixirConfAfrica.Events do
{:error, %Ecto.Changeset{}}

"""
@spec create_event(map()) :: {:ok, Event.t()} | {:error, Ecto.Changeset.t()}
def create_event(attrs \\ %{}) do
%Event{}
|> Event.changeset(attrs)
Expand All @@ -87,6 +99,7 @@ defmodule ElixirConfAfrica.Events do
{:error, %Ecto.Changeset{}}

"""
@spec update_event(Event.t(), map()) :: {:ok, Event.t()} | {:error, Ecto.Changeset.t()}
def update_event(%Event{} = event, attrs) do
event
|> Event.changeset(attrs)
Expand All @@ -105,6 +118,7 @@ defmodule ElixirConfAfrica.Events do
{:error, %Ecto.Changeset{}}

"""
@spec delete_event(Event.t()) :: {:ok, Event.t()} | {:error, Ecto.Changeset.t()}
def delete_event(%Event{} = event) do
Repo.delete(event)
end
Expand All @@ -118,6 +132,7 @@ defmodule ElixirConfAfrica.Events do
%Ecto.Changeset{data: %Event{}}

"""
@spec change_event(Event.t(), map()) :: Ecto.Changeset.t()
def change_event(%Event{} = event, attrs \\ %{}) do
Event.changeset(event, attrs)
end
Expand Down
5 changes: 2 additions & 3 deletions lib/elixir_conf_africa/events/event.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
defmodule ElixirConfAfrica.Events.Event do
@moduledoc false

use Ecto.Schema
use TypedEctoSchema
import Ecto.Changeset

schema "events" do
typed_schema "events" do
field :name, :string
field :description, :string
field :location, :string
Expand Down
7 changes: 7 additions & 0 deletions lib/elixir_conf_africa/ticket_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule ElixirConfAfrica.TicketTypes do
[%TicketType{}, ...]

"""
@spec list_ticket_types() :: list()
def list_ticket_types do
Repo.all(TicketType)
end
Expand All @@ -35,6 +36,7 @@ defmodule ElixirConfAfrica.TicketTypes do
** (Ecto.NoResultsError)

"""
@spec get_ticket_type!(non_neg_integer()) :: TicketType.t() | Ecto.NoResultsError
def get_ticket_type!(id), do: Repo.get!(TicketType, id)

@doc """
Expand All @@ -49,6 +51,7 @@ defmodule ElixirConfAfrica.TicketTypes do
{:error, %Ecto.Changeset{}}

"""
@spec create_ticket_type(map()) :: {:ok, TicketType.t()} | {:error, Ecto.Changeset.t()}
def create_ticket_type(attrs \\ %{}) do
%TicketType{}
|> TicketType.changeset(attrs)
Expand All @@ -67,6 +70,8 @@ defmodule ElixirConfAfrica.TicketTypes do
{:error, %Ecto.Changeset{}}

"""
@spec update_ticket_type(TicketType.t(), map()) ::
{:ok, TicketType.t()} | {:error, Ecto.Changeset.t()}
def update_ticket_type(%TicketType{} = ticket_type, attrs) do
ticket_type
|> TicketType.changeset(attrs)
Expand All @@ -85,6 +90,7 @@ defmodule ElixirConfAfrica.TicketTypes do
{:error, %Ecto.Changeset{}}

"""
@spec delete_ticket_type(TicketType.t()) :: {:ok, TicketType.t()} | {:error, Ecto.Changeset.t()}
def delete_ticket_type(%TicketType{} = ticket_type) do
Repo.delete(ticket_type)
end
Expand All @@ -98,6 +104,7 @@ defmodule ElixirConfAfrica.TicketTypes do
%Ecto.Changeset{data: %TicketType{}}

"""
@spec change_ticket_type(TicketType.t(), map()) :: Ecto.Changeset.t()
def change_ticket_type(%TicketType{} = ticket_type, attrs \\ %{}) do
TicketType.changeset(ticket_type, attrs)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/elixir_conf_africa/ticket_types/ticket_type.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule ElixirConfAfrica.TicketTypes.TicketType do
@moduledoc false

use Ecto.Schema
use TypedEctoSchema
import Ecto.Changeset

schema "ticket_types" do
typed_schema "ticket_types" do
field :name, :string
field :description, :string
field :price, :decimal
Expand Down
18 changes: 14 additions & 4 deletions lib/elixir_conf_africa_web/live/home_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ defmodule ElixirConfAfricaWeb.HomeLive.Index do
alias ElixirConfAfrica.Events

def mount(_params, _session, socket) do
elixir_conf_africa_event = Events.get_elixir_conf_event_and_ticket_types()
availabe_tickets = Events.get_all_available_tickets()
# these value are more static and we should find away of display this data to home page
event_name = "ElixirConf Africa #{get_current_year()}"

event =
Events.get_event_with_ticket_types_by_event_name(event_name)

available_ticket = Events.get_total_number_of_available_tickets(event_name)

{:ok,
socket
|> assign(:available_tickets, availabe_tickets)
|> assign(:event, elixir_conf_africa_event)}
|> assign(:event, event)}
|> assign(available_ticket: available_ticket)
end

defp get_current_year do
%{year: year} = DateTime.utc_now()
year
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ defmodule ElixirConfAfrica.MixProject do
{:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false},
{:credo, "~> 1.7", only: :dev, runtime: false},
{:excoveralls, "~> 0.18.0", only: :test},
{:faker, "~> 0.17.0", only: [:dev, :test], runtime: false}
{:faker, "~> 0.17.0", only: [:dev, :test], runtime: false},
{:typed_ecto_schema, "~> 0.4.1"}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.1", "315d9163a1d4660aedc3fee73f33f1d355dcc76c5c3ab3d59e76e3edf80eef1f", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7be9e0871c41732c233be71e4be11b96e56177bf15dde64a8ac9ce72ac9834c6"},
"telemetry_poller": {:hex, :telemetry_poller, "1.0.0", "db91bb424e07f2bb6e73926fcafbfcbcb295f0193e0a00e825e589a0a47e8453", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b3a24eafd66c3f42da30fc3ca7dda1e9d546c12250a2d60d7b81d264fbec4f6e"},
"typed_ecto_schema": {:hex, :typed_ecto_schema, "0.4.1", "a373ca6f693f4de84cde474a67467a9cb9051a8a7f3f615f1e23dc74b75237fa", [:mix], [{:ecto, "~> 3.5", [hex: :ecto, repo: "hexpm", optional: false]}], "hexpm", "85c6962f79d35bf543dd5659c6adc340fd2480cacc6f25d2cc2933ea6e8fcb3b"},
"websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"},
"websock_adapter": {:hex, :websock_adapter, "0.5.4", "7af8408e7ed9d56578539594d1ee7d8461e2dd5c3f57b0f2a5352d610ddde757", [:mix], [{:bandit, ">= 0.6.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "d2c238c79c52cbe223fcdae22ca0bb5007a735b9e933870e241fce66afb4f4ab"},
}
49 changes: 34 additions & 15 deletions test/elixir_conf_africa/events_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ defmodule ElixirConfAfrica.EventsTest do
alias ElixirConfAfrica.Events

describe "events" do
import ElixirConfAfrica.Factory
alias ElixirConfAfrica.Events.Event

import ElixirConfAfrica.EventsFixtures

@invalid_attrs %{
name: nil,
description: nil,
Expand All @@ -16,17 +15,31 @@ 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_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_event_with_ticket_types_by_event_name/1 returns the elixir conf event with ticket types",
%{event: event} do
ticket_type =
insert!(:elixir_conf_ticket_type, event_id: event.id)

event_with_ticket_types = ElixirConfAfrica.Repo.preload(event, :ticket_types)
assert event = Events.get_event_with_ticket_types_by_event_name(event.name)

assert event_with_ticket_types.ticket_types == [ticket_type]
assert event == event_with_ticket_types
end

test "create_event/1 with valid data creates a event" do
valid_attrs = %{
name: "some name",
Expand All @@ -50,9 +63,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 @@ -71,21 +82,29 @@ 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

test "get_total_number_of_available_tickets/1 returns total number of tickets", %{
event: event
} do
%{number: number} = insert!(:elixir_conf_ticket_type, event_id: event.id)
%{number: number1} = insert!(:elixir_conf_ticket_type, event_id: event.id)
total_number_of_available_tickets = number1 + number

assert Events.get_total_number_of_available_tickets(event.name) ==
total_number_of_available_tickets
end
end
end
Loading
Loading