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(CreateValueGeneratorCallback);
            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);
        }
Exemple #2
0
        public void Returns_null_when_no_value_generation_not_required()
        {
            var selector = new ValueGeneratorSelector(new SimpleValueGeneratorFactory <GuidValueGenerator>());

            Assert.Null(selector.Select(CreateProperty(typeof(int), ValueGeneration.None)));
            Assert.Null(selector.Select(CreateProperty(typeof(int), ValueGeneration.OnAddAndUpdate)));
        }
Exemple #3
0
        public void Returns_in_memory_GUID_generator_for_GUID_types_setup_for_value_generation()
        {
            var guidFactory = new SimpleValueGeneratorFactory <GuidValueGenerator>();

            var selector = new ValueGeneratorSelector(guidFactory);

            Assert.Same(guidFactory, selector.Select(CreateProperty(typeof(Guid), ValueGeneration.OnAdd)));
        }
        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_null_if_selector_returns_null()
        {
            var property = CreateProperty(generateValues: false);
            var selector = new ValueGeneratorSelector(new SimpleValueGeneratorFactory <GuidValueGenerator>());
            var cache    = new ValueGeneratorCache(selector);

            Assert.Null(cache.GetGenerator(property));
        }
Exemple #6
0
    public void Returns_built_in_generators_for_types_setup_for_value_generation()
    {
        var model      = BuildModel();
        var entityType = model.FindEntityType(typeof(AnEntity));

        var selector = new ValueGeneratorSelector(
            new ValueGeneratorSelectorDependencies(new ValueGeneratorCache(new ValueGeneratorCacheDependencies())));

        Assert.IsType <CustomValueGenerator>(selector.Select(entityType.FindProperty("Custom"), entityType));

        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("Id"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("Long"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("Short"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("Byte"), entityType));

        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableInt"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableLong"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableShort"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableByte"), entityType));

        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("UInt"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("ULong"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("UShort"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("SByte"), entityType));

        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableUInt"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableULong"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableUShort"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableSByte"), entityType));

        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("Decimal"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableDecimal"), entityType));

        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("Float"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableFloat"), entityType));

        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("Double"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableDouble"), entityType));

        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("DateTime"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableDateTime"), entityType));

        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("DateTimeOffset"), entityType));
        Assert.Throws <NotSupportedException>(() => selector.Select(entityType.FindProperty("NullableDateTimeOffset"), entityType));

        Assert.IsType <StringValueGenerator>(selector.Select(entityType.FindProperty("String"), entityType));

        Assert.IsType <GuidValueGenerator>(selector.Select(entityType.FindProperty("Guid"), entityType));
        Assert.IsType <GuidValueGenerator>(selector.Select(entityType.FindProperty("NullableGuid"), entityType));

        Assert.IsType <BinaryValueGenerator>(selector.Select(entityType.FindProperty("Binary"), entityType));
    }
Exemple #7
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(Random), ValueGeneration.OnAdd);

            Assert.Equal(
                Strings.FormatNoValueGenerator("MyProperty", "MyType", "Random"),
                Assert.Throws <NotSupportedException>(() => selector.Select(property)).Message);
        }
        public void Uses_pool_per_cache_key_when_pool_size_is_greater_than_one()
        {
            var property = CreateProperty(ValueGeneration.OnAdd);

            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));
        }
 public AtsValueGeneratorCache(
     [NotNull] ValueGeneratorSelector selector, [NotNull] ForeignKeyValueGenerator foreignKeyValueGenerator)
     : base(selector, foreignKeyValueGenerator)
 {
 }
Exemple #10
0
 // TODO: Value generators for SQLite
 public SQLiteValueGeneratorCache([NotNull] ValueGeneratorSelector selector)
     : base(selector)
 {
 }