public void SetTable_RegisterTable()
        {
            var db = new EasyDb();

            db.SetTable <User>("users");
            db.SetTable <Role>("roles");

            Assert.IsTrue(db.IsTableRegistered <User>());
            Assert.IsTrue(db.IsTableRegistered <Role>());
        }
Exemple #2
0
        public async Task SelectOne_WithCompleteMappingAndeIgnoreCase_Success()
        {
            var db = new EasyDb();

            db.SetConnectionStringSettings(DbConstants.SqlDbLikeMySql, DbConstants.SqlProviderName);

            var table = db.SetTable <User>("users")
                        .SetPrimaryKeyColumn("id", p => p.Id)
                        .SetColumn("username", p => p.UserName)
                        .SetColumn("age", p => p.Age)
                        .SetColumn("email", p => p.Email);

            table.IgnoreCase = false;

            User result = null;

            using (var command = db.CreateSqlCommand("select * from [users] where [id]=@id").AddInParameter("@id", 2))
            {
                result = await db.SelectOneAsync <User>(command);
            }

            Assert.AreEqual("Pat", result.UserName);
            Assert.AreEqual(30, result.Age);
            Assert.AreEqual(null, result.Email);
        }
Exemple #3
0
        public async Task SelectAll_WithCompleteMappingAndeIgnoreCase_Success()
        {
            var db = new EasyDb();

            db.SetConnectionStringSettings(DbConstants.SqlDbLikeMySql, DbConstants.SqlProviderName);

            var table = db.SetTable <User>("users")
                        .SetPrimaryKeyColumn("id", p => p.Id)
                        .SetColumn("username", p => p.UserName)
                        .SetColumn("age", p => p.Age)
                        .SetColumn("email", p => p.Email);

            table.IgnoreCase = false;

            List <User> result = null;

            using (var command = db.CreateSqlCommand("select * from [users]"))
            {
                result = await db.SelectAllAsync <User>(command);
            }

            Assert.AreEqual(4, result.Count);

            Assert.AreEqual("Marie", result[0].UserName);
            Assert.AreEqual(null, result[0].Age);
            Assert.AreEqual("*****@*****.**", result[0].Email);

            Assert.AreEqual("Pat", result[1].UserName);
            Assert.AreEqual(30, result[1].Age);
            Assert.AreEqual(null, result[1].Email);
        }
        public void GetQuery_WithMappingAndNoColumnToSet_ThrowException()
        {
            var service = this.GetService();

            var db = new EasyDb();

            db.SetConnectionStringSettings(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=testdb;Integrated Security=True", "System.Data.SqlClient");

            db.SetTable <User>("User")
            .SetPrimaryKeyColumn("Id", p => p.Id)
            .SetColumn("UserName", p => p.UserName, true);

            service.SetDb(db);

            var model = new User {
            };

            bool failed = false;

            try
            {
                var result = service.GetCommand <User>(model, Check.Op("Id", 10).And(Check.Op("RoleId", 5)));
            }
            catch (System.Exception)
            {
                failed = true;
            }

            Assert.IsTrue(failed);
        }
        public async Task ConcurrencyException()
        {
            var db = new EasyDb();

            db.SetConnectionStringSettings(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=MyDb;Integrated Security=True", DbConstants.SqlProviderName);

            db.AddInterceptor(new ConcurrencyInterceptor());

            db.SetTable <UserWithRowVersion>("User")
            .SetPrimaryKeyColumn("Id", p => p.Id)
            .SetColumn("Version", p => p.Version, true);

            var model = await db.SelectOneAsync <UserWithRowVersion>(Check.Op("Id", 2));

            model.UserName  += " updated";
            model.Version[4] = 2;

            bool failed = false;

            try
            {
                int result = await db.UpdateAsync <UserWithRowVersion>(model, Check.Op("Id", model.Id).And(Check.Op("Version", model.Version)));
            }
            catch (OptimisticConcurrencyException ex)
            {
                failed = true;
            }

            Assert.IsTrue(failed);
        }
Exemple #6
0
        public void CreateModel_WithMapping()
        {
            var service = this.GetService();

            var db = new EasyDb();

            db.SetTable <FakeModelMapped>("users")
            .SetColumn("id", p => p.Id)
            .SetColumn("first", p => p.FirstName);

            var data = new Dictionary <string, FakeContainerItem>
            {
                ["id"] = new FakeContainerItem {
                    IsDBNull = false, Name = "id", Value = 1
                },
                ["first"] = new FakeContainerItem {
                    IsDBNull = false, Name = "first", Value = "Marie"
                }
            };
            var reader = new MyReaderContainer(data);

            var result = (FakeModelMapped)service.CreateModel <FakeModelMapped>(reader, db);

            Assert.AreEqual(1, result.Id);
            Assert.AreEqual("Marie", result.FirstName);
        }
Exemple #7
0
        public EasyDb GetService()
        {
            var db = new EasyDb();

            db.SetConnectionStringSettings(DbConstants.OleDbConnectionString, DbConstants.OleDbProviderName);

            var table = db.SetTable <OleDbCategory>("Categories")
                        .SetPrimaryKeyColumn("CategoryID", p => p.CategoryID, false);

            return(db);
        }
        public void TryGetTable()
        {
            var db = new EasyDb();

            db.DefaultMappingBehavior = DefaultMappingBehavior.None;

            var table = db.SetTable <User>("users");

            Assert.IsNotNull(db.TryGetTable <User>());
            Assert.AreEqual(table, db.TryGetTable <User>());
            Assert.IsNull(db.TryGetTable <Role>());
        }
        public async Task Update()
        {
            var db = new EasyDb();

            db.DefaultMappingBehavior = DefaultMappingBehavior.CreateEmptyTable;

            db.SetTable <User>("User")
            .SetPrimaryKeyColumn("Id", p => p.Id);

            db.SetConnectionStringSettings(DbConstants.SqlDb1, DbConstants.SqlProviderName);

            var model = await db.SelectOneAsync <User>(Check.Op("Id", 2));

            model.UserName += " updated";
            var result = await db.UpdateAsync <User>(model, Check.Op("Id", model.Id));

            Assert.AreEqual(1, result);
        }
        public void SetTable_WithColumn()
        {
            var db = new EasyDb();

            db
            .SetTable <User>("users")
            .SetColumn("username", p => p.UserName);

            var table  = db.GetTable <User>();
            var result = table.MappingByColumnName["username"] as Column;

            Assert.AreEqual(typeof(User), table.ModelType);
            Assert.AreEqual("users", table.TableName);
            Assert.AreEqual(typeof(User), result.ModelType);
            Assert.AreEqual("users", result.TableName);
            Assert.AreEqual(typeof(Column), result.GetType());
            Assert.AreEqual("username", result.ColumnName);
            Assert.AreEqual("UserName", result.Property.Name);
            Assert.AreEqual(typeof(User), result.ModelType);
            Assert.AreEqual(false, result.IsDatabaseGenerated);
            Assert.AreEqual(false, result.IsIgnored);
        }