Пример #1
0
        public void MapInterfaces_Test()
        {
            var mapper = new BsonMapper();

            var c1 = new MyClassWithInterface { Id = 1, Impl = new MyClassImpl { Name = "John Doe" } };
            var c2 = new MyClassWithObject { Id = 1, Impl = new MyClassImpl { Name = "John Doe" } };
            var c3 = new MyClassWithClassName { Id = 1, Impl = new MyClassImpl { Name = "John Doe" } };

            var bson1 = mapper.ToDocument(c1); // add _type in Impl property
            var bson2 = mapper.ToDocument(c2); // add _type in Impl property
            var bson3 = mapper.ToDocument(c3); // do not add _type in Impl property

            //string dllName = this.GetType().Assembly.GetName().Name;
            //
            //Assert.AreEqual("LiteDB.Tests.MapperInterfaceTest+MyClassImpl, " + dllName, bson1["Impl"].AsDocument["_type"].AsString);
            //Assert.AreEqual("LiteDB.Tests.MapperInterfaceTest+MyClassImpl, " + dllName, bson2["Impl"].AsDocument["_type"].AsString);
            //Assert.AreEqual(false, bson3["Impl"].AsDocument.ContainsKey("_type"));

            var k1 = mapper.ToObject<MyClassWithInterface>(bson1);
            var k2 = mapper.ToObject<MyClassWithObject>(bson2);
            var k3 = mapper.ToObject<MyClassWithClassName>(bson3);

            Assert.AreEqual(c1.Impl.Name, k1.Impl.Name);
            Assert.AreEqual((c2.Impl as MyClassImpl).Name, (k2.Impl as MyClassImpl).Name);
            Assert.AreEqual(c3.Impl.Name, k3.Impl.Name);
        }
Пример #2
0
        public void MapInterfaces_Test()
        {
            var mapper = new BsonMapper();

            var c1 = new MyClassWithInterface {
                Id = 1, Impl = new MyClassImpl {
                    Name = "John Doe"
                }
            };
            var c2 = new MyClassWithObject {
                Id = 1, Impl = new MyClassImpl {
                    Name = "John Doe"
                }
            };
            var c3 = new MyClassWithClassName {
                Id = 1, Impl = new MyClassImpl {
                    Name = "John Doe"
                }
            };

            var bson1 = mapper.ToDocument(c1); // add _type in Impl property
            var bson2 = mapper.ToDocument(c2); // add _type in Impl property
            var bson3 = mapper.ToDocument(c3); // do not add _type in Impl property

            string dllName = this.GetType().GetTypeInfo().Assembly.GetName().Name;

            Assert.AreEqual("LiteDB.Tests.MapperInterfaceTest+MyClassImpl, " + dllName, bson1["Impl"].AsDocument["_type"].AsString);
            Assert.AreEqual("LiteDB.Tests.MapperInterfaceTest+MyClassImpl, " + dllName, bson2["Impl"].AsDocument["_type"].AsString);
            Assert.AreEqual(false, bson3["Impl"].AsDocument.ContainsKey("_type"));

            var k1 = mapper.ToObject <MyClassWithInterface>(bson1);
            var k2 = mapper.ToObject <MyClassWithObject>(bson2);
            var k3 = mapper.ToObject <MyClassWithClassName>(bson3);

            Assert.AreEqual(c1.Impl.Name, k1.Impl.Name);
            Assert.AreEqual((c2.Impl as MyClassImpl).Name, (k2.Impl as MyClassImpl).Name);
            Assert.AreEqual(c3.Impl.Name, k3.Impl.Name);
        }
Пример #3
0
        public void ThreadedMappingShouldNotCauseConstructorException()
        {
            // arrange
            var exceptionOccured = false;

            var rand   = new Random();
            var mapper = new BsonMapper();
            var list   = new List <BsonDocument>();

            for (var r = 0; r < rand.Next(10); r++)
            {
                var obj = new MyClassWithInterface {
                    Id = r, Impl = new MyClassImpl {
                        Name = $"Name_{r}"
                    }
                };
                list.Add(mapper.ToDocument(obj));
            }

            // act
            Parallel.ForEach(list, t =>
            {
                try
                {
                    mapper.ToObject <MyClassWithInterface>(t);
                }
                catch (LiteException exception)
                {
                    if (exception.ErrorCode == 202)
                    {
                        exceptionOccured = true;
                    }
                }
            });

            // assert
            Assert.IsFalse(exceptionOccured);
        }