예제 #1
0
        public void Table_IfBoundToICollectorJObject_AddInsertsEntity()
        {
            // Arrange
            const string    expectedValue = "abc";
            IStorageAccount account       = CreateFakeStorageAccount();
            IStorageQueue   triggerQueue  = CreateQueue(account, TriggerQueueName);

            triggerQueue.AddMessage(triggerQueue.CreateMessage(expectedValue));

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

            // Assert
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable       table  = client.GetTableReference(TableName);

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

            Assert.NotNull(entity);
            Assert.NotNull(entity.Properties);

            AssertPropertyValue(entity, "ValueStr", "abcdef");
            AssertPropertyValue(entity, "ValueNum", 123);
        }
예제 #2
0
        public void Table_IfBoundToICollectorPoco_AddInsertsEntity()
        {
            // Arrange
            const string    expectedValue = "abc";
            IStorageAccount account       = CreateFakeStorageAccount();
            IStorageQueue   triggerQueue  = CreateQueue(account, TriggerQueueName);

            triggerQueue.AddMessage(triggerQueue.CreateMessage(expectedValue));

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

            // Assert
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable       table  = client.GetTableReference(TableName);

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

            Assert.NotNull(entity);
            Assert.NotNull(entity.Properties);
            Assert.True(entity.Properties.ContainsKey(PropertyName));
            EntityProperty property = entity.Properties[PropertyName];

            Assert.NotNull(property);
            Assert.Equal(EdmType.String, property.PropertyType);
            Assert.Equal(expectedValue, property.StringValue);
        }
예제 #3
0
        private static IStorageTable CreateTable(IStorageAccount account, string tableName)
        {
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable       table  = client.GetTableReference(tableName);

            table.CreateIfNotExists();
            return(table);
        }
예제 #4
0
        public async Task <IBinding> TryCreateAsync(BindingProviderContext context)
        {
            ParameterInfo  parameter      = context.Parameter;
            TableAttribute tableAttribute = parameter.GetCustomAttribute <TableAttribute>(inherit: false);

            if (tableAttribute == null)
            {
                return(null);
            }

            string          tableName = Resolve(tableAttribute.TableName);
            IStorageAccount account   = await _accountProvider.GetStorageAccountAsync(context.Parameter, context.CancellationToken, _nameResolver);

            // requires storage account with table support
            account.AssertTypeOneOf(StorageAccountType.GeneralPurpose);

            StorageClientFactoryContext clientFactoryContext = new StorageClientFactoryContext
            {
                Parameter = context.Parameter
            };
            IStorageTableClient client = account.CreateTableClient(clientFactoryContext);

            bool     bindsToEntireTable = tableAttribute.RowKey == null;
            IBinding binding;

            if (bindsToEntireTable)
            {
                IBindableTablePath path = BindableTablePath.Create(tableName);
                path.ValidateContractCompatibility(context.BindingDataContract);

                IStorageTableArgumentBinding argumentBinding = _tableBindingProvider.TryCreate(parameter);

                if (argumentBinding == null)
                {
                    throw new InvalidOperationException("Can't bind Table to type '" + parameter.ParameterType + "'.");
                }

                binding = new TableBinding(parameter.Name, argumentBinding, client, path);
            }
            else
            {
                string partitionKey           = Resolve(tableAttribute.PartitionKey);
                string rowKey                 = Resolve(tableAttribute.RowKey);
                IBindableTableEntityPath path = BindableTableEntityPath.Create(tableName, partitionKey, rowKey);
                path.ValidateContractCompatibility(context.BindingDataContract);

                IArgumentBinding <TableEntityContext> argumentBinding = _entityBindingProvider.TryCreate(parameter);

                if (argumentBinding == null)
                {
                    throw new InvalidOperationException("Can't bind Table entity to type '" + parameter.ParameterType + "'.");
                }

                binding = new TableEntityBinding(parameter.Name, argumentBinding, client, path);
            }

            return(binding);
        }
예제 #5
0
        // [Table] has some pre-existing behavior where the storage account can be specified outside of the [Table] attribute.
        // The storage account is pulled from the ParameterInfo (which could pull in a [Storage] attribute on the container class)
        // Resolve everything back down to a single attribute so we can use the binding helpers.
        // This pattern should be rare since other extensions can just keep everything directly on the primary attribute.
        private async Task <TableAttribute> CollectAttributeInfo(TableAttribute attrResolved, ParameterInfo parameter, INameResolver nameResolver)
        {
            // Look for [Storage] attribute and squirrel over
            IStorageAccount account = await _accountProvider.GetStorageAccountAsync(parameter, CancellationToken.None, nameResolver);

            StorageClientFactoryContext clientFactoryContext = new StorageClientFactoryContext
            {
                Parameter = parameter
            };
            IStorageTableClient client = account.CreateTableClient(clientFactoryContext);

            return(new ResolvedTableAttribute(attrResolved, client));
        }
예제 #6
0
        public void Table_IfBoundToCloudTable_BindsAndCreatesTable()
        {
            // Arrange
            IStorageAccount account = CreateFakeStorageAccount();
            IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName);
            triggerQueue.AddMessage(triggerQueue.CreateMessage("ignore"));

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

            // Assert
            Assert.NotNull(result);
            Assert.Equal(TableName, result.Name);
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable table = client.GetTableReference(TableName);
            Assert.True(table.Exists());
        }
예제 #7
0
        public void Table_IfBoundToIQueryableDynamicTableEntityAndDoesNotExist_BindsAndDoesNotCreateTable()
        {
            // Arrange
            IStorageAccount account = CreateFakeStorageAccount();
            IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName);
            triggerQueue.AddMessage(triggerQueue.CreateMessage("ignore"));

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

            // Assert
            Assert.NotNull(result);
            Assert.Empty(result);
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable table = client.GetTableReference(TableName);
            Assert.False(table.Exists());
        }
예제 #8
0
        public void Table_IfBoundToIQueryableDynamicTableEntityAndExists_Binds()
        {
            // Arrange
            Guid            expectedValue = Guid.NewGuid();
            IStorageAccount account       = CreateFakeStorageAccount();
            IStorageQueue   triggerQueue  = CreateQueue(account, TriggerQueueName);

            triggerQueue.AddMessage(triggerQueue.CreateMessage("ignore"));
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable       table  = client.GetTableReference(TableName);

            table.CreateIfNotExists();
            Dictionary <string, EntityProperty> properties = new Dictionary <string, EntityProperty>
            {
                { PropertyName, new EntityProperty(expectedValue) }
            };

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

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

            // Assert
            Assert.NotNull(result);
            DynamicTableEntity[] entities = result.ToArray();
            Assert.Equal(1, entities.Length);
            DynamicTableEntity entity = entities[0];

            Assert.NotNull(entity);
            Assert.Equal(PartitionKey, entity.PartitionKey);
            Assert.Equal(RowKey, entity.RowKey);
            Assert.NotNull(entity.Properties);
            Assert.True(entity.Properties.ContainsKey(PropertyName));
            EntityProperty property = entity.Properties[PropertyName];

            Assert.NotNull(property);
            Assert.Equal(EdmType.Guid, property.PropertyType);
            Assert.Equal(expectedValue, property.GuidValue);
        }
예제 #9
0
        public void FlushAfterAdd_PersistsEntity()
        {
            // Arrange
            IStorageAccount     account = CreateFakeStorageAccount();
            IStorageTableClient client  = account.CreateTableClient();
            IStorageTable       table   = client.GetTableReference("Table");

            TableEntityWriter <ITableEntity> product = new TableEntityWriter <ITableEntity>(table);
            const string       partitionKey          = "PK";
            const string       rowKey = "RK";
            DynamicTableEntity entity = new DynamicTableEntity(partitionKey, rowKey);

            product.Add(entity);

            // Act
            product.FlushAsync().GetAwaiter().GetResult();

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

            Assert.NotNull(persisted);
        }
예제 #10
0
        // Assert the given table has the given entity with PropertyName=ExpectedValue
        void AssertStringProperty(
            IStorageAccount account,
            string propertyName,
            string expectedValue,
            string tableName    = TableName,
            string partitionKey = PartitionKey,
            string rowKey       = RowKey)
        {
            // Assert
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable       table  = client.GetTableReference(tableName);

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

            Assert.NotNull(entity);
            Assert.NotNull(entity.Properties);
            Assert.True(entity.Properties.ContainsKey(propertyName));
            EntityProperty property = entity.Properties[propertyName];

            Assert.NotNull(property);
            Assert.Equal(EdmType.String, property.PropertyType);
            Assert.Equal(expectedValue, property.StringValue);
        }
예제 #11
0
        public void Table_IfBoundToICollectorPoco_AddInsertsUsingNativeTableTypes()
        {
            // Arrange
            PocoWithAllTypes expected = new PocoWithAllTypes
            {
                PartitionKey                   = PartitionKey,
                RowKey                         = RowKey,
                BooleanProperty                = true,
                NullableBooleanProperty        = null,
                ByteArrayProperty              = new byte[] { 0x12, 0x34 },
                DateTimeProperty               = DateTime.Now,
                NullableDateTimeProperty       = null,
                DateTimeOffsetProperty         = DateTimeOffset.MaxValue,
                NullableDateTimeOffsetProperty = null,
                DoubleProperty                 = 3.14,
                NullableDoubleProperty         = null,
                GuidProperty                   = Guid.NewGuid(),
                NullableGuidProperty           = null,
                Int32Property                  = 123,
                NullableInt32Property          = null,
                Int64Property                  = 456,
                NullableInt64Property          = null,
                StringProperty                 = "abc",
                PocoProperty                   = new Poco
                {
                    PartitionKey = "def",
                    RowKey       = "ghi",
                    Property     = "jkl"
                }
            };
            IStorageAccount account      = CreateFakeStorageAccount();
            IStorageQueue   triggerQueue = CreateQueue(account, TriggerQueueName);

            triggerQueue.AddMessage(triggerQueue.CreateMessage(JsonConvert.SerializeObject(expected)));

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

            // Assert
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable       table  = client.GetTableReference(TableName);

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

            Assert.Equal(expected.PartitionKey, entity.PartitionKey);
            Assert.Equal(expected.RowKey, entity.RowKey);
            IDictionary <string, EntityProperty> properties = entity.Properties;

            AssertNullablePropertyEqual(expected.BooleanProperty, EdmType.Boolean, properties, "BooleanProperty",
                                        (p) => p.BooleanValue);
            AssertPropertyNull(EdmType.Boolean, properties, "NullableBooleanProperty", (p) => p.BooleanValue);
            AssertPropertyEqual(expected.ByteArrayProperty, EdmType.Binary, properties, "ByteArrayProperty",
                                (p) => p.BinaryValue);
            AssertNullablePropertyEqual(expected.DateTimeProperty, EdmType.DateTime, properties, "DateTimeProperty",
                                        (p) => p.DateTime);
            AssertPropertyNull(EdmType.DateTime, properties, "NullableDateTimeProperty", (p) => p.DateTime);
            AssertNullablePropertyEqual(expected.DateTimeOffsetProperty, EdmType.DateTime, properties,
                                        "DateTimeOffsetProperty", (p) => p.DateTime);
            AssertPropertyNull(EdmType.DateTime, properties, "NullableDateTimeOffsetProperty",
                               (p) => p.DateTimeOffsetValue);
            AssertNullablePropertyEqual(expected.DoubleProperty, EdmType.Double, properties, "DoubleProperty",
                                        (p) => p.DoubleValue);
            AssertPropertyNull(EdmType.Double, properties, "NullableDoubleProperty", (p) => p.DoubleValue);
            AssertNullablePropertyEqual(expected.GuidProperty, EdmType.Guid, properties, "GuidProperty",
                                        (p) => p.GuidValue);
            AssertPropertyNull(EdmType.Guid, properties, "NullableGuidProperty", (p) => p.GuidValue);
            AssertNullablePropertyEqual(expected.Int32Property, EdmType.Int32, properties, "Int32Property",
                                        (p) => p.Int32Value);
            AssertPropertyNull(EdmType.Int32, properties, "NullableInt32Property", (p) => p.Int32Value);
            AssertNullablePropertyEqual(expected.Int64Property, EdmType.Int64, properties, "Int64Property",
                                        (p) => p.Int64Value);
            AssertPropertyNull(EdmType.Int64, properties, "NullableInt64Property", (p) => p.Int64Value);
            AssertPropertyEqual(expected.StringProperty, EdmType.String, properties, "StringProperty",
                                (p) => p.StringValue);
            AssertPropertyEqual(JsonConvert.SerializeObject(expected.PocoProperty, Formatting.Indented), EdmType.String,
                                properties, "PocoProperty", (p) => p.StringValue);
        }
예제 #12
0
        private IStorageTable GetTable(TableAttribute attribute, IStorageAccount account)
        {
            var tableClient = account.CreateTableClient();

            return(tableClient.GetTableReference(attribute.TableName));
        }