public void Upsert(string tableName, TableItem tableItem) { var genericTableEntity = GenericTableEntity.HydrateFrom(tableItem); Action <MemoryStorageAccount> action = tables => tables.GetTable(tableName).GetPartition(tableItem.PartitionKey).Upsert(genericTableEntity); _pendingActions.Enqueue(new TableAction(action, tableItem.PartitionKey, tableItem.RowKey, tableName)); }
public void SimpleItemWithDontSerializeAttributeConvertsToGenericTableEntityCorrectly() { var itemToSave = new SimpleItemWithDontSerializeAttribute { SerializedString = "foo", NotSerializedString = "bar" }; TableItem tableItem = TableItem.Create(itemToSave, "pk", "rk"); var genericItemToTest = GenericTableEntity.HydrateFrom(tableItem); var wereCool = true; wereCool &= itemToSave.SerializedString == genericItemToTest.WriteEntity(null)["SerializedString"].StringValue; try { wereCool &= null == genericItemToTest.WriteEntity(null)["NotSerializedString"].StringValue; } catch (KeyNotFoundException) { wereCool &= true; } wereCool &= genericItemToTest.WriteEntity(null).Count == 1; Assert.IsTrue(wereCool); }
public void Upsert(GenericTableEntity entity) { lock ( _entities ) { entity.ETag = GetNewETag(); entity.Timestamp = DateTimeOffset.UtcNow; _entities[entity.RowKey] = entity; } }
public void ConvertTo_ItemHasColumnWithNonBoolType_ResultingObjectHasFalsePropertyValue() { dynamic item = new ExpandoObject(); item.IsAwesome = "yes!"; TableItem tableItem = TableItem.Create(item, "pk", "rk"); var tableEntity = GenericTableEntity.HydrateFrom(tableItem); Assert.ThrowsException <InvalidOperationException>(() => tableEntity.ConvertTo <TypeWithBoolProperty>()); }
public void DeleteItem(string tableName, TableItem tableItem, ConflictHandling conflictHandling) { var genericTableEntity = GenericTableEntity.HydrateFrom(tableItem); if (ShouldForceOverwrite(conflictHandling, genericTableEntity)) { genericTableEntity.ETag = "*"; } Action <MemoryStorageAccount> action = tables => tables.GetTable(tableName).GetPartition(tableItem.PartitionKey).Delete(genericTableEntity); _pendingActions.Enqueue(new TableAction(action, tableItem.PartitionKey, tableItem.RowKey, tableName)); }
public void Hydrate_ItemDecoratedWithETagAttribute_ReturnedGenericTableEntityHasETag() { var itemToSave = new DecoratedItemWithETag { Id = "id", Name = "name", ETag = "etag" }; TableItem tableItem = TableItem.Create(itemToSave); var genericEntity = GenericTableEntity.HydrateFrom(tableItem); Assert.AreEqual("etag", genericEntity.ETag); }
public void Delete(GenericTableEntity entity) { lock ( _entities ) { if (_entities.ContainsKey(entity.RowKey)) { if (EntityHasBeenChanged(entity)) { throw new EntityHasBeenChangedException(); } _entities.Remove(entity.RowKey); } } }
public void Add(GenericTableEntity entity) { AzureKeyValidator.ValidatePartitionKey(entity.PartitionKey); AzureKeyValidator.ValidateRowKey(entity.RowKey); lock ( _entities ) { if (_entities.ContainsKey(entity.RowKey)) { throw new EntityAlreadyExistsException(); } entity.ETag = GetNewETag(); entity.Timestamp = DateTimeOffset.UtcNow; _entities[entity.RowKey] = entity; } }
public void CreateInstanceFromProperties_TargetTypeDecoratedWithETagAttribute_ETagSetCorrectly() { var genericEntity = new GenericTableEntity { ETag = "tag" }; var entityProperties = new Dictionary <string, EntityProperty>(); entityProperties["Age"] = new EntityProperty(42); genericEntity.ReadEntity(entityProperties, null); var item = genericEntity.ConvertTo <DecoratedItemWithETag>(); Assert.AreEqual("tag", item.ETag, "Incorrect ETag"); }
private bool EntityHasBeenChanged(GenericTableEntity entity) { var hasETagProperty = !string.IsNullOrEmpty(entity.ETag); if (hasETagProperty && entity.ETag.Equals("*")) { return(false); } var entityHasChanged = false; if (hasETagProperty) { entityHasChanged = !entity.ETag.Equals(_entities[entity.RowKey].ETag); } return(entityHasChanged); }
public async Task SetAsync(string partitionKey, string rowKey, T value) { var entity = new GenericTableEntity <T> { PartitionKey = partitionKey, RowKey = rowKey, Content = value }; TableOperation operation = TableOperation.InsertOrReplace(entity); TimeSpan duration = await EventTimer.TimeAsync(async() => { await _cloudTable.ExecuteAsync(operation); }); _eventSource.OnSavedRecordToAzure(partitionKey, rowKey, duration); }
public void CreateInstanceFromProperties_TargetTypeDecoratedWithTimestampAttribute_TimestampSetCorrectly() { var dt = DateTime.Now; var genericEntity = new GenericTableEntity { Timestamp = dt }; var entityProperties = new Dictionary <string, EntityProperty>(); entityProperties["Age"] = new EntityProperty(42); genericEntity.ReadEntity(entityProperties, null); var item = genericEntity.ConvertTo <DecoratedItemWithTimestamp>(); Assert.AreEqual(dt, item.Timestamp, "Incorrect Timestamp"); }
public void Hydrate_ItemDecoratedWithRowAndPartitionKeyAttributes_ReturnedGenericTableEntityHasCorrectProperties() { var itemToSave = new DecoratedItem { Id = "id", Name = "name", Age = 42, }; TableItem tableItem = TableItem.Create(itemToSave); var genericEntity = GenericTableEntity.HydrateFrom(tableItem); Assert.AreEqual("id", genericEntity.PartitionKey, "incorrect partition key"); Assert.AreEqual("name", genericEntity.RowKey, "incorrect row key"); Assert.IsFalse(genericEntity.WriteEntity(null).ContainsKey("Id"), "partition key property should not be serialized as separate property"); Assert.IsFalse(genericEntity.WriteEntity(null).ContainsKey("Name"), "row key property should not be serialized as separate property"); }
public void CreateInstanceFromProperties_TargetTypeDecoratedWithRowAndPartitionKeyAttributes_RowAndPartitionKeySetCorrectly() { var genericEntity = new GenericTableEntity { PartitionKey = "foo", RowKey = "bar" }; var entityProperties = new Dictionary <string, EntityProperty>(); entityProperties["Age"] = new EntityProperty(42); genericEntity.ReadEntity(entityProperties, null); var item = genericEntity.ConvertTo <DecoratedItem>(); Assert.AreEqual("foo", item.Id, "Incorrect partition key"); Assert.AreEqual("bar", item.Name, "Incorrect row key"); }
public void Update(GenericTableEntity entity) { lock ( _entities ) { if (!_entities.ContainsKey(entity.RowKey)) { throw new EntityDoesNotExistException(); } if (EntityHasBeenChanged(entity)) { throw new EntityHasBeenChangedException(); } entity.ETag = GetNewETag(); entity.Timestamp = DateTimeOffset.UtcNow; _entities[entity.RowKey] = entity; } }
public void DynamicItemConvertsToGenericTableEntityCorrectly() { dynamic itemToSave = new ExpandoObject(); itemToSave.FirstType = "foo"; itemToSave.SecondType = "bar"; TableItem tableItem = TableItem.Create(itemToSave, "pk", "rk"); var genericItemToTest = GenericTableEntity.HydrateFrom(tableItem); var wereCool = true; wereCool &= itemToSave.FirstType == genericItemToTest.WriteEntity(null)["FirstType"].StringValue; wereCool &= itemToSave.SecondType == genericItemToTest.WriteEntity(null)["SecondType"].StringValue; wereCool &= genericItemToTest.WriteEntity(null).Count == 2; Assert.IsTrue(wereCool); }
public void Merge(GenericTableEntity entity) { lock ( _entities ) { if (!_entities.ContainsKey(entity.RowKey)) { throw new EntityDoesNotExistException(); } if (EntityHasBeenChanged(entity)) { throw new EntityHasBeenChangedException(); } var currentEntity = _entities[entity.RowKey]; foreach (var property in entity.WriteEntity(null)) { currentEntity.SetProperty(property.Key, property.Value); } currentEntity.ETag = GetNewETag(); currentEntity.Timestamp = DateTimeOffset.UtcNow; } }
public void SimpleItemConvertsToGenericTableEntityCorrectly() { var itemToSave = new SimpleDataItem { FirstType = "foo", SecondType = 0 }; TableItem tableItem = TableItem.Create(itemToSave, "pk", "rk"); var genericItemToTest = GenericTableEntity.HydrateFrom(tableItem); var wereCool = true; wereCool &= itemToSave.FirstType == genericItemToTest.WriteEntity(null)["FirstType"].StringValue; wereCool &= itemToSave.SecondType == genericItemToTest.WriteEntity(null)["SecondType"].Int32Value; wereCool &= null == genericItemToTest.WriteEntity(null)["UriTypeProperty"].StringValue;; wereCool &= genericItemToTest.WriteEntity(null).Count == 3; Assert.IsTrue(wereCool); }
internal override dynamic Convert(GenericTableEntity e) { return(StripNullValues(e.ConvertToDynamic(IncludeETagForDynamic))); }
internal abstract T Convert(GenericTableEntity e);
internal override T Convert(GenericTableEntity e) { return(e.ConvertTo <T>()); }
private static bool ShouldForceOverwrite(ConflictHandling conflictHandling, GenericTableEntity genericTableEntity) { return(string.IsNullOrEmpty(genericTableEntity.ETag) || conflictHandling.Equals(ConflictHandling.Overwrite)); }