public void CanBeEqual() { var code1 = new CodeWScope("code", new Document("key", "value")); var code2 = new CodeWScope("code", new Document("key", "value")); Assert.AreEqual(code1, code2); }
/// <summary> /// Calculates the size. /// </summary> /// <param name = "codeScope">The code scope.</param> /// <returns></returns> public int CalculateSize(CodeWScope codeScope) { var size = 4; size += CalculateSize(codeScope.Value, true); size += CalculateSizeObject(codeScope.Scope); return(size); }
public void CanBeXmlSerializedWithNullValue() { var source = new CodeWScope(null,null); var serializer = new XmlSerializer(typeof(CodeWScope)); var writer = new StringWriter(); serializer.Serialize(writer, source); var dest = (CodeWScope)serializer.Deserialize(new StringReader(writer.ToString())); Assert.AreEqual(source, dest); }
public void CanBeXmlSerialized() { var source = new CodeWScope("code",new Document("key","value")); var serializer = new XmlSerializer(typeof(CodeWScope)); var writer = new StringWriter(); serializer.Serialize(writer, source); var dest = (CodeWScope)serializer.Deserialize(new StringReader(writer.ToString())); Assert.AreEqual(source, dest); }
public void CanBeXmlSerialized() { var source = new CodeWScope("code", new Document("key", "value")); var serializer = new XmlSerializer(typeof(CodeWScope)); var writer = new StringWriter(); serializer.Serialize(writer, source); var dest = (CodeWScope)serializer.Deserialize(new StringReader(writer.ToString())); Assert.AreEqual(source, dest); }
public void CanBeXmlSerializedWithNullValue() { var source = new CodeWScope(null, null); var serializer = new XmlSerializer(typeof(CodeWScope)); var writer = new StringWriter(); serializer.Serialize(writer, source); var dest = (CodeWScope)serializer.Deserialize(new StringReader(writer.ToString())); Assert.AreEqual(source, dest); }
public void CanBeBinarySerialized() { var source = new CodeWScope("code", new Document("key", "value")); var formatter = new BinaryFormatter(); var mem = new MemoryStream(); formatter.Serialize(mem, source); mem.Position = 0; var dest = (CodeWScope)formatter.Deserialize(mem); Assert.AreEqual(source, dest); }
public void TestFindWhereEquivalency() { var col = DB["finds"]; var lt = new Document().Add("j", new Document().Add("$lt", 5)); var where = "this.j < 5"; var explicitWhere = new Document().Add("$where", new Code(where)); var func = new CodeWScope("function() { return this.j < 5; }", new Document()); var funcDoc = new Document().Add("$where", func); Assert.AreEqual(4, CountDocs(col.Find(lt)), "Basic find didn't return 4 docs"); Assert.AreEqual(4, CountDocs(col.Find(where)), "String where didn't return 4 docs"); Assert.AreEqual(4, CountDocs(col.Find(explicitWhere)), "Explicit where didn't return 4 docs"); Assert.AreEqual(4, CountDocs(col.Find(funcDoc)), "Function where didn't return 4 docs"); }
public void TestFindWhereEquivalency() { var col = DB.GetCollection <FindsEntity>("finds"); var lt = new { Index = Op.LessThan(5) }; var where = "this.j < 5"; var explicitWhere = new Document().Add("$where", new Code(where)); var func = new CodeWScope("function() { return this.j < 5; }", new Document()); var funcDoc = new Document().Add("$where", func); Assert.AreEqual(4, col.Find(lt).Documents.Count(), "Basic find didn't return 4 docs"); Assert.AreEqual(4, col.Find(where).Documents.Count(), "String where didn't return 4 docs"); Assert.AreEqual(4, col.Find(explicitWhere).Documents.Count(), "Explicit where didn't return 4 docs"); Assert.AreEqual(4, col.Find(funcDoc).Documents.Count(), "Function where didn't return 4 docs"); }
public void CanBeConstructedWithValue() { var code = new CodeWScope("code"); Assert.AreEqual("code", code.Value); }
public void CanBeConstructoredWithNull() { var code = new CodeWScope(null); Assert.IsNull(code.Value); }
public int CalculateSize(Object val) { if (val == null) { return(0); } switch (TranslateToBsonType(val)) { case BsonDataType.MinKey: case BsonDataType.MaxKey: case BsonDataType.Null: return(0); case BsonDataType.Boolean: return(1); case BsonDataType.Integer: return(4); case BsonDataType.Long: case BsonDataType.Date: return(8); case BsonDataType.Oid: return(12); case BsonDataType.Number: return(sizeof(Double)); case BsonDataType.String: return(CalculateSize((string)val)); case BsonDataType.Obj: { Type t = val.GetType(); if (t == typeof(Document)) { return(CalculateSize((Document)val)); } else if (t == typeof(DBRef)) { return(CalculateSize((Document)((DBRef)val))); } else { throw new NotImplementedException(String.Format("Calculating size of {0} is not implemented yet.", t.Name)); } } case BsonDataType.Array: return(CalculateSize((IEnumerable)val)); case BsonDataType.Regex: { MongoRegex r = (MongoRegex)val; int size = CalculateSize(r.Expression, false); size += CalculateSize(r.Options, false); return(size); } case BsonDataType.Code: Code c = (Code)val; return(CalculateSize(c.Value, true)); case BsonDataType.CodeWScope: { CodeWScope cw = (CodeWScope)val; int size = 4; size += CalculateSize(cw.Value, true); size += CalculateSize(cw.Scope); return(size); } case BsonDataType.Binary: { if (val is Guid) { return(21); } else { Binary b = (Binary)val; int size = 4; //size int size += 1; //subtype if (b.Subtype == Binary.TypeCode.General) { size += 4; //embedded size int } size += b.Bytes.Length; return(size); } } default: throw new NotImplementedException(String.Format("Calculating size of {0} is not implemented.", val.GetType().Name)); } }
/// <summary> /// Writes the specified code scope. /// </summary> /// <param name = "codeScope">The code scope.</param> private void Write(CodeWScope codeScope) { _writer.Write(CalculateSize(codeScope)); WriteValue(BsonType.String, codeScope.Value); WriteValue(BsonType.Obj, codeScope.Scope); }
public void WriteValue(BsonDataType dt, Object obj) { switch (dt) { case BsonDataType.MinKey: case BsonDataType.MaxKey: case BsonDataType.Null: return; case BsonDataType.Boolean: writer.Write((bool)obj); return; case BsonDataType.Integer: writer.Write((int)obj); return; case BsonDataType.Long: writer.Write((long)obj); return; case BsonDataType.Date: DateTime d = (DateTime)obj; TimeSpan diff = d.ToUniversalTime() - epoch; double time = Math.Floor(diff.TotalMilliseconds); writer.Write((long)time); return; case BsonDataType.Oid: Oid id = (Oid)obj; writer.Write(id.Value); return; case BsonDataType.Number: writer.Write((double)obj); return; case BsonDataType.String: { String str = (String)obj; writer.Write(CalculateSize(str, false)); this.WriteString(str); return; } case BsonDataType.Obj: if (obj is Document) { this.Write((Document)obj); } else if (obj is DBRef) { this.Write((Document)((DBRef)obj)); } return; case BsonDataType.Array: this.WriteArray((IEnumerable)obj); return; case BsonDataType.Regex: { MongoRegex r = (MongoRegex)obj; this.WriteString(r.Expression); this.WriteString(r.Options); return; } case BsonDataType.Code: { Code c = (Code)obj; this.WriteValue(BsonDataType.String, c.Value); return; } case BsonDataType.CodeWScope: { CodeWScope cw = (CodeWScope)obj; writer.Write(CalculateSize(cw)); this.WriteValue(BsonDataType.String, cw.Value); this.WriteValue(BsonDataType.Obj, cw.Scope); return; } case BsonDataType.Binary: { if (obj is Guid) { writer.Write((int)16); writer.Write((byte)3); writer.Write(((Guid)obj).ToByteArray()); } else { Binary b = (Binary)obj; if (b.Subtype == Binary.TypeCode.General) { writer.Write(b.Bytes.Length + 4); writer.Write((byte)b.Subtype); writer.Write(b.Bytes.Length); } else { writer.Write(b.Bytes.Length); writer.Write((byte)b.Subtype); } writer.Write(b.Bytes); } return; } default: throw new NotImplementedException(String.Format("Writing {0} types not implemented.", obj.GetType().Name)); } }
public BsonCodeWScope(CodeWScope code) { this.Val = code.Value; this.Scope = BsonConvert.From(code.Scope); }
public object ToNative() { CodeWScope ret = new CodeWScope(); ret.Scope = (Document)this.Scope.ToNative(); ret.Value = this.Val; return ret; }
public void TestFindWhereEquivalency(){ var col = DB.GetCollection<FindsEntity>("finds"); var lt = new {Index = Op.LessThan(5)}; var where = "this.j < 5"; var explicitWhere = new Document().Add("$where", new Code(where)); var func = new CodeWScope("function() { return this.j < 5; }", new Document()); var funcDoc = new Document().Add("$where", func); Assert.AreEqual(4, col.Find(lt).Documents.Count(), "Basic find didn't return 4 docs"); Assert.AreEqual(4, col.Find(where).Documents.Count(), "String where didn't return 4 docs"); Assert.AreEqual(4, col.Find(explicitWhere).Documents.Count(), "Explicit where didn't return 4 docs"); Assert.AreEqual(4, col.Find(funcDoc).Documents.Count(), "Function where didn't return 4 docs"); }
/// <summary> /// Calculates the size. /// </summary> /// <param name = "codeScope">The code scope.</param> /// <returns></returns> public int CalculateSize(CodeWScope codeScope) { var size = 4; size += CalculateSize(codeScope.Value, true); size += CalculateSizeObject(codeScope.Scope); return size; }