Ejemplo n.º 1
0
def test_has_id_tag():
    # Given
    should_match = has_id_tag("fish", has_class("banana"))
    should_not_match_1 = has_id_tag("fish", has_class("foo"))
    should_not_match_2 = has_id_tag("grrgug", anything())

    assert_that(HTML, should_match)
    assert_that(HTML, not_(should_not_match_1))
    assert_that(HTML, not_(should_not_match_2))
    assert_that(
        should_match,
        has_string(
            matches_regexp(
                r"HTML with tag id=['<]fish['>] matching tag with class matching 'banana'"
            )),
    )
    assert_that(
        should_not_match_1,
        mismatches_with(
            HTML,
            matches_regexp(
                r"""got HTML with tag id=['<]fish['>] values """
                r"""\['<div class="banana grapes" id="fish"><p>Some text.</p></div>'\]"""
            ),
        ),
    )
    assert_that(
        should_not_match_2,
        mismatches_with(
            HTML,
            matches_regexp(
                r"got HTML with tag id=['<]grrgug['>] values \[\]")),
    )
Ejemplo n.º 2
0
def test_call_has_positional_arg():
    # Given
    m = mock.MagicMock()

    # When
    m("first", "second", "third")
    call = m.mock_calls[0]

    # Then
    assert_that(call, call_has_arg(1, "second"))
    assert_that(call, call_has_arg(2, "third"))
    assert_that(call, not_(call_has_arg(4, "nope")))
    assert_that(call, call_has_arg(1, contains_string("eco")))
    assert_that(
        call_has_arg(1, contains_string("eco")),
        has_string(
            "mock.call with argument index <1> matching a string containing 'eco'"
        ),
    )
    assert_that(
        call_has_arg(1, "fifth"),
        mismatches_with(
            call, "got mock.call with argument index <1> with value 'second'"),
    )
    assert_that(
        call_has_arg(4, "nope"),
        mismatches_with(call, "got mock.call with without argument index <4>"),
    )
Ejemplo n.º 3
0
def test_call_has_keyword_arg():
    # Given
    m = mock.MagicMock()

    # When
    m(f="first", s="second", t="third")
    call = m.mock_calls[0]

    # Then
    assert_that(call, call_has_arg("s", "second"))
    assert_that(call, not_(call_has_arg("s", "nope")))
    assert_that(call, not_(call_has_arg("w", "nope")))
    assert_that(call, call_has_arg("s", contains_string("eco")))
    assert_that(
        call_has_arg("s", contains_string("eco")),
        has_string(
            "mock.call with keyword argument 's' matching a string containing 'eco'"
        ),
    )
    assert_that(
        call_has_arg("s", "fifth"),
        mismatches_with(
            call,
            "got mock.call with keyword argument 's' with value 'second'"),
    )
    assert_that(
        call_has_arg("n", "nope"),
        mismatches_with(call,
                        "got mock.call with without keyword argument 'n'"),
    )
Ejemplo n.º 4
0
def test_has_rows(db):
    assert_that(
        db,
        has_table_with_rows(
            "sausages",
            contains_inanyorder(
                has_properties(kind="cumberland"),
                has_properties(kind="lincolnshire"),
                has_properties(kind="vegetarian"),
            ),
        ),
    )
    assert_that(
        db,
        not_(
            has_table_with_rows("sausages",
                                has_item(has_properties(kind="vegan")))))
    assert_that(
        db,
        not_(
            has_table_with_rows(
                "bacon",
                contains_exactly(has_properties(kind="smoked"),
                                 has_properties(kind="unsmoked")),
            )),
    )
    assert_that(
        has_table_with_rows(
            "sausages",
            contains_exactly(has_properties(kind="cumberland"),
                             has_properties(kind="lincolnshire")),
        ),
        has_string(
            matches_regexp(
                r"DB connection with table 'sausages' with rows matching a sequence containing \["
                r"\(?an object with a property 'kind' matching 'cumberland'\)?, "
                r"\(?an object with a property 'kind' matching 'lincolnshire'\)?"
                r"\]")),
    )
    assert_that(
        has_table_with_rows("sausages",
                            has_item(has_properties(kind="vegan"))),
        mismatches_with(
            db,
            all_of(contains_string("was <["),
                   contains_string("RowTuple(kind='vegetarian', rating=0)")),
        ),
    )
    assert_that(
        has_table_with_rows(
            "bacon",
            contains_exactly(has_properties(kind="smoked"),
                             has_properties(kind="unsmoked")),
        ),
        mismatches_with(
            db,
            "SQL statement 'SELECT * FROM bacon;' gives 'OperationalError' <no such table: bacon>",
        ),
    )
Ejemplo n.º 5
0
def test_url_with_host():
    should_match = is_url().with_host("brunni.ng")
    should_not_match = is_url().with_host("example.com")

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

    assert_that(should_match, has_string("URL with host: 'brunni.ng'"))
    assert_that(should_not_match,
                mismatches_with(URL, "was URL with host: was 'brunni.ng'"))
    assert_that(should_not_match,
                mismatches_with(URL, "was URL with host: was 'brunni.ng'"))
Ejemplo n.º 6
0
def test_matcher_mismatches_with():
    # Given
    banana_matcher = contains_string("Banana")

    # When

    # Then
    assert_that(banana_matcher, mismatches("Apple"))
    assert_that(banana_matcher, mismatches_with("Apple", "was 'Apple'"))
    assert_that(
        mismatches_with("Apple", "was 'Apple'"),
        has_string("a matcher which mismatches the value 'Apple'\ngiving message \"was 'Apple'\""),
    )
Ejemplo n.º 7
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'}>"),
    )
Ejemplo n.º 8
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']>"),
    )
Ejemplo n.º 9
0
def test_request_matcher():
    # Given
    server = MagicMock()
    request = HttpRequestBuilder().with_path("/test").and_method("GET").build()
    server.get_actual_requests.return_value = [request, request]

    # When

    # Then
    assert_that(server, had_request().with_path("/test").and_method("GET"))
    assert_that(server, had_request().with_times(2).and_path("/test").and_method("GET"))
    assert_that(server, not_(had_request().with_path("/somewhereelse").and_method("GET")))
    assert_that(
        had_request().with_path("/sausages").and_method("PUT"),
        has_string("call with method: 'PUT' path: '/sausages'"),
    )
    assert_that(
        had_request().with_times(4).and_query(has_entries(a="b")).and_body("chips"),
        has_string(
            "<4> call(s) with query parameters: a dictionary containing {'a': 'b'} body: 'chips'"
        ),
    )
    assert_that(
        had_request().with_path("/sausages").and_method("PUT").and_times(99),
        mismatches_with(
            server,
            all_of(
                contains_string(
                    "found <0> matching requests: <[]>. All requests: <[mbtest.imposters.imposters.HttpRequest"
                ),
                contains_string("path='/test'"),
                contains_string("method='GET'"),
            ),
        ),
    )
Ejemplo n.º 10
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>")),
    )
Ejemplo n.º 11
0
def test_request_matcher():
    # Given
    server = MagicMock()
    request = {"path": "/test", "method": "GET"}
    server.get_actual_requests.return_value = {"someport": [request, request]}

    # When

    # Then
    assert_that(server, had_request(path="/test", method="GET"))
    assert_that(server, had_request(path="/test", method="GET", times=2))
    assert_that(server, not_(had_request(path="/somewhereelse", method="GET")))
    assert_that(
        had_request(path="/sausages", method="PUT"),
        has_string("call with method: 'PUT' path: '/sausages'"),
    )
    assert_that(
        had_request(path="/sausages", times=4), has_string("<4> call(s) with path: '/sausages'")
    )
    assert_that(
        had_request(path="/sausages", method="PUT"),
        mismatches_with(
            server,
            all_of(
                contains_string("found <0> matching requests: <[]>. All requests: <[{"),
                contains_string("'path': '/test'"),
                contains_string("'method': 'GET'"),
            ),
        ),
    )
Ejemplo n.º 12
0
def test_identical_properties():
    # Given
    class SomeClass(ReprFromDict):
        def __init__(self, a, b):
            self.a = a
            self.b = b

    class OtherClass(ReprFromDict):
        def __init__(self, a, b):
            self.a = a
            self._b = b

        @property
        def b(self):
            return self._b

    class YetAnotherClass(ReprFromDict):
        def __init__(self, a, b):
            self.a = a
            self.b = b

    a = SomeClass(1, 2)
    b = OtherClass(1, 2)
    c = YetAnotherClass(1, 3)

    # Then
    assert_that(a, has_identical_properties_to(b))
    assert_that(a, not_(has_identical_properties_to(c)))
    assert_that(
        has_identical_properties_to(a),
        has_string(f"object with identical properties to object <{a}>"),
    )
    assert_that(has_identical_properties_to(a),
                mismatches_with(c, f"was <{c}>"))
Ejemplo n.º 13
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'>")),
    )
Ejemplo n.º 14
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>")),
    )
Ejemplo n.º 15
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'}")),
    )
Ejemplo n.º 16
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'}>")),
    )
Ejemplo n.º 17
0
def test_nested_identical_properties():
    # Given
    class SomeClass(object):
        def __init__(self, a, b, c):
            self.a = a
            self.b = b
            self._c = c

        @property
        def c(self):
            return self._c

        def some_method(self):
            pass

    a = SomeClass(1, SomeClass(2, 3, 4), 4)
    b = SomeClass(1, SomeClass(2, 3, 4), 4)
    c = SomeClass(1, SomeClass(2, 4, 5), 6)

    # Then
    assert_that(a, has_identical_properties_to(b))
    assert_that(a, not_(has_identical_properties_to(c)))
    assert_that(
        has_identical_properties_to(a),
        has_string(f"object with identical properties to object {a}"),
    )
    assert_that(has_identical_properties_to(a), mismatches_with(c, f"was {c}"))
Ejemplo n.º 18
0
def test_has_row():
    # Given
    soup = BeautifulSoup(HTML, "html.parser")
    table = soup.table
    should_match = has_row(
        index_matches=2,
        row_matches=has_class("bazz"),
        cells_match=contains_exactly(tag_has_string("fizz"),
                                     tag_has_string("buzz")),
    )
    should_not_match_1 = has_row(
        index_matches=2,
        row_matches=has_class("bazz"),
        cells_match=contains_exactly(tag_has_string("egg"),
                                     tag_has_string("chips")),
    )
    should_not_match_2 = has_row(
        index_matches=3,
        row_matches=has_class("bazz"),
        cells_match=contains_exactly(tag_has_string("fizz"),
                                     tag_has_string("buzz")),
    )
    should_not_match_3 = has_row(
        index_matches=2,
        row_matches=has_class("eden"),
        cells_match=contains_exactly(tag_has_string("fizz"),
                                     tag_has_string("buzz")),
    )

    # Then
    assert_that(table, should_match)
    assert_that(table, not_(should_not_match_1))
    assert_that(table, not_(should_not_match_2))
    assert_that(table, not_(should_not_match_3))
    assert_that(
        should_match,
        has_string(
            "table with row "
            "cells matching a sequence containing [tag with string matching 'fizz', tag with string matching 'buzz'] "
            "row matching tag with class matching 'bazz' "
            "index matching <2>"),
    )
    assert_that(
        has_row(row_matches=has_class("banana")),
        has_string(
            "table with row row matching tag with class matching 'banana'"),
    )
    assert_that(
        should_not_match_1,
        mismatches_with(
            table,
            all_of(
                starts_with(f"was {table}"),
                contains_string("found rows:"),
                contains_string("<tr><td>baz</td><td>qux</td></tr>"),
            ),
        ),
    )
Ejemplo n.º 19
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'"
            ),
        ),
    )
Ejemplo n.º 20
0
def test_contains_bytestring():
    should_match = contains_bytestring(b"foo")
    should_not_match = contains_bytestring(b"bar")

    assert_that(b"a foo b", should_match)
    assert_that(b"a foo b", not_(should_not_match))

    assert_that(should_match, has_string("bytestring containing <b'foo'>"))
    assert_that(should_not_match, mismatches_with(b" a foo b", "was <b' a foo b'>"))
Ejemplo n.º 21
0
def test_is_weekday():
    assert_that(datetime.date(1968, 7, 19), is_weekday())
    assert_that(datetime.date(1968, 7, 21), not_(is_weekday()))

    assert_that(is_weekday(), has_string("A weekday"))
    assert_that(
        is_weekday(),
        mismatches_with(datetime.date(1968, 7, 21),
                        "was <1968-07-21> with weekday <6>, a Sunday"),
    )
Ejemplo n.º 22
0
def test_json_matching():
    # Given
    j = json.dumps([1, 2, 3])

    # When

    # Then
    assert_that(j, json_matching([1, 2, 3]))
    assert_that(j, json_matching(contains_exactly(1, 2, 3)))
    assert_that(j, not_(json_matching([1, 2, 5])))
    assert_that(json_matching([1, 2, 3]),
                has_string("JSON structure matching <[1, 2, 3]>"))
    assert_that(
        json_matching([]),
        mismatches_with(
            "WTF is this?",
            matches_regexp(r"Got invalid JSON ['<]WTF is this\?['>]")),
    )
    assert_that(json_matching([]), mismatches_with("[1]", "was <[1]>"))
Ejemplo n.º 23
0
def test_HasWeekday():
    assert_that(datetime.date(1968, 7, 21), HasWeekday(6))
    assert_that(datetime.date(1968, 7, 21), not_(HasWeekday(2)))

    assert_that(HasWeekday(2), has_string("Date with weekday matching <2>"))
    assert_that(
        HasWeekday(2),
        mismatches_with(datetime.date(1968, 7, 21),
                        "was <1968-07-21> with weekday <6>, a Sunday"),
    )
Ejemplo n.º 24
0
def test_mdash():
    assert_that(HTML, has_named_tag("h2", "what is — this"))
    assert_that(
        has_named_tag("h2", "what is this"),
        mismatches_with(
            HTML,
            matches_regexp(
                r"got HTML with tag name=['<]h2['>] values \['<h2>what is (\\u2014|—) this</h2>'\]"
            ),
        ),
    )
Ejemplo n.º 25
0
def test_has_table(db):
    assert_that(db, has_table("sausages"))
    assert_that(db, not_(has_table("bacon")))
    assert_that(has_table("sausages"),
                has_string("DB connection has table named 'sausages'"))
    assert_that(
        has_table("bacon"),
        mismatches_with(
            db,
            "SQL statement 'SELECT * FROM bacon;' gives 'OperationalError' <no such table: bacon>",
        ),
    )
Ejemplo n.º 26
0
def test_has_repr():
    # Given
    r = [1, "2"]

    # When

    # Then
    assert_that(r, has_repr(contains_string("[1, '2']")))
    assert_that(r, has_repr(contains_string("[1")))
    assert_that(has_repr("a"),
                has_string("an object with repr() matching 'a'"))
    assert_that(has_repr("a"), mismatches_with("b", "was 'b'"))
Ejemplo n.º 27
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>"))
Ejemplo n.º 28
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>"))
Ejemplo n.º 29
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'"))
Ejemplo n.º 30
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'"))