public async Task TableEntity_IfBoundToExistingPoco_Binds()
        {
            // Arrange
            const string   expectedValue = "abc";
            StorageAccount account       = CreateFakeStorageAccount();
            CloudQueue     triggerQueue  = await account.CreateQueueAsync(TriggerQueueName);

            await triggerQueue.AddMessageAsync(new CloudQueueMessage("ignore"));

            CloudTable table = await account.CreateTableAsync(TableName);

            Dictionary <string, EntityProperty> properties = new Dictionary <string, EntityProperty>
            {
                { "Value", new EntityProperty(expectedValue) }
            };

            table.Insert(new DynamicTableEntity(PartitionKey, RowKey, etag: null, properties: properties));

            // Act
            Poco result = RunTrigger <Poco>(account, typeof(BindToPocoProgram),
                                            (s) => BindToPocoProgram.TaskSource = s);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(expectedValue, result.Value);
        }
        public async Task TableEntity_IfBoundToExistingDynamicTableEntity_Binds()
        {
            // Arrange
            const string   expectedKey   = "abc";
            const int      expectedValue = 123;
            StorageAccount account       = CreateFakeStorageAccount();
            CloudQueue     triggerQueue  = await account.CreateQueueAsync(TriggerQueueName);

            await triggerQueue.AddMessageAsync(new CloudQueueMessage("ignore"));

            CloudTable table = await account.CreateTableAsync(TableName);

            Dictionary <string, EntityProperty> properties = new Dictionary <string, EntityProperty>
            {
                { expectedKey, new EntityProperty(expectedValue) }
            };

            table.Insert(new DynamicTableEntity(PartitionKey, RowKey, etag: null, properties: properties));

            // Act
            DynamicTableEntity result = RunTrigger <DynamicTableEntity>(account, typeof(BindToDynamicTableEntityProgram),
                                                                        (s) => BindToDynamicTableEntityProgram.TaskSource = s);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(PartitionKey, result.PartitionKey);
            Assert.Equal(RowKey, result.RowKey);
            Assert.NotNull(result.Properties);
            Assert.True(result.Properties.ContainsKey(expectedKey));
            EntityProperty property = result.Properties[expectedKey];

            Assert.NotNull(property);
            Assert.Equal(EdmType.Int32, property.PropertyType);
            Assert.Equal(expectedValue, property.Int32Value);
        }
        public async Task TableEntity_IfUpdatesRowKey_Throws()
        {
            // Arrange
            StorageAccount account      = CreateFakeStorageAccount();
            CloudQueue     triggerQueue = await account.CreateQueueAsync(TriggerQueueName);

            await triggerQueue.AddMessageAsync(new CloudQueueMessage("ignore"));

            CloudTable table = await account.CreateTableAsync(TableName);

            table.Insert(new DynamicTableEntity(PartitionKey, RowKey));

            // Act
            Exception exception = RunTriggerFailure(account, typeof(UpdatePocoRowKeyProgram));

            // Assert
            Assert.NotNull(exception);
            Assert.IsType <InvalidOperationException>(exception);
            Assert.Equal("Error while handling parameter entity after function returned:", exception.Message);
            Exception innerException = exception.InnerException;

            Assert.NotNull(innerException);
            Assert.IsType <InvalidOperationException>(innerException);
            Assert.Equal("When binding to a table entity, the row key must not be changed.", innerException.Message);
        }
        public async Task TableEntity_IfBoundUsingRouteParameters_Binds()
        {
            // Arrange
            StorageAccount account      = CreateFakeStorageAccount();
            CloudQueue     triggerQueue = await account.CreateQueueAsync(TriggerQueueName);

            const string       tableName    = TableName + "B";
            const string       partitionKey = PartitionKey + "B";
            const string       rowKey       = RowKey + "B";
            TableEntityMessage message      = new TableEntityMessage
            {
                TableName    = tableName,
                PartitionKey = partitionKey,
                RowKey       = rowKey
            };
            await triggerQueue.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(message)));

            CloudTable table = await account.CreateTableAsync(tableName);

            Dictionary <string, EntityProperty> originalProperties = new Dictionary <string, EntityProperty>
            {
                { "Value", new EntityProperty(123) }
            };

            table.Insert(new DynamicTableEntity(partitionKey, rowKey, etag: null, properties: originalProperties));

            // Act
            RunTrigger(account, typeof(BindUsingRouteParametersProgram));

            // Assert
            DynamicTableEntity entity = table.Retrieve <DynamicTableEntity>(partitionKey, rowKey);

            Assert.NotNull(entity);
            IDictionary <string, EntityProperty> properties = entity.Properties;

            Assert.NotNull(properties);
            Assert.True(properties.ContainsKey("Value"));
            EntityProperty property = properties["Value"];

            Assert.NotNull(property);
            Assert.Equal(EdmType.Int32, property.PropertyType);
            Assert.True(property.Int32Value.HasValue);
            Assert.Equal(456, property.Int32Value.Value);
        }
        public async Task TableEntity_IfUpdatesPoco_Persists()
        {
            // Arrange
            const string   originalValue = "abc";
            const string   expectedValue = "def";
            StorageAccount account       = CreateFakeStorageAccount();
            CloudQueue     triggerQueue  = await account.CreateQueueAsync(TriggerQueueName);

            await triggerQueue.AddMessageAsync(new CloudQueueMessage(expectedValue));

            CloudTable table = await account.CreateTableAsync(TableName);

            Dictionary <string, EntityProperty> originalProperties = new Dictionary <string, EntityProperty>
            {
                { "Value", new EntityProperty(originalValue) }
            };

            table.Insert(new DynamicTableEntity(PartitionKey, RowKey, etag: null, properties: originalProperties));

            // Act
            RunTrigger(account, typeof(UpdatePocoProgram));

            // Assert
            DynamicTableEntity entity = table.Retrieve <DynamicTableEntity>(PartitionKey, RowKey);

            Assert.NotNull(entity);
            IDictionary <string, EntityProperty> properties = entity.Properties;

            Assert.NotNull(properties);
            Assert.True(properties.ContainsKey("Value"));
            EntityProperty property = properties["Value"];

            Assert.NotNull(property);
            Assert.Equal(EdmType.String, property.PropertyType);
            Assert.Equal(expectedValue, property.StringValue);
        }
        public async Task TableEntity_IfUpdatesPoco_PersistsUsingNativeTableTypes()
        {
            // Arrange
            byte[]         originalValue = new byte[] { 0x12, 0x34 };
            byte[]         expectedValue = new byte[] { 0x56, 0x78 };
            StorageAccount account       = CreateFakeStorageAccount();
            CloudQueue     triggerQueue  = await account.CreateQueueAsync(TriggerQueueName);

            await triggerQueue.AddMessageAsync(CloudQueueMessage.CreateCloudQueueMessageFromByteArray(expectedValue));

            CloudTable table = await account.CreateTableAsync(TableName);

            Dictionary <string, EntityProperty> originalProperties = new Dictionary <string, EntityProperty>
            {
                { "Value", new EntityProperty(originalValue) }
            };

            table.Insert(new DynamicTableEntity(PartitionKey, RowKey, etag: null, properties: originalProperties));

            // Act
            RunTrigger(account, typeof(UpdatePocoWithByteArrayValueProgram));

            // Assert
            DynamicTableEntity entity = table.Retrieve <DynamicTableEntity>(PartitionKey, RowKey);

            Assert.NotNull(entity);
            IDictionary <string, EntityProperty> properties = entity.Properties;

            Assert.NotNull(properties);
            Assert.True(properties.ContainsKey("Value"));
            EntityProperty property = properties["Value"];

            Assert.NotNull(property);
            Assert.Equal(EdmType.Binary, property.PropertyType);
            Assert.Equal(expectedValue, property.BinaryValue);
        }