Skip to content

Commit

Permalink
test(backend): full tests for create_event
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Oct 10, 2024
1 parent 0c9f805 commit 86cc275
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 6 deletions.
8 changes: 5 additions & 3 deletions backend/lib/peach/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Peach.Event do
field :location, :string
field :cover, :string
field :onchain, :boolean, default: false
field :treasury, :string

has_many :ticket_tiers, Peach.TicketTier

Expand All @@ -19,8 +20,9 @@ defmodule Peach.Event do
@doc false
def changeset(event, attrs) do
event
|> cast(attrs, [:name, :description, :location, :date, :cover])
|> cast_assoc(:ticket_tiers, with: &Peach.TicketTier.changeset/2)
|> validate_required([:name, :description, :location, :date, :cover])
|> cast(attrs, [:name, :description, :location, :date, :cover, :treasury])
|> cast_assoc(:ticket_tiers, with: &Peach.TicketTier.changeset/2, required: true)
|> validate_required([:name, :description, :location, :date, :cover, :treasury])
|> validate_format(:treasury, ~r/^0x[0-9a-fA-F]{1,64}$/)
end
end
1 change: 0 additions & 1 deletion backend/lib/peach/events.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Events do
alias Peach.Repo
alias Peach.Event
alias Peach.TicketTier

@doc """
Creates an event with the given attributes.
Expand Down
9 changes: 7 additions & 2 deletions backend/lib/peach_web/controllers/event_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ defmodule PeachWeb.EventController do
{:ok, event} ->
conn
|> put_status(:created)
|> json(%{message: "Event created successfully", event: event.name})
|> json(%{message: "Event created successfully", event_id: event.id})

{:error, changeset} ->
errors =
Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} ->
Phoenix.Naming.humanize(msg)
end)

conn
|> put_status(:unprocessable_entity)
|> json(%{errors: changeset})
|> json(%{errors: errors})
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Peach.Repo.Migrations.CreateEvents do
add :date, :utc_datetime
add :cover, :string
add :onchain, :boolean, default: false
add :treasury, :string

timestamps(type: :utc_datetime)
end
Expand Down
101 changes: 101 additions & 0 deletions backend/test/peach_web/controllers/create_event_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
defmodule PeachWeb.EventControllerTest do
use PeachWeb.ConnCase, async: true

import Ecto.Query

alias Peach.Repo
alias Peach.Event
alias Peach.TicketTier

@valid_event_attrs %{
"name" => "Blockchain Conference",
"date" => "2024-11-10T10:00:00",
"description" => "A conference about blockchain technology.",
"location" => "San Francisco, CA",
"cover" => "https://example.com/cover.jpg",
"treasury" => "0xdead",
"ticket_tiers" => [
%{
"name" => "General Admission",
"description" => "Access to all sessions",
"max_supply" => 100
},
%{
"name" => "VIP",
"description" => "Access to VIP sessions and perks",
"max_supply" => 20
}
]
}

test "creates an event with ticket tiers", %{conn: conn} do
conn = post(conn, "/api/create_event", %{"event" => @valid_event_attrs})

# Assert response status
assert json_response(conn, 201)["message"] == "Event created successfully"
assert json_response(conn, 201)["event_id"] == 1

# Fetch the created event from the database
event = Repo.get_by(Event, id: 1)
assert event
assert event.description == "A conference about blockchain technology."
assert event.location == "San Francisco, CA"
assert event.cover == "https://example.com/cover.jpg"
assert not event.onchain

# Check that the ticket tiers were created
ticket_tiers = Repo.all(from tt in TicketTier, where: tt.event_id == ^event.id)
assert length(ticket_tiers) == 2
assert Enum.any?(ticket_tiers, fn tier -> tier.name == "General Admission" end)
assert Enum.any?(ticket_tiers, fn tier -> tier.name == "VIP" end)
end

test "returns error when required fields are missing", %{conn: conn} do
required_fields = ["name", "date", "description", "location", "cover", "ticket_tiers"]

Enum.each(required_fields, fn field ->
# Remove one required field at a time
invalid_attrs = Map.drop(@valid_event_attrs, [field])

conn = post(conn, "/api/create_event", %{"event" => invalid_attrs})

assert json_response(conn, 422)["errors"][field] == ["Can't be blank"]
end)

# Test with an empty ticket tier list
empty_tiers = Map.replace(@valid_event_attrs, "ticket_tiers", [])
conn = post(conn, "/api/create_event", %{"event" => empty_tiers})

assert json_response(conn, 422)["errors"]["ticket_tiers"] == ["Can't be blank"]
end

test "returns error when fields are in the wrong format", %{conn: conn} do
required_fields = ["name", "date", "description", "location", "cover", "ticket_tiers"]

Enum.each(required_fields, fn field ->
invalid_attrs =
cond do
is_bitstring(@valid_event_attrs[field]) ->
Map.replace(@valid_event_attrs, field, 1)

is_integer(@valid_event_attrs[field]) ->
Map.replace(@valid_event_attrs, field, "Some string")

is_list(@valid_event_attrs[field]) ->
Map.replace(@valid_event_attrs, field, true)
end

conn = post(conn, "/api/create_event", %{"event" => invalid_attrs})

assert json_response(conn, 422)["errors"][field] == ["Is invalid"]
end)

# Test with an empty ticket tier list
invalid_address_format =
Map.replace(@valid_event_attrs, "treasury", "Some string that is not a starknet address")

conn = post(conn, "/api/create_event", %{"event" => invalid_address_format})

assert json_response(conn, 422)["errors"]["treasury"] == ["Has invalid format"]
end
end
2 changes: 2 additions & 0 deletions backend/test/support/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ defmodule Peach.DataCase do
"""
def setup_sandbox(tags) do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Peach.Repo, shared: not tags[:async])
# Reset the sequence for the `events` table before each test
Peach.Repo.query!("ALTER SEQUENCE events_id_seq RESTART WITH 1")
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
end

Expand Down

0 comments on commit 86cc275

Please sign in to comment.