def test_read(self):
     Annotation(text=u"Foo", id=123)
     session.commit()
     response = self.app.get('/annotations/123')
     data = json.loads(response.data)
     assert data['id'] == 123, "annotation id should be returned in response"
     assert data['text'] == "Foo", "annotation text should be returned in response"
    def test_delete(self):
        ann = Annotation(text=u"Bar", id=456)
        session.commit()

        response = self.app.delete('/annotations/456')
        assert response.status_code == 204, "response should be 204 NO CONTENT"

        assert Annotation.get(456) == None, "annotation wasn't deleted in db"
    def test_search(self):
        uri1 = u'http://xyz.com'
        uri2 = u'urn:uuid:xxxxx'
        user = u'levin'
        user2 = u'anna'
        anno = Annotation(
                uri=uri1,
                text=uri1,
                user=user,
                )
        anno2 = Annotation(
                uri=uri1,
                text=uri1 + uri1,
                user=user2,
                )
        anno3 = Annotation(
                uri=uri2,
                text=uri2,
                user=user
                )
        session.commit()
        annoid = anno.id
        anno2id = anno2.id
        session.remove()

        url = '/search'
        res = self.app.get(url)
        body = json.loads(res.data)
        assert body['total'] == 3, body

        url = '/search?limit=1'
        res = self.app.get(url)
        body = json.loads(res.data)
        assert body['total'] == 3, body
        assert len(body['rows']) == 1

        url = '/search?uri=' + uri1 + '&all_fields=1'
        res = self.app.get(url)
        body = json.loads(res.data)
        assert body['total'] == 2, body
        out = body['rows']
        assert len(out) == 2
        assert out[0]['uri'] == uri1
        assert out[0]['id'] in [ annoid, anno2id ]

        url = '/search?uri=' + uri1
        res = self.app.get(url)
        body = json.loads(res.data)
        assert body['rows'][0].keys() == ['id'], body['rows']

        url = '/search?limit=-1'
        res = self.app.get(url)
        body = json.loads(res.data)
        assert len(body['rows']) == 3, body
    def test_update(self):
        ann = Annotation(text=u"Foo", id=123)
        session.commit() # commits expire all properties of `ann'

        payload = json.dumps({'id': 123, 'text': 'Bar'})
        response = self.app.put('/annotations/123', data=payload, content_type='application/json')

        assert ann.text == "Bar", "annotation wasn't updated in db"

        data = json.loads(response.data)
        assert data['text'] == "Bar", "update annotation should be returned in response"
 def test_ttl_required_or_default(self):
     c = Consumer(key='foo', secret='bar', ttl=None)
     session.commit()
     assert c.ttl == 3600, "TTL not set to default of one hour"
 def test_id(self):
     ann = Annotation()
     session.commit()
     assert isinstance(ann.id, int), "annotation id wasn't an integer"
 def test_id(self):
     rng = Range()
     session.commit()
     assert isinstance(rng.id, int), "range id wasn't an integer"