~terrycojones/txfluiddb/add-slash-values-support-821418

« back to all changes in this revision

Viewing changes to txfluiddb/test/test_client.py

  • Committer: Tristan Seligmann
  • Date: 2010-01-17 11:45:16 UTC
  • mfrom: (4.1.2 terry-modernize)
  • Revision ID: mithrandi@mithrandi.net-20100117114516-g7dais8djysy88a9
MergeĀ lp:~terrycojones/txfluiddb/modernize-Oct-2009.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
import simplejson as json
5
5
 
6
 
from txfluiddb.errors import InvalidName, UnsupportedEncoding
 
6
from txfluiddb.errors import InvalidName
7
7
from txfluiddb.client import (
8
8
    Namespace, Tag, Endpoint, _HasPath, Object, Blob, BasicCreds)
9
9
 
387
387
        self.method = method
388
388
        self.data = postdata
389
389
        self.headers = headers
390
 
        return succeed(self.response)
 
390
        rh = self.responseHeaders if hasattr(self, 'responseHeaders') else {}
 
391
        rc = self.responseCode if hasattr(self, 'responseCode') else 200
 
392
        return succeed((rc, rh, self.response))
391
393
 
392
394
 
393
395
 
492
494
        """
493
495
        Utility wrapper for testing L{Object.get} operations.
494
496
        """
495
 
        self.endpoint.response = json.dumps(response)
 
497
        value, contentType = response
 
498
        self.endpoint.response = value
 
499
        self.endpoint.responseHeaders = { 'content-type' : contentType, }
496
500
        obj = Object(u'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx')
497
501
        tag = Namespace(u'test').child(u'tag')
498
502
        d = obj.get(self.endpoint, tag)
500
504
        self.assertEqual(self.endpoint.data, None)
501
505
        self.assertEqual(
502
506
            self.endpoint.url,
503
 
            'http://fluiddb.test.url/objects/xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx/test/tag?format=json')
 
507
            'http://fluiddb.test.url/objects/xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx/test/tag')
504
508
        return d
505
509
 
506
510
 
507
511
    def test_getString(self):
508
512
        """
509
 
        Retrieving a JSON string value results in a C{unicode} object.
 
513
        Retrieving a string value results in a C{unicode} object.
510
514
        """
511
 
        response = {u'value': u'Data goes where?'}
 
515
        response = (json.dumps('Data goes where?'),
 
516
                    'application/vnd.fluiddb.value+json')
512
517
        def _gotResponse(response):
513
518
            self.assertIsInstance(response, unicode)
514
519
            self.assertEqual(response, u'Data goes where?')
517
522
 
518
523
    def test_getInteger(self):
519
524
        """
520
 
        Retrieving a JSON integer value results in an integer object.
 
525
        Retrieving a integer value results in an integer object.
521
526
        """
522
 
        response = {u'value': 42}
 
527
        response = (json.dumps(42), 'application/vnd.fluiddb.value+json')
523
528
        def _gotResponse(response):
524
529
            self.assertEqual(response, 42)
525
530
        return self.makeGetRequest(response).addCallback(_gotResponse)
529
534
        """
530
535
        Retrieving a binary value results in a C{Blob} object.
531
536
        """
532
 
        response = {u'value': u'PHA+Zm9vPC9wPg==',
533
 
                    u'valueEncoding': u'base-64',
534
 
                    u'valueType': u'text/html'}
 
537
        response = ('<p>foo</p>', 'text/html')
535
538
        def _gotResponse(response):
536
 
            self.assertEqual(self.endpoint.headers['Accept-Encoding'], 'base-64')
537
539
            self.assertEqual(response.contentType, u'text/html')
538
540
            self.assertEqual(response.data, '<p>foo</p>')
539
541
        return self.makeGetRequest(response).addCallback(_gotResponse)
540
542
 
541
543
 
542
 
    def test_unsupportedEncoding(self):
543
 
        """
544
 
        Parsing a value payload with an unsupported encoding raises an
545
 
        exception.
546
 
        """
547
 
        response = {u'value': u'tralala',
548
 
                    u'valueEncoding': u'inconceivable',
549
 
                    u'valueType': u'application/lulz-stream'}
550
 
 
551
 
        obj = Object(u'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx')
552
 
        self.assertRaises(UnsupportedEncoding, obj._parseTagValue, response)
553
 
 
554
 
 
555
544
    def test_deleteTag(self):
556
545
        """
557
546
        Deleting a tag returns nothing if successful.
581
570
        self.assertEqual(self.endpoint.method, 'PUT')
582
571
        self.assertEqual(
583
572
            self.endpoint.url,
584
 
            'http://fluiddb.test.url/objects/xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx/test/tag?format=json')
585
 
        self.assertEqual(json.loads(self.endpoint.data),
586
 
                         {u'value': 42})
 
573
            'http://fluiddb.test.url/objects/xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx/test/tag')
 
574
        self.assertEqual(json.loads(self.endpoint.data), 42)
587
575
        return d
588
576
 
589
577
 
621
609
        d = Object.create(self.endpoint).addCallback(_gotResponse)
622
610
        self.assertEqual(self.endpoint.method, 'POST')
623
611
        self.assertEqual(self.endpoint.url, 'http://fluiddb.test.url/objects')
624
 
        self.assertEqual(json.loads(self.endpoint.data), {})
 
612
        self.assertEqual(self.endpoint.data, None)
625
613
        return d
626
614
 
627
615