Пример #1
0
        public void TestActiveRecord()
        {
            var cmdFactory = new NIntegrate.Data.QueryCommandFactory();

            Farm farm = new Farm();

            farm.Attach(new ActiveRecordConnection <Farm>(cmdFactory));

            //test insert
            farm.FarmID          = 1000;
            farm.FarmAddress     = "Test Farm";
            farm.LoadBalancePath = "/TestPath/";
            Assert.IsTrue(farm.IsNew());
            Assert.AreEqual(true, farm.Save());
            Assert.IsFalse(farm.IsNew());

            //test find
            var loadFarm = farm.FindOne(farm.GetObjectId());

            Assert.AreEqual(farm.FarmID, loadFarm.FarmID);
            Assert.AreEqual(farm.FarmAddress, loadFarm.FarmAddress);
            Assert.AreEqual(farm.LoadBalancePath, loadFarm.LoadBalancePath);

            //test update
            farm.LoadBalancePath = "modified";
            farm.Save();

            var criteria = farm.GetObjectId().ToCriteria();

            farm.Save(Farm.Q.Update(Farm.Q.LoadBalancePath.Set("modified2")).Where(Farm.Q.Farm_id == farm.FarmID));

            //test find
            loadFarm = farm.FindOne(farm.GetObjectId());
            Assert.AreEqual(farm.FarmID, loadFarm.FarmID);
            Assert.AreEqual(farm.FarmAddress, loadFarm.FarmAddress);
            Assert.AreEqual("modified2", loadFarm.LoadBalancePath);

            farm.LoadBalancePath = "modified2";

            //test findMany
            var loadManyFarms = farm.FindMany(Farm.Q.Select().SortBy(Farm.Q.Farm_id, true));

            Assert.IsTrue(loadManyFarms.Count > 0);
            loadFarm = loadManyFarms.First(f => f.FarmID == farm.FarmID);
            Assert.AreEqual(farm.FarmAddress, loadFarm.FarmAddress);
            Assert.AreEqual(farm.LoadBalancePath, loadFarm.LoadBalancePath);

            var count = farm.Count(Farm.Q.Select().Where(Farm.Q.Farm_id > 0));

            Assert.IsTrue(count > 0);

            //test delete
            farm.Delete();
            Assert.IsTrue(farm.IsNew());
            loadFarm = farm.FindOne(farm.GetObjectId());
            Assert.IsNull(loadFarm);

            //test activerecord serialization
            var serializer = new DataContractSerializer(typeof(Farm));
            var stream     = new MemoryStream();

            serializer.WriteObject(stream, farm);
            stream.Seek(0, SeekOrigin.Begin);
            var deserializedFarm = (Farm)serializer.ReadObject(stream);

            Assert.AreEqual(farm.FarmID, deserializedFarm.FarmID);
            Assert.AreEqual(farm.FarmAddress, deserializedFarm.FarmAddress);
            Assert.AreEqual(farm.LoadBalancePath, deserializedFarm.LoadBalancePath);
            Assert.IsFalse(deserializedFarm.IsAttached());
            Assert.IsTrue(deserializedFarm.IsNew());

            //test save/delete after deserialization
            bool caughtExpectedException = false;

            try
            {
                deserializedFarm.Save();
            }
            catch (InvalidOperationException)
            {
                caughtExpectedException = true;
            }
            Assert.IsTrue(caughtExpectedException);
            caughtExpectedException = false;
            try
            {
                deserializedFarm.Delete();
            }
            catch (InvalidOperationException)
            {
                caughtExpectedException = true;
            }
            Assert.IsTrue(caughtExpectedException);
            deserializedFarm.Attach(new ActiveRecordConnection <Farm>(cmdFactory));
            Assert.IsTrue(deserializedFarm.Save());
            Assert.AreEqual(1, deserializedFarm.Delete(Farm.Q.Delete(Farm.Q.Farm_id == farm.FarmID)));

            //test ObjectId serialization
            var objectId = farm.GetObjectId();

            serializer = new DataContractSerializer(typeof(ObjectId <Farm, TestClasses.QueryClasses.Farm>));
            stream     = new MemoryStream();
            serializer.WriteObject(stream, objectId);
            stream.Seek(0, SeekOrigin.Begin);
            var deserializedObjectId = (ObjectId <Farm, TestClasses.QueryClasses.Farm>)serializer.ReadObject(stream);

            Assert.AreEqual(objectId.AutoGenerated, deserializedObjectId.AutoGenerated);
        }