def test_object_retrieve_by_tag_id_raises_on_invalid_limit_offset(self): self.assertEquals(0, self.object_service.count(), msg='Database is not empty in beginning') self.object_service.add(Object(name=u'Concept', digest=u'Desc')) tag_id = 1L with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_tag_id(tag_id, offset=-1) with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_tag_id(tag_id, offset='0') with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_tag_id(tag_id, offset='ID') with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_tag_id(tag_id, offset=3.1415297) with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_tag_id(tag_id, offset=Object(id=0)) with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_tag_id(tag_id, limit=-1) with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_tag_id(tag_id, limit='0') with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_tag_id(tag_id, limit='ID') with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_tag_id(tag_id, limit=3.1415297) with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_tag_id(tag_id, limit=Object(id=0))
def test_object_adding_duplicate_fails(self): name1 = u'test-title' name2 = u'updated-title' digest1 = u'test-digest' digest2 = u'updated-digest' o1 = self.object_service.add(Object(name=name1, digest=digest1)) self.assertEqual(o1.name, name1) self.assertEqual(o1.digest, digest1) o1 = self.object_service.retrieve_by_id(o1.id) self.assertEqual(o1.name, name1) self.assertEqual(o1.digest, digest1) # Changing only digest is OK o2 = self.object_service.add(Object(name=name1, digest=digest2)) self.assertEqual(o2.name, name1) self.assertEqual(o2.digest, digest2) o2 = self.object_service.retrieve_by_id(o2.id) self.assertEqual(o2.name, name1) self.assertEqual(o2.digest, digest2) # Changing only name is OK o3 = self.object_service.add(Object(name=name2, digest=digest1)) self.assertEqual(o3.name, name2) self.assertEqual(o3.digest, digest1) o3 = self.object_service.retrieve_by_id(o3.id) self.assertEqual(o3.name, name2) self.assertEqual(o3.digest, digest1) # Changing neither is not OK with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name=name1, digest=digest1))
def _create_objects(self, num_objects, name_prefix=u'object_'): objects = [] for i in range(num_objects): objects.append( self.object_service.add( Object(digest=unicode(md5_from_value(i)), name=u'{0}{1}'.format(name_prefix, i)))) return objects
def test_object_add_object_returns_object(self): in_object = Object(digest=unicode(md5_from_value(u'x')), name=u'foo.jpg') db_object = self.object_service.add(in_object) self.assertTrue( isinstance(db_object, Object), msg='The object add function should return value of type Object.') self.assertTrue(db_object.id)
def test_object_retrieve_by_regex_raises_on_invalid_name(self): with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_regex(name=0) with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_regex(name=1) with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_regex(name=-1) with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_regex(name=3.1415297) with self.assertRaises(ObjectCubeException): self.object_service.retrieve_by_regex(name=Object())
def test_update_updates_object_name(self): before_change_title = u'test-title' after_change_title = u'updated-title' o1 = self.object_service.add( Object(name=before_change_title, digest=u'12345')) o1.name = after_change_title o2 = self.object_service.update(o1) self.assertEquals(o2.name, after_change_title) o3 = self.object_service.retrieve_by_id(o1.id) self.assertEquals(o3.name, after_change_title)
def test_object_retrieve_by_id_finds_correct_object(self): num_objects = 10 for i in range(1, num_objects): object_name = u'{}'.format(i) digest = unicode(md5_from_value(object_name)) object_ = Object(digest=digest, name=object_name) db_object = self.object_service.add(object_) db_object2 = self.object_service.retrieve_by_id(long(i)) self.assertEquals(db_object2.id, db_object.id) self.assertEquals(db_object2.name, object_name) self.assertEquals(db_object2.digest, db_object.digest)
def post(self): data = json.loads(request.data) if data is None: return 'No data to post', 400 name = data.get('name') digest = data.get('digest') if name is None: return 'Object must have a name', 400 else: name = unicode(name) if digest is None: digest = unicode(md5_from_value(name)) try: object_ = self.object_service.add(Object(name=name, digest=digest)) except Exception as ex: return ex.message, 401 return object_.to_dict(), 201
def test_object_update_updates_object(self): name1 = u'test-title' name2 = u'updated-title' digest1 = u'test-digest' digest2 = u'updated-digest' o1 = self.object_service.add(Object(name=name1, digest=digest1)) o1.name = name2 o2 = self.object_service.update(o1) self.assertEqual(o2.name, name2) self.assertEqual(o2.digest, digest1) o2 = self.object_service.retrieve_by_id(o1.id) self.assertEqual(o2.name, name2) self.assertEqual(o2.digest, digest1) o2.digest = digest2 o3 = self.object_service.update(o2) self.assertEqual(o3.name, name2) self.assertEqual(o3.digest, digest2) o3 = self.object_service.retrieve_by_id(o2.id) self.assertEqual(o3.name, name2) self.assertEqual(o3.digest, digest2) o3.name = name1 o3.digest = digest1 o4 = self.object_service.update(o3) self.assertEqual(o4.name, name1) self.assertEqual(o4.digest, digest1) o4 = self.object_service.retrieve_by_id(o3.id) self.assertEqual(o4.name, name1) self.assertEqual(o4.digest, digest1)
def test_object_add_object_raises_on_illegal_inputs(self): with self.assertRaises(ObjectCubeException): self.object_service.add(None) with self.assertRaises(ObjectCubeException): self.object_service.add('ID') with self.assertRaises(ObjectCubeException): self.object_service.add(0) with self.assertRaises(ObjectCubeException): self.object_service.add(1) with self.assertRaises(ObjectCubeException): self.object_service.add(-1) with self.assertRaises(ObjectCubeException): self.object_service.add(3.1415297) with self.assertRaises(ObjectCubeException): self.object_service.add(Object()) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(id=0L, name=u'Name', digest=u'Desc')) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(id=1L, name=u'Name', digest=u'Desc')) with self.assertRaises(ObjectCubeException): self.object_service.add( Object(id=-1L, name=u'Name', digest=u'Desc')) with self.assertRaises(ObjectCubeException): self.object_service.add( Object(id='ID', name=u'Name', digest=u'Desc')) with self.assertRaises(ObjectCubeException): self.object_service.add( Object(id=3.1415297, name=u'Name', digest=u'Desc')) with self.assertRaises(ObjectCubeException): self.object_service.add( Object(id=Object(), name=u'Name', digest=u'Desc')) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name=0)) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name=1)) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name=-1)) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name=3.1415297)) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name=Object())) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name='')) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name='Name', digest=0)) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name='Name', digest=1)) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name='Name', digest=-1)) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name='Name', digest=3.1415297)) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name='Name', digest=Object())) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(name='Name', digest=''))
def test_object_update_raises_on_illegal_name_digest(self): o = self.object_service.add(Object(name=u'name', digest=u'digest')) with self.assertRaises(ObjectCubeException): self.object_service.update( Object(id=o.id, name=o.name, digest=None)) with self.assertRaises(ObjectCubeException): self.object_service.update(Object(id=o.id, name=o.name, digest='')) with self.assertRaises(ObjectCubeException): self.object_service.update(Object(id=o.id, name=o.name, digest=0)) with self.assertRaises(ObjectCubeException): self.object_service.update(Object(id=o.id, name=o.name, digest=1)) with self.assertRaises(ObjectCubeException): self.object_service.update(Object(id=o.id, name=o.name, digest=-1)) with self.assertRaises(ObjectCubeException): self.object_service.update( Object(id=o.id, name=o.name, digest=3.1415297)) with self.assertRaises(ObjectCubeException): self.object_service.update( Object(id=o.id, name=o.name, digest=Object())) with self.assertRaises(ObjectCubeException): self.object_service.update( Object(id=o.id, digest=o.name, name=None)) with self.assertRaises(ObjectCubeException): self.object_service.update(Object(id=o.id, digest=o.name, name='')) with self.assertRaises(ObjectCubeException): self.object_service.update(Object(id=o.id, digest=o.name, name=0)) with self.assertRaises(ObjectCubeException): self.object_service.update(Object(id=o.id, digest=o.name, name=1)) with self.assertRaises(ObjectCubeException): self.object_service.update(Object(id=o.id, digest=o.name, name=-1)) with self.assertRaises(ObjectCubeException): self.object_service.update( Object(id=o.id, digest=o.name, name=3.1415297)) with self.assertRaises(ObjectCubeException): self.object_service.update( Object(id=o.id, digest=o.name, name=Object()))
def test_object_update_raises_if_update_object_that_does_not_exist(self): with self.assertRaises(ObjectCubeException): o = Object(name=u'test.jpg', digest=u'12345', id=234L) self.object_service.update(o)
def test_object_update_raises_if_object_with_no_id(self): with self.assertRaises(ObjectCubeException): o = Object(name=u'test.jpg', digest=u'12345') self.object_service.update(o)
def test_object_delete_by_id_returns_none_if_object_does_exist(self): o = self.object_service.add(Object(name=u'test.jpg', digest=u'12345')) delete_return_value = self.object_service.delete_by_id(o.id) self.assertEquals(delete_return_value, None)
def test_object_delete_raises_if_deleted_object_does_not_exist(self): o = Object(name=u'test.jpg', digest=u'12345', id=1337L) with self.assertRaises(ObjectCubeException): self.object_service.delete(o)
def test_object_add_object_raises_exception_if_digest_is_missing(self): with self.assertRaises(ObjectCubeException): self.object_service.add(Object(digest=None, name=u'x')) with self.assertRaises(ObjectCubeException): self.object_service.add(Object(digest='', name=u'x'))