Exemplo n.º 1
0
 public void InitializeAInitializedDatabaseShouldDoNothingTest()
 {
     _objectProvider.Register <Entity1>();
     _objectProvider.Register <Entity2>();
     _objectProvider.InitializeDatabase();
     _commandCalls = 0;
     _objectProvider.InitializeDatabase();
     Assert.Equal(0, _commandCalls);
 }
Exemplo n.º 2
0
        public TestServerFixture()
        {
            {
                string directory = Path.GetDirectoryName(typeof(TestServerFixture).GetTypeInfo().Assembly.Location);
                int    i         = 0;
                _databaseFileName = $"Database{i}.db";
                while (File.Exists(Path.Combine(directory, _databaseFileName)))
                {
                    try
                    {
                        File.Delete(Path.Combine(directory, _databaseFileName));
                        break;
                    }
                    catch
                    {
                        _databaseFileName = $"Database{++i}.db";
                    }
                }
            }


            _server = new TestServer(new WebHostBuilder()
                                     .ConfigureServices(services =>
            {
                services.AddObjectStoreWithSqlite($"Data Source={_databaseFileName}");
                services.AddIdentity <E.User, E.Role>()
                .AddObjectStoreUserStores <E.User, E.Role, E.UserInRole>();
            })
                                     .Configure(app =>
            {
                app.UseIdentity().Use(d => c => _function == null ? d(c) : _function(c));
                RelationalObjectStore objectStore = app.ApplicationServices.GetService(typeof(IObjectProvider)) as RelationalObjectStore;
                objectStore.Register <E.User>().Register <E.Role>().Register <E.UserInRole>();

                objectStore.InitializeDatabase(databaseInitializer => {
                    databaseInitializer.RegisterTableStatement(x => x.Tablename == "`dbo.Roles`", (x, s) => s + ";INSERT INTO `dbo.Roles` (`Name`, `NormalizedRolename`) VALUES ('Admin', 'ADMIN'), ('Test', 'TEST')");
                    databaseInitializer.RegisterTableStatement(x => x.Tablename == "`dbo.Users`", (x, s) => s + ";INSERT INTO `dbo.Users` (`Name`, `Password`, `NormalizedUsername`) VALUES ('Admin', 'AQAAAAEAACcQAAAAEH7ZcGTOm+i5+wDjYcKunrChCybl3/XfsGoRnchwXssH9swyQYCLETt+H39cXjacaA==', 'ADMIN'), ('User1', 'AQAAAAEAACcQAAAAEH3QfNo5cHGK4myXlU1XHm6I1t+e02CPXhdAqlBiE4h+7VcJdwkI0u2A3uNC0TgGkQ==', 'USER1')");
                    databaseInitializer.RegisterTableStatement(x => x.Tablename == "`dbo.UsersInRole`", (x, s) => "CREATE TABLE `dbo.UsersInRole` ( `User` INTEGER NOT NULL, `Role` INTEGER NOT NULL, PRIMARY KEY(User,Role), FOREIGN KEY(`User`) REFERENCES `dbo.Users`(`Id`), FOREIGN KEY(`Role`) REFERENCES `dbo.Roles`(`Id`) );INSERT INTO `dbo.UsersInRole` (`User`, `Role`) VALUES (1, 1)");
                });
            }));
            _client = _server.CreateClient();
        }
Exemplo n.º 3
0
        public SqliteDatabaseFixture()
        {
            _isInitialized = false;

            string connectionString;
            {
                string directory        = Path.GetDirectoryName(typeof(SqliteDatabaseFixture).GetTypeInfo().Assembly.Location);
                int    i                = 0;
                string databaseFileName = $"Database{i}.db";
                while (File.Exists(Path.Combine(directory, databaseFileName)))
                {
                    try
                    {
                        File.Delete(Path.Combine(directory, databaseFileName));
                        break;
                    }
                    catch
                    {
                        databaseFileName = $"Database{++i}.db";
                    }
                }

                connectionString = $"Data Source={databaseFileName}";
            }


            RelationalObjectStore relationalObjectProvider = new RelationalObjectStore(connectionString, DataBaseProvider.Instance, new MappingOptionsSet().AddDefaultRules(), true);

            ObjectStoreManager.DefaultObjectStore.RegisterObjectProvider(ObjectProvider = relationalObjectProvider);
            relationalObjectProvider.Register <Entities.DifferentTypes>();
            relationalObjectProvider.Register <Entities.DifferentWritabilityLevels>();
            relationalObjectProvider.Register <Entities.ForeignObjectKey>();
            relationalObjectProvider.Register <Entities.ForeignObjectKeyChain>();
            relationalObjectProvider.Register <Entities.NonInitializedKey>();
            relationalObjectProvider.Register <Entities.SubTest>();
            relationalObjectProvider.Register <Entities.Test>();

            relationalObjectProvider.InitializeDatabase(databaseInitializer => {
                databaseInitializer.RegisterFieldStatment(x => x.Statement is IAddTableStatement && (x.Fieldname == "`Updateable`" || x.Fieldname == "`Readonly`") && x.Statement.Tablename == "`dbo.DifferentWritabilityLevels`", (x, s) => s + " DEFAULT 1");
                databaseInitializer.RegisterTableStatement(x => x is IAddTableStatement && x.Tablename == "`dbo.TestTable`", (x, s) => s + ";INSERT INTO `dbo.TestTable` (`Name`, `Description`) VALUES ('Testname', 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.'), ('Testname 2', 'Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.')");
                databaseInitializer.RegisterTableStatement(x => x is IAddTableStatement && x.Tablename == "`dbo.SubTestTable`", (x, s) => s + ";INSERT INTO `dbo.SubTestTable` (`Test`, `Name`, `First`, `Second`, `Nullable`) VALUES (1,'SubEntity0',0,10,NULL),(1,'SubEntity2',1,9,NULL),(1,'SubEntity4',2,8,NULL),(1,'SubEntity6',3,7,NULL),(1,'SubEntity8',4,6,NULL),(1,'SubEntity10',5,5,NULL),(1,'SubEntity12',6,4,NULL),(1,'SubEntity14',7,3,'2016-06-23 17:31:49'),(1,'SubEntity16',8,2,NULL),(1,'SubEntity18',9,1,NULL),(2,'SubEntity1',0,10,NULL),(2,'SubEntity3',1,9,NULL),(2,'SubEntity5',2,8,NULL),(2,'SubEntity7',3,7,NULL),(2,'SubEntity9',4,6,NULL),(2,'SubEntity11',5,5,NULL),(2,'SubEntity13',6,4,NULL),(2,'SubEntity15',7,3,'2016-06-23 17:31:49'),(2,'SubEntity17',8,2,NULL),(2,'SubEntity19',9,1,NULL)");
                databaseInitializer.RegisterTableStatement(x => x is IAddTableStatement && x.Tablename == "`dbo.ForeignObjectKeyTable`", (x, s) => s + ";INSERT INTO `dbo.ForeignObjectKeyTable` (`Id`, `Value`) VALUES (1, 'Testentry')");
                databaseInitializer.RegisterTableStatement(x => x is IAddTableStatement && x.Tablename == "`dbo.DifferentWritabilityLevels`", (x, s) =>
                {
                    s  = s.Replace("`dbo.DifferentWritabilityLevels`", "`dbo.DifferentWritabilityLevelsTable`");
                    s += ";CREATE VIEW `dbo.DifferentWritabilityLevels` AS SELECT Id, Writeable, Updateable, Insertable, Readonly FROM`dbo.DifferentWritabilityLevelsTable`";
                    s += ";CREATE TRIGGER `dbo.II_DifferentWritabilityLevels` INSTEAD OF INSERT ON `dbo.DifferentWritabilityLevels` FOR EACH ROW BEGIN INSERT INTO `dbo.DifferentWritabilityLevelsTable` (Writeable, Insertable) VALUES(NEW.Writeable, NEW.Insertable); UPDATE sqlite_sequence SET seq = last_insert_rowid() WHERE name = \"dbo.DifferentWritabilityLevels\"; END";
                    s += ";CREATE TRIGGER `dbo.IU_DifferentWritabilityLevels` INSTEAD OF UPDATE ON `dbo.DifferentWritabilityLevels` FOR EACH ROW BEGIN UPDATE `dbo.DifferentWritabilityLevelsTable` SET Writeable = NEW.Writeable, Updateable = NEW.Updateable, Readonly = Readonly + 1 WHERE Id = OLD.Id; END";
                    s += ";INSERT INTO sqlite_sequence (name, seq) VALUES ('dbo.DifferentWritabilityLevels', 1)";
                    return(s + ";INSERT INTO `dbo.DifferentWritabilityLevelsTable` (`Id`, `Writeable`, `Updateable`, `Insertable`, `Readonly`) VALUES (1, 5, 1, 10, 1)");
                });
            });

            Func <DbCommand> getCommand = () => new Command(GetReader);

            typeof(DataBaseProvider).GetTypeInfo().GetField("_getCommand", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Static)
            .SetValue(null, getCommand);

            _resultManager = new ResultManager <Query>();

            _connection = (SqliteConnection)DataBaseProvider.Instance.GetConnection(connectionString);
        }