Exemplo n.º 1
0
        public async Task DynamoDBDataManager_UpsertItemAsync()
        {
            var expression = "attribute_not_exists(PartitionKey) AND attribute_not_exists(RowKey)";
            var toPersist  = GenerateNewData();

            toPersist.StringData = "Create";
            await manager.PutEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, GetValues(toPersist, true), expression);

            toPersist.StringData = "Replaced";
            await manager.UpsertEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, GetKeys(toPersist), GetValues(toPersist));

            var persisted = await manager.ReadSingleEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, GetKeys(toPersist), response => new UnitTestDynamoDBTableData(response));

            Assert.Equal("Replaced", persisted.StringData);
            Assert.True(persisted.ETag == 0); //Yes, ETag didn't changed cause we didn't

            persisted.StringData = "Updated";
            var persistedEtag = persisted.ETag;

            expression = $"ETag = :OldETag";
            persisted.ETag++; //Increase ETag
            var expValues = new Dictionary <string, AttributeValue> {
                { ":OldETag", new AttributeValue {
                      N = persistedEtag.ToString()
                  } }
            };
            await manager.UpsertEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, GetKeys(persisted), GetValues(persisted), expression, expValues);

            persisted = await manager.ReadSingleEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, GetKeys(toPersist), response => new UnitTestDynamoDBTableData(response));

            Assert.Equal("Updated", persisted.StringData);
            Assert.NotEqual(persistedEtag, persisted.ETag); //Now ETag changed cause we did it

            await Assert.ThrowsAsync <ConditionalCheckFailedException>(async() =>
            {
                await manager.UpsertEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, GetKeys(toPersist), GetValues(toPersist), expression, expValues);
            });
        }