Example #1
0
        private static void AccessorTest(
            string navigationName, Func <MyEntity, IEnumerable <MyOtherEntity> > reader, bool initializeCollections = true)
        {
            var accessor = new ClrCollectionAccessorSource(new CollectionTypeFactory()).GetAccessor(CreateNavigation(navigationName));

            var entity = new MyEntity();
            var value  = new MyOtherEntity();

            if (initializeCollections)
            {
                entity.InitializeCollections();
            }

            Assert.False(accessor.Contains(entity, value));
            Assert.DoesNotThrow(() => accessor.Remove(entity, value));

            accessor.Add(entity, value);

            Assert.True(accessor.Contains(entity, value));
            Assert.Equal(1, reader(entity).Count());

            accessor.Remove(entity, value);

            Assert.False(accessor.Contains(entity, value));
            Assert.Equal(0, reader(entity).Count());
        }
Example #2
0
        public void Initialization_for_navigation_without_setter_throws()
        {
            var accessor = new ClrCollectionAccessorSource(new CollectionTypeFactory()).GetAccessor(CreateNavigation("WithNoSetter"));

            Assert.Equal(
                Strings.FormatNavigationNoSetter("WithNoSetter", typeof(MyEntity).FullName),
                Assert.Throws <InvalidOperationException>(() => accessor.Add(new MyEntity(), new MyOtherEntity())).Message);
        }
Example #3
0
        public void Initialization_for_navigation_without_parameterless_constructor_throws()
        {
            var accessor = new ClrCollectionAccessorSource(new CollectionTypeFactory()).GetAccessor(CreateNavigation("AsMyUnavailableCollection"));

            Assert.Equal(
                Strings.FormatNavigationCannotCreateType("AsMyUnavailableCollection", typeof(MyEntity).FullName, typeof(MyUnavailableCollection).FullName),
                Assert.Throws <InvalidOperationException>(() => accessor.Add(new MyEntity(), new MyOtherEntity())).Message);
        }
        private static void AccessorTest(string navigationName, Func <MyEntity, IEnumerable <Random> > reader)
        {
            var entityType = new EntityType(typeof(MyEntity));
            var navigation = entityType.AddNavigation(new Navigation(Mock.Of <ForeignKey>(), navigationName));

            var accessor = new ClrCollectionAccessorSource().GetAccessor(navigation);

            var entity = new MyEntity();
            var value  = new Random();

            Assert.False(accessor.Contains(entity, value));
            Assert.DoesNotThrow(() => accessor.Remove(entity, value));

            accessor.Add(entity, value);

            Assert.True(accessor.Contains(entity, value));
            Assert.Equal(1, reader(entity).Count());

            accessor.Remove(entity, value);

            Assert.False(accessor.Contains(entity, value));
            Assert.Equal(0, reader(entity).Count());
        }