Пример #1
0
        public void DeleteTest()
        {
            var session  = cache.StartSession();
            var penguin1 = new BasePenguin("DeleteTest1");

            penguin1 = session.Insert(penguin1);

            var penguin2 = new BasePenguin("DeleteTest1");

            penguin2.AddRelation(PenguinRelationshipDirection.OUT, "test", penguin1.ID.Value);
            penguin2 = session.Insert(penguin2);

            Assert.Equal(1, penguin2.Datastore.Relations["test"].Count);

            session.Delete(penguin1);

            Assert.Throws(typeof(PersistenceException), () => session.GetById(penguin1.ID.Value));
            var newPenguin2 = session.GetById(penguin2.ID.Value);

            Assert.Equal(0, newPenguin2.Datastore.Relations.Count);

            session.Commit();
            Assert.Throws(typeof(PersistenceException), () => driver.GetById(penguin1.ID.Value));
            var newPenguin3 = driver.GetById(penguin2.ID.Value);

            Assert.Equal(0, newPenguin3.Datastore.Relations.Count);
        }
Пример #2
0
        public void InsertTest2()
        {
            var relatedPenguins = new List <BasePenguin> {
                new BasePenguin("in"), new BasePenguin("in"), new BasePenguin("out")
            };

            this.driver.RunTransaction(context =>
            {
                relatedPenguins = context.Insert(relatedPenguins);
            });

            Assert.True(relatedPenguins.All(i => i.ID.HasValue));

            var penguin = new BasePenguin("obj");

            penguin.AddRelation(PenguinRelationshipDirection.IN, "rel_in", relatedPenguins[0].ID.Value);
            penguin.AddRelation(PenguinRelationshipDirection.IN, "rel_in", relatedPenguins[1].ID.Value);
            penguin.AddRelation(PenguinRelationshipDirection.OUT, "rel_out", relatedPenguins[2].ID.Value);
            BasePenguin insertedPenguin = null;

            this.driver.RunTransaction(context =>
            {
                insertedPenguin = context.Insert(penguin);
            });

            Assert.True(insertedPenguin.ID.HasValue);
            Assert.True(insertedPenguin.Datastore.Relations.ContainsKey("rel_in") && insertedPenguin.Datastore.Relations["rel_in"].Count == 2);
            Assert.True(insertedPenguin.Datastore.Relations.ContainsKey("rel_out") && insertedPenguin.Datastore.Relations["rel_out"].Count == 1);
            Assert.Equal(new[] { relatedPenguins[0], relatedPenguins[1] }.Select(i => i.ID.Value).OrderBy(i => i).ToList(), insertedPenguin.Datastore.Relations["rel_in"].Select(i => i.Target.ID.Value).OrderBy(i => i).ToList());
            Assert.True(insertedPenguin.Datastore.Relations["rel_out"].First().Target.ID.Value == relatedPenguins[2].ID.Value);

            var getPenguin = driver.GetById(insertedPenguin.ID.Value);

            Assert.True(getPenguin.Datastore.Relations.ContainsKey("rel_in") && getPenguin.Datastore.Relations["rel_in"].Count == 2);
            Assert.True(getPenguin.Datastore.Relations.ContainsKey("rel_out") && getPenguin.Datastore.Relations["rel_out"].Count == 1);
            Assert.Equal(new[] { relatedPenguins[0], relatedPenguins[1] }.Select(i => i.ID.Value).OrderBy(i => i).ToList(), getPenguin.Datastore.Relations["rel_in"].Select(i => i.Target.ID.Value).OrderBy(i => i).ToList());
            Assert.True(getPenguin.Datastore.Relations["rel_out"].First().Target.ID.Value == relatedPenguins[2].ID.Value);

            relatedPenguins = driver.GetById(relatedPenguins.Select(i => i.ID.Value).ToList());
            Assert.True(relatedPenguins[0].Datastore.Relations.ContainsKey("rel_in") && relatedPenguins[0].Datastore.Relations["rel_in"].First().Target.ID.Value == insertedPenguin.ID.Value);
            Assert.Equal(PenguinRelationshipDirection.OUT, relatedPenguins[0].Datastore.Relations["rel_in"].First().Direction);
            Assert.True(relatedPenguins[1].Datastore.Relations.ContainsKey("rel_in") && relatedPenguins[1].Datastore.Relations["rel_in"].First().Target.ID.Value == insertedPenguin.ID.Value);
            Assert.Equal(PenguinRelationshipDirection.OUT, relatedPenguins[1].Datastore.Relations["rel_in"].First().Direction);
            Assert.True(relatedPenguins[2].Datastore.Relations.ContainsKey("rel_out") && relatedPenguins[2].Datastore.Relations["rel_out"].First().Target.ID.Value == insertedPenguin.ID.Value);
            Assert.Equal(PenguinRelationshipDirection.IN, relatedPenguins[2].Datastore.Relations["rel_out"].First().Direction);
        }