Пример #1
0
def test_matcher_matches_with():
    # Given
    banana_matcher = contains_string("Banana")

    # When

    # Then

    assert_that(banana_matcher, matches("Banana"))
    assert_that(banana_matcher, matches_with("Banana", "was 'Banana'"))
    assert_that(
        matches_with("Apple", "was 'Apple'"),
        has_string("a matcher which matches the value 'Apple'\ngiving message \"was 'Apple'\""),
    )
Пример #2
0
def test_url_with_query():
    should_match = is_url().with_query(
        has_entries(key1="value1", key2="value2"))
    should_not_match = is_url().with_query(
        has_entries(key1="value1", key2="nope"))

    assert_that(URL, should_match)
    assert_that(URL, not_(should_not_match))

    assert_that(
        should_match,
        has_string(
            "URL with query: a dictionary containing {'key1': 'value1', 'key2': 'value2'}"
        ),
    )
    assert_that(
        should_not_match,
        mismatches_with(URL,
                        "was URL with query: value for 'key2' was 'value2'"))
    assert_that(
        should_match,
        matches_with(
            URL,
            "was URL with query: was <{'key1': 'value1', 'key2': 'value2'}>"),
    )
Пример #3
0
def test_response_matcher_json():
    # Given
    stub_response = MOCK_RESPONSE

    # When

    # Then
    assert_that(stub_response, is_response().with_json({"a": "b"}))
    assert_that(stub_response, not_(is_response().with_json({"a": "c"})))
    assert_that(
        str(is_response().with_json({"a": "b"})),
        contains_string("response with json: <{'a': 'b'}>"),
    )
    assert_that(
        is_response().with_json([1, 2, 4]),
        mismatches_with(
            stub_response,
            contains_string("was response with json: was <{'a': 'b'}>")),
    )
    assert_that(
        is_response().with_json({"a": "b"}),
        matches_with(
            stub_response,
            contains_string("was response with json: was <{'a': 'b'}>")),
    )
Пример #4
0
def test_response_matcher_elapsed():
    # Given
    response = MOCK_RESPONSE

    # When

    # Then
    assert_that(response, is_response().with_elapsed(timedelta(seconds=1)))
    assert_that(response,
                not_(is_response().with_elapsed(timedelta(seconds=60))))
    assert_that(
        str(is_response().with_elapsed(timedelta(seconds=1))),
        contains_string("response with elapsed: <0:00:01>"),
    )
    assert_that(
        is_response().with_elapsed(timedelta(seconds=60)),
        mismatches_with(
            response,
            contains_string("was response with elapsed: was <0:00:01>")),
    )
    assert_that(
        is_response().with_elapsed(timedelta(seconds=1)),
        matches_with(
            response,
            contains_string("was response with elapsed: was <0:00:01>")),
    )
Пример #5
0
def test_url_with_path_segments():
    should_match = is_url().with_path_segments(
        contains_exactly("path1", "path2", "path3"))
    should_not_match = is_url().with_path_segments(empty())

    assert_that(URL, should_match)
    assert_that(URL, not_(should_not_match))

    assert_that(
        should_match,
        has_string(
            "URL with path segments: a sequence containing ['path1', 'path2', 'path3']"
        ),
    )
    assert_that(
        should_not_match,
        mismatches_with(
            URL,
            "was URL with path segments: was <['path1', 'path2', 'path3']>"),
    )
    assert_that(
        should_match,
        matches_with(
            URL,
            "was URL with path segments: was <['path1', 'path2', 'path3']>"),
    )
Пример #6
0
def test_response_matcher_content():
    # Given
    stub_response = MOCK_RESPONSE

    # When

    # Then
    assert_that(stub_response, is_response().with_content(b"content"))
    assert_that(stub_response, not_(is_response().with_content(b"chips")))
    assert_that(
        str(is_response().with_content(b"content")),
        contains_string("response with content: <b'content'>"),
    )
    assert_that(
        is_response().with_content(b"chips"),
        mismatches_with(
            stub_response,
            contains_string("was response with content: was <b'content'>")),
    )
    assert_that(
        is_response().with_content(b"content"),
        matches_with(
            stub_response,
            contains_string("was response with content: was <b'content'>")),
    )
Пример #7
0
def test_response_matcher_url():
    # Given
    response = MOCK_RESPONSE

    # When

    # Then
    assert_that(response, is_response().with_url(is_url().with_path("/path0")))
    assert_that(response,
                not_(is_response().with_url(is_url().with_path("/nope"))))
    assert_that(
        str(is_response().with_url(is_url().with_path("/path0"))),
        contains_string("response with url: URL with path: '/path0'"),
    )
    assert_that(
        is_response().with_url(is_url().with_path("/nope")),
        mismatches_with(
            response,
            contains_string(
                "was response with url: was URL with path: was </path0>")),
    )
    assert_that(
        is_response().with_url(is_url().with_path("/path0")),
        matches_with(
            response,
            contains_string(
                "was response with url: was URL with path: was </path0>")),
    )
Пример #8
0
def test_response_matcher_cookies():
    # Given
    response = MOCK_RESPONSE

    # When

    # Then
    assert_that(response, is_response().with_cookies({"name": "value"}))
    assert_that(response, not_(is_response().with_cookies({"name": "nope"})))
    assert_that(
        str(is_response().with_cookies({"name": "value"})),
        contains_string("response with cookies: <{'name': 'value'}"),
    )
    assert_that(
        is_response().with_cookies({"name": "nope"}),
        mismatches_with(
            response,
            contains_string(
                "was response with cookies: was <{'name': 'value'}")),
    )
    assert_that(
        is_response().with_cookies({"name": "value"}),
        matches_with(
            response,
            contains_string(
                "was response with cookies: was <{'name': 'value'}")),
    )
Пример #9
0
def test_response_matcher_builder():
    # Given
    stub_response = MOCK_RESPONSE
    matcher = (is_response().with_status_code(200).and_body(
        "sausages").and_content(b"content").and_json(has_entries(
            a="b")).and_headers(has_entries(key="value")).and_cookies(
                has_entries(name="value")).and_elapsed(
                    between(timedelta(seconds=1),
                            timedelta(minutes=1))).and_history(
                                contains_exactly(
                                    is_response().with_url(
                                        is_url().with_path("/path1")),
                                    is_response().with_url(
                                        is_url().with_path("/path2")),
                                )).and_url(is_url().with_path(
                                    "/path0")).and_encoding("utf-8"))
    mismatcher = is_response().with_body("kale").and_status_code(404)

    # When

    # Then
    assert_that(stub_response, matcher)
    assert_that(stub_response, not_(mismatcher))
    assert_that(
        matcher,
        has_string(
            "response with "
            "status_code: <200> "
            "body: 'sausages' "
            "content: <b'content'> "
            "json: a dictionary containing {'a': 'b'} "
            "headers: a dictionary containing {'key': 'value'} "
            "cookies: a dictionary containing {'name': 'value'} "
            "elapsed: (a value greater than or equal to <0:00:01> and a value less than or equal to <0:01:00>) "
            "history: a sequence containing "
            "[response with url: URL with path: '/path1', response with url: URL with path: '/path2'] "
            "url: URL with path: '/path0' "
            "encoding: 'utf-8'"),
    )
    assert_that(
        mismatcher,
        mismatches_with(
            stub_response,
            contains_string(
                "was response with status code: was <200> body: was 'sausages'"
            ),
        ),
    )
    assert_that(
        matcher,
        matches_with(
            stub_response,
            contains_string(
                "was response with status code: was <200> body: was 'sausages'"
            ),
        ),
    )
Пример #10
0
def test_url_with_fragment():
    should_match = is_url().with_fragment("fragment")
    should_not_match = is_url().with_fragment("banana")

    assert_that(URL, should_match)
    assert_that(URL, not_(should_not_match))

    assert_that(should_match, has_string("URL with fragment: 'fragment'"))
    assert_that(should_not_match,
                mismatches_with(URL, "was URL with fragment: was <fragment>"))
    assert_that(should_match,
                matches_with(URL, "was URL with fragment: was <fragment>"))
Пример #11
0
def test_url_with_port():
    should_match = is_url().with_port(1234)
    should_not_match = is_url().with_port(5678)

    assert_that(URL, should_match)
    assert_that(URL, not_(should_not_match))

    assert_that(should_match, has_string("URL with port: <1234>"))
    assert_that(should_not_match,
                mismatches_with(URL, "was URL with port: was <1234>"))
    assert_that(should_match, matches_with(URL,
                                           "was URL with port: was <1234>"))
Пример #12
0
def test_url_with_password():
    should_match = is_url().with_password("password")
    should_not_match = is_url().with_password("nope")

    assert_that(URL, should_match)
    assert_that(URL, not_(should_not_match))

    assert_that(should_match, has_string("URL with password: '******'"))
    assert_that(should_not_match,
                mismatches_with(URL, "was URL with password: was 'password'"))
    assert_that(should_match,
                matches_with(URL, "was URL with password: was 'password'"))
Пример #13
0
def test_url_with_username():
    should_match = is_url().with_username("username")
    should_not_match = is_url().with_username("nope")

    assert_that(URL, should_match)
    assert_that(URL, not_(should_not_match))

    assert_that(should_match, has_string("URL with username: '******'"))
    assert_that(should_not_match,
                mismatches_with(URL, "was URL with username: was 'username'"))
    assert_that(should_match,
                matches_with(URL, "was URL with username: was 'username'"))
Пример #14
0
def test_url_with_scheme():
    should_match = is_url().with_scheme("https")
    should_not_match = is_url().and_scheme("http")

    assert_that(URL, should_match)
    assert_that(URL, not_(should_not_match))

    assert_that(should_match, has_string("URL with scheme: 'https'"))
    assert_that(should_not_match,
                mismatches_with(URL, "was URL with scheme: was 'https'"))
    assert_that(should_match,
                matches_with(URL, "was URL with scheme: was 'https'"))
Пример #15
0
def test_url_matcher_builder():
    # Given
    should_match = (is_url().with_scheme("https").and_username(
        "username").and_password("password").and_host("brunni.ng").and_port(
            1234).and_path("/path1/path2/path3").and_path_segments(
                contains_exactly("path1", "path2", "path3")).and_query(
                    has_entries(key1="value1",
                                key2="value2")).and_fragment("fragment"))
    should_not_match = is_url().with_path("woah!").and_host("example.com")

    assert_that(URL, should_match)
    assert_that(URL, not_(should_not_match))

    assert_that(
        should_match,
        has_string(
            "URL with "
            "scheme: 'https' "
            "username: '******' "
            "password: '******' "
            "host: 'brunni.ng' "
            "port: <1234> "
            "path: '/path1/path2/path3' "
            "path segments: a sequence containing ['path1', 'path2', 'path3'] "
            "query: a dictionary containing {'key1': 'value1', 'key2': 'value2'} "
            "fragment: 'fragment'"),
    )
    assert_that(
        should_not_match,
        mismatches_with(
            URL,
            "was URL with host: was 'brunni.ng' path: was </path1/path2/path3>"
        ),
    )
    assert_that(
        should_match,
        matches_with(
            URL,
            "was URL with scheme: was 'https' "
            "username: was 'username' "
            "password: was 'password' "
            "host: was 'brunni.ng' "
            "port: was <1234> "
            "path: was </path1/path2/path3> "
            "path segments: was <['path1', 'path2', 'path3']> "
            "query: was <{'key1': 'value1', 'key2': 'value2'}> "
            "fragment: was <fragment>",
        ),
    )
Пример #16
0
def test_url_with_path():
    should_match = is_url().with_path("/path1/path2/path3")
    should_not_match = is_url().with_path("/banana")

    assert_that(URL, should_match)
    assert_that(URL, not_(should_not_match))

    assert_that(should_match,
                has_string("URL with path: '/path1/path2/path3'"))
    assert_that(
        should_not_match,
        mismatches_with(URL, "was URL with path: was </path1/path2/path3>"))
    assert_that(
        should_match,
        matches_with(URL, "was URL with path: was </path1/path2/path3>"))
Пример #17
0
def test_email_matcher():
    # Given
    message = (EmailMessageBuilder().with_to(
        "*****@*****.**", "simon").and_from(
            "*****@*****.**",
            "fred").and_subject("chips").and_body_text("bananas").as_string())

    # When

    # Then
    assert_that(message, is_email().with_to_name("simon"))
    assert_that(
        message,
        not_(is_email().with_to_address("*****@*****.**").and_to_name(
            "Banana")))
    assert_that(
        is_email().with_to_name("Jenny").and_to_address(
            "*****@*****.**").and_from_name("Fred").and_from_address(
                "*****@*****.**").and_subject("Foo").and_body_text("Bar"),
        has_string("email with to_name: 'Jenny' "
                   "to_address: '*****@*****.**' "
                   "from_name: 'Fred' "
                   "from_address: '*****@*****.**' "
                   "subject: 'Foo' "
                   "body_text: 'Bar'"),
    )
    assert_that(
        is_email().with_to_address("*****@*****.**").and_to_name("Banana"),
        mismatches_with(
            message,
            "was email with to_name: was 'simon' to_address: was '*****@*****.**'"
        ),
    )
    assert_that(
        is_email().with_to_address("*****@*****.**").and_to_name("simon"),
        matches_with(
            message,
            "was email with to_name: was 'simon' to_address: was '*****@*****.**'"
        ),
    )
Пример #18
0
def test_response_matcher_status_code():
    # Given
    stub_response = MOCK_RESPONSE

    # When

    # Then
    assert_that(stub_response, is_response().with_status_code(200))
    assert_that(stub_response, not_(is_response().with_status_code(201)))
    assert_that(is_response().with_status_code(200),
                has_string("response with status_code: <200>"))
    assert_that(
        is_response().with_status_code(201),
        mismatches_with(
            stub_response,
            contains_string("was response with status code: was <200>")),
    )
    assert_that(
        is_response().with_status_code(200),
        matches_with(stub_response,
                     "was response with status code: was <200>"),
    )
Пример #19
0
def test_response_matcher_body():
    # Given
    stub_response = MOCK_RESPONSE

    # When

    # Then
    assert_that(stub_response, is_response().with_body("sausages"))
    assert_that(stub_response, not_(is_response().with_body("chips")))
    assert_that(is_response().with_body("chips"),
                has_string("response with body: 'chips'"))
    assert_that(
        is_response().with_body("chips"),
        mismatches_with(
            stub_response,
            contains_string("was response with body: was 'sausages'")),
    )
    assert_that(
        is_response().with_body("sausages"),
        matches_with(
            stub_response,
            contains_string("was response with body: was 'sausages'")),
    )
Пример #20
0
def test_response_matcher_encoding():
    # Given
    stub_response = MOCK_RESPONSE

    # When

    # Then
    assert_that(stub_response, is_response().with_encoding("utf-8"))
    assert_that(stub_response, not_(is_response().with_encoding("ISO-8859-1")))
    assert_that(is_response().with_encoding("utf-8"),
                has_string("response with encoding: 'utf-8'"))
    assert_that(
        is_response().with_encoding("ISO-8859-1"),
        mismatches_with(
            stub_response,
            contains_string("was response with encoding: was 'utf-8'")),
    )
    assert_that(
        is_response().with_encoding("utf-8"),
        matches_with(
            stub_response,
            contains_string("was response with encoding: was 'utf-8'")),
    )