def test_hydrate(self): note = Note.objects.get(pk=1) bundle_1 = Bundle(data={ 'date': '2010-05-12', }) field_1 = DateField(attribute='created') field_1.instance_name = 'date' self.assertEqual(field_1.hydrate(bundle_1), datetime.date(2010, 5, 12)) bundle_2 = Bundle() field_2 = DateField(default=datetime.date(2010, 4, 1)) field_2.instance_name = 'date' self.assertEqual(field_2.hydrate(bundle_2), datetime.date(2010, 4, 1)) bundle_3 = Bundle(data={ 'date': 'Wednesday, May 12, 2010', }) field_3 = DateField(attribute='created_string') field_3.instance_name = 'date' self.assertEqual(field_3.hydrate(bundle_3), datetime.date(2010, 5, 12)) bundle_4 = Bundle(data={ 'date': '5 Apr 2010', }) field_4 = DateField(attribute='created') field_4.instance_name = 'date' self.assertEqual(field_4.hydrate(bundle_4), datetime.date(2010, 4, 5)) bundle_5 = Bundle(data={ 'date': None, }) field_5 = DateField(attribute='created', null=True) field_5.instance_name = 'date' self.assertEqual(field_5.hydrate(bundle_5), None)
def test_dehydrate(self): bit = MediaBit.objects.get(pk=1) bundle = Bundle(obj=bit) field_1 = FileField(attribute='image', default=True) self.assertEqual(field_1.dehydrate(bundle), u'http://localhost:8080/media/lulz/catz.gif') field_2 = FileField( default='http://media.example.com/img/default_avatar.jpg') self.assertEqual(field_2.dehydrate(bundle), u'http://media.example.com/img/default_avatar.jpg') bit = MediaBit.objects.get(pk=1) bit.image = '' bundle = Bundle(obj=bit) field_3 = FileField(attribute='image', default=True) self.assertEqual(field_3.dehydrate(bundle), None) bit.image = None bundle = Bundle(obj=bit) field_4 = FileField(attribute='image', null=True) self.assertEqual(field_4.dehydrate(bundle), None)
def test_is_valid(self): valid = Validation() bundle = Bundle() self.assertEqual(valid.is_valid(bundle), {}) bundle = Bundle(data={ 'title': 'Foo.', 'slug': 'bar', 'content': '', 'is_active': True, }) self.assertEqual(valid.is_valid(bundle), {})
def test_dehydrate(self): note = Note() bundle_1 = Bundle(obj=note) field_1 = ManyToManyField(SubjectResource, 'subjects') field_1.instance_name = 'm2m' try: # self.assertRaises isn't cooperating here. Do it the hard way. field_1.dehydrate(bundle_1) self.fail() except ApiFieldError: pass field_2 = ManyToManyField(SubjectResource, 'subjects', null=True) field_2.instance_name = 'm2m' self.assertEqual(field_2.dehydrate(bundle_1), []) field_3 = ManyToManyField(SubjectResource, 'subjects') field_3.instance_name = 'm2m' bundle_3 = Bundle(obj=self.note_1) self.assertEqual(field_3.dehydrate(bundle_3), ['/api/v1/subjects/1/', '/api/v1/subjects/2/']) field_4 = ManyToManyField(SubjectResource, 'subjects', full=True) field_4.instance_name = 'm2m' bundle_4 = Bundle(obj=self.note_1) subject_bundle_list = field_4.dehydrate(bundle_4) self.assertEqual(len(subject_bundle_list), 2) self.assertEqual(isinstance(subject_bundle_list[0], Bundle), True) self.assertEqual(subject_bundle_list[0].data['name'], u'News') self.assertEqual(subject_bundle_list[0].data['url'], u'/news/') self.assertEqual(subject_bundle_list[0].obj.name, u'News') self.assertEqual(subject_bundle_list[0].obj.url, u'/news/') self.assertEqual(isinstance(subject_bundle_list[1], Bundle), True) self.assertEqual(subject_bundle_list[1].data['name'], u'Photos') self.assertEqual(subject_bundle_list[1].data['url'], u'/photos/') self.assertEqual(subject_bundle_list[1].obj.name, u'Photos') self.assertEqual(subject_bundle_list[1].obj.url, u'/photos/') field_5 = ManyToManyField(SubjectResource, 'subjects') field_5.instance_name = 'm2m' bundle_5 = Bundle(obj=self.note_2) self.assertEqual(field_5.dehydrate(bundle_5), ['/api/v1/subjects/1/', '/api/v1/subjects/3/']) field_6 = ManyToManyField(SubjectResource, 'subjects') field_6.instance_name = 'm2m' bundle_6 = Bundle(obj=self.note_3) self.assertEqual(field_6.dehydrate(bundle_6), [])
def test_dehydrate(self): note = Note.objects.get(pk=1) bundle = Bundle(obj=note) # With no attribute or default, we should get ``None``. field_1 = ApiField() self.assertEqual(field_1.dehydrate(bundle), None) # Still no attribute, so we should pick up the default field_2 = ApiField(default=True) self.assertEqual(field_2.dehydrate(bundle), True) # Wrong attribute should yield default. field_3 = ApiField(attribute='foo', default=True) self.assertEqual(field_3.dehydrate(bundle), True) # Wrong attribute should yield null. field_4 = ApiField(attribute='foo', null=True) self.assertEqual(field_4.dehydrate(bundle), None) # Correct attribute. field_5 = ApiField(attribute='title', default=True) self.assertEqual(field_5.dehydrate(bundle), u'First Post!') # Correct callable attribute. field_6 = ApiField(attribute='what_time_is_it', default=True) self.assertEqual(field_6.dehydrate(bundle), datetime.datetime(2010, 4, 1, 0, 48))
def test_is_valid(self): valid = FormValidation(form_class=NoteForm) bundle = Bundle() self.assertEqual( valid.is_valid(bundle), { 'is_active': [u'This field is required.'], 'slug': [u'This field is required.'], '__all__': [u'Having no content makes for a very boring note.'], 'title': [u'This field is required.'], }) bundle = Bundle( data={ 'title': 'Foo.', 'slug': '123456789012345678901234567890123456789012345678901234567890', 'content': '', 'is_active': True, }) self.assertEqual( valid.is_valid(bundle), { 'slug': [u'Ensure this value has at most 50 characters (it has 60).'], '__all__': [u'Having no content makes for a very boring note.'], }) bundle = Bundle(data={ 'title': 'Foo.', 'slug': 'bar', 'content': '', 'is_active': True, }) self.assertEqual(valid.is_valid(bundle), { '__all__': [u'Having no content makes for a very boring note.'], }) bundle = Bundle( data={ 'title': 'Foo.', 'slug': 'bar', 'content': 'This! Is! CONTENT!', 'is_active': True, }) self.assertEqual(valid.is_valid(bundle), {})
def test_dehydrate(self): note = Note.objects.get(pk=1) bundle = Bundle(obj=note) field_1 = BooleanField(attribute='is_active', default=False) self.assertEqual(field_1.dehydrate(bundle), True) field_2 = BooleanField(default=True) self.assertEqual(field_2.dehydrate(bundle), True)
def test_dehydrate(self): note = Note.objects.get(pk=1) bundle = Bundle(obj=note) field_1 = FloatField(default=20) self.assertEqual(field_1.dehydrate(bundle), 20.0) field_2 = IntegerField(default=18.5) self.assertEqual(field_2.dehydrate(bundle), 18)
def test_dehydrate(self): note = Note.objects.get(pk=1) bundle = Bundle(obj=note) field_1 = CharField(attribute='title', default=True) self.assertEqual(field_1.dehydrate(bundle), u'First Post!') field_2 = CharField(default=20) self.assertEqual(field_2.dehydrate(bundle), u'20')
def test_hydrate(self): note = Note.objects.get(pk=1) bundle = Bundle(obj=note) # With no value or nullable, we should get an ``ApiFieldError``. field_1 = ForeignKey(UserResource, 'author') self.assertRaises(ApiFieldError, field_1.hydrate, bundle) # The nullable case. field_2 = ForeignKey(UserResource, 'author', null=True) field_2.instance_name = 'fk' bundle.data['fk'] = None self.assertEqual(field_2.hydrate(bundle), None) # Wrong resource URI. field_3 = ForeignKey(UserResource, 'author') field_3.instance_name = 'fk' bundle.data['fk'] = '/api/v1/users/abc/' self.assertRaises(NotFound, field_3.hydrate, bundle) # A real, live attribute! field_4 = ForeignKey(UserResource, 'author') field_4.instance_name = 'fk' bundle.data['fk'] = '/api/v1/users/1/' fk_bundle = field_4.hydrate(bundle) self.assertEqual(fk_bundle.data['username'], u'johndoe') self.assertEqual(fk_bundle.data['email'], u'*****@*****.**') self.assertEqual(fk_bundle.obj.username, u'johndoe') self.assertEqual(fk_bundle.obj.email, u'*****@*****.**') field_5 = ForeignKey(UserResource, 'author') field_5.instance_name = 'fk' bundle.data['fk'] = { 'username': u'mistersmith', 'email': u'*****@*****.**', 'password': u'foobar', } fk_bundle = field_5.hydrate(bundle) self.assertEqual(fk_bundle.data['username'], u'mistersmith') self.assertEqual(fk_bundle.data['email'], u'*****@*****.**') self.assertEqual(fk_bundle.obj.username, u'mistersmith') self.assertEqual(fk_bundle.obj.email, u'*****@*****.**') # Regression - Make sure Unicode keys get converted to regular strings # so that we can **kwargs them. field_6 = ForeignKey(UserResource, 'author') field_6.instance_name = 'fk' bundle.data['fk'] = { u'username': u'mistersmith', u'email': u'*****@*****.**', u'password': u'foobar', } fk_bundle = field_6.hydrate(bundle) self.assertEqual(fk_bundle.data['username'], u'mistersmith') self.assertEqual(fk_bundle.data['email'], u'*****@*****.**') self.assertEqual(fk_bundle.obj.username, u'mistersmith') self.assertEqual(fk_bundle.obj.email, u'*****@*****.**')
def test_dehydrate(self): note = Note() bundle = Bundle(obj=note) field_1 = ForeignKey(UserResource, 'author') self.assertRaises(ApiFieldError, field_1.dehydrate, bundle) field_2 = ForeignKey(UserResource, 'author', null=True) self.assertEqual(field_2.dehydrate(bundle), None) note = Note.objects.get(pk=1) bundle = Bundle(obj=note) field_3 = ForeignKey(UserResource, 'author') self.assertEqual(field_3.dehydrate(bundle), '/api/v1/users/1/') field_4 = ForeignKey(UserResource, 'author', full=True) user_bundle = field_4.dehydrate(bundle) self.assertEqual(isinstance(user_bundle, Bundle), True) self.assertEqual(user_bundle.data['username'], u'johndoe') self.assertEqual(user_bundle.data['email'], u'*****@*****.**')
def test_hydrate(self): note = Note.objects.get(pk=1) bundle = Bundle(obj=note) # With no value, default or nullable, we should get an ``ApiFieldError``. field_1 = ApiField() field_1.instance_name = 'api' self.assertRaises(ApiFieldError, field_1.hydrate, bundle) # The default. field_2 = ApiField(default='foo') field_2.instance_name = 'api' self.assertEqual(field_2.hydrate(bundle), 'foo') # The callable default. def foo(): return 'bar' field_3 = ApiField(default=foo) field_3.instance_name = 'api' self.assertEqual(field_3.hydrate(bundle), 'bar') # The nullable case. field_4 = ApiField(null=True) field_4.instance_name = 'api' self.assertEqual(field_4.hydrate(bundle), None) # The readonly case. field_5 = ApiField(readonly=True) field_5.instance_name = 'api' bundle.data['api'] = 'abcdef' self.assertEqual(field_5.hydrate(bundle), None) # A real, live attribute! field_6 = ApiField(attribute='title') field_6.instance_name = 'api' bundle.data['api'] = note.title self.assertEqual(field_6.hydrate(bundle), u'First Post!')
def test_dehydrate(self): note = Note.objects.get(pk=1) bundle = Bundle(obj=note) field_1 = DateField(attribute='created') self.assertEqual(field_1.dehydrate(bundle), datetime.datetime(2010, 3, 30, 20, 5)) field_2 = DateField(default=datetime.date(2010, 4, 1)) self.assertEqual(field_2.dehydrate(bundle), datetime.date(2010, 4, 1)) note.created_string = '2010-04-02' field_3 = DateField(attribute='created_string') self.assertEqual(field_3.dehydrate(bundle), datetime.date(2010, 4, 2))
def test_hydrate(self): note = Note.objects.get(pk=1) bundle_1 = Bundle(data={ 'datetime': '2010-05-12 10:36:28', }) field_1 = DateTimeField(attribute='created') field_1.instance_name = 'datetime' self.assertEqual(field_1.hydrate(bundle_1), datetime.datetime(2010, 5, 12, 10, 36, 28)) bundle_2 = Bundle() field_2 = DateTimeField(default=datetime.datetime(2010, 4, 1, 2, 0)) field_2.instance_name = 'datetime' self.assertEqual(field_2.hydrate(bundle_2), datetime.datetime(2010, 4, 1, 2, 0)) bundle_3 = Bundle(data={ 'datetime': 'Tue, 30 Mar 2010 20:05:00 -0500', }) field_3 = DateTimeField(attribute='created_string') field_3.instance_name = 'datetime' self.assertEqual( field_3.hydrate(bundle_3), datetime.datetime(2010, 3, 30, 20, 5, tzinfo=tzoffset(None, -18000))) bundle_4 = Bundle(data={ 'datetime': None, }) field_4 = DateField(attribute='created', null=True) field_4.instance_name = 'datetime' self.assertEqual(field_4.hydrate(bundle_4), None)
def test_hydrate(self): note = Note.objects.get(pk=1) bundle = Bundle(obj=note) # With no value or nullable, we should get an ``ApiFieldError``. field_1 = ManyToManyField(SubjectResource, 'subjects') field_1.instance_name = 'm2m' self.assertRaises(ApiFieldError, field_1.hydrate_m2m, bundle) # The nullable case. field_2 = ManyToManyField(SubjectResource, 'subjects', null=True) field_2.instance_name = 'm2m' empty_bundle = Bundle() self.assertEqual(field_2.hydrate_m2m(empty_bundle), []) field_3 = ManyToManyField(SubjectResource, 'subjects', null=True) field_3.instance_name = 'm2m' bundle_3 = Bundle(data={'m2m': []}) self.assertEqual(field_3.hydrate_m2m(bundle_3), []) # Wrong resource URI. field_4 = ManyToManyField(SubjectResource, 'subjects') field_4.instance_name = 'm2m' bundle_4 = Bundle(data={'m2m': ['/api/v1/subjects/abc/']}) self.assertRaises(NotFound, field_4.hydrate_m2m, bundle_4) # A real, live attribute! field_5 = ManyToManyField(SubjectResource, 'subjects') field_5.instance_name = 'm2m' bundle_5 = Bundle(data={'m2m': ['/api/v1/subjects/1/']}) subject_bundle_list = field_5.hydrate_m2m(bundle_5) self.assertEqual(len(subject_bundle_list), 1) self.assertEqual(subject_bundle_list[0].data['name'], u'News') self.assertEqual(subject_bundle_list[0].data['url'], u'/news/') self.assertEqual(subject_bundle_list[0].obj.name, u'News') self.assertEqual(subject_bundle_list[0].obj.url, u'/news/') field_6 = ManyToManyField(SubjectResource, 'subjects') field_6.instance_name = 'm2m' bundle_6 = Bundle( data={ 'm2m': [ { 'name': u'Foo', 'url': u'/foo/', }, { 'name': u'Bar', 'url': u'/bar/', }, ] }) subject_bundle_list = field_6.hydrate_m2m(bundle_6) self.assertEqual(len(subject_bundle_list), 2) self.assertEqual(subject_bundle_list[0].data['name'], u'Foo') self.assertEqual(subject_bundle_list[0].data['url'], u'/foo/') self.assertEqual(subject_bundle_list[0].obj.name, u'Foo') self.assertEqual(subject_bundle_list[0].obj.url, u'/foo/') self.assertEqual(subject_bundle_list[1].data['name'], u'Bar') self.assertEqual(subject_bundle_list[1].data['url'], u'/bar/') self.assertEqual(subject_bundle_list[1].obj.name, u'Bar') self.assertEqual(subject_bundle_list[1].obj.url, u'/bar/')