diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/suite.lisp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/suite.lisp b/tests/suite.lisp index 4ef0c5f..af48426 100644 --- a/tests/suite.lisp +++ b/tests/suite.lisp @@ -11,10 +11,20 @@ (defparameter +csv-header+ "cep,logradouro,complemento,unidade,bairro,localidade,uf,estado,regiao,ibge,gia,ddd,siafi") +(defparameter +areal-faixa+ + "\"faixa\":{\"localidade\":\"Areal\",\"uf\":\"RJ\",\"estado\":\"Rio de Janeiro\",\"regiao\":\"Sudeste\",\"ibge\":\"3300225\",\"cep_ini\":\"25845001\",\"cep_fim\":\"25849899\",\"ceps\":118}" + "The band 25845000 falls in: the CEP that fails is the DNE's own start for +Areal, which is why the band begins one number later.") + (defun se-json (&rest extra-fields) "The fixture address, with EXTRA-FIELDS spliced in as raw JSON members." (format nil "{~a~{,~a~}}" +se-fields+ extra-fields)) +(defun not-found-json (&rest extra-fields) + "The 404 body, with EXTRA-FIELDS spliced in as raw JSON members." + (format nil "{\"erro\":\"cep_nao_encontrado\",\"mensagem\":\"CEP não existe na base do DNE\"~{,~a~}}" + extra-fields)) + (defun csv-body (&rest rows) (format nil "~a~%~{~a~%~}" +csv-header+ rows)) @@ -90,6 +100,64 @@ (signals cep-not-found (lookup "99999999" :client client)) (is (= 1 (call-count server "/99999999"))))) +(test not-found-carries-the-band-it-falls-in + (with-fake-server (server client) + (respond server "/25845000" (reply 404 (not-found-json +areal-faixa+))) + (handler-case (progn (lookup "25845000" :client client) (fail "expected a cep-not-found")) + (cep-not-found (condition) + (let ((faixa (cep-not-found-faixa condition))) + (is (string= "25845000" (cell-error-name condition))) + (is (string= "Areal" (faixa-localidade faixa))) + (is (string= "RJ" (faixa-uf faixa))) + (is (string= "Rio de Janeiro" (faixa-estado faixa))) + (is (string= "Sudeste" (faixa-regiao faixa))) + (is (string= "3300225" (faixa-ibge faixa))) + ;; The ends are the CEPs that exist, not the bounds the Correios + ;; publish: the DNE's own start for Areal is the 25845000 that just + ;; failed. And the 118 of them are scattered over the ~4,900 numbers + ;; in between, so the pair is not a list of valid CEPs. + (is (string= "25845001" (faixa-cep-ini faixa))) + (is (string= "25849899" (faixa-cep-fim faixa))) + (is (= 118 (faixa-ceps faixa)))))))) + +(test the-band-does-not-make-a-not-found-succeed + (with-fake-server (server client) + (respond server "/25845000" (reply 404 (not-found-json +areal-faixa+))) + (signals cep-not-found (lookup "25845000" :client client)) + (is (= 1 (call-count server "/25845000"))))) + +(test not-found-without-a-band + (with-fake-server (server client) + (respond server "/99999999" (reply 404 (not-found-json))) + (handler-case (progn (lookup "99999999" :client client) (fail "expected a cep-not-found")) + (cep-not-found (condition) + (is (null (cep-not-found-faixa condition))))))) + +(test malformed-band-does-not-lose-the-not-found + (dolist (field '("\"faixa\":\"Areal\"" "\"faixa\":{}" "\"faixa\":[]" + "\"faixa\":null" "\"faixa\":{\"uf\":\"RJ\"}")) + (with-fake-server (server client) + (respond server "/25845000" (reply 404 (not-found-json field))) + (handler-case (progn (lookup "25845000" :client client) (fail "expected a cep-not-found")) + (cep-not-found (condition) + (is (null (cep-not-found-faixa condition)))))))) + +(test not-found-body-that-is-not-json-still-signals-not-found + (with-fake-server (server client) + (respond server "/25845000" (reply 404 "<html>captive portal</html>")) + (signals cep-not-found (lookup "25845000" :client client)))) + +(test the-report-names-the-municipality + (with-fake-server (server client) + (respond server "/25845000" (reply 404 (not-found-json +areal-faixa+))) + (respond server "/99999999" (reply 404 (not-found-json))) + (handler-case (progn (lookup "25845000" :client client) (fail "expected a cep-not-found")) + (cep-not-found (condition) + (is (search "Areal" (princ-to-string condition))))) + (handler-case (progn (lookup "99999999" :client client) (fail "expected a cep-not-found")) + (cep-not-found (condition) + (is (search "99999999" (princ-to-string condition))))))) + (test invalid-cep-never-leaves-the-machine (with-fake-server (server client) (signals invalid-cep (lookup "123" :client client)) |