public void Insert_with_Identity_column()
        {
            using (var testStore = MySqlTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextIdentity(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    context.AddRange(new Blog {
                        Name = "One Unicorn"
                    }, new Blog {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

                using (var context = new BlogContextIdentity(testStore.Name))
                {
                    var blogs = context.Blogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal(1, blogs[0].Id);
                    Assert.Equal(2, blogs[1].Id);
                }
            }
        }
        public void Insert_with_explicit_with_default_keys()
        {
            using (var testStore = MySqlTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextNoKeyGenerationNullableKey(testStore.Name))
                {
                    context.Database.EnsureCreatedResiliently();

                    context.AddRange(
                        new NullableKeyBlog
                    {
                        Id   = 0,
                        Name = "One Unicorn"
                    },
                        new NullableKeyBlog
                    {
                        Id   = 1,
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

                using (var context = new BlogContextNoKeyGenerationNullableKey(testStore.Name))
                {
                    var blogs = context.NullableKeyBlogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal(0, blogs[0].Id);
                    Assert.Equal(1, blogs[1].Id);
                }
            }
        }
        public void Insert_and_update_with_computed_column()
        {
            using (var testStore = MySqlTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextComputedColumn(testStore.Name))
                {
                    context.Database.EnsureCreatedResiliently();

                    var blog = context.Add(
                        new FullNameBlog
                    {
                        FirstName = "One",
                        LastName  = "Unicorn"
                    }).Entity;

                    context.SaveChanges();

                    Assert.Equal("One Unicorn", blog.FullName);
                }

                using (var context = new BlogContextComputedColumn(testStore.Name))
                {
                    var blog = context.FullNameBlogs.Single();

                    Assert.Equal("One Unicorn", blog.FullName);

                    blog.LastName = "Pegasus";

                    context.SaveChanges();

                    Assert.Equal("One Pegasus", blog.FullName);
                }
            }
        }
        private static async Task Returns_true_when_database_exists_test(bool async, bool ambientTransaction, bool useCanConnect, bool file)
        {
            using (var testDatabase = file
                ? MySqlTestStore.CreateInitialized("ExistingBloggingFile", useFileName: true)
                : MySqlTestStore.GetOrCreateInitialized("ExistingBlogging"))
            {
                using (var context = new BloggingContext(testDatabase))
                {
                    var creator = GetDatabaseCreator(context);
                    using (CreateTransactionScope(ambientTransaction))
                    {
                        if (useCanConnect)
                        {
                            Assert.True(async ? await creator.CanConnectAsync() : creator.CanConnect());
                        }
                        else
                        {
                            Assert.True(async ? await creator.ExistsAsync() : creator.Exists());
                        }
                    }

                    Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);
                }
            }
        }
        public void Insert_with_default_string_value_from_sequence()
        {
            using (var testStore = MySqlTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextStringDefaultValue(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    context.AddRange(new BlogWithStringKey {
                        Name = "One Unicorn"
                    }, new BlogWithStringKey {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

                using (var context = new BlogContextStringDefaultValue(testStore.Name))
                {
                    var blogs = context.StringyBlogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal("i77", blogs[0].Id);
                    Assert.Equal("i78", blogs[1].Id);
                }
            }
        }
        public static async Task Deletes_database(bool async, bool ambientTransaction)
        {
            using (var testDatabase = MySqlTestStore.CreateInitialized("DeleteBlogging"))
            {
                testDatabase.CloseConnection();

                var creator = GetDatabaseCreator(testDatabase);

                Assert.True(async ? await creator.ExistsAsync() : creator.Exists());

                using (CreateTransactionScope(ambientTransaction))
                {
                    if (async)
                    {
                        await creator.DeleteAsync();
                    }
                    else
                    {
                        creator.Delete();
                    }
                }

                Assert.False(async ? await creator.ExistsAsync() : creator.Exists());
            }
        }
        private static async Task Delete_database_test(bool async, bool open, bool ambientTransaction, bool file)
        {
            using (var testDatabase = MySqlTestStore.CreateInitialized("EnsureDeleteBlogging" + (file ? "File" : ""), file))
            {
                if (!open)
                {
                    testDatabase.CloseConnection();
                }

                using (var context = new BloggingContext(testDatabase))
                {
                    var creator = GetDatabaseCreator(context);

                    Assert.True(async ? await creator.ExistsAsync() : creator.Exists());

                    using (CreateTransactionScope(ambientTransaction))
                    {
                        if (async)
                        {
                            Assert.True(await context.Database.EnsureDeletedAsync());
                        }
                        else
                        {
                            Assert.True(context.Database.EnsureDeleted());
                        }
                    }

                    Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);

                    Assert.False(async ? await creator.ExistsAsync() : creator.Exists());

                    Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);
                }
            }
        }
        public void Insert_with_sequence_HiLo()
        {
            using (var testStore = MySqlTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextHiLo(testStore.Name))
                {
                    context.Database.EnsureCreatedResiliently();

                    context.AddRange(
                        new Blog
                    {
                        Name = "One Unicorn"
                    }, new Blog
                    {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

                using (var context = new BlogContextHiLo(testStore.Name))
                {
                    var blogs = context.Blogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal(1, blogs[0].Id);
                    Assert.Equal(2, blogs[0].OtherId);
                    Assert.Equal(3, blogs[1].Id);
                    Assert.Equal(4, blogs[1].OtherId);
                }
            }
        }
        public void Insert_with_key_default_value_from_sequence()
        {
            using (var testStore = MySqlTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextKeyColumnWithDefaultValue(testStore.Name))
                {
                    context.Database.EnsureCreatedResiliently();

                    context.AddRange(
                        new Blog
                    {
                        Name = "One Unicorn"
                    }, new Blog
                    {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();
                }

                using (var context = new BlogContextKeyColumnWithDefaultValue(testStore.Name))
                {
                    var blogs = context.Blogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal(77, blogs[0].Id);
                    Assert.Equal(78, blogs[1].Id);
                }
            }
        }
        public void Insert_with_non_key_default_value()
        {
            using (var testStore = MySqlTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextNonKeyDefaultValue(testStore.Name))
                {
                    context.Database.EnsureCreatedResiliently();

                    var blogs = new List <Blog>
                    {
                        new Blog
                        {
                            Name = "One Unicorn"
                        },
                        new Blog
                        {
                            Name      = "Two Unicorns",
                            CreatedOn = new DateTime(1969, 8, 3, 0, 10, 0)
                        }
                    };

                    context.AddRange(blogs);

                    context.SaveChanges();

                    Assert.NotEqual(new DateTime(), blogs[0].CreatedOn);
                    Assert.NotEqual(new DateTime(), blogs[1].CreatedOn);
                    Assert.Null(blogs[0].OtherId);
                    Assert.Null(blogs[1].OtherId);
                }

                using (var context = new BlogContextNonKeyDefaultValue(testStore.Name))
                {
                    var blogs = context.Blogs.OrderBy(e => e.Name).ToList();

                    Assert.NotEqual(new DateTime(), blogs[0].CreatedOn);
                    Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn);

                    blogs[0].CreatedOn = new DateTime(1973, 9, 3, 0, 10, 0);
                    blogs[1].Name      = "Zwo Unicorns";

                    context.SaveChanges();
                }

                using (var context = new BlogContextNonKeyDefaultValue(testStore.Name))
                {
                    var blogs = context.Blogs.OrderBy(e => e.Name).ToList();

                    Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn);
                    Assert.Equal(new DateTime(1973, 9, 3, 0, 10, 0), blogs[0].CreatedOn);
                }
            }
        }
        public void Insert_with_non_key_default_value_readonly()
        {
            using (var testStore = MySqlTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextNonKeyReadOnlyDefaultValue(testStore.Name))
                {
                    context.Database.EnsureCreatedResiliently();

                    context.AddRange(
                        new Blog
                    {
                        Name = "One Unicorn"
                    },
                        new Blog
                    {
                        Name = "Two Unicorns"
                    });

                    context.SaveChanges();

                    Assert.NotEqual(new DateTime(), context.Blogs.ToList()[0].CreatedOn);
                }

                DateTime dateTime0;

                using (var context = new BlogContextNonKeyReadOnlyDefaultValue(testStore.Name))
                {
                    var blogs = context.Blogs.OrderBy(e => e.Id).ToList();

                    dateTime0 = blogs[0].CreatedOn;

                    Assert.NotEqual(new DateTime(), dateTime0);
                    Assert.NotEqual(new DateTime(), blogs[1].CreatedOn);

                    blogs[0].Name      = "One Pegasus";
                    blogs[1].CreatedOn = new DateTime(1973, 9, 3, 0, 10, 0);

                    context.SaveChanges();
                }

                using (var context = new BlogContextNonKeyReadOnlyDefaultValue(testStore.Name))
                {
                    var blogs = context.Blogs.OrderBy(e => e.Id).ToList();

                    Assert.Equal(dateTime0, blogs[0].CreatedOn);
                    Assert.Equal(new DateTime(1973, 9, 3, 0, 10, 0), blogs[1].CreatedOn);
                }
            }
        }
        public void Insert_with_client_generated_GUID_key()
        {
            using (var testStore = MySqlTestStore.CreateInitialized(DatabaseName))
            {
                Guid afterSave;
                using (var context = new BlogContextClientGuidKey(testStore.Name))
                {
                    context.Database.EnsureCreated();

                    var blog = context.Add(new GuidBlog {
                        Name = "One Unicorn"
                    }).Entity;

                    var beforeSave      = blog.Id;
                    var beforeSaveNotId = blog.NotId;

                    Assert.NotEqual(default, beforeSave);
        public void Insert_and_update_with_computed_column_with_function()
        {
            using (var testStore = MySqlTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextComputedColumnWithFunction(testStore.Name))
                {
                    context.Database.ExecuteSqlRaw
                    (
                        @"CREATE FUNCTION
[dbo].[GetFullName](@First NVARCHAR(MAX), @Second NVARCHAR(MAX))
RETURNS NVARCHAR(MAX) WITH SCHEMABINDING AS BEGIN RETURN @First + @Second END");

                    context.GetService <IRelationalDatabaseCreator>().CreateTables();
                }

                using (var context = new BlogContextComputedColumnWithFunction(testStore.Name))
                {
                    var blog = context.Add(
                        new FullNameBlog
                    {
                        FirstName = "One",
                        LastName  = "Unicorn"
                    }).Entity;

                    context.SaveChanges();

                    Assert.Equal("OneUnicorn", blog.FullName);
                }

                using (var context = new BlogContextComputedColumnWithFunction(testStore.Name))
                {
                    var blog = context.FullNameBlogs.Single();

                    Assert.Equal("OneUnicorn", blog.FullName);

                    blog.LastName = "Pegasus";

                    context.SaveChanges();

                    Assert.Equal("OnePegasus", blog.FullName);
                }
            }
        }
        public void Insert_and_update_with_computed_column_with_querying_function()
        {
            using (var testStore = MySqlTestStore.CreateInitialized(DatabaseName))
            {
                using (var context = new BlogContextComputedColumn(testStore.Name))
                {
                    context.GetService <IRelationalDatabaseCreator>().CreateTables();

                    context.Database.ExecuteSqlRaw("ALTER TABLE dbo.FullNameBlogs DROP COLUMN FullName;");

                    context.Database.ExecuteSqlRaw(
                        @"CREATE FUNCTION [dbo].[GetFullName](@Id int)
RETURNS NVARCHAR(MAX) WITH SCHEMABINDING AS
BEGIN
    DECLARE @FullName NVARCHAR(MAX);
    SELECT @FullName = [FirstName] + [LastName] FROM [dbo].[FullNameBlogs] WHERE [Id] = @Id;
    RETURN @FullName
END");

                    context.Database.ExecuteSqlRaw("ALTER TABLE dbo.FullNameBlogs ADD FullName AS [dbo].[GetFullName]([Id]); ");
                }

                try
                {
                    using (var context = new BlogContextComputedColumn(testStore.Name))
                    {
                        var blog = context.Add(
                            new FullNameBlog
                        {
                            FirstName = "One",
                            LastName  = "Unicorn"
                        }).Entity;

                        context.SaveChanges();

                        Assert.Equal("OneUnicorn", blog.FullName);
                    }

                    using (var context = new BlogContextComputedColumn(testStore.Name))
                    {
                        var blog = context.FullNameBlogs.Single();

                        Assert.Equal("OneUnicorn", blog.FullName);

                        blog.LastName = "Pegasus";

                        context.SaveChanges();

                        Assert.Equal("OnePegasus", blog.FullName);
                    }

                    using (var context = new BlogContextComputedColumn(testStore.Name))
                    {
                        var blog1 = context.Add(
                            new FullNameBlog
                        {
                            FirstName = "Hank",
                            LastName  = "Unicorn"
                        }).Entity;
                        var blog2 = context.Add(
                            new FullNameBlog
                        {
                            FirstName = "Jeff",
                            LastName  = "Unicorn"
                        }).Entity;

                        context.SaveChanges();

                        Assert.Equal("HankUnicorn", blog1.FullName);
                        Assert.Equal("JeffUnicorn", blog2.FullName);
                    }
                }
                finally
                {
                    using (var context = new BlogContextComputedColumn(testStore.Name))
                    {
                        context.Database.ExecuteSqlRaw("ALTER TABLE dbo.FullNameBlogs DROP COLUMN FullName;");
                        context.Database.ExecuteSqlRaw("DROP FUNCTION [dbo].[GetFullName];");
                    }
                }
            }
        }
 public ComputedColumnTest()
 {
     TestStore = MySqlTestStore.CreateInitialized("ComputedColumnTest");
 }
Esempio n. 16
0
 public TestBase()
 {
     TestStore = MySqlTestStore.CreateInitialized(StoreName);
 }
 public SequenceEndToEndTest()
 {
     TestStore = MySqlTestStore.CreateInitialized("SequenceEndToEndTest");
 }
Esempio n. 18
0
 public DefaultValuesTest()
 {
     TestStore = MySqlTestStore.CreateInitialized("DefaultValuesTest");
 }
Esempio n. 19
0
 public CompositeKeyEndToEndTest()
 {
     TestStore = MySqlTestStore.CreateInitialized("CompositeKeyEndToEndTest");
 }