/// <summary>
        /// Create a <see cref="MockEntitySet"/> with cities loaded into it.  Load those cities
        /// into the <paramref name="pagedCollection"/>.
        /// </summary>
        /// <param name="raiseCollectionChangedEvents">Whether or not the <see cref="MockEntitySet"/> should raise
        /// <see cref="INotifyCollectionChanged.CollectionChanged"/> events.</param>
        /// <param name="pagedCollection">The <see cref="PagedEntityCollection"/> to hook up to
        /// the loaded <see cref="MockEntitySet"/>.  If <c>null</c>, it will be instantiated.
        /// The cities loaded into the <see cref="MockEntitySet"/> will also be loaded into
        /// the <paramref name="pagedCollection"/>.</param>
        /// <returns>The <see cref="MockEntitySet"/> with cities loaded into it.</returns>
        private MockEntitySet LoadCities(bool raiseCollectionChangedEvents, PagedEntityCollection pagedCollection)
        {
            // Get a mock entity set of cities
            MockEntitySet cities = new MockEntitySet(raiseCollectionChangedEvents);

            // Load a couple of cities into the set
            cities.Add(new City
            {
                Name       = "First City",
                StateName  = "ST",
                CountyName = "County"
            });

            cities.Add(new City
            {
                Name       = "Second City",
                StateName  = "ST",
                CountyName = "County"
            });

            // Create a paged collection for the entity set
            pagedCollection.BackingEntitySet = cities;

            // Process the cities as loaded entities
            pagedCollection.BeginLoad();
            foreach (City city in cities)
            {
                pagedCollection.AddLoadedEntity(city);
            }
            pagedCollection.CompleteLoad();

            return(cities);
        }
        public void ResetAddsTrackedAddedEntities()
        {
            PagedEntityCollection pagedCollection = new PagedEntityCollection(p => { return(true); });
            MockEntitySet         cities          = LoadCities(true, pagedCollection);

            // Remove one entity that won't get resurrected
            Entity toRemove = pagedCollection[1];

            cities.Remove(toRemove);

            // And remove another entity that will get resurrected
            Entity toRemoveAndAdd = pagedCollection[0];

            cities.Remove(toRemoveAndAdd);

            // Turn off the events so that the Add isn't raised
            cities.RaiseCollectionChangedEvents = false;
            cities.Add(toRemoveAndAdd);

            // Raise the Reset event to pick up the city that was added back
            cities.ResetCollection();
            Assert.AreEqual <int>(cities.Count, pagedCollection.Count, "pagedCollection.Count should equal the cities count after adding the city back and resetting the collection");
            Assert.IsTrue(pagedCollection.Contains(toRemoveAndAdd), "The city should be back in the pagedCollection after adding it back and resetting the collection");
            Assert.IsFalse(pagedCollection.Contains(toRemove), "The city that was removed but not added back should not be in the pagedCollection");
        }
        public void GetQueryForPartitionKey()
        {
            MockEntitySet entitySet = new MockEntitySet();

            entitySet.PartitionKey = null;

            IQueryable <MockEntity> entities = entitySet.GetQuery();

            Assert.IsNotNull(entities,
                             "Query should not be null.");

            entitySet.PartitionKey = "PK";

            IQueryable <MockEntity> entities2 = entitySet.GetQuery();

            Assert.IsNotNull(entities2,
                             "Query with partition key should not be null.");
            Assert.IsInstanceOfType(entities2.Expression, typeof(MethodCallExpression),
                                    "Expression should be a method call.");
            Assert.AreEqual("Where", ((MethodCallExpression)entities2.Expression).Method.Name,
                            "Expression should be a Where call.");

            Assert.AreNotEqual(entities, entities2,
                               "Queries should not be equal.");

            // We stop short of actually enumerating the query as that would try to resolve
            // our mock uri (and fail)
        }
        public void UpdateExistingEntity()
        {
            MockEntitySet entitySet = new MockEntitySet();
            MockEntity    entity    = new MockEntity()
            {
                PartitionKey = "PK", RowKey = "RK"
            };

            Assert.AreEqual(EntityStates.Detached, entitySet.GetEntityState(entity),
                            "Entity should start detached.");

            entitySet.Attach(entity);

            Assert.AreEqual(EntityStates.Unchanged, entitySet.GetEntityState(entity),
                            "Entity should now be attached.");

            entitySet.Update(entity);

            Assert.AreEqual(EntityStates.Modified, entitySet.GetEntityState(entity),
                            "Entity should be modified.");

            entitySet.Detach(entity);

            Assert.AreEqual(EntityStates.Detached, entitySet.GetEntityState(entity),
                            "Entity should return to detached.");

            entitySet.Update(entity);

            Assert.AreEqual(EntityStates.Modified, entitySet.GetEntityState(entity),
                            "Entity should be modified after being detached.");
        }
        public void ResetEventRetainsEntitiesStillInSet()
        {
            PagedEntityCollection pagedCollection = new PagedEntityCollection(p => { return(true); });
            MockEntitySet         cities          = LoadCities(false, pagedCollection);

            cities.ResetCollection();
            Assert.AreEqual <int>(cities.Count, pagedCollection.Count, "pagedCollection.Count should match cities.Count after resetting the collection");
        }
        public void ResetRemovesClearedEntities()
        {
            PagedEntityCollection pagedCollection = new PagedEntityCollection(p => { return(true); });
            MockEntitySet         cities          = LoadCities(true, pagedCollection);

            // This will clear the set and raise a Reset event
            // This particular scenario is what led to the bug reported
            cities.Clear();
            Assert.AreEqual <int>(0, pagedCollection.Count, "pagedCollection.Count should be 0 after clearing the cities set");
        }
Beispiel #7
0
 public MockDataRepository()
 {
     _roles = new MockEntitySet<Role>(this);
     _users = new MockEntitySet<User>(this);
     _reviews = new MockEntitySet<Review>(this);
     _changes = new MockEntitySet<FileChange>(this);
     _comments = new MockEntitySet<Comment>(this);
     _iterations = new MockEntitySet<Iteration>(this);
     _credentials = new MockEntitySet<Credential>(this);
 }
        public void TableName()
        {
            MockEntitySet entitySet = new MockEntitySet();

            Assert.AreEqual(typeof(MockEntity).Name, entitySet.TableName,
                            "The table name should equal MockEntity.");

            string tableName = "TableName";

            entitySet = new MockEntitySet(tableName);

            Assert.AreEqual(tableName, entitySet.TableName,
                            "The table name should equal tableName.");
        }
        public void ResetRemovesRemovedEntities()
        {
            PagedEntityCollection pagedCollection = new PagedEntityCollection(p => { return(true); });
            MockEntitySet         cities          = LoadCities(false, pagedCollection);

            Entity toRemove = pagedCollection[0];

            cities.Remove(toRemove);
            Assert.AreEqual <int>(cities.Count + 1, pagedCollection.Count, "pagedCollection.Count should be one more than cities.Count after removing the city from the cities set");
            Assert.IsTrue(pagedCollection.Contains(toRemove), "pagedCollection should still contain the city removed from the cities set until the Reset is raised");

            cities.ResetCollection();
            Assert.AreEqual <int>(cities.Count, pagedCollection.Count, "pagedCollection.Count should match cities.Count after resetting the collection");
            Assert.IsFalse(pagedCollection.Contains(toRemove), "pagedCollection should no longer contain the city removed from the cities set after resetting the collection");
        }
        public void RepeatedEntitiesNoDuplicated()
        {
            PagedEntityCollection pagedCollection = new PagedEntityCollection(p => { return(true); });

            // This will load the cities into the collection the first time
            MockEntitySet cities = LoadCities(false, pagedCollection);

            // Now we'll load the cities into the collection a second time
            pagedCollection.BeginLoad();
            foreach (City city in cities)
            {
                pagedCollection.AddLoadedEntity(city);
            }
            pagedCollection.CompleteLoad();

            Assert.AreEqual(cities.Count, pagedCollection.Count, "The counts should still match");
        }
        public void PartitionKey()
        {
            MockEntitySet entitySet = new MockEntitySet();

            string partitionKey = "PK";

            Assert.IsNull(entitySet.PartitionKey, "PartitionKey should be null by default.");

            entitySet.PartitionKey = partitionKey;

            Assert.AreEqual(partitionKey, entitySet.PartitionKey,
                            "PartitionKey should equal partitionKey.");

            entitySet.PartitionKey = null;

            Assert.IsNull(entitySet.PartitionKey, "PartitionKey should be set back to null.");
        }
        public void UpdateNewEntity()
        {
            MockEntitySet entitySet = new MockEntitySet();
            MockEntity    entity    = new MockEntity();

            Assert.AreEqual(EntityStates.Detached, entitySet.GetEntityState(entity),
                            "Entity should start detached.");

            entitySet.Add(entity);

            Assert.AreEqual(EntityStates.Added, entitySet.GetEntityState(entity),
                            "Entity should be added.");

            entitySet.Update(entity);

            Assert.AreEqual(EntityStates.Added, entitySet.GetEntityState(entity),
                            "Entity should still be added.");
        }
        public void ResetIgnoresUntrackedAddedEntities()
        {
            PagedEntityCollection pagedCollection = new PagedEntityCollection(p => { return(true); });
            MockEntitySet         cities          = LoadCities(false, pagedCollection);

            City toAdd = new City
            {
                Name       = "New City",
                StateName  = "ST",
                CountyName = "County"
            };

            cities.Add(toAdd);
            Assert.AreEqual <int>(cities.Count - 1, pagedCollection.Count, "pagedCollection.Count should be one less than cities.Count after adding the city to the cities set");
            Assert.IsFalse(pagedCollection.Contains(toAdd), "pagedCollection should not contain the city added to the cities set before the Reset is raised");

            cities.ResetCollection();
            Assert.AreEqual <int>(cities.Count - 1, pagedCollection.Count, "pagedCollection.Count should be one less than cities.Count after resetting the collection");
            Assert.IsFalse(pagedCollection.Contains(toAdd), "pagedCollection should not contain the city added to the cities set after resetting the collection");
        }
        public void PartitionKeyIsUsedForNewEntity()
        {
            MockEntitySet entitySet = new MockEntitySet();

            entitySet.PartitionKey = "PK";

            MockEntity entity1 = new MockEntity();

            entitySet.Add(entity1);

            Assert.AreEqual(entitySet.PartitionKey, entity1.PartitionKey,
                            "First entity PartitionKey should equal the TableEntitySet PartitionKey.");

            MockEntity entity2 = new MockEntity();

            entitySet.Add(entity2);

            Assert.AreEqual(entitySet.PartitionKey, entity2.PartitionKey,
                            "Second entity PartitionKey should equal the TableEntitySet PartitionKey.");
        }
        public void AddEventRelayed()
        {
            PagedEntityCollection pagedCollection = new PagedEntityCollection(p => { return(true); });
            MockEntitySet         cities          = LoadCities(true, pagedCollection);

            // Remove an item
            pagedCollection.RemoveAt(0);

            // Add an item
            pagedCollection.Add(new City
            {
                Name       = "Added City",
                StateName  = "ST",
                CountyName = "County"
            });

            City removedCity = pagedCollection.OfType <City>().First();

            cities.Remove(removedCity);

            // A new city should not be added to the collection
            City addedCity = new City
            {
                Name       = "Added City 2",
                StateName  = "ST",
                CountyName = "County"
            };

            this.AssertCollectionChanged(
                () => cities.Add(addedCity),
                pagedCollection,
                (NotifyCollectionChangedEventArgs)null,
                "when adding a new city.");

            // A restored city should be re-added to the collection
            this.AssertCollectionChanged(
                () => cities.Add(removedCity),
                pagedCollection,
                new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, removedCity, pagedCollection.Count - 1),
                "when re-adding a removed city.");
        }
        public void UniqueRowKeyIsUsedForNewEntity()
        {
            MockEntitySet entitySet = new MockEntitySet();

            MockEntity entity1 = new MockEntity();

            entitySet.Add(entity1);

            Assert.IsNotNull(entity1.RowKey,
                             "First entity RowKey should not be null.");

            MockEntity entity2 = new MockEntity();

            entitySet.Add(entity2);

            Assert.IsNotNull(entity2.RowKey,
                             "Second entity RowKey should not be null.");

            Assert.AreNotEqual(entity1.RowKey, entity2.RowKey,
                               "RowKeys should be unique.");
        }
        public void ResetEventRelayed()
        {
            PagedEntityCollection pagedCollection = new PagedEntityCollection(p => { return(true); });
            MockEntitySet         cities          = LoadCities(false, pagedCollection);

            // Remove an item
            pagedCollection.RemoveAt(0);

            // Add an item
            pagedCollection.Add(new City
            {
                Name       = "Added City",
                StateName  = "ST",
                CountyName = "County"
            });

            this.AssertCollectionChanged(
                () => cities.ResetCollection(),
                pagedCollection,
                new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset),
                "when resetting the collection.");
        }
        public void PresetValuesAreUsedForNewEntity()
        {
            MockEntitySet entitySet = new MockEntitySet();

            entitySet.PartitionKey = "PK";

            string partitionKey = "MyPartitionKey";
            string rowKey       = "MyRowKey";

            MockEntity entity1 = new MockEntity()
            {
                PartitionKey = partitionKey, RowKey = rowKey
            };

            entitySet.Add(entity1);

            Assert.AreEqual(partitionKey, entity1.PartitionKey,
                            "First entity PartitionKey should not be overwritten by the TableEntitySet PartitionKey.");
            Assert.AreEqual(rowKey, entity1.RowKey,
                            "First entity RowKey should not be overwritten by the TableEntitySet.");

            entitySet.PartitionKey = null;
            rowKey = "MyRowKey2";

            MockEntity entity2 = new MockEntity()
            {
                PartitionKey = partitionKey, RowKey = rowKey
            };

            entitySet.Add(entity2);

            Assert.AreEqual(partitionKey, entity2.PartitionKey,
                            "Second entity PartitionKey should not be overwritten by the TableEntitySet PartitionKey.");
            Assert.AreEqual(rowKey, entity2.RowKey,
                            "Second entity RowKey should not be overwritten by the TableEntitySet.");
        }
        public void RemoveEventRelayed()
        {
            PagedEntityCollection pagedCollection = new PagedEntityCollection(p => { return(true); });
            MockEntitySet         cities          = LoadCities(true, pagedCollection);

            // Remove an item
            pagedCollection.RemoveAt(0);

            // Add an item
            pagedCollection.Add(new City
            {
                Name       = "Added City",
                StateName  = "ST",
                CountyName = "County"
            });

            City removedCity = pagedCollection.OfType <City>().First();

            this.AssertCollectionChanged(
                () => cities.Remove(removedCity),
                pagedCollection,
                new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, removedCity, 0),
                "when removing a city.");
        }
        /// <summary>
        /// Create a <see cref="MockEntitySet"/> with cities loaded into it.  Load those cities
        /// into the <paramref name="pagedCollection"/>.
        /// </summary>
        /// <param name="raiseCollectionChangedEvents">Whether or not the <see cref="MockEntitySet"/> should raise
        /// <see cref="INotifyCollectionChanged.CollectionChanged"/> events.</param>
        /// <param name="pagedCollection">The <see cref="PagedEntityCollection"/> to hook up to
        /// the loaded <see cref="MockEntitySet"/>.  If <c>null</c>, it will be instantiated.
        /// The cities loaded into the <see cref="MockEntitySet"/> will also be loaded into
        /// the <paramref name="pagedCollection"/>.</param>
        /// <returns>The <see cref="MockEntitySet"/> with cities loaded into it.</returns>
        private MockEntitySet LoadCities(bool raiseCollectionChangedEvents, PagedEntityCollection pagedCollection)
        {
            // Get a mock entity set of cities
            MockEntitySet cities = new MockEntitySet(raiseCollectionChangedEvents);

            // Load a couple of cities into the set
            cities.Add(new City
            {
                Name = "First City",
                StateName = "ST",
                CountyName = "County"
            });

            cities.Add(new City
            {
                Name = "Second City",
                StateName = "ST",
                CountyName = "County"
            });

            // Create a paged collection for the entity set
            pagedCollection.BackingEntitySet = cities;

            // Process the cities as loaded entities
            pagedCollection.BeginLoad();
            foreach (City city in cities)
            {
                pagedCollection.AddLoadedEntity(city);
            }
            pagedCollection.CompleteLoad();

            return cities;
        }