defmodule Cepis.BatchTest do use Cepis.ServerCase test "order is preserved and a missing CEP becomes nil", %{server: server, client: client} do body = csv([ "01001000,Praça da Sé,lado ímpar,,Sé,São Paulo,SP,São Paulo," <> "Sudeste,3550308,1004,11,7107", "99999999,,,,,,,,,,,,", "20040020,Avenida Rio Branco,,,Centro,Rio de Janeiro,RJ," <> "Rio de Janeiro,Sudeste,3304557,,21,6001" ]) FakeServer.on_request(server, fn _path, _n -> {200, body, []} end) assert {:ok, [first, nil, third]} = Cepis.lookup_many(["01001000", "99999999", "20040020"], client) assert first.localidade == "São Paulo" assert third.uf == "RJ" assert [path] = FakeServer.calls(server) assert path == "/csv?ceps=01001000%2C99999999%2C20040020" end test "batches never carry a coordinate", %{server: server, client: client} do body = csv(["01001000,Praça da Sé,,,Sé,São Paulo,SP,São Paulo,Sudeste,3550308,1004,11,7107"]) FakeServer.on_request(server, fn _path, _n -> {200, body, []} end) assert {:ok, [address]} = Cepis.lookup_many(["01001000"], client) assert address.geo == nil end test "a quoted field containing a comma is parsed, not split", %{server: server, client: client} do body = csv([~s(01001000,"Rua Um, Dois",,,Centro,São Paulo,SP,São Paulo,Sudeste,1,,11,1)]) FakeServer.on_request(server, fn _path, _n -> {200, body, []} end) assert {:ok, [address]} = Cepis.lookup_many(["01001000"], client) assert address.logradouro == "Rua Um, Dois" end test "an escaped quote inside a quoted field is unescaped", %{server: server, client: client} do body = csv([~s(01001000,"Rua ""A""",,,Centro,São Paulo,SP,São Paulo,Sudeste,1,,11,1)]) FakeServer.on_request(server, fn _path, _n -> {200, body, []} end) assert {:ok, [address]} = Cepis.lookup_many(["01001000"], client) assert address.logradouro == ~s(Rua "A") end test "an empty list makes no request", %{server: server, client: client} do assert Cepis.lookup_many([], client) == {:ok, []} assert FakeServer.calls(server) == [] end test "more than 500 CEPs become more than one request", %{server: server, client: client} do ceps = Enum.map(0..500, &String.pad_leading(to_string(&1), 8, "0")) {first, second} = Enum.split(ceps, 500) rows = fn chunk -> csv(Enum.map(chunk, &"#{&1},,,,,X,SP,São Paulo,Sudeste,1,,11,1")) end bodies = %{1 => rows.(first), 2 => rows.(second)} FakeServer.on_request(server, fn _path, n -> {200, Map.fetch!(bodies, n), []} end) assert {:ok, result} = Cepis.lookup_many(ceps, client) assert length(result) == 501 assert Enum.all?(result, &(&1.localidade == "X")) assert length(FakeServer.calls(server)) == 2 end test "a truncated response pads rather than shifting", %{server: server, client: client} do body = csv(["01001000,,,,,São Paulo,SP,São Paulo,Sudeste,1,,11,1"]) FakeServer.on_request(server, fn _path, _n -> {200, body, []} end) assert {:ok, [first, nil]} = Cepis.lookup_many(["01001000", "20040020"], client) assert first.localidade == "São Paulo" end test "an empty body yields a full list of nils", %{server: server, client: client} do FakeServer.on_request(server, fn _path, _n -> {200, "", []} end) assert {:ok, [nil, nil]} = Cepis.lookup_many(["01001000", "20040020"], client) end test "a transport failure fails the whole batch", %{server: server, client: client} do FakeServer.on_request(server, fn _path, _n -> {503, "", []} end) assert {:error, %Cepis.ServerError{status: 503}} = Cepis.lookup_many(["01001000"], client) end test "the bang variant returns the list", %{server: server, client: client} do body = csv(["01001000,,,,,São Paulo,SP,São Paulo,Sudeste,1,,11,1"]) FakeServer.on_request(server, fn _path, _n -> {200, body, []} end) assert [%Cepis.Address{}] = Cepis.lookup_many!(["01001000"], client) end end