public void Test_EntityRepo_AttachDetach() { var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); using (var ctx = dbService.GetDatabaseContext(true)) { #region prepare data var jordan = new Author() { FirstName = "Robert", LastName = "Jordan", IsAlive = false, Born = new DateTime(1948, 10, 17), Rating = 10.0m }; var feist = new Author() { FirstName = "Raymond", LastName = "Feist", IsAlive = true, Born = new DateTime(1963, 2, 14), Rating = 6.7m }; var fb1 = new Book() { Title = "The Apprentice", Price = 19.90m }; var fb2 = new Book() { Title = "The Magician", Price = 17.10m }; repository.Create(jordan); repository.Create(feist); repository.Create(fb1); repository.Create(fb2); #endregion repository.Attach(feist, new Relation("author", fb1)); var rel2 = new Relation("author", fb2); var writtenOn = new DateTime(1996, 4, 25); rel2.SetData<DateTime>("WrittenOn", writtenOn); repository.Attach(feist, rel2); var q = new EntityQuery2("author", feist.Id); q.AddProperties("FirstName", "lastname", "isalive", "born", "rating"); q.Include("book", "author"); var e = repository.Read(q); Assert.AreEqual(2, e.GetManyRelations("book", "author").Count()); var bq = new EntityQuery2("book"); bq.Include("author", "author"); var bes = repository.Search(bq); foreach (var be in bes) { Assert.AreEqual(1, be.RelationsData.Count); Assert.AreEqual(feist.Id, be.GetSingleRelation("author", "author").Entity.Id); if (be.Id == fb2.Id) Assert.AreEqual(writtenOn, be.GetSingleRelation("author", "author").GetData<DateTime>("writtenon")); } repository.Detach(feist, new Relation("author", fb1)); e = repository.Read(q); Assert.AreEqual(1, e.GetManyRelations("book", "author").Count()); repository.Attach(fb1, new Relation("author", feist)); e = repository.Read(q); Assert.AreEqual(2, e.GetManyRelations("book", "author").Count()); repository.Detach(fb1, new Relation("author", feist)); e = repository.Read(q); Assert.AreEqual(1, e.GetManyRelations("book", "author").Count()); bool ex = false; try { repository.Attach(fb2, new Relation("author", jordan)); } catch (Exception) { ex = true; } Assert.IsTrue(ex, "Exception not thrown when attaching two authors to single book"); } }
public void Test_EntityRepo_Count() { var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); using (var ctx = dbService.GetDatabaseContext(true)) { #region prepare data var jordan = new Author() { FirstName = "Robert", LastName = "Jordan", IsAlive = false, Born = new DateTime(1948, 10, 17), Rating = 10.0m }; var feist = new Author() { FirstName = "Raymond", LastName = "Feist", IsAlive = true, Born = new DateTime(1963, 2, 14), Rating = 6.7m }; var fb1 = new Book() { Title = "The Apprentice", Price = 19.90m }; var fb2 = new Book() { Title = "The Magician", Price = 17.10m }; var jb1 = new Book() { Title = "The Shadow is Rising", Price = 21.15m }; var jb2 = new Book() { Title = "The Eye of the World", Price = 25.80m }; repository.Create(jordan); repository.Create(feist); repository.Create(fb1); repository.Create(fb2); repository.Create(jb1); repository.Create(jb2); repository.Attach(feist, new Relation("author", fb1)); repository.Attach(feist, new Relation("author", fb2)); repository.Attach(jordan, new Relation("author", jb1)); repository.Attach(jordan, new Relation("author", jb2)); #endregion var query = new EntityQuery2("author"); query.AddProperties("firstname", "lastname", "born"); var res = repository.Search(query); Assert.AreEqual(2, res.Count()); Assert.AreEqual(2, repository.Count(query)); //greater then EntityQuery2 q = new EntityQuery2("book"); q.WhereGreaterThen("price", 19.0m); Assert.AreEqual(3, repository.Search(q).Count()); Assert.AreEqual(3, repository.Count(q)); //less then q = new EntityQuery2("book"); q.WhereLessThen("price", 20.0m); Assert.AreEqual(2, repository.Search(q).Count()); Assert.AreEqual(2, repository.Count(q)); //is boolean q = new EntityQuery2("author"); q.WhereIs("isalive", false); var r = repository.Search(q); Assert.AreEqual(1, r.Count()); Assert.AreEqual(1, repository.Count(q)); //is string (ignore case) q = new EntityQuery2("author"); q.WhereIs("lastname", "jordan"); r = repository.Search(q); Assert.AreEqual(1, r.Count()); Assert.AreEqual(1, repository.Count(q)); //starts with q = new EntityQuery2("author"); q.WhereStartsWith("firstname", "ra"); r = repository.Search(q); Assert.AreEqual(1, r.Count()); Assert.AreEqual(1, repository.Count(q)); //ends with q = new EntityQuery2("book"); q.WhereEndsWith("title", "world"); r = repository.Search(q); Assert.AreEqual(1, r.Count()); Assert.AreEqual(1, repository.Count(q)); //less then q = new EntityQuery2("book"); q.WhereAnyOf("id", new object[] { fb1.Id, jb1.Id, jb2.Id }); Assert.AreEqual(3, repository.Search(q).Count()); Assert.AreEqual(3, repository.Count(q)); //between decimal q = new EntityQuery2("book"); q.WhereBetween("price", 19.0m, 22.0m); Assert.AreEqual(2, repository.Search(q).Count()); Assert.AreEqual(2, repository.Count(q)); //between datetime q = new EntityQuery2("author"); q.WhereBetween("born", new DateTime(1948, 1, 1), new DateTime(1949, 1, 1)); r = repository.Search(q); Assert.AreEqual(1, r.Count()); Assert.AreEqual(1, repository.Count(q)); q = new EntityQuery2("author"); q.WhereBetween("born", new DateTime(1948, 1, 1), new DateTime(1949, 1, 1)); q.WhereIs("isalive", true); Assert.AreEqual(0, repository.Search(q).Count()); Assert.AreEqual(0, repository.Count(q)); q = new EntityQuery2("author"); q.WhereBetween("born", new DateTime(1960, 1, 1), new DateTime(1970, 1, 1)); q.WhereIs("isalive", true); q.WhereStartsWith("firstname", "ra"); Assert.AreEqual(1, repository.Search(q).Count()); Assert.AreEqual(1, repository.Count(q)); } }
public void Test_DomainModelService_Merge() { var dm = new DomainModel(); ModelBuilder ba = new ModelBuilder("Author"); ba.AddIdentity("Id"); ba.AddString("FirstName", 128); ba.AddString("LastName", 128); ba.AddComputed("FullName", "[LastName]+N', '+[FirstName]"); ba.AddBoolean("IsAlive", true); ba.AddInteger("NumberOfAwards"); ba.AddDateTime("Born"); ba.AddDecimal("Rating"); ModelBuilder bb = new ModelBuilder("Book"); bb.AddIdentity("Id"); bb.AddString("Title", 256); bb.AddString("ISBN", 20); bb.AddDecimal("Price"); bb.AddEnum<Genre>("Genre", Genre.Fantasy); bb.Rules.AddRequired("title"); bb.Rules.AddUnique("ISBN"); ba.AddRelationTo(bb.EntityModel, RelationType.OneToMany, "Author"); dm.Entities.Add(ba.EntityModel); dm.Entities.Add(bb.EntityModel); svc.Save(dm); using (var conn = new TestDatabaseService().GetSqlConnection()) { conn.Open(); DatabaseManager dbm = new DatabaseManager(conn); dbm.LoadSchema(); var tableA = dbm.Tables.Find(t => t.Name == "Author"); Assert.IsNotNull(tableA); var fnCol = tableA.Columns.Find(c => c.Name.Equals("FullName", StringComparison.InvariantCultureIgnoreCase)); Assert.IsNotNull(fnCol); } ModelBuilder bo = new ModelBuilder("Order"); bo.AddDecimal("Total"); bo.AddBoolean("Paid"); bo.AddDateTime("CreatedOn"); bo.AddDateTime("ShipmentDate"); bo.AddRelationTo(dm.Entities["book"], RelationType.ManyToMany, "Ordered"); dm.Entities.Add(bo.EntityModel); ba.EntityModel.Properties.Remove(ba.EntityModel.Properties["IsAlive"]); (ba.EntityModel.Properties["LastName"] as StringPropertyModel).Length = 256; (ba.EntityModel.Properties["firstName"] as StringPropertyModel).Length = 256; (bb.EntityModel.Properties["genre"] as EnumPropertyModel).DefaultValue = Genre.Horror; ba.AddString("Nickname", 256); svc.Merge(dm); Assert.AreEqual(3, svc.Domain.Entities.Count); //foreach (var em1 in dm.Entities) //{ // var em2 = svc.Domain.Entities[em1.Name]; // Assert.AreEqual(em1.Properties.Count, em2.Properties.Count); //} //TODO-tests:finish domainmodelservice_merge test //Computed columns are droped and recreated when upgrading the columns in their computed definition using (var conn = new TestDatabaseService().GetSqlConnection()) { conn.Open(); DatabaseManager dbm = new DatabaseManager(conn); dbm.LoadSchema(); var tableA = dbm.Tables.Find(t => t.Name == "Author"); Assert.IsNotNull(tableA); var fnCol = tableA.Columns.Find(c => c.Name.Equals("FullName", StringComparison.InvariantCultureIgnoreCase)); Assert.IsNotNull(fnCol); } }
public void Test_EntityOperation_Update() { var dbService = new TestDatabaseService(); var repo = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); IEntityOperationService svc = new EntityOperationService(repo, dbService, new IEntityOperationInspector[] { new Inspector() }, new IEntityQueryInspector[] { new Inspector() }, new IEntityOperationLogic[] { new Logic() }); EntityUpdate update = new EntityUpdate("Author"); update.Set("FirstName", "John"); update.Set("LastName", "Tolkin"); update.Set("Numberofawards", 2); update.Set("IsAlive", false); EntityUpdate book = new EntityUpdate("book"); book.Set("Title", "The Eye of the World"); book.Set("genre", Genre.Fantasy); svc.Update(book); update.Attach("Book", "author", book.Id.Value); var result = svc.Update(update); Assert.AreEqual(true, result.Success); EntityQuery2 query = new EntityQuery2("Author"); query.AddProperties("FirstName", "LastName", "IsAlive", "CreatedOn"); query.Include("book", "author"); var res = repo.Search(query); Assert.AreEqual(1, res.Count()); var a = res.Single(); Assert.AreEqual("John", a.GetData<string>("Firstname")); Assert.AreEqual("Tolkin", a.GetData<string>("LastName")); var created = a.GetData<DateTime>("createdon"); Assert.AreEqual(DateTime.Now.Date, created.Date); var books = a.GetManyRelations("book", "author"); Assert.AreEqual(1, books.Count()); var b = books.Single().Entity; Assert.AreEqual("The Eye of the World", b.GetData<string>("title")); Assert.AreEqual(Genre.Fantasy, b.GetData<Genre>("genre")); created = b.GetData<DateTime>("createdon"); Assert.AreEqual(DateTime.Now.Date, created.Date); repo.Delete(a, true); repo.Delete(b); }
public void Test_EntityRepo_Update() { var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); using (var ctx = dbService.GetDatabaseContext(true)) { #region prepare data var jordan = new Author() { FirstName = "Robert", LastName = "Jordan", IsAlive = false, Born = new DateTime(1948, 10, 17), Rating = 10.0m }; var feist = new Author() { FirstName = "Raymond", LastName = "Feist", IsAlive = true, Born = new DateTime(1963, 2, 14), Rating = 6.7m }; var fb1 = new Book() { Title = "The Apprentice", Price = 19.90m }; var fb2 = new Book() { Title = "The Magician", Price = 17.10m }; repository.Create(jordan); repository.Create(feist); repository.Create(fb1); repository.Create(fb2); repository.Attach(feist, new Relation("author", fb1)); repository.Attach(feist, new Relation("author", fb2)); #endregion jordan.LastName += "EDIT"; jordan.IsAlive = true; jordan.Born = jordan.Born.AddDays(2.0); jordan.Rating -= 0.5m; repository.Update(jordan); var q = new EntityQuery2("author", jordan.Id); q.AddProperties("lastname", "isalive", "born", "rating"); var e = repository.Read(q); var updated = new Author(e); Assert.AreEqual(jordan.LastName, updated.LastName); Assert.AreEqual(jordan.Born, updated.Born); Assert.AreEqual(jordan.IsAlive, updated.IsAlive); Assert.AreEqual(jordan.Rating, updated.Rating); } }
private static void CreateDomain() { var dm = new DomainModel(); ModelBuilder ba = new ModelBuilder("Author"); ba.AddIdentity("Id"); ba.AddString("FirstName", 128); ba.AddString("LastName", 128); ba.AddBoolean("IsAlive", true); ba.AddInteger("NumberOfAwards"); ba.AddDateTime("Born"); ba.AddDecimal("Rating"); ModelBuilder bb = new ModelBuilder("Book"); bb.AddIdentity("Id"); bb.AddString("Title", 256); bb.AddString("ISBN", 20); bb.AddDecimal("Price"); bb.AddEnum<Genre>("Genre", Genre.Mistery); bb.Rules.AddRequired("title"); bb.Rules.AddUnique("ISBN"); var rel = ba.AddRelationTo(bb.EntityModel, RelationType.OneToMany, "Author"); var br = new ModelBuilder(rel); br.AddDateTime("WrittenOn"); dm.Entities.Add(ba.EntityModel); dm.Entities.Add(bb.EntityModel); ModelBuilder bo = new ModelBuilder("Order"); bo.AddDecimal("Total"); bo.AddBoolean("Paid"); bo.AddDateTime("CreatedOn"); bo.AddDateTime("ShipmentDate"); bo.AddRelationTo(dm.Entities["book"], RelationType.ManyToMany, "Ordered"); bo.AddUri("Number", "BO"); dm.Entities.Add(bo.EntityModel); dms.Save(dm); using (var conn = new TestDatabaseService().GetSqlConnection()) { conn.Open(); var dbManager = new DatabaseManager(conn); SequenceProvider.Initialize(dbManager); } }
public void Test_EntityRepo_SearchRelated() { var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); using (var ctx = dbService.GetDatabaseContext(true)) { #region prepare data var jordan = new Author() { FirstName = "Robert", LastName = "Jordan", IsAlive = false, Born = new DateTime(1948, 10, 17), Rating = 10.0m }; var feist = new Author() { FirstName = "Raymond", LastName = "Feist", IsAlive = true, Born = new DateTime(1963, 2, 14), Rating = 6.7m }; var fb1 = new Book() { Title = "The Apprentice", Price = 19.90m }; var fb2 = new Book() { Title = "The Magician", Price = 17.10m }; var jb1 = new Book() { Title = "The Shadow is Rising", Price = 21.15m }; var jb2 = new Book() { Title = "The Eye of the World", Price = 25.80m }; repository.Create(jordan); repository.Create(feist); repository.Create(fb1); repository.Create(fb2); repository.Create(jb1); repository.Create(jb2); repository.Attach(feist, new Relation("author", fb1)); repository.Attach(feist, new Relation("author", fb2)); repository.Attach(jordan, new Relation("author", jb1)); repository.Attach(jordan, new Relation("author", jb2)); #endregion EntityQuery2 query = new EntityQuery2("book"); query.AllProperties = true; var fq = new RelationQuery("author", "author", feist.Id); query.RelatedTo.Add(fq); var res = repository.Search(query); Assert.AreEqual(2, res.Count()); Assert.IsNotNull(res.First(b => b.GetData<string>("title") == fb1.Title)); Assert.IsNotNull(res.First(b => b.GetData<string>("title") == fb2.Title)); query.Include("author", "author"); res = repository.Search(query); Assert.AreEqual(2, res.Count()); Assert.IsNotNull(res.First(b => b.GetData<string>("title") == fb1.Title)); Assert.IsNotNull(res.First(b => b.GetData<string>("title") == fb2.Title)); } }
public void Test_EntityRepo_SearchWithRels() { var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); using (var ctx = dbService.GetDatabaseContext(true)) { var jordan = new Author() { FirstName = "Robert", LastName = "Jordan", IsAlive = false, Born = new DateTime(1948, 10, 17), Rating = 10.0m }; var feist = new Author() { FirstName = "Raymond", LastName = "Feist", IsAlive = true, Born = new DateTime(1963, 2, 14), Rating = 6.7m }; var fb1 = new Book() { Title = "The Apprentice", Price = 19.90m }; var fb2 = new Book() { Title = "The Magician", Price = 17.10m }; repository.Create(jordan); repository.Create(feist); repository.Create(fb1); repository.Create(fb2); repository.Attach(feist, new Relation("author", fb1)); repository.Attach(feist, new Relation("author", fb2)); var query = new EntityQuery2("author"); query.AddProperties("firstname", "lastname", "born"); query.Include("book", "author"); var res = repository.Search(query); Assert.AreEqual(2, res.Count()); var rf = res.Single(e => e.Id == feist.Id); var rj = res.Single(e => e.Id == jordan.Id); Assert.AreEqual(1, rf.RelationsData.Count); var books = rf.GetManyRelations("book", "author"); Assert.AreEqual(2, books.Count()); foreach (var r in books) { var orig = r.Entity.Id == fb1.Id ? fb1 : fb2; foreach (var pm in dms.Domain.Entities["book"].Properties) { if (orig.Data.ContainsKey(pm.Name)) Assert.AreEqual(orig.Data[pm.Name], r.Entity.Data[pm.Name]); } } Assert.AreEqual(0, rj.RelationsData.Count); Assert.AreEqual(0, rj.GetManyRelations("book", "author").Count()); //TODO: rules! //repository.Complete(); } }
public void Test_EntityRepo_Read() { var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); using (var ctx = dbService.GetDatabaseContext(true)) { var jordan = new Author() { FirstName = "Robert", LastName = "Jordan", IsAlive = false, Born = new DateTime(1948, 10, 17), Rating = 10.0m }; var feist = new Author() { FirstName = "Raymond", LastName = "Feist", IsAlive = true, Born = new DateTime(1963, 2, 14), Rating = 6.7m }; jordan.Id = repository.Create(jordan); Assert.IsTrue(jordan.Id > 0); feist.Id = repository.Create(feist); Assert.IsTrue(feist.Id > 0); EntityQuery2 q = new EntityQuery2("author", jordan.Id); q.AddProperties("FirstName", "lastname", "isalive", "born", "rating"); var e = repository.Read(q); Assert.AreEqual(5, e.Data.Count); foreach (var p in q.Properties) { Assert.AreEqual(jordan.Data[p], e.Data[p]); } } }
public void Test_EntityRepo_ReadWithRel() { var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); using (var ctx = dbService.GetDatabaseContext(true)) { var jordan = new Author() { FirstName = "Robert", LastName = "Jordan", IsAlive = false, Born = new DateTime(1948, 10, 17), Rating = 10.0m }; var feist = new Author() { FirstName = "Raymond", LastName = "Feist", IsAlive = true, Born = new DateTime(1963, 2, 14), Rating = 6.7m }; var fb1 = new Book() { Title = "The Apprentice", Price = 19.90m }; var fb2 = new Book() { Title = "The Magician", Price = 17.10m }; repository.Create(jordan); repository.Create(feist); repository.Create(fb1); repository.Create(fb2); repository.Attach(feist, new Relation("author", fb1)); repository.Attach(feist, new Relation("author", fb2)); EntityQuery2 q = new EntityQuery2("book", fb1.Id); q.AddProperties("title", "price"); q.Include("author", "author"); var e = repository.Read(q); Assert.AreEqual(2, e.Data.Count); foreach (var p in q.Properties) { Assert.AreEqual(fb1.Data[p], e.Data[p]); } Assert.AreEqual(1, e.RelationsData.Count); var authorRel = e.GetSingleRelation("author", "author"); foreach (var d in feist.Data) { Assert.AreEqual(d.Value, authorRel.Entity.Data[d.Key]); } //repository.Complete(); } }
//[TestMethod] public void Test_EntityRepo_Perf() { var r = new Random(); var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); using (var ctx = dbService.GetDatabaseContext(true)) { for (int i = 0; i < 1000; i++) { EntityUpdate update = new EntityUpdate("author"); update.Set("firstname", "Robert" + i); update.Set("lastname", "Jordan"); update.Set("isalive", false); update.Set("Born", new DateTime(1948, 10, 17)); update.Set("Rating", 5.5m + r.Next(4)); var id = repository.Create(update.ToEntity()); Assert.IsTrue(id > 0); EntityQuery2 q = new EntityQuery2("author", id); q.AddProperties("FirstName", "lastname", "isalive", "born", "rating"); var e = repository.Read(q); foreach (var pu in update.PropertyUpdates) { Assert.AreEqual(pu.Value, e.Data[pu.Key]); } EntityUpdate update2 = new EntityUpdate(e.Name, e.Id); update2.Set("rating", 5.5m + r.Next(4)); update2.Set("lastname", e.Data["lastname"] + "_EDIT"); repository.Update(update2.ToEntity()); e = repository.Read(q); foreach (var pu in update2.PropertyUpdates) { Assert.AreEqual(pu.Value, e.Data[pu.Key]); } foreach (var pu in update.PropertyUpdates) { if (!pu.Key.Equals("rating", StringComparison.InvariantCultureIgnoreCase) && !pu.Key.Equals("lastname", StringComparison.InvariantCultureIgnoreCase)) Assert.AreEqual(pu.Value, e.Data[pu.Key]); } } ctx.Complete(); } using (var ctx = dbService.GetDatabaseContext(true)) { var qAll = new EntityQuery2("Author"); var all = repository.Search(qAll); Assert.AreEqual(1000, all.Count()); foreach (var a in all) repository.Delete(a); Assert.AreEqual(0, repository.Search(qAll).Count()); } }
public void Test_EntityRepo_Paging() { var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); using (var ctx = dbService.GetDatabaseContext(true)) { #region prepare data int aCnt = 20; int bCnt = 3; for (int i = 0; i < aCnt; i++) { var a = new Author() { FirstName = "Fname" + i, LastName = "Lname" + i, Born = DateTime.Now.AddYears(-20).AddDays(i), NumberOfAwards = i / 3 }; repository.Create(a); for (int j = 0; j < bCnt; j++) { var b = new Book() { Title = string.Format("Book_{0}_{1}", i, j), Genre = Genre.SciFi, Price = 10.0m + j, ISBN = string.Format("{0}_{1}", a.LastName, j) }; repository.Create(b); repository.Attach(b, new Relation("author", a)); } } Assert.AreEqual(aCnt, repository.Search(new EntityQuery2("author")).Count()); Assert.AreEqual(aCnt * bCnt, repository.Search(new EntityQuery2("book")).Count()); #endregion var query = new EntityQuery2("Author"); query.AddProperties("FirstName", "LastName"); query.Include("book", "author"); query.Paging = new Paging(1, 10); var res = repository.Search(query); Assert.AreEqual(10, res.Count()); int idx = 0; foreach (var r in res) { Assert.AreEqual("Fname" + idx, r.GetData<string>("firstname")); var books = r.GetManyRelations("book", "author"); int bidx = 0; foreach (var b in books) { Assert.AreEqual(string.Format("Book_{0}_{1}", idx, bidx++), b.Entity.GetData<string>("title")); } idx++; } //assert second page query.Paging.Page++; res = repository.Search(query); Assert.AreEqual(10, res.Count()); foreach (var r in res) { Assert.AreEqual("Fname" + idx, r.GetData<string>("firstname")); var books = r.GetManyRelations("book", "author"); int bidx = 0; foreach (var b in books) { Assert.AreEqual(string.Format("Book_{0}_{1}", idx, bidx++), b.Entity.GetData<string>("title")); } idx++; } } }
public void Test_EntityRepo_Delete() { var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); bool ex = false; using (var ctx = dbService.GetDatabaseContext(true)) { #region prepare data var jordan = new Author() { FirstName = "Robert", LastName = "Jordan", IsAlive = false, Born = new DateTime(1948, 10, 17), Rating = 10.0m }; var feist = new Author() { FirstName = "Raymond", LastName = "Feist", IsAlive = true, Born = new DateTime(1963, 2, 14), Rating = 6.7m }; var fb1 = new Book() { Title = "The Apprentice", Price = 19.90m }; var fb2 = new Book() { Title = "The Magician", Price = 17.10m }; //var jb1 = new Book() //{ // Title = "The Wheel of Time", // Price = 21.0m //}; repository.Create(jordan); repository.Create(feist); repository.Create(fb1); repository.Create(fb2); //repository.Create(jb1); repository.Attach(feist, new Relation("author", fb1)); repository.Attach(feist, new Relation("author", fb2)); //repository.Attach(jordan, new Relation("author", jb1)); #endregion var r1 = new EntityQuery2("author", jordan.Id); Assert.IsNotNull(repository.Read(r1)); repository.Delete(jordan); Assert.IsNull(repository.Read(r1)); var r2 = new EntityQuery2("author", feist.Id); Assert.IsNotNull(repository.Read(r2)); try { repository.Delete(feist); } catch (Exception) { ex = true; } } Assert.IsTrue(ex, "Exception not thrown when deleting author with attached relations"); }
public void Test_EntityRepo_CreateSequences() { var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); using (var ctx = dbService.GetDatabaseContext(true)) { var feist = new Author() { FirstName = "Raymond", LastName = "Feist", Born = new DateTime(1963, 2, 14), Rating = 6.7m }; var fb1 = new Book() { Title = "The Apprentice", Price = 19.90m }; var fb2 = new Book() { Title = "The Magician", Price = 17.10m, Genre = Genre.Fantasy }; repository.Create(feist); repository.Create(fb1); repository.Create(fb2); repository.Attach(feist, new Relation("author", fb1)); repository.Attach(feist, new Relation("author", fb2)); var order = new Entity("Order"); order.SetData<decimal>("total", fb1.Price + fb2.Price); order.SetData<DateTime>("createdon", DateTime.Now); repository.Create(order); repository.Attach(order, new Relation("ordered", fb1)); repository.Attach(order, new Relation("ordered", fb2)); var q = new EntityQuery2("order", order.Id); q.AllProperties = true; var saved = repository.Read(q); Assert.AreEqual(string.Format("BO-1-{0}", DateTime.Now.Year), saved.GetData<string>("number")); var order2 = new Entity("Order"); order2.SetData<decimal>("total", fb1.Price); order2.SetData<DateTime>("createdon", DateTime.Now); repository.Create(order2); repository.Attach(order2, new Relation("ordered", fb1)); q = new EntityQuery2("order", order2.Id); q.AllProperties = true; saved = repository.Read(q); Assert.AreEqual(string.Format("BO-2-{0}", DateTime.Now.Year), saved.GetData<string>("number")); } }
public void Test_EntityRepo_Create() { var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); using (var ctx = dbService.GetDatabaseContext(true)) { #region prepare data var jordan = new Author() { FirstName = "Robert", LastName = "Jordan", IsAlive = false, Born = new DateTime(1948, 10, 17), Rating = 10.0m }; var feist = new Author() { FirstName = "Raymond", LastName = "Feist", Born = new DateTime(1963, 2, 14), Rating = 6.7m }; var fb1 = new Book() { Title = "The Apprentice", Price = 19.90m }; var fb2 = new Book() { Title = "The Magician", Price = 17.10m, Genre = Genre.Fantasy }; #endregion repository.Create(jordan); repository.Create(feist); repository.Create(fb1); repository.Create(fb2); repository.Attach(feist, new Relation("author", fb1)); repository.Attach(feist, new Relation("author", fb2)); fb1.Genre = Genre.Mistery; //default value feist.IsAlive = true;//default value var q = new EntityQuery2("author", feist.Id); q.AddProperties("FirstName", "lastname", "isalive", "born", "rating"); q.Include("book", "author"); var e = repository.Read(q); var created = new Author(e); Assert.AreEqual(feist.FirstName, created.FirstName); Assert.AreEqual(feist.LastName, created.LastName); Assert.AreEqual(feist.Born, created.Born); Assert.AreEqual(feist.IsAlive, created.IsAlive); Assert.AreEqual(feist.Rating, created.Rating); Assert.AreEqual(2, e.GetManyRelations("book", "author").Count());//repository.Detach(feist, new Relation("author", fb1)); var eb1 = e.GetManyRelations("book", "author").First(); Book b = new Book(eb1.Entity); Assert.AreEqual(fb1.Genre, b.Genre); } }