A Wrapper for a reference to another object
Пример #1
1
 public static DBRef FromDocument(Document doc)
 {
     if(IsDocumentDBRef(doc) == false) throw new ArgumentException("Document is not a DBRef");
     DBRef ret = new DBRef();
     ret.CollectionName = (String)doc["$ref"];
     ret.Id = (String)doc["$id"];
     return ret;
 }
Пример #2
0
 public void TestCastsToDocument()
 {
     OidGenerator ogen = new OidGenerator();
     DBRef dref = new DBRef("tests.dbrefs", ogen.Generate());
     Document doc = (Document)dref;
     Assert.AreEqual(dref.CollectionName, doc[DBRef.RefName]);
 }
Пример #3
0
 public void DBRefConstructorTest()
 {
     IDBCollection collection = Mongo.DefaultDatabase["ref"];
     object id = Oid.NewOid();
     DBRef target = new DBRef(collection, id);
     target.ID.Should().Be(id);
     target.Collection.Should().BeEquivalentTo(collection);
 }
Пример #4
0
        public void TestCastsToDocument()
        {
            OidGenerator ogen = new OidGenerator();
            DBRef        dref = new DBRef("tests.dbrefs", ogen.Generate());
            Document     doc  = (Document)dref;

            Assert.AreEqual(dref.CollectionName, doc[DBRef.RefName]);
        }
Пример #5
0
        public void TestFollowNonReference()
        {
            Oid id = new Oid("BAD067c30a57000000008ecb");
            DBRef rf = new DBRef("refs", id);

            Document target = DB.FollowReference(rf);
            Assert.IsNull(target, "FollowReference returned wasn't null");
        }
Пример #6
0
        public void TestFromDocument()
        {
            String colname = "tests";
            String id = "32312312";
            Document doc = new Document().Append(DBRef.RefName,colname).Append(DBRef.IdName,id);

            DBRef expected = new DBRef(colname, id);
            Assert.AreEqual(expected, DBRef.FromDocument(doc));
        }
Пример #7
0
 public override bool Equals(object obj)
 {
     if (obj is DBRef)
     {
         DBRef comp = (DBRef)obj;
         return(comp.Id.Equals(this.Id) && comp.CollectionName.Equals(this.CollectionName));
     }
     return(base.Equals(obj));
 }
Пример #8
0
        public void TestEqualsUsingSameValues()
        {
            String colname = "tests";
            String id      = "32312312";
            DBRef  r       = new DBRef(colname, id);
            DBRef  r2      = new DBRef(colname, id);

            Assert.AreEqual(r, r2);
        }
Пример #9
0
        public void TestEqualsUsingSameValues()
        {
            String colname = "tests";
            String id = "32312312";
            DBRef r = new DBRef(colname,id);
            DBRef r2 = new DBRef(colname, id);

            Assert.AreEqual(r,r2);
        }
Пример #10
0
        /// <summary>
        /// Gets the document that a reference is pointing to.
        /// </summary>
        public Document FollowReference(DBRef reference)
        {
            if (reference == null)
            {
                throw new ArgumentNullException("reference cannot be null");
            }
            Document query = new Document().Append("_id", reference.Id);

            return(this[reference.CollectionName].FindOne(query));
        }
Пример #11
0
        public void TestFromDocument()
        {
            String   colname = "tests";
            String   id      = "32312312";
            Document doc     = new Document().Append(DBRef.RefName, colname).Append(DBRef.IdName, id);

            DBRef expected = new DBRef(colname, id);

            Assert.AreEqual(expected, DBRef.FromDocument(doc));
        }
Пример #12
0
        public void TestFollowNonReference()
        {
            Database tests = mongo["tests"];
            Oid      id    = new Oid("BAD067c30a57000000008ecb");
            DBRef    rf    = new DBRef("reads", id);

            Document target = tests.FollowReference(rf);

            Assert.IsNull(target, "FollowReference returned wasn't null");
        }
Пример #13
0
        public void TestFromIncompleteDocumentThrowsArguementException()
        {
            bool thrown = false;

            try {
                DBRef.FromDocument(new Document().Append(DBRef.RefName, "tests"));
            } catch (ArgumentException) {
                thrown = true;
            }
            Assert.IsTrue(thrown, "ArgumentException should have been thrown when trying to create convert from incomplete document");
        }
Пример #14
0
        public void TestFollowReference()
        {
            Database tests = mongo["tests"];
            Oid id = new Oid("4a7067c30a57000000008ecb");
            DBRef rf = new DBRef("reads", id);

            Document target = tests.FollowReference(rf);
            Assert.IsNotNull(target, "FollowReference returned null");
            Assert.IsTrue(target.Contains("j"));
            Assert.AreEqual((double)9980, (double)target["j"]);
        }
Пример #15
0
        public void TestFollowReference()
        {
            Database tests = mongo["tests"];
            Oid      id    = new Oid("4a7067c30a57000000008ecb");
            DBRef    rf    = new DBRef("reads", id);

            Document target = tests.FollowReference(rf);

            Assert.IsNotNull(target, "FollowReference returned null");
            Assert.IsTrue(target.Contains("j"));
            Assert.AreEqual((double)9980, (double)target["j"]);
        }
Пример #16
0
        public void TestFollowReference()
        {
            IMongoCollection refs = DB["refs"];
            Oid id = new Oid("4a7067c30a57000000008ecb");
            string msg = "this has an oid key";
            Document doc = new Document(){{"_id", id},{"msg", msg}};
            refs.Insert(doc);

            DBRef rf = new DBRef("refs", id);

            Document target = DB.FollowReference(rf);
            Assert.IsNotNull(target, "FollowReference returned null");
            Assert.IsTrue(target.Contains("msg"));
            Assert.AreEqual(msg, target["msg"]);
        }
Пример #17
0
        public void TestReferenceNonOid()
        {
            Database tests = mongo["tests"];
            Collection refs = tests["refs"];

            Document doc = new Document().Append("_id",123).Append("msg", "this has a non oid key");
            refs.Insert(doc);

            DBRef rf = new DBRef("refs",123);

            Document recv = tests.FollowReference(rf);

            Assert.IsNotNull(recv);
            Assert.IsTrue(recv.Contains("msg"));
            Assert.AreEqual(recv["_id"], (long)123);
        }
Пример #18
0
        public void TestReferenceNonOid()
        {
            Database         tests = mongo["tests"];
            IMongoCollection refs  = tests["refs"];

            Document doc = new Document().Append("_id", 123).Append("msg", "this has a non oid key");

            refs.Insert(doc);

            DBRef rf = new DBRef("refs", 123);

            Document recv = tests.FollowReference(rf);

            Assert.IsNotNull(recv);
            Assert.IsTrue(recv.Contains("msg"));
            Assert.AreEqual(recv["_id"], (long)123);
        }
Пример #19
0
        public void TestIsDocumentDBRef()
        {
            Document doc = new Document();

            Assert.IsFalse(DBRef.IsDocumentDBRef(null));
            Assert.IsFalse(DBRef.IsDocumentDBRef(doc));

            doc[DBRef.RefName] = "tests";
            Assert.IsFalse(DBRef.IsDocumentDBRef(doc));

            doc.Remove(DBRef.RefName);
            doc[DBRef.IdName] = "12312131";
            Assert.IsFalse(DBRef.IsDocumentDBRef(doc));

            doc[DBRef.RefName] = "tests";
            Assert.IsTrue(DBRef.IsDocumentDBRef(doc));

            doc[DBRef.MetaName] = new Document();
            Assert.IsTrue(DBRef.IsDocumentDBRef(doc));
        }
Пример #20
0
 public void TestEqualsAreSameObject()
 {
     DBRef r = new DBRef("tests","2312314");
     Assert.AreEqual(r,r);
 }
Пример #21
0
 private void Write(DBRef dbref)
 {
     Write(dbref.FullName, true);
       Write(dbref.Id);
 }
Пример #22
0
 /// <summary>
 /// Gets the document that a reference is pointing to.
 /// </summary>
 public Document FollowReference(DBRef reference)
 {
     if(reference == null)
         throw new ArgumentNullException("reference", "cannot be null");
     Document query = new Document().Append("_id", reference.Id);
     return this[reference.CollectionName].FindOne(query);
 }
Пример #23
0
        void element_object(string name, DBRef reference)
        {
            element_type(TypeByte.OBJECT);
            element_name(name);
            long sizePos = BaseStream.Position;
            Write(0);

            element_string("$ref", reference.Collection.Name);
            element("$id", reference.ID);

            Write((byte)TypeByte.EOO);
            RewindAndWriteSize(sizePos);
        }
Пример #24
0
 protected DBRef ToDBRef()
 {
     DBRef rf = new DBRef();
     rf.CollectionName = (String)this["$ref"].Val.ToNative();
     rf.Id = this["$id"].Val.ToNative();
     return rf;
 }
Пример #25
0
 public static BsonDocument From(DBRef val)
 {
     BsonDocument ret = new BsonDocument();
     ret.Add(new BsonElement("$ref", new BsonString(val.CollectionName)));
     ret.Add(new BsonElement("$id", From(val.Id)));
     return ret;
 }
Пример #26
0
        public void TestEqualsAreSameObject()
        {
            DBRef r = new DBRef("tests", "2312314");

            Assert.AreEqual(r, r);
        }
Пример #27
0
        public void FetchTest()
        {
            IDBCollection c = Mongo.DefaultDatabase.GetCollection("test");
            c.Drop();

            Document obj = new Document() { { "test", "me" } };
            c.Save(obj);

            DBRef r = new DBRef(c, obj.ID);
            IDocument deref = r.Fetch();

            deref.Should().NotBeNull();
            deref.ID.Should().Be(obj.ID);
        }
Пример #28
-1
        public void TestFollowNonReference()
        {
            Database tests = mongo["tests"];
            Oid id = new Oid("BAD067c30a57000000008ecb");
            DBRef rf = new DBRef("reads", id);

            Document target = tests.FollowReference(rf);
            Assert.IsNull(target, "FollowReference returned wasn't null");
        }