Skip to content

Commit

Permalink
My name changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Bald committed Nov 20, 2022
1 parent c418122 commit f27d2f2
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 44 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License

Copyright (c) 2022 David Baldauf
Copyright (c) 2022 David Potschka

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ $ _build/prod/rel/sportfest/bin/sportfest start
```

## Lizenz
MIT License. Copyright (c) 2022 David Baldauf.
MIT License. Copyright (c) 2022 David Potschka.
4 changes: 3 additions & 1 deletion lib/sportfest/ergebnisse.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ defmodule Sportfest.Ergebnisse do
%Score{}
|> change_score(attrs)
|> Repo.insert()
|> Repo.preload_on_result_tuple([klasse: [scores: [:station]], schueler: [scores: [:station]], station: []])
|> broadcast(:score_created)
end

Expand All @@ -93,6 +94,7 @@ defmodule Sportfest.Ergebnisse do
score
|> change_score(attrs)
|> Repo.update()
|> Repo.preload_on_result_tuple([klasse: [scores: [:station]], schueler: [scores: [:station]], station: []])
|> broadcast(:score_updated)
end

Expand Down Expand Up @@ -124,7 +126,7 @@ defmodule Sportfest.Ergebnisse do
"""
def change_score(%Score{} = score, attrs \\ %{}) do
score
|> Repo.preload([klasse: [scores: [:station]], schueler: [scores: [:station]], station: []])
# |> Repo.preload([klasse: [scores: [:station]], schueler: [scores: [:station]], station: []])
|> Score.changeset(attrs)
end

Expand Down
15 changes: 15 additions & 0 deletions lib/sportfest/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@ defmodule Sportfest.Repo do
use Ecto.Repo,
otp_app: :sportfest,
adapter: Ecto.Adapters.Postgres

@doc """
Used to unpack the result tuple {:ok, struct} and preload some fields of the struct after transactions.
## Examples
iex> preload_on_result_tuple({:ok, struct}, preloads_list)
{:ok, struct_with_preloaded_fields}
"""
def preload_on_result_tuple({:ok, struct}, preloads_list) do
{:ok, preload(struct, preloads_list) }
end
def preload_on_result_tuple(result_tuple, _preloads_list) do
result_tuple
end

end
23 changes: 12 additions & 11 deletions lib/sportfest/vorbereitung.ex
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,11 @@ defmodule Sportfest.Vorbereitung do
with {:ok, klasse} <-
%Klasse{}
|> Klasse.changeset(attrs)
|> Repo.insert() do
|> Repo.insert()
|> Repo.preload_on_result_tuple([scores: [:station, :schueler], schueler: []]) do
create_score_assocs(klasse)
{:ok, klasse}
end
{:ok, klasse }
end
end

@doc """
Expand All @@ -253,6 +254,7 @@ defmodule Sportfest.Vorbereitung do
klasse
|> change_klasse(attrs)
|> Repo.update()
|> Repo.preload_on_result_tuple([scores: [:station, :schueler], schueler: []])
end

@doc """
Expand Down Expand Up @@ -282,7 +284,7 @@ defmodule Sportfest.Vorbereitung do
"""
def change_klasse(%Klasse{} = klasse, attrs \\ %{}) do
klasse
|> Repo.preload([scores: [:station, :schueler], schueler: []])
# |> Repo.preload([scores: [:station, :schueler], schueler: []])
|> Klasse.changeset(attrs)
end

Expand Down Expand Up @@ -397,9 +399,10 @@ defmodule Sportfest.Vorbereitung do
Ecto.build_assoc(klasse, :schueler)
|> change_schueler(attrs)
|> Repo.insert()
|> Repo.preload_on_result_tuple([scores: [:station], klasse: []])
|> broadcast(:schueler_created) do
create_score_assocs(schueler)
{:ok, schueler}
{:ok, schueler }
end
end

Expand Down Expand Up @@ -450,6 +453,7 @@ defmodule Sportfest.Vorbereitung do
schueler
|> change_schueler(attrs)
|> Repo.update()
|> Repo.preload_on_result_tuple([scores: [:station], klasse: []])
|> broadcast(:schueler_updated)
end

Expand Down Expand Up @@ -486,12 +490,9 @@ defmodule Sportfest.Vorbereitung do
"""
def change_schueler(%Schueler{} = schueler, attrs \\ %{}) do
changeset =
schueler
|> Repo.preload([scores: [:station], klasse: []])
|> Schueler.changeset(attrs)

changeset
schueler
# |> Repo.preload([scores: [:station], klasse: []])
|> Schueler.changeset(attrs)
end

@doc """
Expand Down
13 changes: 3 additions & 10 deletions test/sportfest/ergebnisse_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,18 @@ defmodule Sportfest.ErgebnisseTest do
test "list_scores/0 returns all scores", %{valid_attrs: valid_attrs} do
score = score_fixture(valid_attrs)

# Compares list of score ids, because `list_scores` does preload the associations in contrast to the freshly built score
assert Ergebnisse.list_scores() |> Enum.map(fn score -> score.id end)
== [score] |> Enum.map(fn score -> score.id end)
assert Ergebnisse.list_scores() == [score]
end

test "get_score!/1 returns the score with given id", %{valid_attrs: valid_attrs} do

score = score_fixture(valid_attrs)
# Compares score ids, because `get_score!` does preload the associations in contrast to the freshly built score
assert Ergebnisse.get_score!(score.id).id == score.id
assert Ergebnisse.get_score!(score.id) == score
end

test "create_score/1 with valid data creates a score", %{valid_attrs: valid_attrs} do

assert {:ok, %Score{} = score} = Ergebnisse.create_score(valid_attrs)
assert score.medaille == :keine # Default value

# Get score from db, so that associations to klasse and station have been established
score = Ergebnisse.get_score!(score.id)
assert score.klasse.id == valid_attrs[:klasse_id]
assert score.station.id == valid_attrs[:station_id]
end
Expand All @@ -53,7 +46,7 @@ defmodule Sportfest.ErgebnisseTest do
test "update_score/2 with invalid data returns error changeset", %{valid_attrs: valid_attrs} do
score = score_fixture(valid_attrs)
assert {:error, %Ecto.Changeset{}} = Ergebnisse.update_score(score, @invalid_attrs)
assert Ergebnisse.get_score!(score.id).medaille == score.medaille
assert Ergebnisse.get_score!(score.id) == score
end

test "delete_score/1 deletes the score", %{valid_attrs: valid_attrs} do
Expand Down
30 changes: 10 additions & 20 deletions test/sportfest/vorbereitung_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,18 @@ defmodule Sportfest.VorbereitungTest do

test "list_klassen/0 returns all klassen" do
klasse = klasse_fixture()
assert Vorbereitung.list_klassen() |> Enum.map(fn score -> score.id end)
== [klasse] |> Enum.map(fn score -> score.id end)
assert Vorbereitung.list_klassen() == [klasse]
end

test "get_klasse!/1 returns the klasse with given id" do
klasse = klasse_fixture()
klasse_aus_db = Vorbereitung.get_klasse!(klasse.id)
assert klasse_aus_db.name == klasse.name
assert klasse_aus_db.jahrgang == klasse.jahrgang
assert klasse = Vorbereitung.get_klasse!(klasse.id)
end

test "create_klasse/1 with valid data creates a klasse" do
valid_attrs = %{jahrgang: 5, name: "some name"}
assert {:ok, %Klasse{} = klasse} = Vorbereitung.create_klasse(valid_attrs)

klasse = Vorbereitung.get_klasse!(klasse.id)

assert klasse.jahrgang == 5
assert klasse.name == "some name"
assert klasse.schueler == []
Expand All @@ -118,13 +113,14 @@ defmodule Sportfest.VorbereitungTest do
klasse = klasse_fixture()
update_attrs = %{jahrgang: 6, name: "some updated name"}

assert {:ok, %Klasse{} = klasse} = Vorbereitung.update_klasse(klasse, update_attrs)
assert klasse.jahrgang == 6
assert klasse.name == "some updated name"
assert {:ok, %Klasse{} = geänderte_klasse} = Vorbereitung.update_klasse(klasse, update_attrs)

assert geänderte_klasse.jahrgang == 6
assert geänderte_klasse.name == "some updated name"
end

test "update_klasse/2 with invalid data returns error changeset" do
klasse = Vorbereitung.get_klasse!(klasse_fixture().id)
klasse = klasse_fixture()
assert {:error, %Ecto.Changeset{}} = Vorbereitung.update_klasse(klasse, @invalid_attrs)
assert klasse == Vorbereitung.get_klasse!(klasse.id)
end
Expand Down Expand Up @@ -164,10 +160,7 @@ defmodule Sportfest.VorbereitungTest do
valid_attrs = %{name: "some name"}

assert {:ok, %Schueler{} = schueler} = Vorbereitung.create_schueler(klasse, valid_attrs)

schueler = Vorbereitung.get_schueler!(schueler.id)

assert schueler.klasse == klasse
assert schueler.klasse.id == klasse.id
assert schueler.name == "some name"
assert schueler.scores == []
end
Expand All @@ -181,12 +174,9 @@ defmodule Sportfest.VorbereitungTest do
neue_klasse = klasse_fixture()
update_attrs = %{klasse_id: neue_klasse.id, name: "some updated name"}

assert {:ok, %Schueler{} = update} = Vorbereitung.update_schueler(schueler, update_attrs)

geänderter_schueler = Vorbereitung.get_schueler!(update.id)

assert {:ok, %Schueler{} = geänderter_schueler} = Vorbereitung.update_schueler(schueler, update_attrs)
assert geänderter_schueler.name == "some updated name"
assert geänderter_schueler.klasse == neue_klasse
assert geänderter_schueler.klasse_id == neue_klasse.id
end

test "update_schueler/2 with invalid data returns error changeset" do
Expand Down

0 comments on commit f27d2f2

Please sign in to comment.