public static void ChildCollectionShouldHaveCorrectValues() { SqlClientRegistrar.Register <DaoReferenceObject>(); string childName = "Name_".RandomString(3); DaoReferenceObject d = new DaoReferenceObject(); d.StringProperty = "".RandomString(8); d.Commit(); var child = d.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.AddNew(); child.Name = childName; d.Commit(); d = DaoReferenceObject.OneWhere(f => f.Id == d.Id); Expect.AreEqual(1, d.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.Count); Expect.AreEqual(child.Name, d.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId[0].Name); child = DaoReferenceObjectWithForeignKey.OneWhere(f => f.Id == d.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId[0].Id); Expect.AreEqual(childName, child.Name); Expect.IsNotNull(child.DaoReferenceObjectOfDaoReferenceObjectId); Expect.AreEqual(child.DaoReferenceObjectOfDaoReferenceObjectId.Id, d.Id); }
public static void DeleteShouldDeleteChildren() { SqlClientRegistrar.Register <DaoReferenceObject>(); DaoReferenceObject parent = new DaoReferenceObject(); parent.StringProperty = "Parent_".RandomString(3); parent.Commit(); var d1 = parent.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.AddNew(); d1.Name = "".RandomString(3); var d2 = parent.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.AddNew(); d2.Name = "".RandomString(3); parent.Commit(); Expect.IsNotNull(d2.Id); DaoReferenceObject check = DaoReferenceObject.OneWhere(c => c.Id == parent.Id); DaoReferenceObjectWithForeignKey check2 = DaoReferenceObjectWithForeignKey.OneWhere(c => c.Id == d2.Id); Expect.AreEqual(2, check.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.Count); Expect.IsNotNull(check2); check.Delete(); check2 = DaoReferenceObjectWithForeignKey.OneWhere(c => c.Id == d2.Id); Expect.IsNull(check2); }
public static void QuerySetShouldFillIHasDataTableList() { SqlClientRegistrar.Register <DaoReferenceObject>(); QuerySet testQuerySet = new QuerySet(); Expect.IsNull(testQuerySet.DataSet); DaoReferenceObject test = new DaoReferenceObject(); test.StringProperty = "".RandomString(8); test.Commit(); testQuerySet.Select <DaoReferenceObjectColumns, DaoReferenceObject>(f => f.Id == test.Id); DaoReferenceObject dao = new DaoReferenceObject(); dao.StringProperty = "".RandomString(5); testQuerySet.Insert <DaoReferenceObject>(dao); testQuerySet.Execute(Db.For <DaoReferenceObject>()); Expect.IsNotNull(testQuerySet.DataSet); Expect.IsTrue(testQuerySet.DataSet.Tables.Count == 2); Expect.IsGreaterThan(testQuerySet.Results.Count, 0); Expect.AreEqual(2, testQuerySet.Results.Count); DaoReferenceObject d = testQuerySet.Results.ToDao <DaoReferenceObject>(1); Expect.AreEqual(dao.StringProperty, d.StringProperty); DaoReferenceObjectCollection coll = testQuerySet.Results.As <DaoReferenceObjectCollection>(0); Expect.IsNotNull(coll); Expect.IsGreaterThan(coll.Count, 0); }
public static void QuerySetWhereShouldWorkAsExpected() { SqlClientRegistrar.Register <DaoReferenceObject>(); DaoReferenceObject test = Create(); DaoReferenceObject test2 = Create(); QuerySet query = new QuerySet(); DaoReferenceObject third = new DaoReferenceObject(); third.StringProperty = test.StringProperty; query.Insert(third); query.Select <DaoReferenceObject>().Where <DaoReferenceObjectColumns>(c => c.StringProperty == third.StringProperty); query.Execute(Db.For <DaoReferenceObject>()); DaoReferenceObjectCollection coll = query.Results.As <DaoReferenceObjectCollection>(1); foreach (DaoReferenceObject obj in coll) { Out(obj.PropertiesToString()); Out(); } }
public static void UpdateShouldOnlyUpdateOne() { SqlClientRegistrar.Register <DaoReferenceObject>(); string first = "".RandomString(8); string second = "".RandomString(4); DaoReferenceObject test = new DaoReferenceObject(); test.BoolProperty = true; test.StringProperty = first; Expect.IsTrue(test.IsNew); test.Commit(); DaoReferenceObject one = DaoReferenceObject.OneWhere(f => f.StringProperty == test.StringProperty); one.StringProperty = second; Expect.IsFalse(one.IsNew); one.Commit(); DaoReferenceObjectCollection results = new DaoReferenceObjectCollection( Select <DaoReferenceObjectColumns> .From <DaoReferenceObject>() .Where(f => f.StringProperty == second)); Expect.IsTrue(results.Count == 1); }
private static Database RegisterSqlClientForConnection(string connection) { Dao.ProxyConnection(typeof(Item), connection); SqlClientRegistrar.Register <Item>(); Database db = Db.For(connection);//_.Db[connection]; SetupSqlClientDatabase(db); return(db); }
public static void ProxiedDatabasesShouldBeTheSame() { Dao.ProxyConnection(typeof(Item), "CRUD_SqlClient"); SqlClientRegistrar.Register <Item>(); Database db = Db.For(typeof(Item)); //_.Db[typeof(Item)]; Database check = Db.For("CRUD_SqlClient"); //_.Db["CRUD_SqlClient"]; Database forCheck = Db.For <Item>(); Expect.AreSame(db, check); Expect.AreSame(check, forCheck); Expect.AreSame(forCheck, db); }
public static void WhereShortCutShouldWork() { SqlClientRegistrar.Register(Db.For <DaoReferenceObject>().ServiceProvider); DaoReferenceObject test = new DaoReferenceObject(); test.StringProperty = "".RandomString(8); test.Commit(); DaoReferenceObjectCollection checking = DaoReferenceObject.Where(c => c.StringProperty == test.StringProperty); Expect.IsTrue(checking.Count == 1); }
public static void ParentOfCollectionShouldBeRootDao() { SqlClientRegistrar.Register(Db.For <DaoReferenceObject>().ServiceProvider); DaoReferenceObject d = new DaoReferenceObject(); d.StringProperty = "".RandomString(8); d.Commit(); DaoReferenceObjectWithForeignKey dfk = d.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.AddNew(); Expect.AreSame(d, d.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.Parent); }
public static void CrudSqlClient() { Dao.ProxyConnection(typeof(Item), "CRUD_SqlClient"); SqlClientRegistrar.Register <Item>(); Database db = Db.For <Item>(); SchemaWriter sw = db.ServiceProvider.GetNew <SchemaWriter>(); sw.EnableDrop = true; sw.DropAllTables <Item>(); sw.TryExecute(db); sw.Reset(); sw.WriteSchemaScript <Item>(); Exception ex; sw.TryExecute(db, out ex); Expect.IsNull(ex); // Create string name = "".RandomString(8); Item created = new Item(); created.Name = name; created.Commit(); // Retrieve ItemCollection results = Item.Where(p => p.Name == name); Expect.AreEqual(1, results.Count); // Update Item check = results[0]; check.Name = "".RandomString(8); check.Commit(); results = Item.Where(p => p.Name == name); Expect.AreEqual(0, results.Count); results = Item.Where(p => p.Name == check.Name); Expect.AreEqual(1, results.Count); // Delete check.Delete(); results = Item.Where(p => p.Name == name); Expect.AreEqual(0, results.Count); results = Item.Where(p => p.Name == check.Name); Expect.AreEqual(0, results.Count); }
public static void QuerySetCount() { SqlClientRegistrar.Register <Item>(); QuerySet query = new QuerySet(); query.Count <Item>().Where <Bam.Net.Data.Tests.ItemColumns>(c => c.Name.StartsWith("Mort")); query.Execute(Db.For <Item>()); long result = query.Results[0].As <CountResult>().Value; long toCountResult = query.Results.ToCountResult(0); Expect.AreEqual(result, toCountResult); Out(query.Results.ToCountResult(0).ToString()); }
public static void ShortcutShouldWorkLikeLongcut() { SqlClientRegistrar.Register(Db.For <DaoReferenceObject>().ServiceProvider); DaoReferenceObject test = new DaoReferenceObject(); string val = "".RandomString(8); test.StringProperty = val; test.Commit(); DaoReferenceObjectCollection results = DaoReferenceObject.Where(f => f.Id == test.Id); Expect.IsTrue(results.Count > 0); Expect.IsTrue(results[0].StringProperty.Equals(val)); }
public static void CommitShouldSetId() { SqlClientRegistrar.Register(Db.For <DaoReferenceObject>().ServiceProvider); DaoReferenceObject test = new DaoReferenceObject(); test.BoolProperty = true; test.DecimalProperty = (decimal)10.00; test.StringProperty = "".RandomString(8); Expect.IsNull(test.Id); test.Commit(); Expect.IsNotNull(test.Id); OutFormat("The id was {0}", test.Id); }
public static void AddShouldSetAssociation() { SqlClientRegistrar.Register(Db.For <DaoReferenceObject>().ServiceProvider); DaoReferenceObject d = new DaoReferenceObject(); d.StringProperty = "".RandomString(8); d.Commit(); DaoReferenceObjectWithForeignKey test = new DaoReferenceObjectWithForeignKey(); Expect.IsNull(test.DaoReferenceObjectId); d.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.Add(test); Expect.IsNotNull(test.DaoReferenceObjectId); Expect.AreEqual(test.DaoReferenceObjectId, d.Id); }
public static void DeleteShouldWork() { SqlClientRegistrar.Register(Db.For <DaoReferenceObject>().ServiceProvider); DaoReferenceObject test = new DaoReferenceObject(); test.StringProperty = "".RandomString(8); test.Commit(); DaoReferenceObject d = DaoReferenceObject.OneWhere(c => c.Id == test.Id); Expect.IsNotNull(d); Expect.AreEqual(test.StringProperty, d.StringProperty); d.Delete(); d = DaoReferenceObject.OneWhere(c => c.StringProperty == test.StringProperty); Expect.IsNull(d); }
public static void CommitOnCollectionShouldUpdateIds() { SqlClientRegistrar.Register <DaoReferenceObject>(); string childName = "Name_".RandomString(3); DaoReferenceObject d = new DaoReferenceObject(); d.StringProperty = "".RandomString(8); d.Commit(); var child = d.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.AddNew(); child.Name = childName; d.Commit(); Expect.IsNotNull(child.IdValue); }
public void AlternativeSyntaxTest() { SqlClientRegistrar.Register <Item>(); Db.TryEnsureSchema <Item>(); Database db = Db.For(typeof(Item)); Item createdItem = new Item(); createdItem.Name = "Item_".RandomLetters(8); QuerySet query = new QuerySet(); query.Insert <Item>(createdItem); query.Select <Item>().Where <ItemColumns>(c => c.Name.StartsWith("I")); query.Count <Item>(); query.Execute(db); // alternative syntax //query.Insert<Item>(createdItem) // .Select<Item>().Where<ItemColumns>(c => c.Name.StartsWith("I")) // .Count<Item>() // .Execute(db); // -- end alternative syntax Item insertedItem = query.Results.ToDao <Item>(0); OutLineFormat("InsertedItemId: {0}, Name: {1}", ConsoleColor.Green, insertedItem.Id, insertedItem.Name); ItemCollection items = query.Results[1].As <ItemCollection>(); OutLine("** Item Results **", ConsoleColor.DarkYellow); items.Each(item => { OutLineFormat("Id: {0}, Name: {1}", ConsoleColor.DarkYellow, item.Id, item.Name); }); long count = query.Results[2].As <CountResult>().Value; OutLineFormat("Count Result: {0}", ConsoleColor.Yellow, count); }
public static void ChildSetsShouldBeFull() { SqlClientRegistrar.Register <DaoReferenceObject>(); DaoReferenceObject parent = new DaoReferenceObject(); parent.StringProperty = "Parent"; parent.Commit(); DaoReferenceObjectWithForeignKey child = parent.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.AddNew(); child.Name = "Monkey"; parent.Commit(); Expect.IsNotNull(child.Id); DaoReferenceObject check = DaoReferenceObject.OneWhere(c => c.Id == parent.Id); Expect.IsNotNull(check); Expect.AreEqual(1, check.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.Count); }
public static void ShouldBeAbleToQueryById() { SqlClientRegistrar.Register(Db.For <DaoReferenceObject>().ServiceProvider); DaoReferenceObject test = new DaoReferenceObject(); string val = "".RandomString(8); test.StringProperty = val; test.Commit(); DaoCollection <DaoReferenceObjectColumns, DaoReferenceObject> results = new DaoCollection <DaoReferenceObjectColumns, DaoReferenceObject>( Select <DaoReferenceObjectColumns> .From <DaoReferenceObject>() .Where((c) => c.Id == test.Id) ); Expect.IsNotNull(results); Expect.IsTrue(results.Count == 1); Expect.AreEqual(results[0].StringProperty, val); }
public static void UnCommittedParentShouldThrowOnChildAdd() { bool thrown = false; try { SqlClientRegistrar.Register <DaoReferenceObject>(); DaoReferenceObject parent = new DaoReferenceObject(); parent.StringProperty = "Parent"; DaoReferenceObjectWithForeignKey child = parent.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.AddNew(); child.Name = "".RandomString(8); parent.Commit(); } catch (InvalidOperationException ioe) { thrown = true; Out(ioe.Message, ConsoleColor.Cyan); } Expect.IsTrue(thrown); }
public static void CommitParentShouldCallCommitOnCollections() { SqlClientRegistrar.Register <DaoReferenceObject>(); DaoReferenceObject d = new DaoReferenceObject(); d.StringProperty = "".RandomString(8); d.Commit(); var child = d.DaoReferenceObjectWithForeignKeyCollectionByDaoReferenceObjectId.AddNew(); child.Name = "Name_".RandomString(3); bool?handled = false; child.BeforeWriteCommit += (a, i) => { handled = true; }; d.Commit(); Expect.IsTrue(handled.Value); }
public static void ShouldRecreateSchemaSqlClient() { SqlClientRegistrar.Register <Item>(); Database reproduceIn = Db.For("ReproSchemaSqlClient");//_.Db["ReproSchemaSqlClient"]; Database db = Db.For <Item>(); SetupDatabases <SqlClientSqlStringBuilder, SqlClientSqlStringBuilder> (reproduceIn, db.ServiceProvider.Get <IParameterBuilder>()); DropAllTables(reproduceIn); SchemaWriter writer = db.ServiceProvider.GetNew <SchemaWriter>(); Expect.IsTrue(writer.WriteSchemaScript <Item>(), "WriteSchemaScript returned false instead of true"); Exception e; bool executeResult = writer.TryExecute(reproduceIn, out e); Expect.IsNull(e, e == null ? "" : e.Message); // no exception should have occurred Expect.IsTrue(executeResult, "TryExecute returned false instead of true"); Expect.IsFalse(writer.WriteSchemaScript <Item>(), "WriteSchemaScript returned true instead of false"); }