Exemple #1
0
    def test_formatter_uses_annotation_resource(self, group_service, fake_links_service):
        annotation = mock.Mock(id='the-id', extra={})
        resource = AnnotationResource(annotation, group_service, fake_links_service)

        formatters = [IDDuplicatingFormatter()]
        presenter = AnnotationJSONPresenter(resource, formatters)

        output = presenter.asdict()

        assert output['duplicated-id'] == 'the-id'
Exemple #2
0
    def test_asdict_merges_formatters(self, group_service, fake_links_service):
        ann = mock.Mock(id="the-real-id", extra={})
        resource = AnnotationContext(ann, group_service, fake_links_service)

        formatters = [
            FakeFormatter({"flagged": "nope"}),
            FakeFormatter({"nipsa": "maybe"}),
        ]
        presenter = AnnotationJSONPresenter(resource, formatters)
        presented = presenter.asdict()

        assert presented["flagged"] == "nope"
        assert presented["nipsa"] == "maybe"
Exemple #3
0
    def test_asdict_merges_formatters(self, group_service, fake_links_service):
        ann = mock.Mock(id='the-real-id', extra={})
        resource = AnnotationResource(ann, group_service, fake_links_service)

        formatters = [
            FakeFormatter({'flagged': 'nope'}),
            FakeFormatter({'nipsa': 'maybe'})
        ]
        presenter = AnnotationJSONPresenter(resource, formatters)
        presented = presenter.asdict()

        assert presented['flagged'] == 'nope'
        assert presented['nipsa'] == 'maybe'
Exemple #4
0
    def test_immutable_formatters(self, group_service, fake_links_service):
        """Double-check we can't mutate the formatters list after the fact.

        This is an extra check just to make sure we can't accidentally change
        the constructor so that it simply aliases the list that's passed in,
        leaving us open to all kinds of mutability horrors.

        """
        ann = mock.Mock(id="the-real-id", extra={})
        resource = AnnotationContext(ann, group_service, fake_links_service)

        formatters = [FakeFormatter({"flagged": "nope"})]
        presenter = AnnotationJSONPresenter(resource, formatters)
        formatters.append(FakeFormatter({"enterprise": "synergy"}))
        presented = presenter.asdict()

        assert "enterprise" not in presented
Exemple #5
0
    def test_exception_for_wrong_formatter_type(self):
        with pytest.raises(ValueError) as exc:
            AnnotationJSONPresenter(mock.Mock(), formatters=[mock.Mock()])

        assert 'not implementing IAnnotationFormatter interface' in exc.value.message
Exemple #6
0
    def test_asdict(self, document_asdict, group_service, fake_links_service):
        ann = mock.Mock(
            id="the-id",
            created=datetime.datetime(2016, 2, 24, 18, 3, 25, 768),
            updated=datetime.datetime(2016, 2, 29, 10, 24, 5, 564),
            userid="acct:luke",
            target_uri="http://example.com",
            text="It is magical!",
            tags=["magic"],
            groupid="__world__",
            shared=True,
            target_selectors=[{
                "TestSelector": "foobar"
            }],
            references=["referenced-id-1", "referenced-id-2"],
            extra={
                "extra-1": "foo",
                "extra-2": "bar"
            },
        )
        resource = AnnotationContext(ann, group_service, fake_links_service)

        document_asdict.return_value = {"foo": "bar"}

        expected = {
            "id":
            "the-id",
            "created":
            "2016-02-24T18:03:25.000768+00:00",
            "updated":
            "2016-02-29T10:24:05.000564+00:00",
            "user":
            "******",
            "uri":
            "http://example.com",
            "text":
            "It is magical!",
            "tags": ["magic"],
            "group":
            "__world__",
            "permissions": {
                "read": ["group:__world__"],
                "admin": ["acct:luke"],
                "update": ["acct:luke"],
                "delete": ["acct:luke"],
            },
            "target": [{
                "source": "http://example.com",
                "selector": [{
                    "TestSelector": "foobar"
                }],
            }],
            "document": {
                "foo": "bar"
            },
            "links": {
                "giraffe": "http://giraffe.com",
                "toad": "http://toad.net"
            },
            "references": ["referenced-id-1", "referenced-id-2"],
            "extra-1":
            "foo",
            "extra-2":
            "bar",
        }

        result = AnnotationJSONPresenter(resource).asdict()

        assert result == expected
Exemple #7
0
    def test_asdict(self, document_asdict, group_service, fake_links_service):
        ann = mock.Mock(id='the-id',
                        created=datetime.datetime(2016, 2, 24, 18, 3, 25, 768),
                        updated=datetime.datetime(2016, 2, 29, 10, 24, 5, 564),
                        userid='acct:luke',
                        target_uri='http://example.com',
                        text='It is magical!',
                        tags=['magic'],
                        groupid='__world__',
                        shared=True,
                        target_selectors=[{
                            'TestSelector': 'foobar'
                        }],
                        references=['referenced-id-1', 'referenced-id-2'],
                        extra={
                            'extra-1': 'foo',
                            'extra-2': 'bar'
                        })
        resource = AnnotationResource(ann, group_service, fake_links_service)

        document_asdict.return_value = {'foo': 'bar'}

        expected = {
            'id':
            'the-id',
            'created':
            '2016-02-24T18:03:25.000768+00:00',
            'updated':
            '2016-02-29T10:24:05.000564+00:00',
            'user':
            '******',
            'uri':
            'http://example.com',
            'text':
            'It is magical!',
            'tags': ['magic'],
            'group':
            '__world__',
            'permissions': {
                'read': ['group:__world__'],
                'admin': ['acct:luke'],
                'update': ['acct:luke'],
                'delete': ['acct:luke']
            },
            'target': [{
                'source': 'http://example.com',
                'selector': [{
                    'TestSelector': 'foobar'
                }]
            }],
            'document': {
                'foo': 'bar'
            },
            'links': {
                'giraffe': 'http://giraffe.com',
                'toad': 'http://toad.net'
            },
            'references': ['referenced-id-1', 'referenced-id-2'],
            'extra-1':
            'foo',
            'extra-2':
            'bar'
        }

        result = AnnotationJSONPresenter(resource).asdict()

        assert result == expected