public void Test_json_property_conversion_to_int() { var today = DateTime.Today; var now = DateTime.Now; var testObj = new AllKindsOfProperties { ValueDate = today, LastUpdate = now, Nominal = 156.32, Quantity = 35, InstrumentName = "IRS", AreYouSure = Fuzzy.Maybe, IsDeleted = true }; var json = JsonConvert.SerializeObject(testObj); var jo = JObject.Parse(json); var valueDate = jo.Property("ValueDate"); var lval = CachedObject.JTokenToLong(valueDate); Assert.AreEqual(today.Ticks, lval); lval = CachedObject.JTokenToLong(jo.Property("LastUpdate")); Assert.AreEqual(now.Ticks, lval); lval = CachedObject.JTokenToLong(jo.Property("Nominal")); Assert.AreEqual(1563200, lval); lval = CachedObject.JTokenToLong(jo.Property("Quantity")); Assert.AreEqual(35, lval); lval = CachedObject.JTokenToLong(jo.Property("AreYouSure")); Assert.AreEqual(2, lval); // check that the reasdonly property is serialized because it is an index lval = CachedObject.JTokenToLong(jo.Property("Again")); Assert.AreEqual(2, lval); lval = CachedObject.JTokenToLong(jo.Property("IsDeleted")); Assert.AreEqual(1, lval); }
public void Packing_a_binary_object_and_its_json_should_give_identical_results() { var today = DateTime.Today; var now = DateTime.Now; var testObj = new AllKindsOfProperties { Id = 15, ValueDate = today, LastUpdate = now, Nominal = 156.32, Quantity = 35, InstrumentName = "IRS", AreYouSure = Fuzzy.Maybe, IsDeleted = true, Tags = { "news", "science", "space" }, Languages = { "en", "de", "fr" } }; var description = ClientSideTypeDescription.RegisterType <AllKindsOfProperties>(); var typeDescription = description.AsTypeDescription; var packed1 = CachedObject.Pack(testObj); var json = SerializationHelper.ObjectToJson(testObj, typeDescription); var packed2 = CachedObject.PackJson(json, typeDescription); Console.WriteLine(packed1); Console.WriteLine(packed2); Assert.AreEqual(packed1, packed2); // only checks the primary key Assert.AreEqual(packed1.FullTypeName, packed2.FullTypeName); CollectionAssert.AreEqual(packed1.UniqueKeys, packed2.UniqueKeys); CollectionAssert.AreEqual(packed1.IndexKeys, packed2.IndexKeys); CollectionAssert.AreEqual(packed1.ListIndexKeys, packed2.ListIndexKeys); CollectionAssert.AreEqual(packed1.ObjectData, packed2.ObjectData); }
public void Serialize_a_subset_of_properties_as_json() { var date = DateTimeOffset.Now; if (date.DayOfWeek == default) // avoid default values that are ignored in the full json { date = date.AddDays(1); } var testObj = new Order { Amount = 66.5, Date = date, Category = "student", ClientId = 101, ProductId = 405, Id = Guid.NewGuid(), Quantity = 1, IsDelivered = true }; var schema = TypedSchemaFactory.FromType <Order>(); var packed = PackedObject.Pack(testObj, schema); var jsonFull = packed.Json; var data1 = packed.GetData(schema.IndexesOfNames("Amount", "Category")); var json1 = new StreamReader(new MemoryStream(data1)).ReadToEnd(); var partial1 = SerializationHelper.ObjectFromBytes <Order>(data1, SerializationMode.Json, schema.UseCompression); Assert.AreEqual(testObj.Amount, partial1.Amount); Assert.AreEqual(testObj.Category, partial1.Category); var data2 = packed.GetServerSideData(); var json = new StreamReader(new MemoryStream(data2)).ReadToEnd(); // they are equal except for the $type property which is present only in the full json Assert.IsTrue(CompareJson(json, jsonFull)); schema = TypedSchemaFactory.FromType(typeof(AllKindsOfProperties)); var today = DateTime.Today; var now = DateTime.Now; var testObj1 = new AllKindsOfProperties { Id = 15, ValueDate = today, LastUpdate = now, Nominal = 156.32, Quantity = 35, InstrumentName = "IRS", AnotherDate = now, AreYouSure = AllKindsOfProperties.Fuzzy.Maybe, IsDeleted = true, Tags = { "news", "science", "space", "διξ" }, Languages = { "en", "de", "fr" } }; packed = PackedObject.Pack(testObj1, schema); jsonFull = packed.Json; data2 = packed.GetServerSideData(); json = new StreamReader(new MemoryStream(data2)).ReadToEnd(); // they are equal except for the $type property which is present only in the full json Assert.IsTrue(CompareJson(json, jsonFull)); }
public void Atomic_query_match() { var item = new AllKindsOfProperties { Id = 13, Quantity = 2, AreYouSure = AllKindsOfProperties.Fuzzy.Maybe, IsDeleted = false, AnotherDate = DateTimeOffset.Now, InstrumentName = "Swap", Languages = { "fr", "en" }, LastUpdate = DateTime.Now, Nominal = 125.23 }; var schema = TypedSchemaFactory.FromType <AllKindsOfProperties>(); var packed = PackedObject.Pack(item, schema); // scalar operators first var q1 = MakeQuery(schema, "Id", QueryOperator.Eq, 13); Assert.IsTrue(q1.Match(packed)); q1 = MakeQuery(schema, "Id", QueryOperator.Eq, 14); Assert.IsFalse(q1.Match(packed)); q1 = MakeQuery(schema, "Id", QueryOperator.Lt, 13); Assert.IsFalse(q1.Match(packed)); q1 = MakeQuery(schema, "Id", QueryOperator.Le, 13); Assert.IsTrue(q1.Match(packed)); q1 = MakeQuery(schema, "AreYouSure", QueryOperator.Eq, AllKindsOfProperties.Fuzzy.Maybe); Assert.IsTrue(q1.Match(packed)); q1 = MakeQuery(schema, "AreYouSure", QueryOperator.NotEq, AllKindsOfProperties.Fuzzy.No); Assert.IsTrue(q1.Match(packed)); q1 = MakeQuery(schema, "InstrumentName", QueryOperator.StrStartsWith, "Sw"); Assert.IsTrue(q1.Match(packed)); q1 = MakeQuery(schema, "InstrumentName", QueryOperator.StrContains, "Swap"); Assert.IsTrue(q1.Match(packed)); q1 = MakeQuery(schema, "InstrumentName", QueryOperator.StrEndsWith, "Swap"); Assert.IsTrue(q1.Match(packed)); q1 = MakeQuery(schema, "InstrumentName", QueryOperator.Eq, "Swap"); Assert.IsTrue(q1.Match(packed)); q1 = MakeQuery(schema, "IsDeleted", QueryOperator.Eq, false); Assert.IsTrue(q1.Match(packed)); q1 = MakeQuery(schema, "IsDeleted", QueryOperator.Lt, true); Assert.IsTrue(q1.Match(packed)); // IN // vector operators on scalar field q1 = MakeInQuery(schema, "InstrumentName", "Swap", "Flute"); Assert.IsTrue(q1.Match(packed)); q1 = MakeInQuery(schema, "InstrumentName", "Piano", "Flute"); Assert.IsFalse(q1.Match(packed)); // NOT IN q1 = MakeNinQuery(schema, "InstrumentName", "Swap", "Flute"); Assert.IsFalse(q1.Match(packed)); q1 = MakeNinQuery(schema, "InstrumentName", "Piano", "Flute"); Assert.IsTrue(q1.Match(packed)); }