Ejemplo n.º 1
0
 def warning_if_unsupported_image_type(self, context):
     drawing_element = _create_inline_image(
         blip=_embedded_blip("rId5"),
         description="It's a hat",
     )
     
     image_bytes = b"Not an image at all!"
     
     relationships = Relationships({
         "rId5": Relationship(target="media/hat.emf")
     })
     
     docx_file = context.mock()
     funk.allows(docx_file).open("word/media/hat.emf").returns(io.BytesIO(image_bytes))
     
     content_types = context.mock()
     funk.allows(content_types).find_content_type("word/media/hat.emf").returns("image/x-emf")
     
     result = _read_document_xml_element(
         drawing_element,
         content_types=content_types,
         relationships=relationships,
         docx_file=docx_file,
     )
     assert_equal("image/x-emf", result.value[0].content_type)
     expected_warning = results.warning("Image of type image/x-emf is unlikely to display in web browsers")
     assert_equal([expected_warning], result.messages)
    def warning_if_unsupported_image_type(self, context):
        drawing_element = _create_inline_image(
            relationship_id="rId5",
            description="It's a hat",
        )

        image_bytes = b"Not an image at all!"

        relationships = Relationships(
            {"rId5": Relationship(target="media/hat.emf")})

        docx_file = context.mock()
        funk.allows(docx_file).open("word/media/hat.emf").returns(
            io.BytesIO(image_bytes))

        content_types = context.mock()
        funk.allows(content_types).find_content_type(
            "word/media/hat.emf").returns("image/x-emf")

        result = read_document_xml_element(
            drawing_element,
            content_types=content_types,
            relationships=relationships,
            docx_file=docx_file,
        )
        assert_equal("image/x-emf", result.value[0].content_type)
        expected_warning = results.warning(
            "Image of type image/x-emf is unlikely to display in web browsers")
        assert_equal([expected_warning], result.messages)
Ejemplo n.º 3
0
def test_using_allows_in_a_sequence_allows_it_to_be_called_no_times(mocks):
    file = mocks.mock()
    ordering = mocks.sequence()
    allows(file).write.in_sequence(ordering)
    expects(file).close().in_sequence(ordering)
    
    file.close()
Ejemplo n.º 4
0
 def can_read_anchored_pictures(self, context):
     drawing_element = _create_anchored_image(
         relationship_id="rId5",
         description="It's a hat",
     )
     
     image_bytes = b"Not an image at all!"
     
     relationships = Relationships({
         "rId5": Relationship(target="media/hat.png")
     })
     
     docx_file = context.mock()
     funk.allows(docx_file).open("word/media/hat.png").returns(io.BytesIO(image_bytes))
     
     content_types = context.mock()
     funk.allows(content_types).find_content_type("word/media/hat.png").returns("image/png")
     
     image = _read_and_get_document_xml_element(
         drawing_element,
         content_types=content_types,
         relationships=relationships,
         docx_file=docx_file,
     )[0]
     assert_equal(documents.Image, type(image))
     assert_equal("It's a hat", image.alt_text)
     assert_equal("image/png", image.content_type)
     with image.open() as image_file:
         assert_equal(image_bytes, image_file.read())
Ejemplo n.º 5
0
    def warning_if_unsupported_image_type(self, mocks):
        drawing_element = _create_inline_image(
            blip=_embedded_blip("rId5"),
            description="It's a hat",
        )

        relationships = Relationships([
            _image_relationship("rId5", "media/hat.emf"),
        ])

        docx_file = mocks.mock()
        funk.allows(docx_file).open("word/media/hat.emf").returns(io.BytesIO(self.IMAGE_BYTES))

        content_types = mocks.mock()
        funk.allows(content_types).find_content_type("word/media/hat.emf").returns("image/x-emf")

        result = _read_document_xml_element(
            drawing_element,
            content_types=content_types,
            relationships=relationships,
            docx_file=docx_file,
        )
        assert_equal("image/x-emf", result.value.content_type)
        expected_warning = results.warning("Image of type image/x-emf is unlikely to display in web browsers")
        assert_equal([expected_warning], result.messages)
Ejemplo n.º 6
0
def test_unexpected_invocations_display_method_name_and_parameters(mocks):
    class Database(object):
        def save(self):
            pass
            
    mock = mocks.mock(Database)
    allows(mock).save.with_args()
    
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: database.save('positional')
The following expectations on database.save did not match:
    database.save() [wrong number of positional arguments]""",
                      lambda: mock.save("positional"))
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: database.save(key='word')
The following expectations on database.save did not match:
    database.save() [unexpected keyword arguments: key]""",
                      lambda: mock.save(key="word"))
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: database.save('one', 'two', foo='bar', key='word')
The following expectations on database.save did not match:
    database.save() [wrong number of positional arguments]""",
                      lambda: mock.save("one", "two", key="word", foo="bar"))
    
    mock = mocks.mock(Database)
    allows(mock).save.with_args(1)
    
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: database.save()
The following expectations on database.save did not match:
    database.save(1) [wrong number of positional arguments]""",
                      lambda: mock.save())
Ejemplo n.º 7
0
 def can_read_anchored_pictures(self, mocks):
     drawing_element = _create_anchored_image(
         blip=_embedded_blip("rId5"),
         description="It's a hat",
     )
     
     image_bytes = b"Not an image at all!"
     
     relationships = Relationships({
         "rId5": Relationship(target="media/hat.png")
     })
     
     docx_file = mocks.mock()
     funk.allows(docx_file).open("word/media/hat.png").returns(io.BytesIO(image_bytes))
     
     content_types = mocks.mock()
     funk.allows(content_types).find_content_type("word/media/hat.png").returns("image/png")
     
     image = _read_and_get_document_xml_element(
         drawing_element,
         content_types=content_types,
         relationships=relationships,
         docx_file=docx_file,
     )
     assert_equal(documents.Image, type(image))
     assert_equal("It's a hat", image.alt_text)
     assert_equal("image/png", image.content_type)
     with image.open() as image_file:
         assert_equal(image_bytes, image_file.read())
Ejemplo n.º 8
0
 def can_read_imagedata_elements_with_rid_attribute(self, context):
     imagedata_element = xml_element("v:imagedata", {"r:id": "rId5", "o:title": "It's a hat"})
     
     image_bytes = b"Not an image at all!"
     
     relationships = Relationships({
         "rId5": Relationship(target="media/hat.png")
     })
     
     docx_file = context.mock()
     funk.allows(docx_file).open("word/media/hat.png").returns(io.BytesIO(image_bytes))
     
     content_types = context.mock()
     funk.allows(content_types).find_content_type("word/media/hat.png").returns("image/png")
     
     image = _read_and_get_document_xml_element(
         imagedata_element,
         content_types=content_types,
         relationships=relationships,
         docx_file=docx_file,
     )
     assert_equal(documents.Image, type(image))
     assert_equal("It's a hat", image.alt_text)
     assert_equal("image/png", image.content_type)
     with image.open() as image_file:
         assert_equal(image_bytes, image_file.read())
Ejemplo n.º 9
0
 def can_read_imagedata_elements_with_rid_attribute(self, mocks):
     imagedata_element = xml_element("v:imagedata", {"r:id": "rId5", "o:title": "It's a hat"})
     
     image_bytes = b"Not an image at all!"
     
     relationships = Relationships({
         "rId5": Relationship(target="media/hat.png")
     })
     
     docx_file = mocks.mock()
     funk.allows(docx_file).open("word/media/hat.png").returns(io.BytesIO(image_bytes))
     
     content_types = mocks.mock()
     funk.allows(content_types).find_content_type("word/media/hat.png").returns("image/png")
     
     image = _read_and_get_document_xml_element(
         imagedata_element,
         content_types=content_types,
         relationships=relationships,
         docx_file=docx_file,
     )
     assert_equal(documents.Image, type(image))
     assert_equal("It's a hat", image.alt_text)
     assert_equal("image/png", image.content_type)
     with image.open() as image_file:
         assert_equal(image_bytes, image_file.read())
Ejemplo n.º 10
0
 def can_read_linked_pictures(self, context):
     drawing_element = _create_inline_image(
         blip=_linked_blip("rId5"),
         description="It's a hat",
     )
     
     image_bytes = b"Not an image at all!"
     
     relationships = Relationships({
         "rId5": Relationship(target="file:///media/hat.png")
     })
     
     files = context.mock()
     funk.allows(files).open("file:///media/hat.png").returns(io.BytesIO(image_bytes))
     
     content_types = context.mock()
     funk.allows(content_types).find_content_type("file:///media/hat.png").returns("image/png")
     
     image = _read_and_get_document_xml_element(
         drawing_element,
         content_types=content_types,
         relationships=relationships,
         files=files,
     )[0]
     assert_equal(documents.Image, type(image))
     assert_equal("It's a hat", image.alt_text)
     assert_equal("image/png", image.content_type)
     with image.open() as image_file:
         assert_equal(image_bytes, image_file.read())
Ejemplo n.º 11
0
def test_can_use_allows_to_allow_call_with_the_same_syntax_that_it_will_be_called_with(mocks):
    to_save = "Let's go!"
    return_value = "Yippee!"
    database = mocks.mock()
    allows(database).save(to_save).returns(return_value)
    assert_raises(UnexpectedInvocationError, lambda: database.save())
    assert database.save(to_save) is return_value
    assert database.save(to_save) is return_value
Ejemplo n.º 12
0
def test_can_allow_call_without_specifying_arguments_with_the_same_syntax_that_it_will_be_called_with(mocks):
    to_save = "Let's go!"
    return_value = "Yippee!"
    database = mocks.mock()
    allows(database).save.returns(return_value)
    assert database.save(to_save) is return_value
    assert database.save(to_save) is return_value
    assert database.save() is return_value
Ejemplo n.º 13
0
def _reader(dataset_file, **kwargs):
    context = funk.Context()
    requests = context.mock()
    response = context.mock()
    funk.allows(requests).get("http://stats.oecd.org/RestSDMX/sdmx.ashx/GetKeyFamily/MON2012TSE_O/OECD/?resolveRef=true").returns(response)
    funk.allows(response).iter_content(16 * 1024).returns(_dsd_chunks())
    
    return sdmx.compact_data_message_reader(fileobj=dataset_file, requests=requests, **kwargs)
Ejemplo n.º 14
0
def punctuation_is_not_tokenised(context):
    source_string = "one. two"
    
    random = context.mock()
    funk.allows(random).sample(["one", "two"], 1).returns(["two"])
    
    bucket_generator = buckets.BucketGenerator(random=random)
    bucket = bucket_generator.generate_bucket_from_source_string(source_string, size=1)
    assert_equal({"two": 1}, bucket)
Ejemplo n.º 15
0
def bucket_can_have_same_word_multiple_times(context):
    source_string = "one two two"
    
    random = context.mock()
    funk.allows(random).sample(["one", "two", "two"], 2).returns(["two", "two"])
    
    bucket_generator = buckets.BucketGenerator(random=random)
    bucket = bucket_generator.generate_bucket_from_source_string(source_string, size=2)
    assert_equal({"two": 2}, bucket)
Ejemplo n.º 16
0
def non_english_words_are_not_included_in_bucket(context):
    source_string = "garble grijasf"
    
    random = context.mock()
    funk.allows(random).sample(["garble"], 1).returns(["garble"])
    
    bucket_generator = buckets.BucketGenerator(random=random)
    bucket = bucket_generator.generate_bucket_from_source_string(source_string, size=1)
    assert_equal({"garble": 1}, bucket)
Ejemplo n.º 17
0
def bucket_is_generated_using_random_selection_from_source(context):
    source_string = "one two three"
    
    random = context.mock()
    funk.allows(random).sample(["one", "two", "three"], 2).returns(["one", "three"])
    
    bucket_generator = buckets.BucketGenerator(random=random)
    bucket = bucket_generator.generate_bucket_from_source_string(source_string, size=2)
    assert_equal({"one": 1, "three": 1}, bucket)
Ejemplo n.º 18
0
def test_allowing_a_method_without_specifying_arguments_allows_method_to_be_called_any_times_with_any_arguments(mocks):
    return_value = "foo"
    
    mock = mocks.mock()
    allows(mock).save.returns(return_value)
    
    assert mock.save() is return_value
    assert mock.save(1, 2) is return_value
    assert mock.save() is return_value
    assert mock.save(name="Bob") is return_value
Ejemplo n.º 19
0
def test_argument_mismatches_are_show_on_separate_lines(mocks):
    mock = mocks.mock(name="database")
    allows(mock).save("Apples", "Bananas")
    
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: database.save('Apples', 'Peaches')
The following expectations on database.save did not match:
    database.save('Apples' [matched],
                  'Bananas' [got 'Peaches'])""",
                      lambda: mock.save("Apples", "Peaches"))
Ejemplo n.º 20
0
    def _reader(self, dataset_file):
        context = funk.Context()
        requests = context.mock()
        response = context.mock()
        funk.allows(requests).get(
            "http://stats.oecd.org/RestSDMX/sdmx.ashx/GetKeyFamily/MON2012TSE_O/OECD/?resolveRef=true"
        ).returns(response)
        funk.allows(response).iter_content(16 * 1024).returns(_dsd_chunks())

        return self._message_reader(fileobj=dataset_file, requests=requests)
Ejemplo n.º 21
0
def test_calling_allowed_call_in_wrong_place_raises_assertion_error(mocks):
    file = mocks.mock()
    ordering = mocks.sequence()
    allows(file).write.in_sequence(ordering)
    expects(file).close().in_sequence(ordering)
    
    file.close()
    assert_raises_str(AssertionError,
                      'Invocation out of order. Expected no more calls in sequence, but got unnamed.write.',
                      lambda: file.write("Bar"))
Ejemplo n.º 22
0
def test_if_name_is_not_provided_type_is_converted_to_name_if_supplied(mocks):
    class UserRepository(object):
        def fetch_all(self):
            pass
    mock = mocks.mock(UserRepository)
    allows(mock).fetch_all()
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: user_repository.fetch_all(2)
The following expectations on user_repository.fetch_all did not match:
    user_repository.fetch_all() [wrong number of positional arguments]""",
                      lambda: mock.fetch_all(2))
Ejemplo n.º 23
0
def test_can_specify_arguments_using_equality_on_instances_when_using_allows(mocks):
    return_value = "foo"
    
    mock = mocks.mock()
    allows(mock).save.with_args("one", "two", key="word", foo="bar").returns(return_value)
    
    assert mock.save("one", "two", key="word", foo="bar") is return_value
    assert_raises(UnexpectedInvocationError, lambda: mock.save())
    assert_raises(UnexpectedInvocationError, lambda: mock.save("positional"))
    assert_raises(UnexpectedInvocationError, lambda: mock.save(key="word"))
    assert mock.save("one", "two", key="word", foo="bar") is return_value
Ejemplo n.º 24
0
def test_if_mock_is_based_on_a_class_then_can_only_expect_methods_defined_on_that_class(mocks):
    class Database(object):
        def save(self):
            pass
            
        status = False
    
    database = mocks.mock(Database)
    allows(database).save
    assert_raises_str(AssertionError, "Method 'delete' is not defined on type object 'Database'", lambda: allows(database).delete)
    assert_raises_str(AssertionError, "Attribute 'status' is not callable on type object 'Database'", lambda: allows(database).status)
Ejemplo n.º 25
0
def test_if_mock_is_based_on_a_class_then_can_also_expect_methods_defined_on_superclass(mocks):
    class Database(object):
        def save(self):
            pass
    
    class DeletingDatabase(Database):
        def delete(self):
            pass
    
    database = mocks.mock(DeletingDatabase)
    allows(database).save
    allows(database).delete
Ejemplo n.º 26
0
def test_same_method_can_return_different_values_for_different_arguments_using_allows(mocks):
    return_foo = "foo"
    return_bar = "bar"
    
    mock = mocks.mock()
    allows(mock).save.with_args("one", "two", key="word", foo="bar").returns(return_foo)
    allows(mock).save.with_args("positional").returns(return_bar)
    
    assert mock.save("one", "two", key="word", foo="bar") is return_foo
    assert_raises(UnexpectedInvocationError, lambda: mock.save())
    assert mock.save("positional") is return_bar
    assert_raises(UnexpectedInvocationError, lambda: mock.save(key="word"))
    assert mock.save("one", "two", key="word", foo="bar") is return_foo
Ejemplo n.º 27
0
def test_adds_error_with_line_number_if_arrow_is_missing(context):
    rule_set = context.mock(RuleSet)
    allows(rule_set).add
    errors = []
    
    parse("\n\n$SENTENCE - You're ${RUDE_ADJ}er than I thought\n" +
          "$RUDE_ADJ ->\n" +
          "$SENTENCE -> $RUDE_ADJ",
          rule_set,
          errors)
    
    assert_that(errors, m.contains_exactly(m.all_of(
        m.has_attr(message="Missing symbol on line 3: ->", line_number=3),
        m.is_a(MissingArrow)
    )))
Ejemplo n.º 28
0
def test_name_of_mock_is_used_in_exceptions_and_expectations_on_that_method_are_shown(mocks):
    unnamed = mocks.mock()
    named = mocks.mock(name='database')
    allows(unnamed).save.with_args("positional")
    allows(named).save.with_args("positional")
    
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: unnamed.save()
The following expectations on unnamed.save did not match:
    unnamed.save('positional') [wrong number of positional arguments]""",
                      lambda: unnamed.save())
    assert_raises_str(UnexpectedInvocationError,
"""Unexpected invocation: database.save()
The following expectations on database.save did not match:
    database.save('positional') [wrong number of positional arguments]""",
                      lambda: named.save())
Ejemplo n.º 29
0
    def _read_embedded_image(self, element):
        relationships = Relationships([
            _image_relationship(self.IMAGE_RELATIONSHIP_ID, "media/hat.png"),
        ])

        mocks = funk.Mocks()
        docx_file = mocks.mock()
        funk.allows(docx_file).open("word/media/hat.png").returns(io.BytesIO(self.IMAGE_BYTES))

        content_types = mocks.mock()
        funk.allows(content_types).find_content_type("word/media/hat.png").returns("image/png")

        return _read_and_get_document_xml_element(
            element,
            content_types=content_types,
            relationships=relationships,
            docx_file=docx_file,
        )
Ejemplo n.º 30
0
    def _read_embedded_image(self, element):
        relationships = Relationships([
            _image_relationship(self.IMAGE_RELATIONSHIP_ID, "media/hat.png"),
        ])

        mocks = funk.Mocks()
        docx_file = mocks.mock()
        funk.allows(docx_file).open("word/media/hat.png").returns(io.BytesIO(self.IMAGE_BYTES))

        content_types = mocks.mock()
        funk.allows(content_types).find_content_type("word/media/hat.png").returns("image/png")

        return _read_and_get_document_xml_element(
            element,
            content_types=content_types,
            relationships=relationships,
            docx_file=docx_file,
        )
Ejemplo n.º 31
0
def test_sequences_do_not_raise_assertions_when_called_in_correct_order(mocks):
    log = mocks.mock()
    
    first_ordering = mocks.sequence()
    second_ordering = mocks.sequence()
    
    expects(log).write("You and your friend").in_sequence(first_ordering)
    expects(log).write("Love Over Gold").in_sequence(second_ordering)
    
    expects(log).close().in_sequence(first_ordering).in_sequence(second_ordering)
    
    allows(log).flush()
    
    log.flush()
    log.write("Love Over Gold")
    log.flush()
    log.write("You and your friend")
    log.flush()
    
    log.close()
Ejemplo n.º 32
0
    def can_read_linked_pictures(self, mocks):
        drawing_element = _create_inline_image(
            blip=_linked_blip("rId5"),
            description="It's a hat",
        )

        relationships = Relationships([
            _image_relationship("rId5", "file:///media/hat.png"),
        ])

        files = mocks.mock()
        funk.allows(files).verify("file:///media/hat.png")
        funk.allows(files).open("file:///media/hat.png").returns(io.BytesIO(self.IMAGE_BYTES))

        content_types = mocks.mock()
        funk.allows(content_types).find_content_type("file:///media/hat.png").returns("image/png")

        image = _read_and_get_document_xml_element(
            drawing_element,
            content_types=content_types,
            relationships=relationships,
            files=files,
        )
        assert_equal(documents.Image, type(image))
        assert_equal("It's a hat", image.alt_text)
        assert_equal("image/png", image.content_type)
        with image.open() as image_file:
            assert_equal(self.IMAGE_BYTES, image_file.read())
Ejemplo n.º 33
0
    def can_read_linked_pictures(self, mocks):
        drawing_element = _create_inline_image(
            blip=_linked_blip("rId5"),
            description="It's a hat",
        )

        relationships = Relationships([
            _image_relationship("rId5", "file:///media/hat.png"),
        ])

        files = mocks.mock()
        funk.allows(files).verify("file:///media/hat.png")
        funk.allows(files).open("file:///media/hat.png").returns(io.BytesIO(self.IMAGE_BYTES))

        content_types = mocks.mock()
        funk.allows(content_types).find_content_type("file:///media/hat.png").returns("image/png")

        image = _read_and_get_document_xml_element(
            drawing_element,
            content_types=content_types,
            relationships=relationships,
            files=files,
        )
        assert_equal(documents.Image, type(image))
        assert_equal("It's a hat", image.alt_text)
        assert_equal("image/png", image.content_type)
        with image.open() as image_file:
            assert_equal(self.IMAGE_BYTES, image_file.read())
Ejemplo n.º 34
0
def test_can_mock_methods_used_internally_by_mock(mocks):
    mock = mocks.mock()
    
    expects(mock)._mocked_calls
    expects(mock).save()
    allows(mock)._mocked_calls
    allows(mock).save()
    expects_call(mock)
    allows_call(mock)
    
    mock._mocked_calls()
    mock.save()
    mock()
    
    class UserRepository(object):
        def _base(self):
            pass
    
    based_mock = mocks.mock(UserRepository)
    allows(based_mock)._base
Ejemplo n.º 35
0
def test_allowing_a_method_without_specifying_arguments_allows_method_to_be_called_no_times(mocks):
    mock = mocks.mock()
    allows(mock).save