public void SavesUnsupportedTypes()
        {
            UnsupportedTypes item = null;

            var serializer = new BlobSerializerDelegate(
                obj =>
            {
                if (obj is DateTimeOffset)
                {
                    Assert.AreEqual(item.DateTimeOffset, obj);
                    var offset = (DateTimeOffset)obj;
                    var bytes  = new byte[16];
                    Buffer.BlockCopy(BitConverter.GetBytes(offset.Ticks), 0, bytes, 0, 8);
                    Buffer.BlockCopy(BitConverter.GetBytes(offset.Offset.Ticks), 0, bytes, 8, 8);
                    return(bytes);
                }

                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", obj.GetType()));
            },
                (d, t) =>
            {
                if (t == typeof(DateTimeOffset))
                {
                    var ticks  = BitConverter.ToInt64(d, 0);
                    var offset = BitConverter.ToInt64(d, 8);
                    return(new DateTimeOffset(ticks, TimeSpan.FromTicks(offset)));
                }

                if (t == typeof(TimeSpan))
                {
                    return(TimeSpan.FromTicks(BitConverter.ToInt64(d, 0)));
                }

                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", t));
            },
                t => true);

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <UnsupportedTypes>();
                item = new UnsupportedTypes()
                {
                    Id             = Guid.NewGuid(),
                    DateTimeOffset = DateTime.Now
                };

                db.Insert(item);
                var dbItem = db.Find <UnsupportedTypes>(item.Id);

                Assert.AreEqual(item.Id, dbItem.Id);
                Assert.AreEqual(item.DateTimeOffset, dbItem.DateTimeOffset);
            }
        }
Пример #2
0
        public void SavesUnsupportedTypes()
        {
            UnsupportedTypes item = null;

            var serializer = new BlobSerializerDelegate(
                obj =>
            {
                if (obj is DivideByZeroException)
                {
                    var e    = (DivideByZeroException)obj;
                    var json = JsonConvert.SerializeObject(e);            // subst your own serializer
                    return(Encoding.UTF8.GetBytes(json));
                }

                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", obj.GetType()));
            },
                (d, t) =>
            {
                if (t == typeof(DivideByZeroException))
                {
                    var json   = Encoding.UTF8.GetString(d);
                    var result = JsonConvert.DeserializeObject <DivideByZeroException>(json);
                    return(result);
                }

                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", t));
            },
                t => true);

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <UnsupportedTypes>();
                item = new UnsupportedTypes()
                {
                    Id                    = Guid.NewGuid(),
                    DateTimeOffset        = DateTime.Now,
                    DivideByZeroException = new DivideByZeroException("a message")
                };

                db.Insert(item);
                var dbItem = db.Find <UnsupportedTypes>(item.Id);

                Assert.AreEqual(item.Id, dbItem.Id);
                Assert.AreEqual(item.DateTimeOffset, dbItem.DateTimeOffset);
                //most db stores datetimeoffset as utc, forgetting the offset part. This one is not an exception.
                //TODO: test fails
                Assert.AreEqual(item.DateTimeOffset.Offset, dbItem.DateTimeOffset.Offset);
                //TODO: test fails
                Assert.AreEqual(item.DivideByZeroException.Message, dbItem.DivideByZeroException.Message);
            }
        }
        public void SavesUnsupportedTypes()
        {
            UnsupportedTypes item = null;

            var serializer = new BlobSerializerDelegate(
                obj =>
            {
                if (obj is DivideByZeroException)
                {
                    var e    = (DivideByZeroException)obj;
                    var json = JsonConvert.SerializeObject(e);            // subst your own serializer
                    return(Encoding.UTF8.GetBytes(json));
                }

                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", obj.GetType()));
            },
                (d, t) =>
            {
                if (t == typeof(DivideByZeroException))
                {
#if __WINRT__ || WINDOWS_PHONE
                    var json = Encoding.UTF8.GetString(d, 0, d.Length);
#else
                    var json = Encoding.UTF8.GetString(d);
#endif
                    var result = JsonConvert.DeserializeObject <DivideByZeroException>(json);
                    return(result);
                }

                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", t));
            },
                t => true);

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <UnsupportedTypes>();
                item = new UnsupportedTypes()
                {
                    Id                    = Guid.NewGuid(),
                    DateTimeOffset        = DateTime.Now,
                    DivideByZeroException = new DivideByZeroException("a message")
                };

                db.Insert(item);
                var dbItem = db.Find <UnsupportedTypes>(item.Id);

                Assert.AreEqual(item.Id, dbItem.Id);
                Assert.AreEqual(item.DateTimeOffset, dbItem.DateTimeOffset);
                Assert.AreEqual(item.DivideByZeroException.Message, dbItem.DivideByZeroException.Message);
            }
        }
Пример #4
0
        public void VerifyTableCreationSucceedsWithSupportedSerializer()
        {
            if (!this.Serializer.CanDeserialize(typeof(ComplexOrder)))
            {
                Assert.Ignore("Serialize does not support this data type");
            }

            using (var db = new BlobDatabase(this.Serializer))
            {
                db.CreateTable <ComplexOrder>();
                var mapping = db.GetMapping <ComplexOrder>();
                Assert.IsNotNull(mapping);
                Assert.AreEqual(4, mapping.Columns.Length);
            }
        }
Пример #5
0
        public void DoesNotCallOnSupportedTypes()
        {
            var types = new List <Type>();

            var serializer = new BlobSerializerDelegate(obj => null, (d, t) => null, t =>
            {
                throw new InvalidOperationException(string.Format("Type {0} should not be requested.", t));
            });

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <SupportedTypes>();
            }

            Assert.AreEqual(0, types.Count, "Types requested from serializer");
        }
Пример #6
0
        public void CallsOnUnsupportedTypes()
        {
            var types = new List <Type>();

            var serializer = new BlobSerializerDelegate(obj => null, (d, t) => null, t =>
            {
                types.Add(t);
                return(true);
            });

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <UnsupportedTypes>();
            }

            Assert.That(types, Has.Member(typeof(DivideByZeroException)));

            Assert.AreEqual(1, types.Count, "Too many types requested by serializer");
        }
        public void CallsOnUnsupportedTypes()
        {
            var types = new List <Type>();

            var serializer = new BlobSerializerDelegate(obj => null, (d, t) => null, t =>
            {
                types.Add(t);
                return(true);
            });

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <UnsupportedTypes>();
            }

            Assert.Contains(typeof(DateTimeOffset), types);

            Assert.AreEqual(1, types.Count, "Too many types requested by serializer");
        }
Пример #8
0
        public void CanDeserializeIsRequested()
        {
            var types = new List <Type>();

            var serializer = new BlobSerializerDelegate(obj => null, (d, t) => null, t =>
            {
                types.Add(t);
                return(true);
            });

            using (var db = new BlobDatabase(serializer))
            {
                db.CreateTable <ComplexOrder>();
            }

            Assert.That(types, Has.Member(typeof(List <ComplexHistory>)));
            Assert.That(types, Has.Member(typeof(List <ComplexLine>)));

            Assert.AreEqual(2, types.Count, "Too many types requested by serializer");
        }
Пример #9
0
        public void VerifyTableCreationFailsWithNoSerializer()
        {
            NotSupportedException ex = null;

            using (var db = new BlobDatabase(null))
            {
                try
                {
                    var count = db.CreateTable <ComplexOrder>();
                    Assert.IsTrue(count == 0);
                    Assert.IsNull(db.GetMapping <ComplexOrder>());
                    return;
                }
                catch (NotSupportedException e)
                {
                    ex = e;
                }
            }

            Assert.IsNotNull(ex);
        }
Пример #10
0
        //[Test]
        public void TestListOfObjects()
        {
            if (!this.Serializer.CanDeserialize(typeof(ComplexOrder)))
            {
                Assert.Ignore("Serialize does not support this data type");
            }

            using (var db = new BlobDatabase(this.Serializer))
            {
                db.CreateTable <ComplexOrder>();
                var order = new ComplexOrder();

                var count = db.Insert(order);
                Assert.AreEqual(count, 1);
                var orderCopy = db.Find <ComplexOrder>(order.Id);
                Assert.AreEqual(order, orderCopy);

                for (var n = 0; n < 10;)
                {
                    order.Lines.Add(new ComplexLine()
                    {
                        ProductId = 1, Quantity = ++n, Status = OrderLineStatus.Placed, UnitPrice = (n / 10m)
                    });
                    db.Update(order);
                    orderCopy = db.Find <ComplexOrder>(order.Id);
                    Assert.AreEqual(order, orderCopy);
                    order.History.Add(new ComplexHistory()
                    {
                        Time = DateTime.UtcNow, Comment = string.Format("New history {0}", n)
                    });
                    db.Update(order);
                    orderCopy = db.Find <ComplexOrder>(order.Id);
                    Assert.AreEqual(order, orderCopy);
                }
            }
        }