Skip to content

Commit

Permalink
Refactor POST publications/bulk tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrés Vidal <[email protected]>
  • Loading branch information
andres-vidal committed Jun 26, 2024
1 parent ca3ac56 commit fb4de2c
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 51 deletions.
4 changes: 4 additions & 0 deletions backend/lib/richard_burton/flat_publication.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ defmodule RichardBurton.FlatPublication do
|> TranslatedBook.link_fingerprint()
end

def all() do
Repo.all(FlatPublication)
end

def validate(attrs) do
%FlatPublication{} |> changeset(attrs) |> validate_changeset()
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ defmodule RichardBurtonWeb.PublicationControllerTest do
@moduledoc """
Tests for the Publication controller
"""
alias RichardBurton.FlatPublication
use RichardBurtonWeb.ConnCase
import Routes, only: [publication_path: 2]

alias RichardBurton.Country
alias RichardBurton.Publication
alias RichardBurton.Publisher

@publication_attrs %{
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
Expand Down Expand Up @@ -33,78 +36,282 @@ defmodule RichardBurtonWeb.PublicationControllerTest do
end

describe "POST /publications/bulk" do
@valid_input_1 %{
"title" => "Manuel de Moraes: A Chronicle of the Seventeenth Century",
"countries" => "GB",
"year" => 1886,
"publishers" => "Bickers & Son",
"authors" => "Richard Burton, Isabel Burton",
"original_authors" => "J. M. Pereira da Silva",
"original_title" => "Manuel de Moraes: crônica do século XVII"
}
test "returns 201 and the created publications when all the publications are valid", meta do
expect_auth_authorize_admin()

@valid_input_2 %{
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
"year" => 1886,
"countries" => "GB",
"publishers" => "Bickers & Son",
"authors" => "Isabel Burton",
"original_authors" => "José de Alencar",
"original_title" => "Iracema"
}
publications = [
%{
"title" => "Manuel de Moraes: A Chronicle of the Seventeenth Century",
"countries" => "GB",
"year" => 1886,
"publishers" => "Bickers & Son",
"authors" => "Richard Burton, Isabel Burton",
"original_authors" => "J. M. Pereira da Silva",
"original_title" => "Manuel de Moraes: crônica do século XVII"
},
%{
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
"year" => 1886,
"countries" => "GB",
"publishers" => "Bickers & Son",
"authors" => "Isabel Burton",
"original_authors" => "José de Alencar",
"original_title" => "Iracema"
}
]

@invalid_input %{
"title" => "",
"year" => "1886",
"countries" => "GB",
"publishers" => "Bickers & Son",
"authors" => "",
"original_authors" => "José de Alencar",
"original_title" => "Iracema"
}
input = %{"_json" => publications}

@invalid_input_errors %{
"title" => "required",
"authors" => "required"
}
result =
meta.conn
|> post(publication_path(meta.conn, :create_all), input)
|> json_response(201)

assert publications == result
end

test "on success, returns 201 and the created publications", meta do
test "returns 201 and inserts publications with several countries", meta do
expect_auth_authorize_admin()
publications = [@valid_input_1, @valid_input_2]

publications = [
%{
"title" => "Manuel de Moraes: A Chronicle of the Seventeenth Century",
"countries" => "GB, US",
"year" => 1886,
"publishers" => "Bickers & Son",
"authors" => "Richard Burton, Isabel Burton",
"original_authors" => "J. M. Pereira da Silva",
"original_title" => "Manuel de Moraes: crônica do século XVII"
},
%{
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
"year" => 1886,
"countries" => "GB,US",
"publishers" => "Bickers & Son",
"authors" => "Isabel Burton",
"original_authors" => "José de Alencar",
"original_title" => "Iracema"
},
%{
"authors" => "Isabel Burton, Richard Burton",
"countries" => "GB, BR,US",
"original_authors" => "José de Alencar",
"original_title" => "Iracema",
"publishers" => "Bickers & Son",
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
"year" => "1886"
}
]

input = %{"_json" => publications}

assert publications ==
meta.conn
|> post(publication_path(meta.conn, :create_all), input)
|> json_response(201)
result =
meta.conn
|> post(publication_path(meta.conn, :create_all), input)
|> json_response(201)

output = [
%{
"title" => "Manuel de Moraes: A Chronicle of the Seventeenth Century",
"countries" => "GB, US",
"year" => 1886,
"publishers" => "Bickers & Son",
"authors" => "Richard Burton, Isabel Burton",
"original_authors" => "J. M. Pereira da Silva",
"original_title" => "Manuel de Moraes: crônica do século XVII"
},
%{
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
"year" => 1886,
"countries" => "GB, US",
"publishers" => "Bickers & Son",
"authors" => "Isabel Burton",
"original_authors" => "José de Alencar",
"original_title" => "Iracema"
},
%{
"authors" => "Isabel Burton, Richard Burton",
"countries" => "GB, BR, US",
"original_authors" => "José de Alencar",
"original_title" => "Iracema",
"publishers" => "Bickers & Son",
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
"year" => 1886
}
]

assert 3 == FlatPublication.all() |> length()
assert ["GB", "US", "BR"] == Country.all() |> Enum.map(&Country.get_code/1)
assert output == result
end

test "returns 201 and inserts publications with several publishers", meta do
expect_auth_authorize_admin()

publications = [
%{
"title" => "Manuel de Moraes: A Chronicle of the Seventeenth Century",
"countries" => "GB",
"year" => 1886,
"publishers" => "Bickers & Son,Noonday Press",
"authors" => "Richard Burton, Isabel Burton",
"original_authors" => "J. M. Pereira da Silva",
"original_title" => "Manuel de Moraes: crônica do século XVII"
},
%{
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
"year" => 1886,
"countries" => "GB",
"publishers" => "Bickers & Son, Noonday Press",
"authors" => "Isabel Burton",
"original_authors" => "José de Alencar",
"original_title" => "Iracema"
},
%{
"authors" => "Isabel Burton, Richard Burton",
"countries" => "GB",
"original_authors" => "José de Alencar",
"original_title" => "Iracema",
"publishers" => "Bickers & Son, Noonday Press,Ronald Massey",
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
"year" => "1886"
}
]

input = %{"_json" => publications}

result =
meta.conn
|> post(publication_path(meta.conn, :create_all), input)
|> json_response(201)

output = [
%{
"title" => "Manuel de Moraes: A Chronicle of the Seventeenth Century",
"countries" => "GB",
"year" => 1886,
"publishers" => "Bickers & Son, Noonday Press",
"authors" => "Richard Burton, Isabel Burton",
"original_authors" => "J. M. Pereira da Silva",
"original_title" => "Manuel de Moraes: crônica do século XVII"
},
%{
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
"year" => 1886,
"countries" => "GB",
"publishers" => "Bickers & Son, Noonday Press",
"authors" => "Isabel Burton",
"original_authors" => "José de Alencar",
"original_title" => "Iracema"
},
%{
"authors" => "Isabel Burton, Richard Burton",
"countries" => "GB",
"original_authors" => "José de Alencar",
"original_title" => "Iracema",
"publishers" => "Bickers & Son, Noonday Press, Ronald Massey",
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
"year" => 1886
}
]

publishers = ["Bickers & Son", "Noonday Press", "Ronald Massey"]

assert 3 == FlatPublication.all() |> length()
assert publishers == Publisher.all() |> Enum.map(&Publisher.get_name/1)
assert output == result
end

test "on conflict, returns 409 and the conflictive publication", meta do
test "returns 409 when publications are repeated, and returns the first repeated one", meta do
expect_auth_authorize_admin()
publications = [@valid_input_1, @valid_input_2, @valid_input_2]

repeated_publication = %{
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
"year" => 1886,
"countries" => "GB",
"publishers" => "Bickers & Son",
"authors" => "Isabel Burton",
"original_authors" => "José de Alencar",
"original_title" => "Iracema"
}

publications = [
%{
"title" => "Manuel de Moraes: A Chronicle of the Seventeenth Century",
"countries" => "GB",
"year" => 1886,
"publishers" => "Bickers & Son",
"authors" => "Richard Burton, Isabel Burton",
"original_authors" => "J. M. Pereira da Silva",
"original_title" => "Manuel de Moraes: crônica do século XVII"
},
repeated_publication,
repeated_publication
]

input = %{"_json" => publications}

assert @valid_input_2 ==
meta.conn
|> post(publication_path(meta.conn, :create_all), input)
|> json_response(409)
result =
meta.conn
|> post(publication_path(meta.conn, :create_all), input)
|> json_response(409)

assert repeated_publication == result
end

test "on validation error, returns 409, the invalid publication and the errors", meta do
test "returns 400 when a publication is invalid, and returns it with its errors", meta do
expect_auth_authorize_admin()
input = %{"_json" => [@valid_input_1, @invalid_input, @valid_input_2]}

invalid_publication = %{
"title" => "",
"year" => 1886,
"countries" => "GB",
"publishers" => "Bickers & Son",
"authors" => "",
"original_authors" => "José de Alencar",
"original_title" => "Iracema"
}

invalid_publication_errors = %{
"title" => "required",
"authors" => "required"
}

publications = [
%{
"title" => "Manuel de Moraes: A Chronicle of the Seventeenth Century",
"countries" => "GB",
"year" => 1886,
"publishers" => "Bickers & Son",
"authors" => "Richard Burton, Isabel Burton",
"original_authors" => "J. M. Pereira da Silva",
"original_title" => "Manuel de Moraes: crônica do século XVII"
},
invalid_publication,
%{
"title" => "Iraçéma the Honey-Lips: A Legend of Brazil",
"year" => 1886,
"countries" => "GB",
"publishers" => "Bickers & Son",
"authors" => "Isabel Burton",
"original_authors" => "José de Alencar",
"original_title" => "Iracema"
}
]

input = %{"_json" => publications}

output = %{
"publication" => @invalid_input,
"errors" => @invalid_input_errors
"publication" => invalid_publication,
"errors" => invalid_publication_errors
}

assert output ==
meta.conn
|> post(publication_path(meta.conn, :create_all), input)
|> json_response(400)
result =
meta.conn
|> post(publication_path(meta.conn, :create_all), input)
|> json_response(400)

assert output == result
end
end

Expand Down

0 comments on commit fb4de2c

Please sign in to comment.