Example #1
0
        public void Uses_single_generator_per_cache_key_when_pool_size_is_one()
        {
            var property = CreateProperty(ValueGenerationOnAdd.Client);

            var factoryMock = new Mock <SimpleValueGeneratorFactory <GuidValueGenerator> >();

            factoryMock.Setup(m => m.Create(property)).Returns(() => new TemporaryValueGenerator());
            factoryMock.Setup(m => m.GetPoolSize(property)).Returns(1);
            factoryMock.Setup(m => m.GetCacheKey(property)).Returns("TheKeyMaster");

            var selector = new ValueGeneratorSelector(factoryMock.Object);
            var cache    = new ValueGeneratorCache(selector, Mock.Of <ForeignKeyValueGenerator>());

            var generator1 = cache.GetGenerator(property);

            Assert.NotNull(generator1);
            Assert.Same(generator1, cache.GetGenerator(property));

            factoryMock.Setup(m => m.GetCacheKey(property)).Returns("TheGatekeeper");

            var generator2 = cache.GetGenerator(property);

            Assert.NotNull(generator2);
            Assert.Same(generator2, cache.GetGenerator(property));
            Assert.NotSame(generator1, generator2);
        }
        public void Uses_pool_per_cache_key_when_pool_size_is_greater_than_one()
        {
            var property = CreateProperty(ValueGenerationOnAdd.Client);

            var factoryMock = new Mock<SimpleValueGeneratorFactory<GuidValueGenerator>>();
            factoryMock.Setup(m => m.Create(property)).Returns(CreateValueGeneratorCallback);
            factoryMock.Setup(m => m.GetPoolSize(property)).Returns(2);
            factoryMock.Setup(m => m.GetCacheKey(property)).Returns("TheKeyMaster");

            var selector = new ValueGeneratorSelector(factoryMock.Object);
            var cache = new ValueGeneratorCache(selector, Mock.Of<ForeignKeyValueGenerator>());

            var generator1a = cache.GetGenerator(property);
            var generator1b = cache.GetGenerator(property);
            Assert.NotSame(generator1a, generator1b);

            Assert.Same(generator1a, cache.GetGenerator(property));
            Assert.Same(generator1b, cache.GetGenerator(property));
            Assert.Same(generator1a, cache.GetGenerator(property));
            Assert.Same(generator1b, cache.GetGenerator(property));

            factoryMock.Setup(m => m.GetCacheKey(property)).Returns("TheGatekeeper");

            var generator2a = cache.GetGenerator(property);
            var generator2b = cache.GetGenerator(property);
            Assert.NotSame(generator2a, generator2b);
            Assert.NotSame(generator1a, generator2a);
            Assert.NotSame(generator1b, generator2a);

            Assert.Same(generator2a, cache.GetGenerator(property));
            Assert.Same(generator2b, cache.GetGenerator(property));
            Assert.Same(generator2a, cache.GetGenerator(property));
            Assert.Same(generator2b, cache.GetGenerator(property));
        }
Example #3
0
        public void Returns_in_memory_GUID_generator_for_GUID_types_setup_for_client_values()
        {
            var guidFactory = new SimpleValueGeneratorFactory <GuidValueGenerator>();

            var selector = new ValueGeneratorSelector(guidFactory);

            Assert.Same(guidFactory, selector.Select(CreateProperty(typeof(Guid), ValueGenerationOnAdd.Client)));
        }
Example #4
0
        public void Returns_null_if_selector_returns_null()
        {
            var property = CreateProperty(ValueGenerationOnAdd.None);
            var selector = new ValueGeneratorSelector(new SimpleValueGeneratorFactory <GuidValueGenerator>());
            var cache    = new ValueGeneratorCache(selector, Mock.Of <ForeignKeyValueGenerator>());

            Assert.Null(cache.GetGenerator(property));
        }
        public void Returns_in_memory_GUID_generator_for_GUID_types_setup_for_client_values()
        {
            var guidFactory = new SimpleValueGeneratorFactory<GuidValueGenerator>();

            var selector = new ValueGeneratorSelector(guidFactory);

            Assert.Same(guidFactory, selector.Select(CreateProperty(typeof(Guid), ValueGenerationOnAdd.Client)));
        }
        public void Returns_null_if_selector_returns_null()
        {
            var property = CreateProperty(ValueGenerationOnAdd.None);
            var selector = new ValueGeneratorSelector(new SimpleValueGeneratorFactory<GuidValueGenerator>());
            var cache = new ValueGeneratorCache(selector, Mock.Of<ForeignKeyValueGenerator>());

            Assert.Null(cache.GetGenerator(property));
        }
Example #7
0
        public ValueGeneratorCache(
            [NotNull] ValueGeneratorSelector selector,
            [NotNull] ForeignKeyValueGenerator foreignKeyValueGenerator)
        {
            Check.NotNull(selector, "selector");
            Check.NotNull(foreignKeyValueGenerator, "foreignKeyValueGenerator");

            _selector = selector;
            _foreignKeyValueGenerator = foreignKeyValueGenerator;
        }
        public ValueGeneratorCache(
            [NotNull] ValueGeneratorSelector selector,
            [NotNull] ForeignKeyValueGenerator foreignKeyValueGenerator)
        {
            Check.NotNull(selector, "selector");
            Check.NotNull(foreignKeyValueGenerator, "foreignKeyValueGenerator");

            _selector = selector;
            _foreignKeyValueGenerator = foreignKeyValueGenerator;
        }
        public void Throws_for_unsupported_combinations()
        {
            var selector = new ValueGeneratorSelector(
                new SimpleValueGeneratorFactory<GuidValueGenerator>());

            var typeMock = new Mock<IEntityType>();
            typeMock.Setup(m => m.Name).Returns("AnEntity");

            var property = CreateProperty(typeof(Guid), ValueGenerationOnAdd.Server);

            Assert.Equal(
                Strings.FormatNoValueGenerator("server", "MyType", "MyProperty", "Guid"),
                Assert.Throws<NotSupportedException>(() => selector.Select(property)).Message);
        }
Example #10
0
        public void Throws_for_unsupported_combinations()
        {
            var selector = new ValueGeneratorSelector(
                new SimpleValueGeneratorFactory <GuidValueGenerator>());

            var typeMock = new Mock <IEntityType>();

            typeMock.Setup(m => m.Name).Returns("AnEntity");

            var property = CreateProperty(typeof(Guid), ValueGenerationOnAdd.Server);

            Assert.Equal(
                Strings.FormatNoValueGenerator("server", "MyType", "MyProperty", "Guid"),
                Assert.Throws <NotSupportedException>(() => selector.Select(property)).Message);
        }
Example #11
0
        public void Uses_pool_per_cache_key_when_pool_size_is_greater_than_one()
        {
            var property = CreateProperty(ValueGenerationOnAdd.Client);

            var factoryMock = new Mock <SimpleValueGeneratorFactory <GuidValueGenerator> >();

            factoryMock.Setup(m => m.Create(property)).Returns(CreateValueGeneratorCallback);
            factoryMock.Setup(m => m.GetPoolSize(property)).Returns(2);
            factoryMock.Setup(m => m.GetCacheKey(property)).Returns("TheKeyMaster");

            var selector = new ValueGeneratorSelector(factoryMock.Object);
            var cache    = new ValueGeneratorCache(selector, Mock.Of <ForeignKeyValueGenerator>());

            var generator1a = cache.GetGenerator(property);
            var generator1b = cache.GetGenerator(property);

            Assert.NotSame(generator1a, generator1b);

            Assert.Same(generator1a, cache.GetGenerator(property));
            Assert.Same(generator1b, cache.GetGenerator(property));
            Assert.Same(generator1a, cache.GetGenerator(property));
            Assert.Same(generator1b, cache.GetGenerator(property));

            factoryMock.Setup(m => m.GetCacheKey(property)).Returns("TheGatekeeper");

            var generator2a = cache.GetGenerator(property);
            var generator2b = cache.GetGenerator(property);

            Assert.NotSame(generator2a, generator2b);
            Assert.NotSame(generator1a, generator2a);
            Assert.NotSame(generator1b, generator2a);

            Assert.Same(generator2a, cache.GetGenerator(property));
            Assert.Same(generator2b, cache.GetGenerator(property));
            Assert.Same(generator2a, cache.GetGenerator(property));
            Assert.Same(generator2b, cache.GetGenerator(property));
        }
Example #12
0
        public ValueGeneratorCache([NotNull] ValueGeneratorSelector selector)
        {
            Check.NotNull(selector, "selector");

            _selector = selector;
        }
Example #13
0
        public void Returns_null_when_no_value_generation_configured()
        {
            var selector = new ValueGeneratorSelector(new SimpleValueGeneratorFactory <GuidValueGenerator>());

            Assert.Null(selector.Select(CreateProperty(typeof(int), ValueGenerationOnAdd.None)));
        }
        public void Returns_null_when_no_value_generation_configured()
        {
            var selector = new ValueGeneratorSelector(new SimpleValueGeneratorFactory<GuidValueGenerator>());

            Assert.Null(selector.Select(CreateProperty(typeof(int), ValueGenerationOnAdd.None)));
        }