public void FromCompatibleWire() { var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Employee)); ddict.AddDatonUsingClassAnnotation(typeof(EmployeeList)); ddict.AddDatonUsingClassAnnotation(typeof(Ogre)); string json = "{'key':'Employee|=9','version':'v2','employee':[{'empId':9,'firstName':'Jill'}]}".Replace('\'', '"'); var jobj = JsonConvert.DeserializeObject <JObject>(json); var jill0 = Retrovert.FromCompatibleWireFull(ddict, jobj); Assert.IsTrue(jill0 is Employee); var jill = jill0 as Employee; Assert.AreEqual(new PersistonKey("Employee", "9", false), jill.Key); Assert.AreEqual("v2", jill.Version); Assert.AreEqual(9, jill.EmpId); Assert.AreEqual("Jill", jill.FirstName); json = "{'key':'EmployeeList','version':'v2','employee':[{'empId':9,'firstName':'Jill','lastName':null,'supervisorId':0,'supervisorLastName':null}]}".Replace('\'', '"'); jobj = JsonConvert.DeserializeObject <JObject>(json); var elist0 = Retrovert.FromCompatibleWireFull(ddict, jobj); Assert.IsTrue(elist0 is EmployeeList); var elist = elist0 as EmployeeList; Assert.AreEqual(new ViewonKey("EmployeeList"), elist.Key); Assert.AreEqual("v2", elist.Version); Assert.AreEqual(9, elist.Employee[0].EmpId); Assert.AreEqual("Jill", elist.Employee[0].FirstName); Assert.IsNull(elist.Employee[0].LastName); }
public void Inheritance() { var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Employee)); ddict.AddDatonUsingClassAnnotation(typeof(EmployeeList)); ddict.FinalizeInheritance(); var viewondef = ddict.DatonDefs["EmployeeList"]; var firstnamedef = viewondef.MainTableDef.Cols.FirstOrDefault(c => c.Name == "FirstName"); Assert.AreEqual(3, firstnamedef.MinLength); //this is inherited }
public void Clone() { var emily = new Ogre() { Name = "Emily", Key = new PersistonKey("Ogre", "3", false), Money = 2, OgreId = 3, PaymentMethod = new List <Ogre.PaymentMethodRow> { new Ogre.PaymentMethodRow { Method = "THUMP", Notes = "taking stuff without money" } } }; var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Ogre)); var ogredef = ddict.DatonDefs["Ogre"]; var clone = emily.Clone(ogredef) as Ogre; Assert.AreEqual("Emily", clone.Name); Assert.AreEqual(1, clone.PaymentMethod.Count); Assert.AreEqual("THUMP", clone.PaymentMethod[0].Method); }
public async Task ValidatePersiston() { var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Customer)); var datondef = ddict.DatonDefs["Customer"]; var cust = new Customer() { Company = "THE COMPANY", Money = -3 }; var validator = new Validator(new User()); await validator.ValidatePersiston(datondef, cust); Assert.AreEqual(2, validator.Errors.Count); //company name bad and money out of range cust.Money = (decimal)1.5; await validator.ValidatePersiston(datondef, cust); Assert.AreEqual(1, validator.Errors.Count); //money fixed cust.Company = "FIXED COMPANY"; await validator.ValidatePersiston(datondef, cust); Assert.AreEqual(0, validator.Errors.Count); //company name fixed }
public void View() { var xorg = new Ogre { Key = new PersistonKey("Ogre", DatonKey.NEWPK, false), Name = "Xorg", Money = 4, PaymentMethod = new List <Ogre.PaymentMethodRow> { new Ogre.PaymentMethodRow { Method = "credit", Notes = "usually declined" } } }; //bill can't view money var bill = new User(); bill.Roles = new[] { new RetroRole { BaseLevel = PermissionLevel.View, TableOverrides = new List <TablePermission> { new TablePermission { TableName = "Ogre", BaseLevel = PermissionLevel.View | PermissionLevel.Modify, ColumnOverrides = new List <ColumnPermission> { new ColumnPermission { ColumnName = "Money", BaseLevel = PermissionLevel.None } } }, new TablePermission { TableName = "PaymentMethod", Level = (usr, daton, tabname) => PermissionLevel.None } } } }; var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Ogre)); var guard = new SecurityGuard(ddict, bill); guard.HidePrivateParts(xorg); Assert.IsNotNull(xorg.Name); Assert.IsNull(xorg.Money); Assert.AreEqual(1, xorg.PaymentMethod.Count); Assert.IsNull(xorg.PaymentMethod[0].Method); Assert.IsNull(xorg.PaymentMethod[0].Notes); }
public void ViewonFromAnnotations() { var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(EmployeeList)); var viewondef = ddict.DatonDefs["EmployeeList"]; Assert.AreEqual(1, viewondef.CriteriaDef.Cols.Count); Assert.AreEqual("LastName", viewondef.CriteriaDef.Cols[0].Name); }
public void MultiRowPersistonFromAnnotations() { var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(ExtCustomer)); var customerdef = ddict.DatonDefs["ExtCustomer"]; Assert.AreEqual(typeof(ExtCustomer), customerdef.Type); Assert.AreEqual(typeof(ExtCustomer.ExtRow), customerdef.MainTableDef.RowType); Assert.IsNull(customerdef.MainTableDef.Children); Assert.AreEqual(1, customerdef.MainTableDef.Cols.Count); }
public void ToCompatibleWire() { var jill = new Employee { EmpId = 9, FirstName = "Jill", Key = new PersistonKey("Employee", "9", false), Version = "v2" }; var elist = new EmployeeList { Key = new ViewonKey("EmployeeList"), Version = "v2", Employee = new List <EmployeeList.TopRow> { new EmployeeList.TopRow { EmpId = 9, FirstName = "Jill" //last name missing } } }; var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Employee)); ddict.AddDatonUsingClassAnnotation(typeof(EmployeeList)); string json = Retrovert.ToWire(ddict, jill, true); //expected has single quotes so the source code is more readable string expected = "{'key':'Employee|=9','version':'v2','employee':{'empId':9,'firstName':'Jill'}}"; Assert.AreEqual(expected.Replace('\'', '"'), json); json = Retrovert.ToWire(ddict, elist, true); //expected has single quotes so the source code is more readable expected = "{'key':'EmployeeList','version':'v2','employee':[{'empId':9,'firstName':'Jill','lastName':null,'supervisorId':0,'supervisorLastName':null}]}"; Assert.AreEqual(expected.Replace('\'', '"'), json); }
public void PersistonFromAnnotations() { var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Customer)); var customerdef = ddict.DatonDefs["Customer"]; Assert.AreEqual(typeof(Customer), customerdef.Type); Assert.AreEqual(typeof(Customer), customerdef.MainTableDef.RowType); Assert.AreEqual(6, customerdef.DatabaseNumber); Assert.AreEqual("CustomerId", customerdef.MainTableDef.PrimaryKeyColName); Assert.IsNull(customerdef.MainTableDef.Children); Assert.AreEqual(5, customerdef.MainTableDef.Cols.Count); }
public void FromDiff() { var emily = new Ogre() { Name = "Emily", Key = new PersistonKey("Ogre", "3", false), Version = "v2", Money = 2, OgreId = 3, PaymentMethod = new List <Ogre.PaymentMethodRow> { new Ogre.PaymentMethodRow { PaymentMethodId = 91, Method = "THUMP", Notes = "n1" }, new Ogre.PaymentMethodRow { PaymentMethodId = 92, Method = "BOP", Notes = null } } }; var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Ogre)); string json = "{'key':'Ogre|=9','version':'v2','ogre':[{'ogreId':9,'money':2.50,'paymentMethod-deleted':[{'paymentMethodId':92}],'paymentMethod-new':[{'method':'STOMP','notes':'n2'}]}]}".Replace('\'', '"'); var jobj = JsonConvert.DeserializeObject <JObject>(json); var emilyChanges = Retrovert.FromDiff(ddict, jobj); Assert.AreEqual(new PersistonKey("Ogre", "9", false), emilyChanges.Key); Assert.AreEqual("v2", emilyChanges.BasedOnVersion); Assert.AreEqual(9, emilyChanges.MainTable[0].Columns["OgreId"]); Assert.AreEqual((decimal)2.5, emilyChanges.MainTable[0].Columns["Money"]); emilyChanges.ApplyTo(ddict.DatonDefs["Ogre"], emily); Assert.AreEqual((decimal)2.5, emily.Money); Assert.IsFalse(emily.PaymentMethod.Any(m => m.Method == "BOP")); //deleted BOP Assert.IsTrue(emily.PaymentMethod.Any(m => m.Method == "STOMP")); //added STOMP Assert.IsTrue(emily.PaymentMethod.Any(m => m.Method == "THUMP")); //no change }
public async Task ValidateViewon() { var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(EmployeeList)); var datondef = ddict.DatonDefs["EmployeeList"]; var validator = new Validator(new User()); var viewonKey = new ViewonKey("EmployeeList", new[] { new ViewonKey.Criterion("LastName", "Wash") }); await validator.ValidateCriteria(datondef, viewonKey); Assert.AreEqual(0, validator.Errors.Count); //last name is ok viewonKey = new ViewonKey("EmployeeList", new[] { new ViewonKey.Criterion("LastName", "Washington") }); await validator.ValidateCriteria(datondef, viewonKey); Assert.AreEqual(1, validator.Errors.Count); //last name criteria is too long viewonKey = new ViewonKey("EmployeeList", new[] { new ViewonKey.Criterion("LastName", "THEM") }); await validator.ValidateCriteria(datondef, viewonKey); Assert.AreEqual(1, validator.Errors.Count); //last name is not valid by custom rule }
public void ComputedCol() { var thor = new Ogre() { Name = "Thor", Money = 2, PaymentMethod = new List <Ogre.PaymentMethodRow> { new Ogre.PaymentMethodRow { Method = "credit" } } }; var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Ogre)); var ogredef = ddict.DatonDefs["Ogre"]; thor.Recompute(ogredef); Assert.AreEqual("2 gold pieces", thor.FormattedMoney); Assert.AreEqual("Arrg, I'll pay with credit", thor.PaymentMethod[0].AngryMethod); }
public void Multilanguage() { var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Employee)); var datondef = ddict.DatonDefs["Employee"]; var firstnamedef = datondef.MainTableDef.FindCol("FirstName"); firstnamedef.SetPrompt("de", "ERSTE"); firstnamedef.SetPrompt("fr", "PREMIER"); Assert.AreEqual(3, firstnamedef.Prompt.Count); var birgitte = new User { LangCode = "de" }; var wiredict = Retrovert.DataDictionaryToWire(ddict, birgitte, null); Assert.AreEqual("ERSTE", wiredict.DatonDefs[0].MainTableDef.Cols.Single(c => c.Name == "firstName").Prompt); var jaques = new User { LangCode = "fr" }; wiredict = Retrovert.DataDictionaryToWire(ddict, jaques, null); Assert.AreEqual("PREMIER", wiredict.DatonDefs[0].MainTableDef.Cols.Single(c => c.Name == "firstName").Prompt); }
public void Update() { var xorg = new Ogre { Key = new PersistonKey("Ogre", DatonKey.NEWPK, false), Name = "Xorg", Money = 4, PaymentMethod = new List <Ogre.PaymentMethodRow> { new Ogre.PaymentMethodRow { Method = "credit", Notes = "usually declined" } } }; //bill can't update money var bill = new User(); bill.Roles = new[] { new RetroRole { BaseLevel = PermissionLevel.All, TableOverrides = new List <TablePermission> { new TablePermission { TableName = "Ogre", BaseLevel = PermissionLevel.View | PermissionLevel.Modify, ColumnOverrides = new List <ColumnPermission> { new ColumnPermission { ColumnName = "Money", BaseLevel = PermissionLevel.None } } }, new TablePermission { TableName = "PaymentMethod", Level = (usr, daton, tabname) => PermissionLevel.None } } } }; var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Ogre)); var ogredef = ddict.DatonDefs["Ogre"]; var paymentdef = ogredef.MainTableDef.Children[0]; var diff = new PersistonDiff(ogredef, xorg.Key, xorg.Version) { MainTable = new List <PersistonDiff.DiffRow> { new PersistonDiff.DiffRow { Kind = DiffKind.Other, Columns = new Dictionary <string, object> { { "Name", "Priscilla" }, //allowed { "Money", (decimal)5.49 } //disallowed }, ChildTables = new Dictionary <TableDef, List <PersistonDiff.DiffRow> > { { paymentdef, new List <PersistonDiff.DiffRow> { new PersistonDiff.DiffRow { Kind = DiffKind.Other, Columns = new Dictionary <string, object> { { "Method", "cash" }, //disallowed by function { "Notes", "cash is best" } } } } } } } } }; var guard = new SecurityGuard(ddict, bill); var errors = guard.GetDisallowedWrites(xorg, ogredef, diff).ToArray(); Assert.AreEqual(3, errors.Length); Assert.IsTrue(errors[0].Contains("Ogre.Money")); Assert.IsTrue(errors[1].Contains("PaymentMethod.Method")); Assert.IsTrue(errors[2].Contains("PaymentMethod.Notes")); }
public void Create() { var xorg = new Ogre { Key = new PersistonKey("Ogre", DatonKey.NEWPK, false), Name = "Xorg", Money = 4, PaymentMethod = new List <Ogre.PaymentMethodRow> { new Ogre.PaymentMethodRow { Method = "credit", Notes = "usually declined" } } }; //nancy can only create new payment rows, not update existing var nancy = new User(); nancy.Roles = new[] { new RetroRole { BaseLevel = PermissionLevel.None, TableOverrides = new List <TablePermission> { new TablePermission { TableName = "PaymentMethod", BaseLevel = PermissionLevel.Create } } } }; var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Ogre)); var ogredef = ddict.DatonDefs["Ogre"]; var paymentdef = ogredef.MainTableDef.Children[0]; var diff = new PersistonDiff(ogredef, xorg.Key, xorg.Version) { MainTable = new List <PersistonDiff.DiffRow> { new PersistonDiff.DiffRow { Kind = DiffKind.Other, Columns = new Dictionary <string, object> { { "Name", "Priscilla" } //disallowed }, ChildTables = new Dictionary <TableDef, List <PersistonDiff.DiffRow> > { { paymentdef, new List <PersistonDiff.DiffRow> { new PersistonDiff.DiffRow { Kind = DiffKind.Other, Columns = new Dictionary <string, object> { { "Notes", "disallowed" } //disallowed update } }, new PersistonDiff.DiffRow { Kind = DiffKind.NewRow, Columns = new Dictionary <string, object> { { "Method", "barter" } //allowed create row } } } } } } } }; var guard = new SecurityGuard(ddict, nancy); var errors = guard.GetDisallowedWrites(xorg, ogredef, diff).ToArray(); Assert.AreEqual(2, errors.Length); Assert.IsTrue(errors[0].Contains("Ogre.Name")); Assert.IsTrue(errors[1].Contains("PaymentMethod.Notes")); }
public void ToDenseWire() { var jill = new Employee { EmpId = 9, FirstName = "Jill", Key = new PersistonKey("Employee", "9", false), Version = "v2" }; var elist = new EmployeeList { Key = new ViewonKey("EmployeeList"), Version = "v2", Employee = new List <EmployeeList.TopRow> { new EmployeeList.TopRow { EmpId = 9, FirstName = "Jill" //last name missing } } }; var emily = new Ogre() { Name = "Emily", Key = new PersistonKey("Ogre", "3", false), Version = "v2", Money = 2, OgreId = 3, PaymentMethod = new List <Ogre.PaymentMethodRow> { new Ogre.PaymentMethodRow { Method = "THUMP", Notes = "n1" }, new Ogre.PaymentMethodRow { Method = "BOP", Notes = null } } }; var ddict = new DataDictionary(); ddict.AddDatonUsingClassAnnotation(typeof(Employee)); ddict.AddDatonUsingClassAnnotation(typeof(EmployeeList)); ddict.AddDatonUsingClassAnnotation(typeof(Ogre)); string json = Retrovert.ToWire(ddict, jill, false); //expected has single quotes so the source code is more readable string expected = "{'key':'Employee|=9','version':'v2','content':[[9,'Jill']]}"; Assert.AreEqual(expected.Replace('\'', '"'), json); json = Retrovert.ToWire(ddict, elist, false); //expected has single quotes so the source code is more readable expected = "{'key':'EmployeeList','version':'v2','content':[[9,'Jill',null,0,null]]}"; Assert.AreEqual(expected.Replace('\'', '"'), json); json = Retrovert.ToWire(ddict, emily, false); //expected has single quotes so the source code is more readable expected = "{'key':'Ogre|=3','version':'v2','content':[[3,'Emily',2,null,[[0,'THUMP','n1',null],[0,'BOP',null,null]]]]}"; Assert.AreEqual(expected.Replace('\'', '"'), json); }