-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(backend): full tests for create_event
- Loading branch information
Showing
6 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
backend/test/peach_web/controllers/create_event_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters