Skip to content

Commit

Permalink
Simplify new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zeezdev committed Oct 24, 2023
1 parent 51f4e56 commit ea1144d
Showing 1 changed file with 15 additions and 68 deletions.
83 changes: 15 additions & 68 deletions responses/tests/test_responses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import inspect
import json
import os
import re
import warnings
Expand Down Expand Up @@ -2036,83 +2035,31 @@ def run():
assert_reset()


def test_response_and_requests_mock_calls_are_equal():
def test_response_calls_and_registry_calls_are_equal():
@responses.activate
def run():
rsp = responses.add(responses.GET, "http://www.example.com")
rsp1 = responses.add(responses.GET, "http://www.example.com")
rsp2 = responses.add(responses.GET, "http://www.example.com/1")
rsp3 = responses.add(
responses.GET, "http://www.example.com/2"
) # won't be requested

requests.get("http://www.example.com")
requests.get("http://www.example.com/1")

assert len(responses.calls) == 2
assert rsp.call_count == 1
assert rsp.calls[0] is responses.calls[0]
assert rsp2.call_count == 1
assert rsp2.calls[0] is responses.calls[1]

run()
assert_reset()


def test_response_call_request():
@responses.activate
def run():
rsp1 = responses.add(responses.GET, "http://www.example.com")
rsp2 = responses.add(
responses.PUT, "http://www.foo.bar/42/", json={"id": 42, "name": "Bazz"}
)
request_payload = {"name": "Bazz"}

requests.get("http://www.example.com")
requests.get("http://www.example.com?hello=world")
requests.put(
"http://www.foo.bar/42/",
json=request_payload,
)

assert rsp1.call_count == 2
rsp1_request1 = rsp1.calls[0].request
assert rsp1_request1.url == "http://www.example.com/"
assert rsp1_request1.method == "GET"
rsp1_request2 = rsp1.calls[1].request
assert rsp1_request2.url == "http://www.example.com/?hello=world"
assert rsp1_request2.method == "GET"
assert rsp2.call_count == 1
rsp2_request1 = rsp2.calls[0].request
assert rsp2_request1.url == "http://www.foo.bar/42/"
assert rsp2_request1.method == "PUT"
assert json.loads(rsp2_request1.body) == request_payload

run()
assert_reset()


def test_response_call_response():
@responses.activate
def run():
rsp1 = responses.add(responses.GET, "http://www.example.com", body=b"test")
rsp2 = responses.add(
responses.POST,
"http://www.foo.bar/42/",
json={"id": 42, "name": "Bazz"},
status=201,
)

requests.get("http://www.example.com")
requests.post(
"http://www.foo.bar/42/",
json={"name": "Bazz"},
assert len(responses.calls) == len(rsp1.calls) + len(rsp2.calls) + len(
rsp3.calls
)

assert rsp1.call_count == 1
rsp1_response = rsp1.calls[0].response
assert rsp1_response.content == b"test"
assert rsp1_response.status_code == 200
assert rsp1.call_count == 2
assert len(rsp1.calls) == 2
assert rsp1.calls[0] is responses.calls[0]
assert rsp1.calls[1] is responses.calls[2]
assert rsp2.call_count == 1
rsp2_response = rsp2.calls[0].response
assert rsp2_response.json() == {"id": 42, "name": "Bazz"}
assert rsp2_response.status_code == 201
assert len(rsp2.calls) == 1
assert rsp2.calls[0] is responses.calls[1]
assert rsp3.call_count == 0
assert len(rsp3.calls) == 0

run()
assert_reset()
Expand Down

0 comments on commit ea1144d

Please sign in to comment.