Exemplo n.º 1
0
        public async Task ClearDefaultModelIdAsyncTest()
        {
            ITable table          = Substitute.For <ITable>();
            var    modelsRegistry = new ModelsRegistry(table);

            // Scenario 1: Model is set as default. Should return true after clearing.
            table.DeleteEntityAsync <ModelIdTableEntity>(ModelsRegistry.DefaultModelIdKeyName, CancellationToken.None)
            .Returns(Task.FromResult(true));
            Assert.IsTrue(await modelsRegistry.ClearDefaultModelIdAsync(CancellationToken.None));
            await table.Received(1).DeleteEntityAsync <ModelIdTableEntity>(ModelsRegistry.DefaultModelIdKeyName, CancellationToken.None);

            // Scenario 2: Table is unable to delete entity
            table.ClearReceivedCalls();
            table.DeleteEntityAsync <ModelIdTableEntity>(Arg.Any <string>(), CancellationToken.None)
            .Returns(Task.FromResult(false));
            Assert.IsFalse(await modelsRegistry.ClearDefaultModelIdAsync(CancellationToken.None));
            await table.Received(1).DeleteEntityAsync <ModelIdTableEntity>(ModelsRegistry.DefaultModelIdKeyName, CancellationToken.None);
        }
Exemplo n.º 2
0
        public async Task UpdateModelAsyncTest()
        {
            ITable table          = Substitute.For <ITable>();
            var    modelsRegistry = new ModelsRegistry(table);

            table.MergeEntityAsync(Arg.Any <ModelTableEntity>(), CancellationToken.None).Returns(Task.FromResult(true));

            var modelId = Guid.NewGuid();

            // Scenario 1: All variables null. nothing should be overridden.
            await modelsRegistry.UpdateModelAsync(modelId, CancellationToken.None);

            await table.Received(1).MergeEntityAsync(
                Arg.Is <ModelTableEntity>(
                    me => Guid.Parse(me.RowKey) == modelId && me.ModelStatus == null && me.StatusMessage == null && me.ModelStatistics == null),
                CancellationToken.None);

            // Scenario 2: Only status needs to be updated
            table.ClearReceivedCalls();
            await modelsRegistry.UpdateModelAsync(modelId, CancellationToken.None, ModelStatus.InProgress);

            await table.Received(1).MergeEntityAsync(
                Arg.Is <ModelTableEntity>(
                    me =>
                    Guid.Parse(me.RowKey) == modelId && me.ModelStatus == ModelStatus.InProgress.ToString() &&
                    me.StatusMessage == null && me.ModelStatistics == null), CancellationToken.None);

            // Scenario 3: Only status message needs to be updated
            table.ClearReceivedCalls();
            await modelsRegistry.UpdateModelAsync(modelId, CancellationToken.None, null, "test");

            await table.Received(1).MergeEntityAsync(
                Arg.Is <ModelTableEntity>(
                    me => Guid.Parse(me.RowKey) == modelId && me.ModelStatus == null && me.StatusMessage == "test" && me.ModelStatistics == null),
                CancellationToken.None);

            // Scenario 4: Only statistics needs to be updated
            var modelStatistics = new ModelStatistics
            {
                NumberOfCatalogItems = 10
            };

            table.ClearReceivedCalls();
            await modelsRegistry.UpdateModelAsync(modelId, CancellationToken.None, null, null, modelStatistics);

            await table.Received(1).MergeEntityAsync(
                Arg.Is <ModelTableEntity>(
                    me =>
                    Guid.Parse(me.RowKey) == modelId && me.ModelStatus == null && me.StatusMessage == null &&
                    me.ModelStatistics == JsonConvert.SerializeObject(modelStatistics)),
                CancellationToken.None);

            // Scenario 5: everything needs to be updated
            table.ClearReceivedCalls();
            await modelsRegistry.UpdateModelAsync(modelId, CancellationToken.None, ModelStatus.Completed, "test", modelStatistics);

            await table.Received(1).MergeEntityAsync(
                Arg.Is <ModelTableEntity>(
                    me =>
                    Guid.Parse(me.RowKey) == modelId && me.ModelStatus == ModelStatus.Completed.ToString() &&
                    me.StatusMessage == "test" && me.ModelStatistics == JsonConvert.SerializeObject(modelStatistics)),
                CancellationToken.None);
        }