コード例 #1
0
        public void DeleteItem_WhenSimilar_IfOneDeleted_ThenAnotherRemains()
        {
            using var cosmosDirectAccess = Fixture.GetDirectAccessToCosmosContainer();

            var itemsBeforeDeletion = AddAndRetrieveItems(new List <TestItem> {
                new TestItem {
                    Name = "Paloma", Family = "Farah"
                },
                new TestItem {
                    Name = "Andressa", Family = "Rabah"
                },
            }, cosmosDirectAccess);

            // Löscht das erste Element:
            TestItem item1 = itemsBeforeDeletion.First();

            Fixture.Service.DeleteItemAsync(item1.PartitionKeyValue, item1.Id).Wait();

            var itemsAfterDeletion =
                cosmosDirectAccess.CollectResultsFromQuery(source => source.Select(item => item));

            Assert.DoesNotContain(itemsAfterDeletion,
                                  remainingItem => item1.Equals(remainingItem));

            Assert.Equal(itemsBeforeDeletion.Count() - 1, itemsAfterDeletion.Count());

            // Löscht das zweite Element:
            TestItem item2 = itemsBeforeDeletion.Last();

            Fixture.Service.DeleteItemAsync(item2.PartitionKeyValue, item2.Id).Wait();

            itemsAfterDeletion =
                cosmosDirectAccess.CollectResultsFromQuery(source => source.Select(item => item));

            Assert.DoesNotContain(itemsAfterDeletion,
                                  remainingItem => item2.Equals(remainingItem));

            Assert.Equal(itemsBeforeDeletion.Count() - 2, itemsAfterDeletion.Count());
        }
コード例 #2
0
        /// <summary>
        /// Fügt dem Container neue Elemente hinzu.
        /// </summary>
        /// <param name="items">Die hinzuzufügenden Elemente.</param>
        public IEnumerable <TestItem> AddToContainer(IList <TestItem> items)
        {
            var tasks      = new List <Task <ItemResponse <TestItem> > >(capacity: maxNumTasks);
            var addedItems = new List <TestItem>(capacity: items.Count);

            var randomizer = new Random();

            for (int idx = 0; idx < items.Count; ++idx)
            {
                TestItem item = items[idx];
                item.Id = randomizer.Next().ToString("X8");
                var asyncCreateTask = Container.CreateItemAsync(item, new PartitionKey(item.PartitionKeyValue));

                tasks.Add(asyncCreateTask);
                if (tasks.Count == maxNumTasks || idx + 1 == items.Count)
                {
                    Task.WaitAll(tasks.ToArray());
                    addedItems.AddRange(from task in tasks select task.Result.Resource);
                    tasks.Clear();
                }
            }

            return(addedItems);
        }