public void ShouldMapAssociationForChild()
        {
            // db config
            var conn = new MockDb()
                .NewReader("Field4")
                    .NewRow("TestOne");

            using (conn)
            using (var db = new DbManager(conn))
            {
                // fluent config
                new FluentMap<AssociationThis2Dbo>()
                    .MapField(_ => _.FieldThis1, "ThisId")
                    .MapField(_ => _.FieldThis3)
                        .Association(_ => _.FieldThis1).ToOne(_ => _.FieldOther3)
                    .MapTo(db);

                var table = db.GetTable<AssociationThis2ChildDbo>();

                // when
                var dbo = (from t in table select t.FieldThis3.FieldOther4).First();

                // then
                Assert.AreEqual("TestOne", dbo, "Fail result for many");
                conn.Commands[0]
                    .Assert().AreField("ThisId", "Fail this key");
                conn.Commands[0]
                    .Assert().AreField("FieldOther3", "Fail other key");
                conn.Commands[0]
                    .Assert().AreField("FieldOther4", "Fail other result");
                Assert.AreEqual(3, conn.Commands[0].Fields.Count, "More fields");
            }
        }
Beispiel #2
0
		public void ShouldMapTableNameForChild()
		{
			// db config
			var conn = new MockDb()
				.NewReader("Field1")
					.NewRow(1);

			using (conn)
			using (var db = new DbManager(conn))
			{
				// fluent config
				new FluentMap<TableNameDbo>()
					.TableName("TableNameDboT1")
					.MapTo(db);

				// when
				db.GetTable<TableNameChildDbo>().ToArray();

				// then
				conn.Commands[0]
					.Assert().AreTable("TableNameDboT1", "Fail mapping");
			}
		}
Beispiel #3
0
		public void ShouldMapValueForTypeForChild()
		{
			// db config
			var conn = new MockDb()
				.NewReader("Field1", "Field2", "Field3")
					.NewRow("one", "two", true);

			using (conn)
			using (var db = new DbManager(conn))
			{
#warning bug for property any different types
#warning bug for maping TO db
				// fluent config
				new FluentMap<MapValueTypeDbo>()
					.MapValue(1, "one", "1")
					.MapValue(2, "two")
					.MapValue(3, true)
					.MapTo(db);

				var table = db.GetTable<MapValueTypeChildDbo>();

				// when
				var dbo = (from t in table select t).First();

				// then
				Assert.AreEqual(1, dbo.Field1, "Not map from value1");
				Assert.AreEqual(2, dbo.Field2, "Not map from value2");
				Assert.AreEqual(3, dbo.Field3, "Not map from value3");
			}
		}
Beispiel #4
0
		public void ShouldMapValueForEnumForChild()
		{
			// db config
			var conn = new MockDb()
				.NewReader("Field1", "Field2", "Field3", "Field4")
					.NewRow("ok", "super", "yes", 10);

			using (conn)
			using (var db = new DbManager(conn))
			{
#warning bug for maping TO db
				// fluent config
				new FluentMap<MapValueEnumDbo>()
					.MapValue(MapValueEnum.Value1, "ok", "yes")
					.MapValue(MapValueEnum.Value2, "super")
					.MapTo(db);

				var table = db.GetTable<MapValueEnumChildDbo>();

				// when
				var dbo = (from t in table select t).First();

				// then
				Assert.AreEqual(MapValueEnum.Value1, dbo.Field1, "Not map from value1");
				Assert.AreEqual(MapValueEnum.Value2, dbo.Field2, "Not map from value2");
				Assert.AreEqual(MapValueEnum.Value1, dbo.Field3, "Not map from value3");
			}
		}
Beispiel #5
0
		public void ShouldMapValueForMemberForChild()
		{
			// db config
			var conn = new MockDb()
				.NewReader("Field1", "Field2")
					.NewRow(true, false);

			using (conn)
			using (var db = new DbManager(conn))
			{
#warning bug for maping TO db
				// fluent config
				new FluentMap<MapValueMemberDbo>()
					.MapField(_ => _.Field1)
						.MapValue("result true", true)
						.MapValue("result false", false)
					.MapField(_ => _.Field2)
						.MapValue("value true", true)
						.MapValue("value false", false)
					.MapTo(db);

				var table = db.GetTable<MapValueMemberChildDbo>();

				// when
				var dbo = (from t in table select t).First();

				// then
				Assert.AreEqual("result true", dbo.Field1, "Not map from value1");
				Assert.AreEqual("value false", dbo.Field2, "Not map from value2");
			}
		}
Beispiel #6
0
		public void ShouldMapTrimmableForChild()
		{
			// db config
			var conn = new MockDb()
				.NewReader("Field1")
					.NewRow("test     ");

			using (conn)
			using (var db = new DbManager(conn))
			{
				// fluent config
				new FluentMap<TrimmableDbo>()
					.Trimmable(_ => _.Field1)
					.MapTo(db);

				var table = db.GetTable<TrimmableChildDbo>();

				// when
				var dbo = (from t in table select t).First();

				// then
				Assert.AreEqual("test", dbo.Field1, "Not trimmable");
			}
		}
Beispiel #7
0
 public MockCommand(MockDb db)
 {
     _db        = db;
     Parameters = new DataParameterCollection();
 }
Beispiel #8
0
		public void ShouldMapField()
		{
			// db config
			var conn = new MockDb()
				.NewNonQuery();

			using (conn)
			using (var db = new DbManager(conn))
			{
				// fluent config
				new FluentMap<MapFieldDbo>()
					.MapField(_ => _.Field1, "f1")
					.MapTo(db);

				// when
				db.GetTable<MapFieldDbo>().Insert(() => new MapFieldDbo { Field1 = 1 });

				// then
				conn.Commands[0]
					.Assert().AreField("f1", "Fail mapping");
			}
		}
Beispiel #9
0
		public void ShouldMapNonUpdatableForChild()
		{
			// db config
			var conn = new MockDb()
				.NewNonQuery();

			using (conn)
			using (var db = new DbManager(conn))
			{
				// fluent config
				new FluentMap<NonUpdatableDbo>()
					.NonUpdatable(_ => _.Field1)
					.MapTo(db);

				// when
				new SqlQuery<NonUpdatableChildDbo>(db).Insert(new NonUpdatableChildDbo { Field1 = 10, Field2 = 1 });

				// then
				Assert.AreEqual(1, conn.Commands[0].Parameters.Count, "Fail params");
			}
		}
Beispiel #10
0
		public void ShouldMapPrimaryKeyForChild()
		{
			// db config
			var conn = new MockDb()
				.NewReader("Field1", "Field2")
					.NewRow(1, 2);

			using (conn)
			using (var db = new DbManager(conn))
			{
				// fluent config
				new FluentMap<PrimaryKeyDbo>()
					.PrimaryKey(_ => _.Field2)
					.MapTo(db);

				// when
				AssertExceptionEx.AreNotException<Exception>(() => new SqlQuery<PrimaryKeyChildDbo>(db).SelectByKey(1)
					, "Fail query");

				// then
				Assert.AreEqual(1, conn.Commands[0].Parameters.Count, "Fail params");
				conn.Commands[0]
					.Assert().AreField("Field2", 2, "Not where part");
			}
		}
Beispiel #11
0
 public AssertDb(MockDb db)
 {
     _db = db;
 }
Beispiel #12
0
		public AssertDb(MockDb db)
		{
			_db = db;
		}
		public void ShouldFailMapValueForTypeForPropertyAnyDifferentTypes()
		{
			// db config
			var conn = new MockDb()
				.NewReader("Field1", "Field2")
					.NewRow("one", 2);

			using (conn)
			using (var db = new DbManager(conn))
			{
				// fluent config
				new FluentMap<MapValueTypeDbo2>()
					.MapValue(1, "one") // Field1 - int
					.MapValue("two", 2) // Filed2 - string
					.MapTo(db);

				var table = db.GetTable<MapValueTypeDbo2>();

				// when / then
				AssertExceptionEx.AreNotException<Exception>(() => { var dbo = (from t in table select t).First(); }
					, "Bug for property any different types");
			}
		}
		public void ShouldFailMapValueForInsert()
		{
			// db config
			var conn = new MockDb()
				.NewNonQuery()
				.NewNonQuery();

			using (conn)
			using (var db = new DbManager(conn))
			{
				// fluent config
				new FluentMap<MapValueMemberDbo>()
					.MapField(_ => _.Field1)
						.MapValue("result true", true)
						.MapValue("result false", false)
					.MapField(_ => _.Field2)
						.MapValue("value true", true)
						.MapValue("value false", false)
					.MapTo(db);

				var table = db.GetTable<MapValueMemberDbo>();

				// when
				table.Insert(() => new MapValueMemberDbo { Field1 = "result true", Field2 = "value false" }); // linq
				db.Insert(new MapValueMemberDbo { Field1 = "result true", Field2 = "value false" }); // other?

				// then
				var i1 = conn.Commands[0].CommandText.IndexOf("result true", StringComparison.Ordinal);
				Assert.IsTrue(-1 == i1, "Not map to value1 for linq");

				var i2 = conn.Commands[0].CommandText.IndexOf("value false", StringComparison.Ordinal);
				Assert.IsTrue(-1 == i2, "Not map to value2 for linq");

				Assert.AreEqual(true, conn.Commands[1].Parameters[0].Value, "Not map to value1");
				Assert.AreEqual(false, conn.Commands[1].Parameters[1].Value, "Not map to value2");
			}
		}
Beispiel #15
0
		public void ShouldMapInheritanceMapping()
		{
			// db config
			var conn = new MockDb()
				.NewReader("Field1", "Field2")
					.NewRow(1, 1)
					.NewRow(2, 2);

			using (conn)
			using (var db = new DbManager(conn))
			{
				// fluent config
				new FluentMap<InheritanceMappingDbo>()
					.InheritanceMapping<InheritanceMappingDbo1>(1)
					.InheritanceMapping<InheritanceMappingDbo2>(2)
					.InheritanceField(_ => _.Field2)
					.MapTo(db);

				var table = db.GetTable<InheritanceMappingDbo>();

				// when
				var dbos = (from t in table select t).ToArray();

				// then
				Assert.IsInstanceOfType(dbos[0], typeof(InheritanceMappingDbo1), "Invalid type1");
				Assert.IsInstanceOfType(dbos[1], typeof(InheritanceMappingDbo2), "Invalid type2");
			}
		}
Beispiel #16
0
		public void ShouldMapInheritanceMappingSelectChild()
		{
			// db config
			var conn = new MockDb()
				.NewReader("Field1", "Field2")
					.NewRow(1, 1);

			using (conn)
			using (var db = new DbManager(conn))
			{
				// fluent config
				new FluentMap<InheritanceMappingChDbo>()
					.TableName("tt")
					.InheritanceMapping<InheritanceMappingChDbo1>(11111)
					.InheritanceMapping<InheritanceMappingChDbo2>(22222)
					.InheritanceField(_ => _.Field2)
					.MapTo(db);

				var table = db.GetTable<InheritanceMappingChDbo1>();

				// when
				var dbos = (from t in table select t).ToArray();

				// then
				Assert.IsInstanceOfType(dbos[0], typeof(InheritanceMappingChDbo1), "Invalid type");
				Assert.IsTrue(conn.Commands[0].CommandText.ToLower().Contains("where"), "Not condition");
				Assert.IsTrue(conn.Commands[0].CommandText.ToLower().Contains("11111"), "Fail condition value");
				conn.Commands[0]
					.Assert().AreField("Field2", 2, "Not in where part");
			}
		}
Beispiel #17
0
		public void ShouldMapIgnoreInsert()
		{
			// db config
			var conn = new MockDb()
				.NewNonQuery();

			using (conn)
			using (var db = new DbManager(conn))
			{
				// fluent config
				new FluentMap<MapIgnoreInsertDbo>()
					.MapIgnore(_ => _.Field2)
					.MapTo(db);

				// when / then
				new SqlQuery<MapIgnoreInsertDbo>(db).Insert(new MapIgnoreInsertDbo { Field1 = 20, Field2 = 2 });

				AssertExceptionEx.AreException<LinqException>(
					() => db.GetTable<MapIgnoreInsertDbo>().Insert(() => new MapIgnoreInsertDbo { Field1 = 10, Field2 = 1 })
					, "Fail for linq");

				// then
				conn.Commands[0]
					.Assert().AreNotField("Field2", "Field exists");
				Assert.AreEqual(1, conn.Commands[0].Parameters.Count, "Fail params");
			}
		}
Beispiel #18
0
		public void ShouldMapRelationForChild()
		{
			// given
			List<Parent2Child> parents = new List<Parent2Child>();
			MapResultSet[] sets = new MapResultSet[2];

			sets[0] = new MapResultSet(typeof(Parent2Child), parents);
			sets[1] = new MapResultSet(typeof(Child2));

			// db config
			var conn = new MockDb()
				.NewReader("ParentID")
					.NewRow(1)
					.NewRow(2)
					.NextResult("ChildID", "ParentID")
					.NewRow(4, 1)
					.NewRow(5, 2)
					.NewRow(6, 2)
					.NewRow(7, 1);

			using (conn)
			using (var db = new DbManager(conn))
			{
				// fluent config
				new FluentMap<Parent2>()
					.MapField(_ => _.ID, "ParentID").PrimaryKey()
					.MapField(_ => _.Children).Relation()
					.MapTo(db);
				new FluentMap<Child2>()
					.MapField(_ => _.Parent.ID, "ParentID")
					.MapField(_ => _.ID, "ChildID").PrimaryKey()
					.MapField(_ => _.Parent).Relation()
					.MapTo(db);

				// when
				db.SetCommand("select *").ExecuteResultSet(sets);
			}

			// then
			Assert.IsTrue(parents.Any());

			foreach (Parent2Child parent in parents)
			{
				Assert.IsNotNull(parent);
				Assert.IsTrue(parent.Children.Any());

				foreach (Child2 child in parent.Children)
				{
					Assert.AreEqual(parent, child.Parent);
				}
			}
		}
Beispiel #19
0
		public void ShouldMapIgnoreForChild()
		{
			// db config
			var conn = new MockDb()
				.NewReader("Field1")
					.NewRow(10, 1);

			using (conn)
			using (var db = new DbManager(conn))
			{
				// fluent config
				new FluentMap<MapIgnoreSelectDbo>()
					.MapIgnore(_ => _.Field2)
					.MapTo(db);

				var table = db.GetTable<MapIgnoreChildDbo>();

				// when
				(from t in table where t.Field1 == 10 select t).First();

				// then
				conn.Commands[0]
					.Assert().AreNotField("Field2", "Field exists");
			}
		}