Skip to content

Commit

Permalink
Add pytest filter to real api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mstuttgart committed Nov 20, 2024
1 parent 2b9ebcb commit 373c3cc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
16 changes: 7 additions & 9 deletions tests/test_apicep.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

from brazilcep import WebService, exceptions, get_address_from_cep

IN_GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS") == "true"
IN_GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS", False)
SKIP_REAL_TEST = os.getenv("SKIP_REAL_TEST", False)


@pytest.mark.skipif(SKIP_REAL_TEST, reason="Skip real teste API.")
@pytest.mark.skipif(IN_GITHUB_ACTIONS, reason="Test doesn't work in Github Actions.")
def test_fetch_address_success_real():
# Realizamos a consulta de CEP

address = get_address_from_cep("37.503-130", webservice=WebService.APICEP)

assert address["district"] == "Santo Antônio"
Expand All @@ -21,6 +23,7 @@ def test_fetch_address_success_real():


def test_fetch_address_success(requests_mock):

req_mock_text = """{
"status":200,
"ok":true,
Expand All @@ -34,7 +37,6 @@ def test_fetch_address_success(requests_mock):

requests_mock.get("https://ws.apicep.com/cep/37503130.json", text=req_mock_text)

# Realizamos a consulta de CEP
address = get_address_from_cep("37.503-130", webservice=WebService.APICEP, timeout=5)

assert address["district"] == "Santo Antônio"
Expand All @@ -59,10 +61,7 @@ def test_fetch_address_success(requests_mock):

proxies = {"https": "00.00.000.000", "http": "00.00.000.000"}

# Realizamos a consulta de CEP
address = get_address_from_cep(
"99999-999", webservice=WebService.APICEP, timeout=5, proxies=proxies
)
address = get_address_from_cep("99999-999", webservice=WebService.APICEP, timeout=5, proxies=proxies)

assert address["district"] == ""
assert address["cep"] == "99999-999"
Expand All @@ -79,7 +78,6 @@ def test_fetch_address_cep_not_found(requests_mock):

requests_mock.get("https://ws.apicep.com/cep/00000000.json", text=req_mock_text)

# Realizamos a consulta de CEP
with pytest.raises(exceptions.CEPNotFound):
get_address_from_cep("00000-000", webservice=WebService.APICEP)

Expand Down Expand Up @@ -109,7 +107,7 @@ def test_fetch_address_blocked_by_flood(requests_mock):


def test_fetch_address_404(requests_mock):
requests_mock.get("https://ws.apicep.com/cep/37503130.json", status_code=404) # noqa
requests_mock.get("https://ws.apicep.com/cep/37503130.json", status_code=404)

with pytest.raises(exceptions.BrazilCEPException):
get_address_from_cep("37503-130", webservice=WebService.APICEP)
4 changes: 3 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from brazilcep import WebService, get_address_from_cep
from brazilcep.client import _format_cep


def test_search_error():
with pytest.raises(KeyError):
get_address_from_cep("37.503-130", webservice=5)
Expand Down Expand Up @@ -44,6 +45,7 @@ def test_format_cep_fail():
with pytest.raises(ValueError):
_format_cep(True)


def test_a_deprecated_enum_value():

with catch_warnings(record=True) as w:
Expand All @@ -55,4 +57,4 @@ def test_a_deprecated_enum_value():
get_address_from_cep("37.503-130", webservice=WebService.CORREIOS)
assert len(w) == 1
assert issubclass(w[0].category, DeprecationWarning)
assert str(w[0].message) == 'CORREIOS is going to be deprecated. Please, use other webservice.'
assert str(w[0].message) == "CORREIOS is going to be deprecated. Please, use other webservice."
13 changes: 5 additions & 8 deletions tests/test_opencep.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

from brazilcep import WebService, exceptions, get_address_from_cep

IN_GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS") == "true"
IN_GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS", False)
SKIP_REAL_TEST = os.getenv("SKIP_REAL_TEST", False)


@pytest.mark.skipif(SKIP_REAL_TEST, reason="Skip real teste API.")
@pytest.mark.skipif(IN_GITHUB_ACTIONS, reason="Test doesn't work in Github Actions.")
def test_fetch_address_success_real():
# Realizamos a consulta de CEP

address = get_address_from_cep("37.503-130", webservice=WebService.OPENCEP)

assert address["district"] == "Santo Antônio"
Expand All @@ -33,7 +35,6 @@ def test_fetch_address_success(requests_mock):

requests_mock.get("https://opencep.com/v1/37503130", text=req_mock_text, status_code=200)

# Realizamos a consulta de CEP
address = get_address_from_cep("37.503-130", webservice=WebService.OPENCEP, timeout=5)

assert address["district"] == "Santo Antônio"
Expand All @@ -58,10 +59,7 @@ def test_fetch_address_success(requests_mock):

proxies = {"https": "00.00.000.000", "http": "00.00.000.000"}

# Realizamos a consulta de CEP
address = get_address_from_cep(
"99999-999", webservice=WebService.OPENCEP, timeout=6, proxies=proxies
)
address = get_address_from_cep("99999-999", webservice=WebService.OPENCEP, timeout=6, proxies=proxies)

assert address["district"] == "Jardim Centro Cívico"
assert address["cep"] == "99999-999"
Expand All @@ -75,7 +73,6 @@ def test_fetch_address_cep_not_found(requests_mock):

requests_mock.get("https://opencep.com/v1/00000000", status_code=404)

# Realizamos a consulta de CEP
with pytest.raises(exceptions.CEPNotFound):
get_address_from_cep("00000-000", webservice=WebService.OPENCEP)

Expand Down
8 changes: 4 additions & 4 deletions tests/test_viacep.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

logger = logging.getLogger(__name__)

IN_GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS") == "true"
IN_GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS", False)
SKIP_REAL_TEST = os.getenv("SKIP_REAL_TEST", False)


@pytest.mark.skipif(SKIP_REAL_TEST, reason="Skip real teste API.")
@pytest.mark.skipif(IN_GITHUB_ACTIONS, reason="Test doesn't work in Github Actions.")
def test_get_address_from_cep_success_real():
# Realizamos a consulta de CEP

try:
address = get_address_from_cep("37.503-130", webservice=WebService.VIACEP)

Expand Down Expand Up @@ -47,7 +49,6 @@ def test_get_address_from_cep_success(requests_mock):

proxies = {"https": "00.00.000.000", "http": "00.00.000.000"}

# Realizamos a consulta de CEP
address = get_address_from_cep("37.503-130", webservice=WebService.VIACEP, timeout=10, proxies=proxies)

assert address["district"] == "Santo Antônio"
Expand Down Expand Up @@ -78,7 +79,6 @@ def test_get_address_from_cep_not_found(requests_mock):
def test_get_address_invalid_cep(requests_mock):
requests_mock.get("http://www.viacep.com.br/ws/3750313/json", status_code=400)

# Realizamos a consulta de CEP
with pytest.raises(exceptions.InvalidCEP):
get_address_from_cep("37503-13", webservice=WebService.VIACEP)

Expand Down

0 comments on commit 373c3cc

Please sign in to comment.