Example #1
0
        public void HandleTenantNotSetWhenDeleting()
        {
            try
            {
                _connection.Open();
                var tenant1 = new TenantInfo
                {
                    Id               = "abc",
                    Identifier       = "abc",
                    Name             = "abc",
                    ConnectionString = "DataSource=testdb.db"
                };

                // TenantNotSetMode.Throw
                using (var db = new TestBlogDbContext(tenant1, _options))
                {
                    db.Database.EnsureDeleted();
                    db.Database.EnsureCreated();

                    var blog1 = new Blog {
                        Title = "abc"
                    };
                    db.Blogs.Add(blog1);
                    db.SaveChanges();

                    db.TenantNotSetMode = TenantNotSetMode.Throw;
                    db.Entry(blog1).Property("TenantId").CurrentValue = null;
                    db.Blogs.Remove(blog1);

                    var e = Assert.Throws <MultiTenantException>(() => db.SaveChanges());
                }

                // TenantNotSetMode.Overwrite
                using (var db = new TestBlogDbContext(tenant1, _options))
                {
                    db.Database.EnsureDeleted();
                    db.Database.EnsureCreated();

                    var blog1 = new Blog {
                        Title = "abc"
                    };
                    db.Blogs.Add(blog1);
                    db.SaveChanges();

                    db.TenantNotSetMode = TenantNotSetMode.Overwrite;
                    db.Entry(blog1).Property("TenantId").CurrentValue = null;
                    db.Blogs.Remove(blog1);

                    Assert.Equal(1, db.SaveChanges());
                }
            }
            finally
            {
                _connection.Close();
            }
        }
Example #2
0
        public void HandleTenantNotSetWhenAdding()
        {
            try
            {
                _connection.Open();
                var tenant1 = new TenantInfo
                {
                    Id               = "abc",
                    Identifier       = "abc",
                    Name             = "abc",
                    ConnectionString = "DataSource=testdb.db"
                };

                // TenantNotSetMode.Throw, should act as Overwrite when adding
                using (var db = new TestBlogDbContext(tenant1, _options))
                {
                    db.Database.EnsureDeleted();
                    db.Database.EnsureCreated();
                    db.TenantNotSetMode = TenantNotSetMode.Throw;

                    var blog1 = new Blog {
                        Title = "abc"
                    };
                    db.Blogs.Add(blog1);
                    db.SaveChanges();
                    Assert.Equal(tenant1.Identifier, db.Entry <Blog>(blog1).Property("TenantId").CurrentValue);
                }

                // TenantNotSetMode.Overwrite
                using (var db = new TestBlogDbContext(tenant1, _options))
                {
                    db.Database.EnsureDeleted();
                    db.Database.EnsureCreated();
                    db.TenantNotSetMode = TenantNotSetMode.Overwrite;

                    var blog1 = new Blog {
                        Title = "abc2"
                    };
                    db.Blogs.Add(blog1);
                    db.SaveChanges();
                    Assert.Equal(tenant1.Id, db.Entry <Blog>(blog1).Property("TenantId").CurrentValue);
                }
            }
            finally
            {
                _connection.Close();
            }
        }
Example #3
0
        public void HandleTenantMismatchWhenAdding()
        {
            try
            {
                _connection.Open();
                var tenant1 = new TenantInfo("abc", "abc", "abc",
                                             "DataSource=testdb.db", null);

                // TenantMismatchMode.Throw
                using (var db = new TestBlogDbContext(tenant1, _options))
                {
                    db.Database.EnsureDeleted();
                    db.Database.EnsureCreated();
                    db.TenantMismatchMode = TenantMismatchMode.Throw;

                    var blog1 = new Blog {
                        Title = "abc"
                    };
                    db.Blogs.Add(blog1);
                    db.Entry(blog1).Property("TenantId").CurrentValue = "77";

                    var e = Assert.Throws <MultiTenantException>(() => db.SaveChanges());
                }

                // TenantMismatchMode.Ignore
                using (var db = new TestBlogDbContext(tenant1, _options))
                {
                    db.Database.EnsureDeleted();
                    db.Database.EnsureCreated();
                    db.TenantMismatchMode = TenantMismatchMode.Ignore;

                    var blog1 = new Blog {
                        Title = "34"
                    };
                    db.Blogs.Add(blog1);
                    db.Entry(blog1).Property("TenantId").CurrentValue = "34";
                    db.SaveChanges();
                    Assert.Equal("34", db.Entry(blog1).Property("TenantId").CurrentValue);
                }

                // TenantMismatchMode.Overwrite
                using (var db = new TestBlogDbContext(tenant1, _options))
                {
                    db.Database.EnsureDeleted();
                    db.Database.EnsureCreated();
                    db.TenantMismatchMode = TenantMismatchMode.Overwrite;

                    var blog1 = new Blog {
                        Title = "77"
                    };
                    db.Blogs.Add(blog1);
                    db.Entry(blog1).Property("TenantId").CurrentValue = "77";
                    db.SaveChanges();
                    Assert.Equal(tenant1.Id, db.Entry(blog1).Property("TenantId").CurrentValue);
                }
            }
            finally
            {
                _connection.Close();
            }
        }
Example #4
0
        public void SetGlobalFilterQuery()
        {
            // Doesn't appear to be a way to test this except to try it out...

            var connection = new SqliteConnection("DataSource=:memory:");

            try
            {
                connection.Open();
                var options = new DbContextOptionsBuilder()
                              .UseSqlite(connection)
                              .Options;
                var tenant1 = new TenantInfo
                {
                    Id               = "abc",
                    Identifier       = "abc",
                    Name             = "abc",
                    ConnectionString = "DataSource=testdb.db"
                };

                using (var db = new TestBlogDbContext(tenant1, options))
                {
                    db.Database.EnsureDeleted();
                    db.Database.EnsureCreated();

                    var blog1 = new Blog {
                        Title = "abc"
                    };
                    db.Blogs.Add(blog1);
                    var post1 = new Post {
                        Title = "post in abc", Blog = blog1
                    };
                    db.Posts.Add(post1);
                    db.SaveChanges();
                }

                var tenant2 = new TenantInfo
                {
                    Id               = "123",
                    Identifier       = "123",
                    Name             = "123",
                    ConnectionString = "DataSource=testdb.db"
                };
                using (var db = new TestBlogDbContext(tenant2, options))
                {
                    var blog1 = new Blog {
                        Title = "123"
                    };
                    db.Blogs.Add(blog1);
                    var post1 = new Post {
                        Title = "post in 123", Blog = blog1
                    };
                    db.Posts.Add(post1);
                    db.SaveChanges();
                }

                int postCount1 = 0;
                int postCount2 = 0;
                using (var db = new TestBlogDbContext(tenant1, options))
                {
                    postCount1 = db.Posts.Count();
                    postCount2 = db.Posts.IgnoreQueryFilters().Count();
                }

                Assert.Equal(1, postCount1);
                Assert.Equal(2, postCount2);
            }
            finally
            {
                connection.Close();
            }
        }