def friend_email_add(mine,friend): #SEE if my tuple is there .. is not then create and add else simple merge print("\n\n\n\n\ running addin table ") table_service = TableService(account_name='*****', account_key='******') try: task = table_service.get_entity('friendlisttable', 'friend', mine) count=task.count print(count) field="friend"+str(count+1) data={field:friend,'count':(count+1)} table_service.insert_or_merge_entity('friendlisttable','friend',mine,data) print("value 1 inserted via merge") except Exception as e: print(e) print("your account was not there") data= {'PartitionKey':'friend','RowKey':mine,'count':1,'friend1':friend} table_service.insert_entity('friendlisttable', data) print("value 1 added via create") try: task = table_service.get_entity('friendlisttable', 'friend', friend) count=task.count print(count) field="friend"+str(count+1) data={field:mine,'count':(count+1)} table_service.insert_or_merge_entity('friendlisttable','friend',friend,data) print("value 2 inserted via merge") except Exception as e: print(e) print("your account was not there") data= {'PartitionKey':'friend','RowKey':friend,'count':1,'friend1':mine} table_service.insert_entity('friendlisttable', data) print("value 2 added via create") print("added al \n\n\n ")
def friend_email_get(mine): l=[] try: # will return a list of email ids of freinds table_service = TableService(account_name='****', account_key='******') #since table is already created #table_service.create_table('friendstable') tuple1 = table_service.get_entity('friendlisttable', 'friend', mine) count=tuple1.count for i in range(1,count+1): field="friend"+str(i); k_dict=tuple1.__dict__ print("\n\n",k_dict,"\n\n") data=k_dict[field] print("\n\n",data) l.append(data) print("\n\n",l,"\n\n") except Exception as e: print(e) return l
image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA) # put to blob smaller version lol ,image_tn = cv2.imencode( '.jpg', image ) blob_service.put_block_blob_from_bytes( blob_container, imgBlobName, str(bytearray(image_tn.flatten().tolist())) ) ############################################################################ # process image colourStructure = getCharacteristics( image, region, results ) blob_service.put_block_blob_from_bytes( blob_analysis, imgBlobName, dumps(colourStructure) ) # {'PartitionKey': 'allPhotos', 'RowKey': 'imageName', 'thumbnail' : 'thumbnailName', # 'userId' : ?, 'local' : ?, 'hue' : 200, 'saturation' : 200, 'value' : 200} ## query for image in table to ensure existence currentTask = table_service.get_entity( tableName, tablePartitionKey, tableRowKey) ## send the quantities to table: save thumbnail ID & save image characteristics # currentTask.thumbnail = tnID currentTask.analysed = True table_service.update_entity( tableName, tablePartitionKey, tableRowKey, currentTask) # dequeue image queue_service.delete_message( imagesQueue, message.message_id, message.pop_receipt ) # Get the mosaic image from some queue
class TableServiceTest(AzureTestCase): def setUp(self): self.ts = TableService(credentials.getStorageServicesName(), credentials.getStorageServicesKey()) set_service_options(self.ts) self.table_name = getUniqueName('uttable') self.additional_table_names = [] def tearDown(self): self.cleanup() return super(TableServiceTest, self).tearDown() def cleanup(self): try: self.ts.delete_table(self.table_name) except: pass for name in self.additional_table_names: try: self.ts.delete_table(name) except: pass #--Helpers----------------------------------------------------------------- def _create_table(self, table_name): ''' Creates a table with the specified name. ''' self.ts.create_table(table_name, True) def _create_table_with_default_entities(self, table_name, entity_count): ''' Creates a table with the specified name and adds entities with the default set of values. PartitionKey is set to 'MyPartition' and RowKey is set to a unique counter value starting at 1 (as a string). ''' entities = [] self._create_table(table_name) for i in range(1, entity_count + 1): entities.append(self.ts.insert_entity( table_name, self._create_default_entity_dict('MyPartition', str(i)))) return entities def _create_default_entity_class(self, partition, row): ''' Creates a class-based entity with fixed values, using all of the supported data types. ''' entity = Entity() entity.PartitionKey = partition entity.RowKey = row entity.age = 39 entity.sex = 'male' entity.married = True entity.deceased = False entity.optional = None entity.ratio = 3.1 entity.large = 9333111000 entity.Birthday = datetime(1973, 10, 4) entity.birthday = datetime(1970, 10, 4) entity.binary = None entity.other = EntityProperty('Edm.Int64', 20) entity.clsid = EntityProperty( 'Edm.Guid', 'c9da6455-213d-42c9-9a79-3e9149a57833') return entity def _create_default_entity_dict(self, partition, row): ''' Creates a dictionary-based entity with fixed values, using all of the supported data types. ''' return {'PartitionKey': partition, 'RowKey': row, 'age': 39, 'sex': 'male', 'married': True, 'deceased': False, 'optional': None, 'ratio': 3.1, 'large': 9333111000, 'Birthday': datetime(1973, 10, 4), 'birthday': datetime(1970, 10, 4), 'other': EntityProperty('Edm.Int64', 20), 'clsid': EntityProperty( 'Edm.Guid', 'c9da6455-213d-42c9-9a79-3e9149a57833')} def _create_updated_entity_dict(self, partition, row): ''' Creates a dictionary-based entity with fixed values, with a different set of values than the default entity. It adds fields, changes field values, changes field types, and removes fields when compared to the default entity. ''' return {'PartitionKey': partition, 'RowKey': row, 'age': 'abc', 'sex': 'female', 'sign': 'aquarius', 'birthday': datetime(1991, 10, 4)} def _assert_default_entity(self, entity): ''' Asserts that the entity passed in matches the default entity. ''' self.assertEqual(entity.age, 39) self.assertEqual(entity.sex, 'male') self.assertEqual(entity.married, True) self.assertEqual(entity.deceased, False) self.assertFalse(hasattr(entity, "aquarius")) self.assertEqual(entity.ratio, 3.1) self.assertEqual(entity.large, 9333111000) self.assertEqual(entity.Birthday, datetime(1973, 10, 4, tzinfo=tzutc())) self.assertEqual(entity.birthday, datetime(1970, 10, 4, tzinfo=tzutc())) self.assertEqual(entity.other, 20) self.assertIsInstance(entity.clsid, EntityProperty) self.assertEqual(entity.clsid.type, 'Edm.Guid') self.assertEqual(entity.clsid.value, 'c9da6455-213d-42c9-9a79-3e9149a57833') self.assertTrue(hasattr(entity, "Timestamp")) def _assert_updated_entity(self, entity): ''' Asserts that the entity passed in matches the updated entity. ''' self.assertEqual(entity.age, 'abc') self.assertEqual(entity.sex, 'female') self.assertFalse(hasattr(entity, "married")) self.assertFalse(hasattr(entity, "deceased")) self.assertEqual(entity.sign, 'aquarius') self.assertFalse(hasattr(entity, "optional")) self.assertFalse(hasattr(entity, "ratio")) self.assertFalse(hasattr(entity, "large")) self.assertFalse(hasattr(entity, "Birthday")) self.assertEqual(entity.birthday, datetime(1991, 10, 4, tzinfo=tzutc())) self.assertFalse(hasattr(entity, "other")) self.assertFalse(hasattr(entity, "clsid")) self.assertTrue(hasattr(entity, "Timestamp")) def _assert_merged_entity(self, entity): ''' Asserts that the entity passed in matches the default entity merged with the updated entity. ''' self.assertEqual(entity.age, 'abc') self.assertEqual(entity.sex, 'female') self.assertEqual(entity.sign, 'aquarius') self.assertEqual(entity.married, True) self.assertEqual(entity.deceased, False) self.assertEqual(entity.sign, 'aquarius') self.assertEqual(entity.ratio, 3.1) self.assertEqual(entity.large, 9333111000) self.assertEqual(entity.Birthday, datetime(1973, 10, 4, tzinfo=tzutc())) self.assertEqual(entity.birthday, datetime(1991, 10, 4, tzinfo=tzutc())) self.assertEqual(entity.other, 20) self.assertIsInstance(entity.clsid, EntityProperty) self.assertEqual(entity.clsid.type, 'Edm.Guid') self.assertEqual(entity.clsid.value, 'c9da6455-213d-42c9-9a79-3e9149a57833') self.assertTrue(hasattr(entity, "Timestamp")) #--Test cases for table service ------------------------------------------- def test_get_set_table_service_properties(self): table_properties = self.ts.get_table_service_properties() self.ts.set_table_service_properties(table_properties) tests = [('logging.delete', True), ('logging.delete', False), ('logging.read', True), ('logging.read', False), ('logging.write', True), ('logging.write', False), ] for path, value in tests: # print path cur = table_properties for component in path.split('.')[:-1]: cur = getattr(cur, component) last_attr = path.split('.')[-1] setattr(cur, last_attr, value) self.ts.set_table_service_properties(table_properties) retry_count = 0 while retry_count < MAX_RETRY: table_properties = self.ts.get_table_service_properties() cur = table_properties for component in path.split('.'): cur = getattr(cur, component) if value == cur: break time.sleep(1) retry_count += 1 self.assertEqual(value, cur) def test_table_service_retention_single_set(self): table_properties = self.ts.get_table_service_properties() table_properties.logging.retention_policy.enabled = False table_properties.logging.retention_policy.days = 5 # TODO: Better error, ValueError? self.assertRaises(WindowsAzureError, self.ts.set_table_service_properties, table_properties) table_properties = self.ts.get_table_service_properties() table_properties.logging.retention_policy.days = None table_properties.logging.retention_policy.enabled = True # TODO: Better error, ValueError? self.assertRaises(WindowsAzureError, self.ts.set_table_service_properties, table_properties) def test_table_service_set_both(self): table_properties = self.ts.get_table_service_properties() table_properties.logging.retention_policy.enabled = True table_properties.logging.retention_policy.days = 5 self.ts.set_table_service_properties(table_properties) table_properties = self.ts.get_table_service_properties() self.assertEqual( True, table_properties.logging.retention_policy.enabled) self.assertEqual(5, table_properties.logging.retention_policy.days) #--Test cases for tables -------------------------------------------------- def test_create_table(self): # Arrange # Act created = self.ts.create_table(self.table_name) # Assert self.assertTrue(created) def test_create_table_fail_on_exist(self): # Arrange # Act created = self.ts.create_table(self.table_name, True) # Assert self.assertTrue(created) def test_create_table_with_already_existing_table(self): # Arrange # Act created1 = self.ts.create_table(self.table_name) created2 = self.ts.create_table(self.table_name) # Assert self.assertTrue(created1) self.assertFalse(created2) def test_create_table_with_already_existing_table_fail_on_exist(self): # Arrange # Act created = self.ts.create_table(self.table_name) with self.assertRaises(WindowsAzureError): self.ts.create_table(self.table_name, True) # Assert self.assertTrue(created) def test_query_tables(self): # Arrange self._create_table(self.table_name) # Act tables = self.ts.query_tables() for table in tables: pass # Assert tableNames = [x.name for x in tables] self.assertGreaterEqual(len(tableNames), 1) self.assertGreaterEqual(len(tables), 1) self.assertIn(self.table_name, tableNames) def test_query_tables_with_table_name(self): # Arrange self._create_table(self.table_name) # Act tables = self.ts.query_tables(self.table_name) for table in tables: pass # Assert self.assertEqual(len(tables), 1) self.assertEqual(tables[0].name, self.table_name) def test_query_tables_with_table_name_no_tables(self): # Arrange # Act with self.assertRaises(WindowsAzureError): self.ts.query_tables(self.table_name) # Assert def test_query_tables_with_top(self): # Arrange self.additional_table_names = [ self.table_name + suffix for suffix in 'abcd'] for name in self.additional_table_names: self.ts.create_table(name) # Act tables = self.ts.query_tables(None, 3) for table in tables: pass # Assert self.assertEqual(len(tables), 3) def test_query_tables_with_top_and_next_table_name(self): # Arrange self.additional_table_names = [ self.table_name + suffix for suffix in 'abcd'] for name in self.additional_table_names: self.ts.create_table(name) # Act tables_set1 = self.ts.query_tables(None, 3) tables_set2 = self.ts.query_tables( None, 3, tables_set1.x_ms_continuation['NextTableName']) # Assert self.assertEqual(len(tables_set1), 3) self.assertGreaterEqual(len(tables_set2), 1) self.assertLessEqual(len(tables_set2), 3) def test_delete_table_with_existing_table(self): # Arrange self._create_table(self.table_name) # Act deleted = self.ts.delete_table(self.table_name) # Assert self.assertTrue(deleted) tables = self.ts.query_tables() self.assertNamedItemNotInContainer(tables, self.table_name) def test_delete_table_with_existing_table_fail_not_exist(self): # Arrange self._create_table(self.table_name) # Act deleted = self.ts.delete_table(self.table_name, True) # Assert self.assertTrue(deleted) tables = self.ts.query_tables() self.assertNamedItemNotInContainer(tables, self.table_name) def test_delete_table_with_non_existing_table(self): # Arrange # Act deleted = self.ts.delete_table(self.table_name) # Assert self.assertFalse(deleted) def test_delete_table_with_non_existing_table_fail_not_exist(self): # Arrange # Act with self.assertRaises(WindowsAzureError): self.ts.delete_table(self.table_name, True) # Assert #--Test cases for entities ------------------------------------------ def test_insert_entity_dictionary(self): # Arrange self._create_table(self.table_name) # Act dict = self._create_default_entity_dict('MyPartition', '1') resp = self.ts.insert_entity(self.table_name, dict) # Assert self.assertIsNotNone(resp) def test_insert_entity_class_instance(self): # Arrange self._create_table(self.table_name) # Act entity = self._create_default_entity_class('MyPartition', '1') resp = self.ts.insert_entity(self.table_name, entity) # Assert self.assertIsNotNone(resp) def test_insert_entity_conflict(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act with self.assertRaises(WindowsAzureError): self.ts.insert_entity( self.table_name, self._create_default_entity_dict('MyPartition', '1')) # Assert def test_get_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act resp = self.ts.get_entity(self.table_name, 'MyPartition', '1') # Assert self.assertEqual(resp.PartitionKey, 'MyPartition') self.assertEqual(resp.RowKey, '1') self._assert_default_entity(resp) def test_get_entity_not_existing(self): # Arrange self._create_table(self.table_name) # Act with self.assertRaises(WindowsAzureError): self.ts.get_entity(self.table_name, 'MyPartition', '1') # Assert def test_get_entity_with_select(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act resp = self.ts.get_entity( self.table_name, 'MyPartition', '1', 'age,sex') # Assert self.assertEqual(resp.age, 39) self.assertEqual(resp.sex, 'male') self.assertFalse(hasattr(resp, "birthday")) self.assertFalse(hasattr(resp, "married")) self.assertFalse(hasattr(resp, "deceased")) def test_query_entities(self): # Arrange self._create_table_with_default_entities(self.table_name, 2) # Act resp = self.ts.query_entities(self.table_name) # Assert self.assertEqual(len(resp), 2) for entity in resp: self.assertEqual(entity.PartitionKey, 'MyPartition') self._assert_default_entity(entity) self.assertEqual(resp[0].RowKey, '1') self.assertEqual(resp[1].RowKey, '2') def test_query_entities_large(self): # Arrange self._create_table(self.table_name) total_entities_count = 1000 entities_per_batch = 50 for j in range(total_entities_count // entities_per_batch): self.ts.begin_batch() for i in range(entities_per_batch): entity = Entity() entity.PartitionKey = 'large' entity.RowKey = 'batch{0}-item{1}'.format(j, i) entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'hello world;' * 100 entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.ts.insert_entity(self.table_name, entity) self.ts.commit_batch() # Act start_time = datetime.now() resp = self.ts.query_entities(self.table_name) elapsed_time = datetime.now() - start_time # Assert print('query_entities took {0} secs.'.format(elapsed_time.total_seconds())) # azure allocates 5 seconds to execute a query # if it runs slowly, it will return fewer results and make the test fail self.assertEqual(len(resp), total_entities_count) def test_query_entities_with_filter(self): # Arrange self._create_table_with_default_entities(self.table_name, 2) self.ts.insert_entity( self.table_name, self._create_default_entity_dict('MyOtherPartition', '3')) # Act resp = self.ts.query_entities( self.table_name, "PartitionKey eq 'MyPartition'") # Assert self.assertEqual(len(resp), 2) for entity in resp: self.assertEqual(entity.PartitionKey, 'MyPartition') self._assert_default_entity(entity) def test_query_entities_with_select(self): # Arrange self._create_table_with_default_entities(self.table_name, 2) # Act resp = self.ts.query_entities(self.table_name, None, 'age,sex') # Assert self.assertEqual(len(resp), 2) self.assertEqual(resp[0].age, 39) self.assertEqual(resp[0].sex, 'male') self.assertFalse(hasattr(resp[0], "birthday")) self.assertFalse(hasattr(resp[0], "married")) self.assertFalse(hasattr(resp[0], "deceased")) def test_query_entities_with_top(self): # Arrange self._create_table_with_default_entities(self.table_name, 3) # Act resp = self.ts.query_entities(self.table_name, None, None, 2) # Assert self.assertEqual(len(resp), 2) def test_query_entities_with_top_and_next(self): # Arrange self._create_table_with_default_entities(self.table_name, 5) # Act resp1 = self.ts.query_entities(self.table_name, None, None, 2) resp2 = self.ts.query_entities( self.table_name, None, None, 2, resp1.x_ms_continuation['NextPartitionKey'], resp1.x_ms_continuation['NextRowKey']) resp3 = self.ts.query_entities( self.table_name, None, None, 2, resp2.x_ms_continuation['NextPartitionKey'], resp2.x_ms_continuation['NextRowKey']) # Assert self.assertEqual(len(resp1), 2) self.assertEqual(len(resp2), 2) self.assertEqual(len(resp3), 1) self.assertEqual(resp1[0].RowKey, '1') self.assertEqual(resp1[1].RowKey, '2') self.assertEqual(resp2[0].RowKey, '3') self.assertEqual(resp2[1].RowKey, '4') self.assertEqual(resp3[0].RowKey, '5') def test_update_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.ts.update_entity( self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.ts.get_entity( self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_update_entity_with_if_matches(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.ts.update_entity( self.table_name, 'MyPartition', '1', sent_entity, if_match=entities[0].etag) # Assert self.assertIsNotNone(resp) received_entity = self.ts.get_entity( self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_update_entity_with_if_doesnt_match(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') with self.assertRaises(WindowsAzureError): self.ts.update_entity( self.table_name, 'MyPartition', '1', sent_entity, if_match=u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"') # Assert def test_insert_or_merge_entity_with_existing_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.ts.insert_or_merge_entity( self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.ts.get_entity( self.table_name, 'MyPartition', '1') self._assert_merged_entity(received_entity) def test_insert_or_merge_entity_with_non_existing_entity(self): # Arrange self._create_table(self.table_name) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.ts.insert_or_merge_entity( self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.ts.get_entity( self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_insert_or_replace_entity_with_existing_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.ts.insert_or_replace_entity( self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.ts.get_entity( self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_insert_or_replace_entity_with_non_existing_entity(self): # Arrange self._create_table(self.table_name) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.ts.insert_or_replace_entity( self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.ts.get_entity( self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_merge_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.ts.merge_entity( self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.ts.get_entity( self.table_name, 'MyPartition', '1') self._assert_merged_entity(received_entity) def test_merge_entity_not_existing(self): # Arrange self._create_table(self.table_name) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') with self.assertRaises(WindowsAzureError): self.ts.merge_entity( self.table_name, 'MyPartition', '1', sent_entity) # Assert def test_merge_entity_with_if_matches(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.ts.merge_entity( self.table_name, 'MyPartition', '1', sent_entity, if_match=entities[0].etag) # Assert self.assertIsNotNone(resp) received_entity = self.ts.get_entity( self.table_name, 'MyPartition', '1') self._assert_merged_entity(received_entity) def test_merge_entity_with_if_doesnt_match(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') with self.assertRaises(WindowsAzureError): self.ts.merge_entity( self.table_name, 'MyPartition', '1', sent_entity, if_match=u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"') # Assert def test_delete_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act resp = self.ts.delete_entity(self.table_name, 'MyPartition', '1') # Assert self.assertIsNone(resp) with self.assertRaises(WindowsAzureError): self.ts.get_entity(self.table_name, 'MyPartition', '1') def test_delete_entity_not_existing(self): # Arrange self._create_table(self.table_name) # Act with self.assertRaises(WindowsAzureError): self.ts.delete_entity(self.table_name, 'MyPartition', '1') # Assert def test_delete_entity_with_if_matches(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act resp = self.ts.delete_entity( self.table_name, 'MyPartition', '1', if_match=entities[0].etag) # Assert self.assertIsNone(resp) with self.assertRaises(WindowsAzureError): self.ts.get_entity(self.table_name, 'MyPartition', '1') def test_delete_entity_with_if_doesnt_match(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act with self.assertRaises(WindowsAzureError): self.ts.delete_entity( self.table_name, 'MyPartition', '1', if_match=u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"') # Assert #--Test cases for batch --------------------------------------------- def test_with_filter_single(self): called = [] def my_filter(request, next): called.append(True) return next(request) tc = self.ts.with_filter(my_filter) tc.create_table(self.table_name) self.assertTrue(called) del called[:] tc.delete_table(self.table_name) self.assertTrue(called) del called[:] def test_with_filter_chained(self): called = [] def filter_a(request, next): called.append('a') return next(request) def filter_b(request, next): called.append('b') return next(request) tc = self.ts.with_filter(filter_a).with_filter(filter_b) tc.create_table(self.table_name) self.assertEqual(called, ['b', 'a']) tc.delete_table(self.table_name) def test_batch_insert(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.ts.begin_batch() self.ts.insert_entity(self.table_name, entity) self.ts.commit_batch() # Assert result = self.ts.get_entity(self.table_name, '001', 'batch_insert') self.assertIsNotNone(result) def test_batch_update(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_update' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.ts.insert_entity(self.table_name, entity) entity = self.ts.get_entity(self.table_name, '001', 'batch_update') self.assertEqual(3, entity.test3) entity.test2 = 'value1' self.ts.begin_batch() self.ts.update_entity(self.table_name, '001', 'batch_update', entity) self.ts.commit_batch() entity = self.ts.get_entity(self.table_name, '001', 'batch_update') # Assert self.assertEqual('value1', entity.test2) def test_batch_merge(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_merge' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.ts.insert_entity(self.table_name, entity) entity = self.ts.get_entity(self.table_name, '001', 'batch_merge') self.assertEqual(3, entity.test3) entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_merge' entity.test2 = 'value1' self.ts.begin_batch() self.ts.merge_entity(self.table_name, '001', 'batch_merge', entity) self.ts.commit_batch() entity = self.ts.get_entity(self.table_name, '001', 'batch_merge') # Assert self.assertEqual('value1', entity.test2) self.assertEqual(1234567890, entity.test4) def test_batch_update_if_match(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') self.ts.begin_batch() resp = self.ts.update_entity( self.table_name, 'MyPartition', '1', sent_entity, if_match=entities[0].etag) self.ts.commit_batch() # Assert self.assertIsNone(resp) received_entity = self.ts.get_entity( self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_batch_update_if_doesnt_match(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 2) # Act sent_entity1 = self._create_updated_entity_dict('MyPartition', '1') sent_entity2 = self._create_updated_entity_dict('MyPartition', '2') self.ts.begin_batch() self.ts.update_entity( self.table_name, 'MyPartition', '1', sent_entity1, if_match=u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"') self.ts.update_entity( self.table_name, 'MyPartition', '2', sent_entity2) try: self.ts.commit_batch() except WindowsAzureBatchOperationError as error: self.assertEqual(error.code, 'UpdateConditionNotSatisfied') self.assertTrue(str(error).startswith('0:The update condition specified in the request was not satisfied.')) else: self.fail('WindowsAzureBatchOperationError was expected') # Assert received_entity = self.ts.get_entity( self.table_name, 'MyPartition', '1') self._assert_default_entity(received_entity) received_entity = self.ts.get_entity( self.table_name, 'MyPartition', '2') self._assert_default_entity(received_entity) def test_batch_insert_replace(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert_replace' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.ts.begin_batch() self.ts.insert_or_replace_entity( self.table_name, entity.PartitionKey, entity.RowKey, entity) self.ts.commit_batch() entity = self.ts.get_entity( self.table_name, '001', 'batch_insert_replace') # Assert self.assertIsNotNone(entity) self.assertEqual('value', entity.test2) self.assertEqual(1234567890, entity.test4) def test_batch_insert_merge(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert_merge' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.ts.begin_batch() self.ts.insert_or_merge_entity( self.table_name, entity.PartitionKey, entity.RowKey, entity) self.ts.commit_batch() entity = self.ts.get_entity( self.table_name, '001', 'batch_insert_merge') # Assert self.assertIsNotNone(entity) self.assertEqual('value', entity.test2) self.assertEqual(1234567890, entity.test4) def test_batch_delete(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_delete' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.ts.insert_entity(self.table_name, entity) entity = self.ts.get_entity(self.table_name, '001', 'batch_delete') #self.assertEqual(3, entity.test3) self.ts.begin_batch() self.ts.delete_entity(self.table_name, '001', 'batch_delete') self.ts.commit_batch() def test_batch_inserts(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') self.ts.begin_batch() for i in range(100): entity.RowKey = str(i) self.ts.insert_entity(self.table_name, entity) self.ts.commit_batch() entities = self.ts.query_entities( self.table_name, "PartitionKey eq 'batch_inserts'", '') # Assert self.assertIsNotNone(entities) self.assertEqual(100, len(entities)) def test_batch_all_operations_together(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '003' entity.RowKey = 'batch_all_operations_together-1' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.ts.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-2' self.ts.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-3' self.ts.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-4' self.ts.insert_entity(self.table_name, entity) self.ts.begin_batch() entity.RowKey = 'batch_all_operations_together' self.ts.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-1' self.ts.delete_entity( self.table_name, entity.PartitionKey, entity.RowKey) entity.RowKey = 'batch_all_operations_together-2' entity.test3 = 10 self.ts.update_entity( self.table_name, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 self.ts.merge_entity( self.table_name, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-4' entity.test3 = 10 self.ts.insert_or_replace_entity( self.table_name, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-5' self.ts.insert_or_merge_entity( self.table_name, entity.PartitionKey, entity.RowKey, entity) self.ts.commit_batch() # Assert entities = self.ts.query_entities( self.table_name, "PartitionKey eq '003'", '') self.assertEqual(5, len(entities)) def test_batch_same_row_operations_fail(self): # Arrange self._create_table(self.table_name) entity = self._create_default_entity_dict('001', 'batch_negative_1') self.ts.insert_entity(self.table_name, entity) # Act with self.assertRaises(WindowsAzureError): self.ts.begin_batch() entity = self._create_updated_entity_dict( '001', 'batch_negative_1') self.ts.update_entity( self.table_name, entity['PartitionKey'], entity['RowKey'], entity) entity = self._create_default_entity_dict( '001', 'batch_negative_1') self.ts.merge_entity( self.table_name, entity['PartitionKey'], entity['RowKey'], entity) self.ts.cancel_batch() # Assert def test_batch_different_partition_operations_fail(self): # Arrange self._create_table(self.table_name) entity = self._create_default_entity_dict('001', 'batch_negative_1') self.ts.insert_entity(self.table_name, entity) # Act with self.assertRaises(WindowsAzureError): self.ts.begin_batch() entity = self._create_updated_entity_dict( '001', 'batch_negative_1') self.ts.update_entity( self.table_name, entity['PartitionKey'], entity['RowKey'], entity) entity = self._create_default_entity_dict( '002', 'batch_negative_1') self.ts.insert_entity(self.table_name, entity) self.ts.cancel_batch() # Assert def test_batch_different_table_operations_fail(self): # Arrange other_table_name = self.table_name + 'other' self.additional_table_names = [other_table_name] self._create_table(self.table_name) self._create_table(other_table_name) # Act with self.assertRaises(WindowsAzureError): self.ts.begin_batch() entity = self._create_default_entity_dict( '001', 'batch_negative_1') self.ts.insert_entity(self.table_name, entity) entity = self._create_default_entity_dict( '001', 'batch_negative_2') self.ts.insert_entity(other_table_name, entity) self.ts.cancel_batch() def test_unicode_property_value(self): ''' regression test for github issue #57''' # Act self._create_table(self.table_name) self.ts.insert_entity( self.table_name, {'PartitionKey': 'test', 'RowKey': 'test1', 'Description': u'ꀕ'}) self.ts.insert_entity( self.table_name, {'PartitionKey': 'test', 'RowKey': 'test2', 'Description': 'ꀕ'}) resp = self.ts.query_entities( self.table_name, "PartitionKey eq 'test'") # Assert self.assertEqual(len(resp), 2) self.assertEqual(resp[0].Description, u'ꀕ') self.assertEqual(resp[1].Description, u'ꀕ') def test_unicode_property_name(self): # Act self._create_table(self.table_name) self.ts.insert_entity( self.table_name, {'PartitionKey': 'test', 'RowKey': 'test1', u'啊齄丂狛狜': u'ꀕ'}) self.ts.insert_entity( self.table_name, {'PartitionKey': 'test', 'RowKey': 'test2', u'啊齄丂狛狜': 'hello'}) resp = self.ts.query_entities( self.table_name, "PartitionKey eq 'test'") # Assert self.assertEqual(len(resp), 2) self.assertEqual(resp[0].__dict__[u'啊齄丂狛狜'], u'ꀕ') self.assertEqual(resp[1].__dict__[u'啊齄丂狛狜'], u'hello') def test_unicode_create_table_unicode_name(self): # Arrange self.table_name = self.table_name + u'啊齄丂狛狜' # Act with self.assertRaises(WindowsAzureError): # not supported - table name must be alphanumeric, lowercase self.ts.create_table(self.table_name) # Assert def test_empty_and_spaces_property_value(self): # Act self._create_table(self.table_name) self.ts.insert_entity( self.table_name, { 'PartitionKey': 'test', 'RowKey': 'test1', 'EmptyByte': '', 'EmptyUnicode': u'', 'SpacesOnlyByte': ' ', 'SpacesOnlyUnicode': u' ', 'SpacesBeforeByte': ' Text', 'SpacesBeforeUnicode': u' Text', 'SpacesAfterByte': 'Text ', 'SpacesAfterUnicode': u'Text ', 'SpacesBeforeAndAfterByte': ' Text ', 'SpacesBeforeAndAfterUnicode': u' Text ', }) resp = self.ts.get_entity(self.table_name, 'test', 'test1') # Assert self.assertIsNotNone(resp) self.assertEqual(resp.EmptyByte, '') self.assertEqual(resp.EmptyUnicode, u'') self.assertEqual(resp.SpacesOnlyByte, ' ') self.assertEqual(resp.SpacesOnlyUnicode, u' ') self.assertEqual(resp.SpacesBeforeByte, ' Text') self.assertEqual(resp.SpacesBeforeUnicode, u' Text') self.assertEqual(resp.SpacesAfterByte, 'Text ') self.assertEqual(resp.SpacesAfterUnicode, u'Text ') self.assertEqual(resp.SpacesBeforeAndAfterByte, ' Text ') self.assertEqual(resp.SpacesBeforeAndAfterUnicode, u' Text ') def test_none_property_value(self): # Act self._create_table(self.table_name) self.ts.insert_entity( self.table_name, { 'PartitionKey': 'test', 'RowKey': 'test1', 'NoneValue': None, }) resp = self.ts.get_entity(self.table_name, 'test', 'test1') # Assert self.assertIsNotNone(resp) self.assertFalse(hasattr(resp, 'NoneValue')) def test_binary_property_value(self): # Act binary_data = b'\x01\x02\x03\x04\x05\x06\x07\x08\t\n' self._create_table(self.table_name) self.ts.insert_entity( self.table_name, { 'PartitionKey': 'test', 'RowKey': 'test1', 'binary': EntityProperty('Edm.Binary', binary_data) }) resp = self.ts.get_entity(self.table_name, 'test', 'test1') # Assert self.assertIsNotNone(resp) self.assertEqual(resp.binary.type, 'Edm.Binary') self.assertEqual(resp.binary.value, binary_data) def test_timezone(self): # Act local_tz = tzoffset('BRST', -10800) local_date = datetime(2003, 9, 27, 9, 52, 43, tzinfo=local_tz) self._create_table(self.table_name) self.ts.insert_entity( self.table_name, { 'PartitionKey': 'test', 'RowKey': 'test1', 'date': local_date, }) resp = self.ts.get_entity(self.table_name, 'test', 'test1') # Assert self.assertIsNotNone(resp) self.assertEqual(resp.date, local_date.astimezone(tzutc())) self.assertEqual(resp.date.astimezone(local_tz), local_date)
#update test cat's owner #whom we will send messages from azure.storage import TableService import config table_service = TableService(account_name=config.ACC_NAME, account_key=config.ACC_KEY) #newMaster = {'masterID' : '188622142'} #table_service.update_entity('bandcredentials', 'band','test', newMaster) #table_service.insert_entity('bandcredentials', newMaster) task = table_service.get_entity('bandcredentials', 'band', 'test') print task.masterID
#coding:utf8 from azure.storage import TableService, Entity table_service = TableService(account_name='portalvhdspbrd34f2fnbl', account_key='y48JkXg+VcHQRCgsylJf4xV4Fd0AuJNkQKSwGhAR+BppHnFhkI+UHPOS/oYaTo0rqFCGQkEBW+thNFZNB9W8yg==') task = table_service.get_entity('tasktable', 'tasksSeattle', '1') print(task.description) print(task.priority)
from azure.storage import TableService, Entity table_service = TableService(account_name='myaccount', account_key='myKey') task = table_service.get_entity('tasktable', 'toDoTasks', '1') print(task.description) print(task.priority) task = table_service.get_entity('tasktable', 'toDoTasks', '4') print(task.description) print(task.priority)
class TableServiceTest(AzureTestCase): def setUp(self): self.tc = TableService(account_name=credentials.getStorageServicesName(), account_key=credentials.getStorageServicesKey()) proxy_host = credentials.getProxyHost() proxy_port = credentials.getProxyPort() if proxy_host: self.tc.set_proxy(proxy_host, proxy_port) __uid = getUniqueTestRunID() table_base_name = u'testtable%s' % (__uid) self.table_name = getUniqueNameBasedOnCurrentTime(table_base_name) self.additional_table_names = [] def tearDown(self): self.cleanup() return super(TableServiceTest, self).tearDown() def cleanup(self): try: self.tc.delete_table(self.table_name) except: pass for name in self.additional_table_names: try: self.tc.delete_table(name) except: pass #--Helpers----------------------------------------------------------------- def _create_table(self, table_name): ''' Creates a table with the specified name. ''' self.tc.create_table(table_name, True) def _create_table_with_default_entities(self, table_name, entity_count): ''' Creates a table with the specified name and adds entities with the default set of values. PartitionKey is set to 'MyPartition' and RowKey is set to a unique counter value starting at 1 (as a string). ''' entities = [] self._create_table(table_name) for i in range(1, entity_count + 1): entities.append(self.tc.insert_entity(table_name, self._create_default_entity_dict('MyPartition', str(i)))) return entities def _create_default_entity_class(self, partition, row): ''' Creates a class-based entity with fixed values, using all of the supported data types. ''' # TODO: Edm.Binary and null entity = Entity() entity.PartitionKey = partition entity.RowKey = row entity.age = 39 entity.sex = 'male' entity.married = True entity.deceased = False entity.optional = None entity.ratio = 3.1 entity.large = 9333111000 entity.Birthday = datetime(1973,10,04) entity.birthday = datetime(1970,10,04) entity.binary = None entity.other = EntityProperty('Edm.Int64', 20) entity.clsid = EntityProperty('Edm.Guid', 'c9da6455-213d-42c9-9a79-3e9149a57833') return entity def _create_default_entity_dict(self, partition, row): ''' Creates a dictionary-based entity with fixed values, using all of the supported data types. ''' # TODO: Edm.Binary and null return {'PartitionKey':partition, 'RowKey':row, 'age':39, 'sex':'male', 'married':True, 'deceased':False, 'optional':None, 'ratio':3.1, 'large':9333111000, 'Birthday':datetime(1973,10,04), 'birthday':datetime(1970,10,04), 'binary':EntityProperty('Edm.Binary', None), 'other':EntityProperty('Edm.Int64', 20), 'clsid':EntityProperty('Edm.Guid', 'c9da6455-213d-42c9-9a79-3e9149a57833')} def _create_updated_entity_dict(self, partition, row): ''' Creates a dictionary-based entity with fixed values, with a different set of values than the default entity. It adds fields, changes field values, changes field types, and removes fields when compared to the default entity. ''' return {'PartitionKey':partition, 'RowKey':row, 'age':'abc', 'sex':'female', 'sign':'aquarius', 'birthday':datetime(1991,10,04)} def _assert_default_entity(self, entity): ''' Asserts that the entity passed in matches the default entity. ''' self.assertEquals(entity.age, 39) self.assertEquals(entity.sex, 'male') self.assertEquals(entity.married, True) self.assertEquals(entity.deceased, False) self.assertFalse(hasattr(entity, "aquarius")) self.assertEquals(entity.ratio, 3.1) self.assertEquals(entity.large, 9333111000) self.assertEquals(entity.Birthday, datetime(1973,10,04)) self.assertEquals(entity.birthday, datetime(1970,10,04)) self.assertEquals(entity.other, 20) self.assertIsInstance(entity.clsid, EntityProperty) self.assertEquals(entity.clsid.type, 'Edm.Guid') self.assertEquals(entity.clsid.value, 'c9da6455-213d-42c9-9a79-3e9149a57833') def _assert_updated_entity(self, entity): ''' Asserts that the entity passed in matches the updated entity. ''' self.assertEquals(entity.age, 'abc') self.assertEquals(entity.sex, 'female') self.assertFalse(hasattr(entity, "married")) self.assertFalse(hasattr(entity, "deceased")) self.assertEquals(entity.sign, 'aquarius') self.assertFalse(hasattr(entity, "optional")) self.assertFalse(hasattr(entity, "ratio")) self.assertFalse(hasattr(entity, "large")) self.assertFalse(hasattr(entity, "Birthday")) self.assertEquals(entity.birthday, datetime(1991,10,04)) self.assertFalse(hasattr(entity, "other")) self.assertFalse(hasattr(entity, "clsid")) def _assert_merged_entity(self, entity): ''' Asserts that the entity passed in matches the default entity merged with the updated entity. ''' self.assertEquals(entity.age, 'abc') self.assertEquals(entity.sex, 'female') self.assertEquals(entity.sign, 'aquarius') self.assertEquals(entity.married, True) self.assertEquals(entity.deceased, False) self.assertEquals(entity.sign, 'aquarius') self.assertEquals(entity.ratio, 3.1) self.assertEquals(entity.large, 9333111000) self.assertEquals(entity.Birthday, datetime(1973,10,04)) self.assertEquals(entity.birthday, datetime(1991,10,04)) self.assertEquals(entity.other, 20) self.assertIsInstance(entity.clsid, EntityProperty) self.assertEquals(entity.clsid.type, 'Edm.Guid') self.assertEquals(entity.clsid.value, 'c9da6455-213d-42c9-9a79-3e9149a57833') #--Test cases for table service ------------------------------------------- def test_get_set_table_service_properties(self): table_properties = self.tc.get_table_service_properties() self.tc.set_table_service_properties(table_properties) tests = [('logging.delete', True), ('logging.delete', False), ('logging.read', True), ('logging.read', False), ('logging.write', True), ('logging.write', False), ] for path, value in tests: #print path cur = table_properties for component in path.split('.')[:-1]: cur = getattr(cur, component) last_attr = path.split('.')[-1] setattr(cur, last_attr, value) self.tc.set_table_service_properties(table_properties) retry_count = 0 while retry_count < MAX_RETRY: table_properties = self.tc.get_table_service_properties() cur = table_properties for component in path.split('.'): cur = getattr(cur, component) if value == cur: break time.sleep(1) retry_count += 1 self.assertEquals(value, cur) def test_table_service_retention_single_set(self): table_properties = self.tc.get_table_service_properties() table_properties.logging.retention_policy.enabled = False table_properties.logging.retention_policy.days = 5 # TODO: Better error, ValueError? self.assertRaises(WindowsAzureError, self.tc.set_table_service_properties, table_properties) table_properties = self.tc.get_table_service_properties() table_properties.logging.retention_policy.days = None table_properties.logging.retention_policy.enabled = True # TODO: Better error, ValueError? self.assertRaises(WindowsAzureError, self.tc.set_table_service_properties, table_properties) def test_table_service_set_both(self): table_properties = self.tc.get_table_service_properties() table_properties.logging.retention_policy.enabled = True table_properties.logging.retention_policy.days = 5 self.tc.set_table_service_properties(table_properties) table_properties = self.tc.get_table_service_properties() self.assertEquals(True, table_properties.logging.retention_policy.enabled) self.assertEquals(5, table_properties.logging.retention_policy.days) #--Test cases for tables -------------------------------------------------- def test_create_table(self): # Arrange # Act created = self.tc.create_table(self.table_name) # Assert self.assertTrue(created) def test_create_table_fail_on_exist(self): # Arrange # Act created = self.tc.create_table(self.table_name, True) # Assert self.assertTrue(created) def test_create_table_with_already_existing_table(self): # Arrange # Act created1 = self.tc.create_table(self.table_name) created2 = self.tc.create_table(self.table_name) # Assert self.assertTrue(created1) self.assertFalse(created2) def test_create_table_with_already_existing_table_fail_on_exist(self): # Arrange # Act created = self.tc.create_table(self.table_name) with self.assertRaises(WindowsAzureError): self.tc.create_table(self.table_name, True) # Assert self.assertTrue(created) def test_query_tables(self): # Arrange self._create_table(self.table_name) # Act tables = self.tc.query_tables() for table in tables: pass # Assert tableNames = [x.name for x in tables] self.assertGreaterEqual(len(tableNames), 1) self.assertGreaterEqual(len(tables), 1) self.assertIn(self.table_name, tableNames) def test_query_tables_with_table_name(self): # Arrange self._create_table(self.table_name) # Act tables = self.tc.query_tables(self.table_name) for table in tables: pass # Assert self.assertEqual(len(tables), 1) self.assertEqual(tables[0].name, self.table_name) def test_query_tables_with_table_name_no_tables(self): # Arrange # Act with self.assertRaises(WindowsAzureError): self.tc.query_tables(self.table_name) # Assert def test_query_tables_with_top(self): # Arrange self.additional_table_names = [self.table_name + suffix for suffix in 'abcd'] for name in self.additional_table_names: self.tc.create_table(name) # Act tables = self.tc.query_tables(None, 3) for table in tables: pass # Assert self.assertEqual(len(tables), 3) def test_query_tables_with_top_and_next_table_name(self): # Arrange self.additional_table_names = [self.table_name + suffix for suffix in 'abcd'] for name in self.additional_table_names: self.tc.create_table(name) # Act tables_set1 = self.tc.query_tables(None, 3) tables_set2 = self.tc.query_tables(None, 3, tables_set1.x_ms_continuation['NextTableName']) # Assert self.assertEqual(len(tables_set1), 3) self.assertGreaterEqual(len(tables_set2), 1) self.assertLessEqual(len(tables_set2), 3) def test_delete_table_with_existing_table(self): # Arrange self._create_table(self.table_name) # Act deleted = self.tc.delete_table(self.table_name) # Assert self.assertTrue(deleted) tables = self.tc.query_tables() self.assertNamedItemNotInContainer(tables, self.table_name) def test_delete_table_with_existing_table_fail_not_exist(self): # Arrange self._create_table(self.table_name) # Act deleted = self.tc.delete_table(self.table_name, True) # Assert self.assertTrue(deleted) tables = self.tc.query_tables() self.assertNamedItemNotInContainer(tables, self.table_name) def test_delete_table_with_non_existing_table(self): # Arrange # Act deleted = self.tc.delete_table(self.table_name) # Assert self.assertFalse(deleted) def test_delete_table_with_non_existing_table_fail_not_exist(self): # Arrange # Act with self.assertRaises(WindowsAzureError): self.tc.delete_table(self.table_name, True) # Assert #--Test cases for entities ------------------------------------------ def test_insert_entity_dictionary(self): # Arrange self._create_table(self.table_name) # Act dict = self._create_default_entity_dict('MyPartition', '1') resp = self.tc.insert_entity(self.table_name, dict) # Assert self.assertIsNotNone(resp) def test_insert_entity_class_instance(self): # Arrange self._create_table(self.table_name) # Act entity = self._create_default_entity_class('MyPartition', '1') resp = self.tc.insert_entity(self.table_name, entity) # Assert self.assertIsNotNone(resp) def test_insert_entity_conflict(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act with self.assertRaises(WindowsAzureError): self.tc.insert_entity(self.table_name, self._create_default_entity_dict('MyPartition', '1')) # Assert def test_get_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act resp = self.tc.get_entity(self.table_name, 'MyPartition', '1') # Assert self.assertEquals(resp.PartitionKey, 'MyPartition') self.assertEquals(resp.RowKey, '1') self._assert_default_entity(resp) def test_get_entity_not_existing(self): # Arrange self._create_table(self.table_name) # Act with self.assertRaises(WindowsAzureError): self.tc.get_entity(self.table_name, 'MyPartition', '1') # Assert def test_get_entity_with_select(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act resp = self.tc.get_entity(self.table_name, 'MyPartition', '1', 'age,sex') # Assert self.assertEquals(resp.age, 39) self.assertEquals(resp.sex, 'male') self.assertFalse(hasattr(resp, "birthday")) self.assertFalse(hasattr(resp, "married")) self.assertFalse(hasattr(resp, "deceased")) def test_query_entities(self): # Arrange self._create_table_with_default_entities(self.table_name, 2) # Act resp = self.tc.query_entities(self.table_name) # Assert self.assertEquals(len(resp), 2) for entity in resp: self.assertEquals(entity.PartitionKey, 'MyPartition') self._assert_default_entity(entity) self.assertEquals(resp[0].RowKey, '1') self.assertEquals(resp[1].RowKey, '2') def test_query_entities_with_filter(self): # Arrange self._create_table_with_default_entities(self.table_name, 2) self.tc.insert_entity(self.table_name, self._create_default_entity_dict('MyOtherPartition', '3')) # Act resp = self.tc.query_entities(self.table_name, "PartitionKey eq 'MyPartition'") # Assert self.assertEquals(len(resp), 2) for entity in resp: self.assertEquals(entity.PartitionKey, 'MyPartition') self._assert_default_entity(entity) def test_query_entities_with_select(self): # Arrange self._create_table_with_default_entities(self.table_name, 2) # Act resp = self.tc.query_entities(self.table_name, None, 'age,sex') # Assert self.assertEquals(len(resp), 2) self.assertEquals(resp[0].age, 39) self.assertEquals(resp[0].sex, 'male') self.assertFalse(hasattr(resp[0], "birthday")) self.assertFalse(hasattr(resp[0], "married")) self.assertFalse(hasattr(resp[0], "deceased")) def test_query_entities_with_top(self): # Arrange self._create_table_with_default_entities(self.table_name, 3) # Act resp = self.tc.query_entities(self.table_name, None, None, 2) # Assert self.assertEquals(len(resp), 2) def test_query_entities_with_top_and_next(self): # Arrange self._create_table_with_default_entities(self.table_name, 5) # Act resp1 = self.tc.query_entities(self.table_name, None, None, 2) resp2 = self.tc.query_entities(self.table_name, None, None, 2, resp1.x_ms_continuation['NextPartitionKey'], resp1.x_ms_continuation['NextRowKey']) resp3 = self.tc.query_entities(self.table_name, None, None, 2, resp2.x_ms_continuation['NextPartitionKey'], resp2.x_ms_continuation['NextRowKey']) # Assert self.assertEquals(len(resp1), 2) self.assertEquals(len(resp2), 2) self.assertEquals(len(resp3), 1) self.assertEquals(resp1[0].RowKey, '1') self.assertEquals(resp1[1].RowKey, '2') self.assertEquals(resp2[0].RowKey, '3') self.assertEquals(resp2[1].RowKey, '4') self.assertEquals(resp3[0].RowKey, '5') def test_update_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition','1') resp = self.tc.update_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_update_entity_with_if_matches(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition','1') resp = self.tc.update_entity(self.table_name, 'MyPartition', '1', sent_entity, if_match=entities[0].etag) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_update_entity_with_if_doesnt_match(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition','1') with self.assertRaises(WindowsAzureError): self.tc.update_entity(self.table_name, 'MyPartition', '1', sent_entity, if_match=u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"') # Assert def test_insert_or_merge_entity_with_existing_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition','1') resp = self.tc.insert_or_merge_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_merged_entity(received_entity) def test_insert_or_merge_entity_with_non_existing_entity(self): # Arrange self._create_table(self.table_name) # Act sent_entity = self._create_updated_entity_dict('MyPartition','1') resp = self.tc.insert_or_merge_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_insert_or_replace_entity_with_existing_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition','1') resp = self.tc.insert_or_replace_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_insert_or_replace_entity_with_non_existing_entity(self): # Arrange self._create_table(self.table_name) # Act sent_entity = self._create_updated_entity_dict('MyPartition','1') resp = self.tc.insert_or_replace_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_merge_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition','1') resp = self.tc.merge_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_merged_entity(received_entity) def test_merge_entity_not_existing(self): # Arrange self._create_table(self.table_name) # Act sent_entity = self._create_updated_entity_dict('MyPartition','1') with self.assertRaises(WindowsAzureError): self.tc.merge_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert def test_merge_entity_with_if_matches(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition','1') resp = self.tc.merge_entity(self.table_name, 'MyPartition', '1', sent_entity, if_match=entities[0].etag) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_merged_entity(received_entity) def test_merge_entity_with_if_doesnt_match(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition','1') with self.assertRaises(WindowsAzureError): self.tc.merge_entity(self.table_name, 'MyPartition', '1', sent_entity, if_match=u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"') # Assert def test_delete_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act resp = self.tc.delete_entity(self.table_name, 'MyPartition', '1') # Assert self.assertIsNone(resp) with self.assertRaises(WindowsAzureError): self.tc.get_entity(self.table_name, 'MyPartition', '1') def test_delete_entity_not_existing(self): # Arrange self._create_table(self.table_name) # Act with self.assertRaises(WindowsAzureError): self.tc.delete_entity(self.table_name, 'MyPartition', '1') # Assert def test_delete_entity_with_if_matches(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act resp = self.tc.delete_entity(self.table_name, 'MyPartition', '1', if_match=entities[0].etag) # Assert self.assertIsNone(resp) with self.assertRaises(WindowsAzureError): self.tc.get_entity(self.table_name, 'MyPartition', '1') def test_delete_entity_with_if_doesnt_match(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act with self.assertRaises(WindowsAzureError): self.tc.delete_entity(self.table_name, 'MyPartition', '1', if_match=u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"') # Assert #--Test cases for batch --------------------------------------------- def test_with_filter_single(self): called = [] def my_filter(request, next): called.append(True) return next(request) tc = self.tc.with_filter(my_filter) tc.create_table(self.table_name) self.assertTrue(called) del called[:] tc.delete_table(self.table_name) self.assertTrue(called) del called[:] def test_with_filter_chained(self): called = [] def filter_a(request, next): called.append('a') return next(request) def filter_b(request, next): called.append('b') return next(request) tc = self.tc.with_filter(filter_a).with_filter(filter_b) tc.create_table(self.table_name) self.assertEqual(called, ['b', 'a']) tc.delete_table(self.table_name) def test_batch_insert(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.begin_batch() self.tc.insert_entity(self.table_name, entity) self.tc.commit_batch() # Assert result = self.tc.get_entity(self.table_name, '001', 'batch_insert') self.assertIsNotNone(result) def test_batch_update(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_update' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.insert_entity(self.table_name, entity) entity = self.tc.get_entity(self.table_name, '001', 'batch_update') self.assertEqual(3, entity.test3) entity.test2 = 'value1' self.tc.begin_batch() self.tc.update_entity(self.table_name, '001', 'batch_update', entity) self.tc.commit_batch() entity = self.tc.get_entity(self.table_name, '001', 'batch_update') # Assert self.assertEqual('value1', entity.test2) def test_batch_merge(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_merge' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.insert_entity(self.table_name, entity) entity = self.tc.get_entity(self.table_name, '001', 'batch_merge') self.assertEqual(3, entity.test3) entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_merge' entity.test2 = 'value1' self.tc.begin_batch() self.tc.merge_entity(self.table_name, '001', 'batch_merge', entity) self.tc.commit_batch() entity = self.tc.get_entity(self.table_name, '001', 'batch_merge') # Assert self.assertEqual('value1', entity.test2) self.assertEqual(1234567890, entity.test4) def test_batch_insert_replace(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert_replace' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.begin_batch() self.tc.insert_or_replace_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) self.tc.commit_batch() entity = self.tc.get_entity(self.table_name, '001', 'batch_insert_replace') # Assert self.assertIsNotNone(entity) self.assertEqual('value', entity.test2) self.assertEqual(1234567890, entity.test4) def test_batch_insert_merge(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert_merge' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.begin_batch() self.tc.insert_or_merge_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) self.tc.commit_batch() entity = self.tc.get_entity(self.table_name, '001', 'batch_insert_merge') # Assert self.assertIsNotNone(entity) self.assertEqual('value', entity.test2) self.assertEqual(1234567890, entity.test4) def test_batch_delete(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_delete' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.insert_entity(self.table_name, entity) entity = self.tc.get_entity(self.table_name, '001', 'batch_delete') #self.assertEqual(3, entity.test3) self.tc.begin_batch() self.tc.delete_entity(self.table_name, '001', 'batch_delete') self.tc.commit_batch() def test_batch_inserts(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') self.tc.begin_batch() for i in range(100): entity.RowKey = str(i) self.tc.insert_entity(self.table_name, entity) self.tc.commit_batch() entities = self.tc.query_entities(self.table_name, "PartitionKey eq 'batch_inserts'", '') # Assert self.assertIsNotNone(entities); self.assertEqual(100, len(entities)) def test_batch_all_operations_together(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '003' entity.RowKey = 'batch_all_operations_together-1' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-2' self.tc.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-3' self.tc.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-4' self.tc.insert_entity(self.table_name, entity) self.tc.begin_batch() entity.RowKey = 'batch_all_operations_together' self.tc.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-1' self.tc.delete_entity(self.table_name, entity.PartitionKey, entity.RowKey) entity.RowKey = 'batch_all_operations_together-2' entity.test3 = 10 self.tc.update_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 self.tc.merge_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-4' entity.test3 = 10 self.tc.insert_or_replace_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-5' self.tc.insert_or_merge_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) self.tc.commit_batch() # Assert entities = self.tc.query_entities(self.table_name, "PartitionKey eq '003'", '') self.assertEqual(5, len(entities)) def test_batch_same_row_operations_fail(self): # Arrange self._create_table(self.table_name) entity = self._create_default_entity_dict('001', 'batch_negative_1') self.tc.insert_entity(self.table_name, entity) # Act with self.assertRaises(WindowsAzureError): self.tc.begin_batch() entity = self._create_updated_entity_dict('001', 'batch_negative_1') self.tc.update_entity(self.table_name, entity['PartitionKey'], entity['RowKey'], entity) entity = self._create_default_entity_dict('001', 'batch_negative_1') self.tc.merge_entity(self.table_name, entity['PartitionKey'], entity['RowKey'], entity) self.tc.cancel_batch() # Assert def test_batch_different_partition_operations_fail(self): # Arrange self._create_table(self.table_name) entity = self._create_default_entity_dict('001', 'batch_negative_1') self.tc.insert_entity(self.table_name, entity) # Act with self.assertRaises(WindowsAzureError): self.tc.begin_batch() entity = self._create_updated_entity_dict('001', 'batch_negative_1') self.tc.update_entity(self.table_name, entity['PartitionKey'], entity['RowKey'], entity) entity = self._create_default_entity_dict('002', 'batch_negative_1') self.tc.insert_entity(self.table_name, entity) self.tc.cancel_batch() # Assert def test_batch_different_table_operations_fail(self): # Arrange other_table_name = self.table_name + 'other' self.additional_table_names = [other_table_name] self._create_table(self.table_name) self._create_table(other_table_name) # Act with self.assertRaises(WindowsAzureError): self.tc.begin_batch() entity = self._create_default_entity_dict('001', 'batch_negative_1') self.tc.insert_entity(self.table_name, entity) entity = self._create_default_entity_dict('001', 'batch_negative_2') self.tc.insert_entity(other_table_name, entity) self.tc.cancel_batch() def test_unicode_property_value(self): ''' regression test for github issue #57''' # Act self._create_table(self.table_name) self.tc.insert_entity(self.table_name, {'PartitionKey': 'test', 'RowKey': 'test1', 'Description': u'ꀕ'}) self.tc.insert_entity(self.table_name, {'PartitionKey': 'test', 'RowKey': 'test2', 'Description': 'ꀕ'}) resp = self.tc.query_entities(self.table_name, "PartitionKey eq 'test'") # Assert self.assertEquals(len(resp), 2) self.assertEquals(resp[0].Description, u'ꀕ') self.assertEquals(resp[1].Description, u'ꀕ') def test_unicode_property_name(self): # Act self._create_table(self.table_name) self.tc.insert_entity(self.table_name, {'PartitionKey': 'test', 'RowKey': 'test1', u'啊齄丂狛狜': u'ꀕ'}) self.tc.insert_entity(self.table_name, {'PartitionKey': 'test', 'RowKey': 'test2', u'啊齄丂狛狜': 'hello'}) resp = self.tc.query_entities(self.table_name, "PartitionKey eq 'test'") # Assert self.assertEquals(len(resp), 2) self.assertEquals(resp[0].__dict__[u'啊齄丂狛狜'], u'ꀕ') self.assertEquals(resp[1].__dict__[u'啊齄丂狛狜'], u'hello') def test_unicode_create_table_unicode_name(self): # Arrange self.table_name = unicode(self.table_name) + u'啊齄丂狛狜' # Act with self.assertRaises(WindowsAzureError): # not supported - table name must be alphanumeric, lowercase self.tc.create_table(self.table_name)
#!/usr/bin/env python from azure.storage import TableService from datetime import datetime ts = TableService(account_name='freedu', account_key='P0WNSyFp2Fs32lTY52bN3DFFq/wPQcaBGbnC7xMOcS4U2zsXJoASgVUqux0TEtVgqn8Wfl6jxXpkGfy5ZaWCzQ==') try: ts.create_table('tasktable') ts.insert_entity( 'tasktable', { 'PartitionKey' : 'tasksSeattle', 'RowKey': '1', 'Description': 'Take out the trash', 'DueDate': datetime(2011, 12, 14, 12) } ) except Exception,ex: print ex print 'create table done.' entity = ts.get_entity('tasktable', 'tasksSeattle', '1') print 'get_entity done.' print entity
class Repository(object): """Azure Table Storage repository.""" def __init__(self, settings): """Initializes the repository with the specified settings dict. Required settings are: - STORAGE_NAME - STORAGE_KEY - STORAGE_TABLE_POLL - STORAGE_TABLE_CHOICE """ self.name = 'Azure Table Storage' self.storage_name = settings['STORAGE_NAME'] self.storage_key = settings['STORAGE_KEY'] self.poll_table = settings['STORAGE_TABLE_POLL'] self.choice_table = settings['STORAGE_TABLE_CHOICE'] self.svc = TableService(self.storage_name, self.storage_key) self.svc.create_table(self.poll_table) self.svc.create_table(self.choice_table) def get_polls(self): """Returns all the polls from the repository.""" poll_entities = self.svc.query_entities(self.poll_table) polls = [_poll_from_entity(entity) for entity in poll_entities] return polls def get_poll(self, poll_key): """Returns a poll from the repository.""" try: partition, row = _key_to_partition_and_row(poll_key) poll_entity = self.svc.get_entity(self.poll_table, partition, row) choice_entities = self.svc.query_entities( self.choice_table, "PollPartitionKey eq '{0}' and PollRowKey eq '{1}'" \ .format(partition, row) ) poll = _poll_from_entity(poll_entity) poll.choices = [_choice_from_entity(choice_entity) for choice_entity in choice_entities] return poll except WindowsAzureMissingResourceError: raise PollNotFound() def increment_vote(self, poll_key, choice_key): """Increment the choice vote count for the specified poll.""" try: partition, row = _key_to_partition_and_row(choice_key) entity = self.svc.get_entity(self.choice_table, partition, row) entity.Votes += 1 self.svc.update_entity(self.choice_table, partition, row, entity) except WindowsAzureMissingResourceError: raise PollNotFound() def add_sample_polls(self): """Adds a set of polls from data stored in a samples.json file.""" poll_partition = '2014' poll_row = 0 choice_partition = '2014' choice_row = 0 for sample_poll in _load_samples_json(): poll_entity = { 'PartitionKey': poll_partition, 'RowKey': str(poll_row), 'Text': sample_poll['text'], } self.svc.insert_entity(self.poll_table, poll_entity) for sample_choice in sample_poll['choices']: choice_entity = { 'PartitionKey': choice_partition, 'RowKey': str(choice_row), 'Text': sample_choice, 'Votes': 0, 'PollPartitionKey': poll_partition, 'PollRowKey': str(poll_row), } self.svc.insert_entity(self.choice_table, choice_entity) choice_row += 1 poll_row += 1
class Repository(object): """Azure Table Storage repository.""" def __init__(self, settings): """Initializes the repository with the specified settings dict. Required settings are: - STORAGE_NAME - STORAGE_KEY - STORAGE_TABLE_POLL - STORAGE_TABLE_CHOICE """ self.name = 'Azure Table Storage' self.storage_name = settings['STORAGE_NAME'] self.storage_key = settings['STORAGE_KEY'] self.poll_table = settings['STORAGE_TABLE_POLL'] self.choice_table = settings['STORAGE_TABLE_CHOICE'] self.svc = TableService(self.storage_name, self.storage_key) self.svc.create_table(self.poll_table) self.svc.create_table(self.choice_table) def get_polls(self): """Returns all the polls from the repository.""" poll_entities = self.svc.query_entities(self.poll_table) polls = [_poll_from_entity(entity) for entity in poll_entities] return polls def get_poll(self, poll_key): """Returns a poll from the repository.""" try: partition, row = _key_to_partition_and_row(poll_key) poll_entity = self.svc.get_entity(self.poll_table, partition, row) choice_entities = self.svc.query_entities( self.choice_table, "PollPartitionKey eq '{0}' and PollRowKey eq '{1}'" \ .format(partition, row) ) poll = _poll_from_entity(poll_entity) poll.choices = [_choice_from_entity(choice_entity) for choice_entity in choice_entities] return poll except WindowsAzureMissingResourceError: raise PollNotFound() def increment_vote(self, poll_key, choice_key): """Increment the choice vote count for the specified poll.""" try: partition, row = _key_to_partition_and_row(choice_key) entity = self.svc.get_entity(self.choice_table, partition, row) entity.Votes += 1 self.svc.update_entity(self.choice_table, partition, row, entity) except WindowsAzureMissingResourceError: raise PollNotFound() def add_sample_polls(self): """Adds a set of polls from data stored in a samples.json file.""" poll_partition = '2014' poll_row = 0 choice_partition = '2014' choice_row = 0 for sample_poll in _load_samples_json(): poll_entity = { 'PartitionKey': poll_partition, 'RowKey': str(poll_row), 'Text': sample_poll['text'], } self.svc.insert_entity(self.poll_table, poll_entity) for sample_choice in sample_poll['choices']: choice_entity = { 'PartitionKey': choice_partition, 'RowKey': str(choice_row), 'Text': sample_choice, 'Votes': 0, 'PollPartitionKey': poll_partition, 'PollRowKey': str(poll_row), } self.svc.insert_entity(self.choice_table, choice_entity) choice_row += 1 poll_row += 1
class TableServiceTest(AzureTestCase): def setUp(self): self.tc = TableService( account_name=credentials.getStorageServicesName(), account_key=credentials.getStorageServicesKey()) proxy_host = credentials.getProxyHost() proxy_port = credentials.getProxyPort() if proxy_host: self.tc.set_proxy(proxy_host, proxy_port) __uid = getUniqueTestRunID() table_base_name = u'testtable%s' % (__uid) self.table_name = getUniqueNameBasedOnCurrentTime(table_base_name) self.additional_table_names = [] def tearDown(self): self.cleanup() return super(TableServiceTest, self).tearDown() def cleanup(self): try: self.tc.delete_table(self.table_name) except: pass for name in self.additional_table_names: try: self.tc.delete_table(name) except: pass #--Helpers----------------------------------------------------------------- def _create_table(self, table_name): ''' Creates a table with the specified name. ''' self.tc.create_table(table_name, True) def _create_table_with_default_entities(self, table_name, entity_count): ''' Creates a table with the specified name and adds entities with the default set of values. PartitionKey is set to 'MyPartition' and RowKey is set to a unique counter value starting at 1 (as a string). ''' entities = [] self._create_table(table_name) for i in range(1, entity_count + 1): entities.append( self.tc.insert_entity( table_name, self._create_default_entity_dict('MyPartition', str(i)))) return entities def _create_default_entity_class(self, partition, row): ''' Creates a class-based entity with fixed values, using all of the supported data types. ''' # TODO: Edm.Binary and null entity = Entity() entity.PartitionKey = partition entity.RowKey = row entity.age = 39 entity.sex = 'male' entity.married = True entity.deceased = False entity.optional = None entity.ratio = 3.1 entity.large = 9333111000 entity.Birthday = datetime(1973, 10, 04) entity.birthday = datetime(1970, 10, 04) entity.binary = None entity.other = EntityProperty('Edm.Int64', 20) entity.clsid = EntityProperty('Edm.Guid', 'c9da6455-213d-42c9-9a79-3e9149a57833') return entity def _create_default_entity_dict(self, partition, row): ''' Creates a dictionary-based entity with fixed values, using all of the supported data types. ''' # TODO: Edm.Binary and null return { 'PartitionKey': partition, 'RowKey': row, 'age': 39, 'sex': 'male', 'married': True, 'deceased': False, 'optional': None, 'ratio': 3.1, 'large': 9333111000, 'Birthday': datetime(1973, 10, 04), 'birthday': datetime(1970, 10, 04), 'binary': EntityProperty('Edm.Binary', None), 'other': EntityProperty('Edm.Int64', 20), 'clsid': EntityProperty('Edm.Guid', 'c9da6455-213d-42c9-9a79-3e9149a57833') } def _create_updated_entity_dict(self, partition, row): ''' Creates a dictionary-based entity with fixed values, with a different set of values than the default entity. It adds fields, changes field values, changes field types, and removes fields when compared to the default entity. ''' return { 'PartitionKey': partition, 'RowKey': row, 'age': 'abc', 'sex': 'female', 'sign': 'aquarius', 'birthday': datetime(1991, 10, 04) } def _assert_default_entity(self, entity): ''' Asserts that the entity passed in matches the default entity. ''' self.assertEquals(entity.age, 39) self.assertEquals(entity.sex, 'male') self.assertEquals(entity.married, True) self.assertEquals(entity.deceased, False) self.assertFalse(hasattr(entity, "aquarius")) self.assertEquals(entity.ratio, 3.1) self.assertEquals(entity.large, 9333111000) self.assertEquals(entity.Birthday, datetime(1973, 10, 04)) self.assertEquals(entity.birthday, datetime(1970, 10, 04)) self.assertEquals(entity.other, 20) self.assertIsInstance(entity.clsid, EntityProperty) self.assertEquals(entity.clsid.type, 'Edm.Guid') self.assertEquals(entity.clsid.value, 'c9da6455-213d-42c9-9a79-3e9149a57833') def _assert_updated_entity(self, entity): ''' Asserts that the entity passed in matches the updated entity. ''' self.assertEquals(entity.age, 'abc') self.assertEquals(entity.sex, 'female') self.assertFalse(hasattr(entity, "married")) self.assertFalse(hasattr(entity, "deceased")) self.assertEquals(entity.sign, 'aquarius') self.assertFalse(hasattr(entity, "optional")) self.assertFalse(hasattr(entity, "ratio")) self.assertFalse(hasattr(entity, "large")) self.assertFalse(hasattr(entity, "Birthday")) self.assertEquals(entity.birthday, datetime(1991, 10, 04)) self.assertFalse(hasattr(entity, "other")) self.assertFalse(hasattr(entity, "clsid")) def _assert_merged_entity(self, entity): ''' Asserts that the entity passed in matches the default entity merged with the updated entity. ''' self.assertEquals(entity.age, 'abc') self.assertEquals(entity.sex, 'female') self.assertEquals(entity.sign, 'aquarius') self.assertEquals(entity.married, True) self.assertEquals(entity.deceased, False) self.assertEquals(entity.sign, 'aquarius') self.assertEquals(entity.ratio, 3.1) self.assertEquals(entity.large, 9333111000) self.assertEquals(entity.Birthday, datetime(1973, 10, 04)) self.assertEquals(entity.birthday, datetime(1991, 10, 04)) self.assertEquals(entity.other, 20) self.assertIsInstance(entity.clsid, EntityProperty) self.assertEquals(entity.clsid.type, 'Edm.Guid') self.assertEquals(entity.clsid.value, 'c9da6455-213d-42c9-9a79-3e9149a57833') #--Test cases for table service ------------------------------------------- def test_get_set_table_service_properties(self): table_properties = self.tc.get_table_service_properties() self.tc.set_table_service_properties(table_properties) tests = [ ('logging.delete', True), ('logging.delete', False), ('logging.read', True), ('logging.read', False), ('logging.write', True), ('logging.write', False), ] for path, value in tests: #print path cur = table_properties for component in path.split('.')[:-1]: cur = getattr(cur, component) last_attr = path.split('.')[-1] setattr(cur, last_attr, value) self.tc.set_table_service_properties(table_properties) retry_count = 0 while retry_count < MAX_RETRY: table_properties = self.tc.get_table_service_properties() cur = table_properties for component in path.split('.'): cur = getattr(cur, component) if value == cur: break time.sleep(1) retry_count += 1 self.assertEquals(value, cur) def test_table_service_retention_single_set(self): table_properties = self.tc.get_table_service_properties() table_properties.logging.retention_policy.enabled = False table_properties.logging.retention_policy.days = 5 # TODO: Better error, ValueError? self.assertRaises(WindowsAzureError, self.tc.set_table_service_properties, table_properties) table_properties = self.tc.get_table_service_properties() table_properties.logging.retention_policy.days = None table_properties.logging.retention_policy.enabled = True # TODO: Better error, ValueError? self.assertRaises(WindowsAzureError, self.tc.set_table_service_properties, table_properties) def test_table_service_set_both(self): table_properties = self.tc.get_table_service_properties() table_properties.logging.retention_policy.enabled = True table_properties.logging.retention_policy.days = 5 self.tc.set_table_service_properties(table_properties) table_properties = self.tc.get_table_service_properties() self.assertEquals(True, table_properties.logging.retention_policy.enabled) self.assertEquals(5, table_properties.logging.retention_policy.days) #--Test cases for tables -------------------------------------------------- def test_create_table(self): # Arrange # Act created = self.tc.create_table(self.table_name) # Assert self.assertTrue(created) def test_create_table_fail_on_exist(self): # Arrange # Act created = self.tc.create_table(self.table_name, True) # Assert self.assertTrue(created) def test_create_table_with_already_existing_table(self): # Arrange # Act created1 = self.tc.create_table(self.table_name) created2 = self.tc.create_table(self.table_name) # Assert self.assertTrue(created1) self.assertFalse(created2) def test_create_table_with_already_existing_table_fail_on_exist(self): # Arrange # Act created = self.tc.create_table(self.table_name) with self.assertRaises(WindowsAzureError): self.tc.create_table(self.table_name, True) # Assert self.assertTrue(created) def test_query_tables(self): # Arrange self._create_table(self.table_name) # Act tables = self.tc.query_tables() for table in tables: pass # Assert tableNames = [x.name for x in tables] self.assertGreaterEqual(len(tableNames), 1) self.assertGreaterEqual(len(tables), 1) self.assertIn(self.table_name, tableNames) def test_query_tables_with_table_name(self): # Arrange self._create_table(self.table_name) # Act tables = self.tc.query_tables(self.table_name) for table in tables: pass # Assert self.assertEqual(len(tables), 1) self.assertEqual(tables[0].name, self.table_name) def test_query_tables_with_table_name_no_tables(self): # Arrange # Act with self.assertRaises(WindowsAzureError): self.tc.query_tables(self.table_name) # Assert def test_query_tables_with_top(self): # Arrange self.additional_table_names = [ self.table_name + suffix for suffix in 'abcd' ] for name in self.additional_table_names: self.tc.create_table(name) # Act tables = self.tc.query_tables(None, 3) for table in tables: pass # Assert self.assertEqual(len(tables), 3) def test_query_tables_with_top_and_next_table_name(self): # Arrange self.additional_table_names = [ self.table_name + suffix for suffix in 'abcd' ] for name in self.additional_table_names: self.tc.create_table(name) # Act tables_set1 = self.tc.query_tables(None, 3) tables_set2 = self.tc.query_tables( None, 3, tables_set1.x_ms_continuation['NextTableName']) # Assert self.assertEqual(len(tables_set1), 3) self.assertGreaterEqual(len(tables_set2), 1) self.assertLessEqual(len(tables_set2), 3) def test_delete_table_with_existing_table(self): # Arrange self._create_table(self.table_name) # Act deleted = self.tc.delete_table(self.table_name) # Assert self.assertTrue(deleted) tables = self.tc.query_tables() self.assertNamedItemNotInContainer(tables, self.table_name) def test_delete_table_with_existing_table_fail_not_exist(self): # Arrange self._create_table(self.table_name) # Act deleted = self.tc.delete_table(self.table_name, True) # Assert self.assertTrue(deleted) tables = self.tc.query_tables() self.assertNamedItemNotInContainer(tables, self.table_name) def test_delete_table_with_non_existing_table(self): # Arrange # Act deleted = self.tc.delete_table(self.table_name) # Assert self.assertFalse(deleted) def test_delete_table_with_non_existing_table_fail_not_exist(self): # Arrange # Act with self.assertRaises(WindowsAzureError): self.tc.delete_table(self.table_name, True) # Assert #--Test cases for entities ------------------------------------------ def test_insert_entity_dictionary(self): # Arrange self._create_table(self.table_name) # Act dict = self._create_default_entity_dict('MyPartition', '1') resp = self.tc.insert_entity(self.table_name, dict) # Assert self.assertIsNotNone(resp) def test_insert_entity_class_instance(self): # Arrange self._create_table(self.table_name) # Act entity = self._create_default_entity_class('MyPartition', '1') resp = self.tc.insert_entity(self.table_name, entity) # Assert self.assertIsNotNone(resp) def test_insert_entity_conflict(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act with self.assertRaises(WindowsAzureError): self.tc.insert_entity( self.table_name, self._create_default_entity_dict('MyPartition', '1')) # Assert def test_get_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act resp = self.tc.get_entity(self.table_name, 'MyPartition', '1') # Assert self.assertEquals(resp.PartitionKey, 'MyPartition') self.assertEquals(resp.RowKey, '1') self._assert_default_entity(resp) def test_get_entity_not_existing(self): # Arrange self._create_table(self.table_name) # Act with self.assertRaises(WindowsAzureError): self.tc.get_entity(self.table_name, 'MyPartition', '1') # Assert def test_get_entity_with_select(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act resp = self.tc.get_entity(self.table_name, 'MyPartition', '1', 'age,sex') # Assert self.assertEquals(resp.age, 39) self.assertEquals(resp.sex, 'male') self.assertFalse(hasattr(resp, "birthday")) self.assertFalse(hasattr(resp, "married")) self.assertFalse(hasattr(resp, "deceased")) def test_query_entities(self): # Arrange self._create_table_with_default_entities(self.table_name, 2) # Act resp = self.tc.query_entities(self.table_name) # Assert self.assertEquals(len(resp), 2) for entity in resp: self.assertEquals(entity.PartitionKey, 'MyPartition') self._assert_default_entity(entity) self.assertEquals(resp[0].RowKey, '1') self.assertEquals(resp[1].RowKey, '2') def test_query_entities_with_filter(self): # Arrange self._create_table_with_default_entities(self.table_name, 2) self.tc.insert_entity( self.table_name, self._create_default_entity_dict('MyOtherPartition', '3')) # Act resp = self.tc.query_entities(self.table_name, "PartitionKey eq 'MyPartition'") # Assert self.assertEquals(len(resp), 2) for entity in resp: self.assertEquals(entity.PartitionKey, 'MyPartition') self._assert_default_entity(entity) def test_query_entities_with_select(self): # Arrange self._create_table_with_default_entities(self.table_name, 2) # Act resp = self.tc.query_entities(self.table_name, None, 'age,sex') # Assert self.assertEquals(len(resp), 2) self.assertEquals(resp[0].age, 39) self.assertEquals(resp[0].sex, 'male') self.assertFalse(hasattr(resp[0], "birthday")) self.assertFalse(hasattr(resp[0], "married")) self.assertFalse(hasattr(resp[0], "deceased")) def test_query_entities_with_top(self): # Arrange self._create_table_with_default_entities(self.table_name, 3) # Act resp = self.tc.query_entities(self.table_name, None, None, 2) # Assert self.assertEquals(len(resp), 2) def test_query_entities_with_top_and_next(self): # Arrange self._create_table_with_default_entities(self.table_name, 5) # Act resp1 = self.tc.query_entities(self.table_name, None, None, 2) resp2 = self.tc.query_entities( self.table_name, None, None, 2, resp1.x_ms_continuation['NextPartitionKey'], resp1.x_ms_continuation['NextRowKey']) resp3 = self.tc.query_entities( self.table_name, None, None, 2, resp2.x_ms_continuation['NextPartitionKey'], resp2.x_ms_continuation['NextRowKey']) # Assert self.assertEquals(len(resp1), 2) self.assertEquals(len(resp2), 2) self.assertEquals(len(resp3), 1) self.assertEquals(resp1[0].RowKey, '1') self.assertEquals(resp1[1].RowKey, '2') self.assertEquals(resp2[0].RowKey, '3') self.assertEquals(resp2[1].RowKey, '4') self.assertEquals(resp3[0].RowKey, '5') def test_update_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.tc.update_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_update_entity_with_if_matches(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.tc.update_entity(self.table_name, 'MyPartition', '1', sent_entity, if_match=entities[0].etag) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_update_entity_with_if_doesnt_match(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') with self.assertRaises(WindowsAzureError): self.tc.update_entity( self.table_name, 'MyPartition', '1', sent_entity, if_match=u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"') # Assert def test_insert_or_merge_entity_with_existing_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.tc.insert_or_merge_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_merged_entity(received_entity) def test_insert_or_merge_entity_with_non_existing_entity(self): # Arrange self._create_table(self.table_name) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.tc.insert_or_merge_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_insert_or_replace_entity_with_existing_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.tc.insert_or_replace_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_insert_or_replace_entity_with_non_existing_entity(self): # Arrange self._create_table(self.table_name) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.tc.insert_or_replace_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_updated_entity(received_entity) def test_merge_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.tc.merge_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_merged_entity(received_entity) def test_merge_entity_not_existing(self): # Arrange self._create_table(self.table_name) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') with self.assertRaises(WindowsAzureError): self.tc.merge_entity(self.table_name, 'MyPartition', '1', sent_entity) # Assert def test_merge_entity_with_if_matches(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') resp = self.tc.merge_entity(self.table_name, 'MyPartition', '1', sent_entity, if_match=entities[0].etag) # Assert self.assertIsNotNone(resp) received_entity = self.tc.get_entity(self.table_name, 'MyPartition', '1') self._assert_merged_entity(received_entity) def test_merge_entity_with_if_doesnt_match(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act sent_entity = self._create_updated_entity_dict('MyPartition', '1') with self.assertRaises(WindowsAzureError): self.tc.merge_entity( self.table_name, 'MyPartition', '1', sent_entity, if_match=u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"') # Assert def test_delete_entity(self): # Arrange self._create_table_with_default_entities(self.table_name, 1) # Act resp = self.tc.delete_entity(self.table_name, 'MyPartition', '1') # Assert self.assertIsNone(resp) with self.assertRaises(WindowsAzureError): self.tc.get_entity(self.table_name, 'MyPartition', '1') def test_delete_entity_not_existing(self): # Arrange self._create_table(self.table_name) # Act with self.assertRaises(WindowsAzureError): self.tc.delete_entity(self.table_name, 'MyPartition', '1') # Assert def test_delete_entity_with_if_matches(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act resp = self.tc.delete_entity(self.table_name, 'MyPartition', '1', if_match=entities[0].etag) # Assert self.assertIsNone(resp) with self.assertRaises(WindowsAzureError): self.tc.get_entity(self.table_name, 'MyPartition', '1') def test_delete_entity_with_if_doesnt_match(self): # Arrange entities = self._create_table_with_default_entities(self.table_name, 1) # Act with self.assertRaises(WindowsAzureError): self.tc.delete_entity( self.table_name, 'MyPartition', '1', if_match=u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"') # Assert #--Test cases for batch --------------------------------------------- def test_with_filter_single(self): called = [] def my_filter(request, next): called.append(True) return next(request) tc = self.tc.with_filter(my_filter) tc.create_table(self.table_name) self.assertTrue(called) del called[:] tc.delete_table(self.table_name) self.assertTrue(called) del called[:] def test_with_filter_chained(self): called = [] def filter_a(request, next): called.append('a') return next(request) def filter_b(request, next): called.append('b') return next(request) tc = self.tc.with_filter(filter_a).with_filter(filter_b) tc.create_table(self.table_name) self.assertEqual(called, ['b', 'a']) tc.delete_table(self.table_name) def test_batch_insert(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.begin_batch() self.tc.insert_entity(self.table_name, entity) self.tc.commit_batch() # Assert result = self.tc.get_entity(self.table_name, '001', 'batch_insert') self.assertIsNotNone(result) def test_batch_update(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_update' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.insert_entity(self.table_name, entity) entity = self.tc.get_entity(self.table_name, '001', 'batch_update') self.assertEqual(3, entity.test3) entity.test2 = 'value1' self.tc.begin_batch() self.tc.update_entity(self.table_name, '001', 'batch_update', entity) self.tc.commit_batch() entity = self.tc.get_entity(self.table_name, '001', 'batch_update') # Assert self.assertEqual('value1', entity.test2) def test_batch_merge(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_merge' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.insert_entity(self.table_name, entity) entity = self.tc.get_entity(self.table_name, '001', 'batch_merge') self.assertEqual(3, entity.test3) entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_merge' entity.test2 = 'value1' self.tc.begin_batch() self.tc.merge_entity(self.table_name, '001', 'batch_merge', entity) self.tc.commit_batch() entity = self.tc.get_entity(self.table_name, '001', 'batch_merge') # Assert self.assertEqual('value1', entity.test2) self.assertEqual(1234567890, entity.test4) def test_batch_insert_replace(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert_replace' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.begin_batch() self.tc.insert_or_replace_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) self.tc.commit_batch() entity = self.tc.get_entity(self.table_name, '001', 'batch_insert_replace') # Assert self.assertIsNotNone(entity) self.assertEqual('value', entity.test2) self.assertEqual(1234567890, entity.test4) def test_batch_insert_merge(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert_merge' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.begin_batch() self.tc.insert_or_merge_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) self.tc.commit_batch() entity = self.tc.get_entity(self.table_name, '001', 'batch_insert_merge') # Assert self.assertIsNotNone(entity) self.assertEqual('value', entity.test2) self.assertEqual(1234567890, entity.test4) def test_batch_delete(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_delete' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.insert_entity(self.table_name, entity) entity = self.tc.get_entity(self.table_name, '001', 'batch_delete') #self.assertEqual(3, entity.test3) self.tc.begin_batch() self.tc.delete_entity(self.table_name, '001', 'batch_delete') self.tc.commit_batch() def test_batch_inserts(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') self.tc.begin_batch() for i in range(100): entity.RowKey = str(i) self.tc.insert_entity(self.table_name, entity) self.tc.commit_batch() entities = self.tc.query_entities(self.table_name, "PartitionKey eq 'batch_inserts'", '') # Assert self.assertIsNotNone(entities) self.assertEqual(100, len(entities)) def test_batch_all_operations_together(self): # Arrange self._create_table(self.table_name) # Act entity = Entity() entity.PartitionKey = '003' entity.RowKey = 'batch_all_operations_together-1' entity.test = EntityProperty('Edm.Boolean', 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty('Edm.Int64', '1234567890') entity.test5 = datetime.utcnow() self.tc.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-2' self.tc.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-3' self.tc.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-4' self.tc.insert_entity(self.table_name, entity) self.tc.begin_batch() entity.RowKey = 'batch_all_operations_together' self.tc.insert_entity(self.table_name, entity) entity.RowKey = 'batch_all_operations_together-1' self.tc.delete_entity(self.table_name, entity.PartitionKey, entity.RowKey) entity.RowKey = 'batch_all_operations_together-2' entity.test3 = 10 self.tc.update_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 self.tc.merge_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-4' entity.test3 = 10 self.tc.insert_or_replace_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) entity.RowKey = 'batch_all_operations_together-5' self.tc.insert_or_merge_entity(self.table_name, entity.PartitionKey, entity.RowKey, entity) self.tc.commit_batch() # Assert entities = self.tc.query_entities(self.table_name, "PartitionKey eq '003'", '') self.assertEqual(5, len(entities)) def test_batch_same_row_operations_fail(self): # Arrange self._create_table(self.table_name) entity = self._create_default_entity_dict('001', 'batch_negative_1') self.tc.insert_entity(self.table_name, entity) # Act with self.assertRaises(WindowsAzureError): self.tc.begin_batch() entity = self._create_updated_entity_dict('001', 'batch_negative_1') self.tc.update_entity(self.table_name, entity['PartitionKey'], entity['RowKey'], entity) entity = self._create_default_entity_dict('001', 'batch_negative_1') self.tc.merge_entity(self.table_name, entity['PartitionKey'], entity['RowKey'], entity) self.tc.cancel_batch() # Assert def test_batch_different_partition_operations_fail(self): # Arrange self._create_table(self.table_name) entity = self._create_default_entity_dict('001', 'batch_negative_1') self.tc.insert_entity(self.table_name, entity) # Act with self.assertRaises(WindowsAzureError): self.tc.begin_batch() entity = self._create_updated_entity_dict('001', 'batch_negative_1') self.tc.update_entity(self.table_name, entity['PartitionKey'], entity['RowKey'], entity) entity = self._create_default_entity_dict('002', 'batch_negative_1') self.tc.insert_entity(self.table_name, entity) self.tc.cancel_batch() # Assert def test_batch_different_table_operations_fail(self): # Arrange other_table_name = self.table_name + 'other' self.additional_table_names = [other_table_name] self._create_table(self.table_name) self._create_table(other_table_name) # Act with self.assertRaises(WindowsAzureError): self.tc.begin_batch() entity = self._create_default_entity_dict('001', 'batch_negative_1') self.tc.insert_entity(self.table_name, entity) entity = self._create_default_entity_dict('001', 'batch_negative_2') self.tc.insert_entity(other_table_name, entity) self.tc.cancel_batch() def test_unicode_property_value(self): ''' regression test for github issue #57''' # Act self._create_table(self.table_name) self.tc.insert_entity(self.table_name, { 'PartitionKey': 'test', 'RowKey': 'test1', 'Description': u'ꀕ' }) self.tc.insert_entity(self.table_name, { 'PartitionKey': 'test', 'RowKey': 'test2', 'Description': 'ꀕ' }) resp = self.tc.query_entities(self.table_name, "PartitionKey eq 'test'") # Assert self.assertEquals(len(resp), 2) self.assertEquals(resp[0].Description, u'ꀕ') self.assertEquals(resp[1].Description, u'ꀕ') def test_unicode_property_name(self): # Act self._create_table(self.table_name) self.tc.insert_entity(self.table_name, { 'PartitionKey': 'test', 'RowKey': 'test1', u'啊齄丂狛狜': u'ꀕ' }) self.tc.insert_entity(self.table_name, { 'PartitionKey': 'test', 'RowKey': 'test2', u'啊齄丂狛狜': 'hello' }) resp = self.tc.query_entities(self.table_name, "PartitionKey eq 'test'") # Assert self.assertEquals(len(resp), 2) self.assertEquals(resp[0].__dict__[u'啊齄丂狛狜'], u'ꀕ') self.assertEquals(resp[1].__dict__[u'啊齄丂狛狜'], u'hello') def test_unicode_create_table_unicode_name(self): # Arrange self.table_name = unicode(self.table_name) + u'啊齄丂狛狜' # Act with self.assertRaises(WindowsAzureError): # not supported - table name must be alphanumeric, lowercase self.tc.create_table(self.table_name)
# Repeat while (True): # get images form *imagesQueue* - it is invoked by CRON messages = queue_service.get_messages(imagesQueue) if len(messages) == 0: sleep(15) for message in messages: # get image: image ID imgBlobName = b64decode(message.message_text) print(imgBlobName) tableRowKey = imgBlobName # Check if analysis completed: if not continue to next message try: currentTableTask = table_service.get_entity( tableName, tablePartitionKey, tableRowKey) except azure.WindowsAzureMissingResourceError: queue_service.delete_message(imagesQueue, message.message_id, message.pop_receipt) continue if hasattr(currentTableTask, 'analysed'): if currentTableTask.analysed: # Do nothing analysis completed pass else: continue else: continue # Check if all miniatures are ready
class AzureGaugeDatastore(GaugeDatastore): """Stores gauge data in Azure Table storage. Utilizes table storage as follows: PartitionKey: gauge name RowKey: date key (e.g. day string) data: stored data for date key Timestamp: last time data updated Replaces existing rows (PK+RK) with upsert queries. Has a limitation of returning max 1000 rows in a query. This suffices for now. """ table_service = None table_name = None def __init__(self, account_name, account_key, table_name): self.table_name = table_name self.table_service = TableService(account_name, account_key) def save_data(self, gauge_name, date_key, data): """Saves data to the table row of date_key (replaces if exists) """ entity = {'PartitionKey': gauge_name, 'RowKey': date_key, 'data': data} self.table_service.insert_or_replace_entity(self.table_name, gauge_name, date_key, entity) def get_gauge_data(self, gauge_name, min_date_key=None, max_date_key=None): """Retrieves all gauge data, returns unsorted. If min_date_key is specified, returns records with date_key >= min_date_key If max_date_key is specified, returns records with date_key < max_date_key IMPORTANT NOTE: Azure Table REST API returns first (oldest) 1000 rows in API call. Unless we add RowKey>min_date_key filter, after 1000 rows (e.g. after 1000 days of recording) no fresh results will be returned. """ query = "PartitionKey eq '{0}'".format(gauge_name) if min_date_key: query = "{0} and RowKey ge '{1}'".format(query, min_date_key) if max_date_key: query = "{0} and RowKey lt '{1}'".format(query, max_date_key) rows = self.table_service.query_entities(self.table_name, filter=query) if rows: return [make_record(record.RowKey, record.data) for record in rows] def get_data(self, gauge_name, date_key): """Retrieves gauge data for a specific date key (e.g. day) """ rec = self.table_service.get_entity(self.table_name, gauge_name, date_key) if not rec: return None return helpers.make_record(rec.RowKey, rec.data)
from azure.storage import TableService, Entity table_service = TableService(account_name='upmsample', account_key='5YC/6x9KL56rtaAUAZMgGsREDvvPHYJIMqH3z1c9IgQLy0qMP+Awr+7j51Tfzniczj//6jn7lvYQutD/mHm6dw==') table_service.create_table('ledtracktable') task = table_service.get_entity('ledtracktable', 'ledswitch', '1') print(task.state)