Пример #1
0
        public async Task Initialize()
        {
            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
            var schema = new DBSchema("common_database");

            schema.Generate(new[] {
                typeof(Customer).Assembly,
                typeof(User).Assembly
            });
            DBService.Schems.Add(schema);
            Assert.IsNotNull(schema);
            Assert.IsNotNull(Book.DBTable);
            Assert.IsNotNull(UserGroup.DBTable);
            Assert.IsNotNull(GroupPermission.DBTable);
            Assert.IsNotNull(User.DBTable);
            Assert.IsNotNull(Position.DBTable);
            Assert.IsNotNull(UserReg.DBTable);
            Assert.IsNotNull(Company.DBTable);
            DBService.Save();
            schema.Connection = new DBConnection
            {
                Name     = "test.common",
                System   = DBSystem.SQLite,
                DataBase = "test.common"
            };
            schema.DropDatabase();
            schema.CreateDatabase();

            var group = new UserGroup()
            {
                Number = "GP1",
                Name   = "Group1"
            };
            await group.Save();

            var position = new Position()
            {
                Code = "PS1",
                Name = "Position"
            };

            var user = new User()
            {
                Login    = "******",
                Name     = "Test User",
                Password = "******",
                Position = position,
                AuthType = UserAuthType.Internal,
                Access   = new AccessValue(new[] { new AccessItem(group, AccessType.Create) })
            };
            await user.Save();

            await User.StartSession("test", "UserCommon1!");

            await GroupPermission.CachePermission();
        }
Пример #2
0
        public override Task CreateNew()
        {
            Schema = new DBSchema()
            {
                Name       = schemaName,
                Connection = new DBConnection
                {
                    Name     = schemaName,
                    System   = DBSystem.SQLite,
                    DataBase = $"{schemaName}.sqlite"
                }
            };

            Generate();
            Schema.DropDatabase();
            Schema.CreateDatabase();
            Save();
            return(Task.CompletedTask);
        }
Пример #3
0
        public async void Generate(DBConnection connection)
        {
            Assert.AreEqual(true, connection.CheckConnection(), $"Connection Fail!");
            schema = DBSchema.Generate(GetType().Assembly, SchemaName);

            Assert.IsNotNull(schema, "Attribute Generator Fail. On Schema");
            Assert.IsNotNull(Employer.DBTable, "Attribute Generator Fail. On Employer Table");
            Assert.IsNotNull(Position.DBTable, "Attribute Generator Fail. On Position Table");

            var idColumn = Employer.DBTable.Columns["id"];

            Assert.IsNotNull(idColumn, "Attribute Generator Fail. On Column Employer Id");
            var positionColumn = Employer.DBTable.Columns["positionid"];

            Assert.IsNotNull(positionColumn, "Attribute Generator Fail. On Column Employer Position");
            Assert.IsNotNull(positionColumn.ReferenceTable, "Attribute Generator Fail. On Column Employer Position Reference");
            schema.Connection = connection;

            schema.DropDatabase();
            schema.CreateDatabase();

            var result = schema.GetTablesInfo(connection.Schema, EmployerTableName);

            Assert.IsTrue(result.Count() == 1, "Generate Sql Table / Get Information Fail.");
            result = schema.GetTablesInfo(connection.Schema, PositionTableName);
            Assert.IsTrue(result.Count() == 1, "Generate Sql Table / Get Information Fail.");
            //Insert
            var employer = new Employer()
            {
                Identifier = $"{1:8}",
                Lodar      = true,
                Age        = 40,
                Height     = 180,
                LongId     = 120321312321L,
                Weight     = 123.12333F,
                DWeight    = 123.1233433424434D,
                Salary     = 231323.32M,
                Name       = "Ivan",
                Access     = new AccessValue(new[]
                {
                    new AccessItem(AccessValue.Groups.First(i => i.Id == 1), AccessType.Read),
                    new AccessItem(AccessValue.Groups.First(i => i.Id == 2), AccessType.Admin),
                    new AccessItem(AccessValue.Groups.First(i => i.Id == 3), AccessType.Create)
                })
            };

            Assert.AreEqual(employer.Type, EmployerType.Type2, "Default Value & Enum");

            employer.GenerateId();
            Assert.NotNull(employer.Id, "Id Generator Fail");

            await employer.Save();

            var qresult = schema.Connection.ExecuteQResult($"select * from {EmployerTableName}");

            Assert.AreEqual(1, qresult.Values.Count, "Insert sql Fail");
            Assert.AreEqual(employer.Id, qresult.Get(0, "id"), "Insert sql Fail Int");
            Assert.AreEqual(employer.Identifier, qresult.Get(0, "identifier"), "Insert sql Fail String");
            Assert.AreEqual((int?)employer.Type, qresult.Get(0, "typeid"), "Insert sql Fail Enum");
            Assert.AreEqual(employer.Age, qresult.Get(0, "age"), "Insert sql Fail Byte");
            Assert.AreEqual(employer.Height, qresult.Get(0, "height"), "Insert sql Fail Short");
            Assert.AreEqual(employer.LongId, qresult.Get(0, "longid"), "Insert sql Fail Long");
            Assert.AreEqual(employer.Weight, qresult.Get(0, "weight"), "Insert sql Fail Float");
            Assert.AreEqual(employer.DWeight, qresult.Get(0, "dweight"), "Insert sql Fail Double");
            Assert.AreEqual(employer.Salary, qresult.Get(0, "salary"), "Insert sql Fail Decimal");
            var lodar = qresult.Get(0, "lodar").ToString();

            Assert.IsTrue(lodar == "1" || lodar == "True", "Insert sql Fail Bool");
            Assert.IsInstanceOf <byte[]>(qresult.Get(0, "group_access"), "Insert sql Fail Byte Array");

            var accessValue = new AccessValue((byte[])qresult.Get(0, "group_access"));

            Assert.AreEqual(3, accessValue.Items.Count(), "Insert sql Fail Byte Array");
            Assert.AreEqual(true, accessValue.Items.ElementAt(0).Read, "Insert sql Fail Byte Array");
            Assert.AreEqual(true, accessValue.Items.ElementAt(1).Admin, "Insert sql Fail Byte Array");
            Assert.AreEqual(false, accessValue.Items.ElementAt(2).Delete, "Insert sql Fail Byte Array");

            Employer.DBTable.Clear();
            Assert.AreEqual(0, Employer.DBTable.Count, "Clear table Fail");

            //Insert Several
            Position.DBTable.Add(new Position()
            {
                Code = "1", Name = "First Position"
            });
            Position.DBTable.Add(new Position()
            {
                Code = "2", Name = "Second Position"
            });
            var position = new Position()
            {
                Id = 0, Code = "3", Name = "Group Position"
            };

            position.Attach();
            var sposition = new Position()
            {
                Code = "4", Parent = position, Name = "Sub Group Position"
            };

            sposition.Attach();

            //Select from internal Index
            Position.DBTable.Add(new Position()
            {
                Code = "t1", Name = "Null Index"
            });
            Position.DBTable.Add(new Position()
            {
                Code = "t2", Name = "Null Index"
            });
            Position.DBTable.Add(new Position()
            {
                Code = "t3", Name = "Null Index"
            });
            var nullIds = Position.DBTable.Select(Position.DBTable.PrimaryKey, CompareType.Is, null).ToList();

            Assert.AreEqual(6, nullIds.Count, "Select by null Fail");

            await Position.DBTable.Save();

            Position.DBTable.Clear();
            var positions = Position.DBTable.Load();

            Assert.AreEqual(7, positions.Count(), "Insert/Read several Fail");

            //GetById
            employer = Employer.DBTable.LoadById(1);
            Assert.IsNotNull(employer, "GetById Fail");
            position = Position.DBTable.LoadById(4);
            Assert.IsNotNull(position, "GetById Fail");
            //Update
            employer.Position = position;
            await employer.Save();

            qresult = schema.Connection.ExecuteQResult($"select * from {EmployerTableName}");
            Assert.AreEqual(4, qresult.Get(0, "positionid"), "Update sql Fail");


            connection.ExecuteQuery(@"create table test_table(
      id int primary key, 
      test_date date, 
      test_varchar varchar(512),
      test_numeric numeric(20,10))");

            result = schema.GetTablesInfo(connection.Schema, "test_table");
            schema.GenerateTablesInfo(result);
            var table = schema.Tables["test_table"] as DBTable <DBItem>;

            Assert.IsNotNull(table, "DBInformation Load Fail");

            table.Load().LastOrDefault();
            for (int i = 0; i < 1000; i++)
            {
                var row = table.NewItem();
                row["id"]           = i;
                row["test_date"]    = DateTime.Now.AddDays(-i);
                row["test_varchar"] = "string value " + i;
                row["test_numeric"] = i / 1000M;
                table.Add(row);
            }
            await table.Save();

            table.Clear();

            int c = 0;

            foreach (var item in table.Load(string.Empty))
            {
                if (++c == 5)
                {
                    break;
                }
            }
        }
Пример #4
0
        public async Task Initialize()
        {
            Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
            var schema = new DBSchema("wf_customer");

            schema.Generate(new[] { typeof(User).Assembly, typeof(Customer).Assembly });
            Assert.IsNotNull(Location.DBTable);
            Assert.IsNotNull(Address.DBTable);
            Assert.IsNotNull(Customer.DBTable);
            Assert.IsNotNull(CustomerAddress.DBTable);
            Assert.IsNotNull(PersoneIdentify.DBTable);
            Assert.IsNotNull(CustomerReference.DBTable);
            DBService.Save();
            schema.Connection = new DBConnection
            {
                Name     = "test.common",
                DataBase = "test.common",
                System   = DBSystem.SQLite
            };
            schema.DropDatabase();
            schema.CreateDatabase();

            Location.DBTable.Add(new Continent {
                Code = "AF", Name = "Africa"
            });
            Location.DBTable.Add(new Continent {
                Code = "AN", Name = "Antarctica"
            });
            Location.DBTable.Add(new Continent {
                Code = "AS", Name = "Asia"
            });
            Location.DBTable.Add(new Continent {
                Code = "EU", Name = "Europa"
            });
            Location.DBTable.Add(new Continent {
                Code = "EUAS", Name = "Eurasia"
            });
            Location.DBTable.Add(new Continent {
                Code = "NA", Name = "North america"
            });
            Location.DBTable.Add(new Continent {
                Code = "OC", Name = "Oceania"
            });
            Location.DBTable.Add(new Continent {
                Code = "SA", Name = "South america"
            });

            var russia = new Country
            {
                Parent = Location.DBTable.LoadByCode("EUAS"),
                Code   = "RU",
                Name   = "Russia"
            };

            Location.DBTable.Add(russia);
            var ruble = new Currency
            {
                Parent = russia,
                Code   = "RUB",
                Name   = "Ruble"
            };

            Location.DBTable.Add(ruble);

            Assert.AreEqual(1, DBTable.GetTable <Country>().Count);
            Assert.AreEqual(1, DBTable.GetTable <Currency>().Count);

            await Location.DBTable.Save();

            Assert.AreEqual(1, DBTable.GetTable <Country>().Count);
            Assert.AreEqual(1, DBTable.GetTable <Currency>().Count);

            Location.DBTable.Clear();
            Location.DBTable.Load().LastOrDefault();

            Assert.AreEqual(1, DBTable.GetTable <Country>().Count);
            Assert.AreEqual(1, DBTable.GetTable <Currency>().Count);
        }