
6 changed files with 1258 additions and 0 deletions
-
2Bson/Bson.csproj
-
393Bson/IO/BsonDocumentWriter.cs
-
92Bson/IO/BsonDocumentWriterContext.cs
-
6Bson/IO/BsonWriter.cs
-
1BsonUnitTests/BsonUnitTests.csproj
-
764BsonUnitTests/IO/BsonDocumentWriterTests.cs
@ -0,0 +1,393 @@ |
|||
/* Copyright 2010 10gen Inc. |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Text; |
|||
|
|||
namespace MongoDB.Bson.IO { |
|||
public class BsonDocumentWriter : BsonBaseWriter { |
|||
#region private fields
|
|||
private BsonDocument topLevelDocument; |
|||
private BsonDocumentWriterContext context; |
|||
#endregion
|
|||
|
|||
#region constructors
|
|||
public BsonDocumentWriter() |
|||
: this(new BsonDocument()) { |
|||
} |
|||
|
|||
public BsonDocumentWriter( |
|||
BsonDocument topLevelDocument |
|||
) { |
|||
this.topLevelDocument = topLevelDocument; |
|||
context = null; |
|||
state = BsonWriterState.Initial; |
|||
} |
|||
#endregion
|
|||
|
|||
#region public properties
|
|||
public BsonDocument TopLevelDocument { |
|||
get { return topLevelDocument; } |
|||
} |
|||
#endregion
|
|||
|
|||
#region public methods
|
|||
public override void Close() { |
|||
// Close can be called on Disposed objects
|
|||
if (state != BsonWriterState.Closed) { |
|||
context = null; |
|||
state = BsonWriterState.Closed; |
|||
} |
|||
} |
|||
|
|||
public override void Flush() { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
} |
|||
|
|||
public override void WriteBinaryData( |
|||
byte[] bytes, |
|||
BsonBinarySubType subType |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteBinaryData cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(new BsonBinaryData(bytes, subType)); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteBoolean( |
|||
bool value |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteBoolean cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(value); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteDateTime( |
|||
DateTime value |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteDateTime cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
if (value.Kind != DateTimeKind.Utc) { |
|||
throw new ArgumentException("DateTime value must be in UTC"); |
|||
} |
|||
|
|||
WriteValue(value); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteDouble( |
|||
double value |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteDouble cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(value); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteEndArray() { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value || context.ContextType != ContextType.Array) { |
|||
var message = string.Format("WriteEndArray cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
var array = context.Array; |
|||
context = context.ParentContext; |
|||
WriteValue(array); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteEndDocument() { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Name || (context.ContextType != ContextType.Document && context.ContextType != ContextType.ScopeDocument)) { |
|||
var message = string.Format("WriteEndDocument cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
if (context.ContextType == ContextType.ScopeDocument) { |
|||
var scope = context.Document; |
|||
context = context.ParentContext; |
|||
var code = context.Code; |
|||
context = context.ParentContext; |
|||
WriteValue(new BsonJavaScriptWithScope(code, scope)); |
|||
} else { |
|||
var document = context.Document; |
|||
context = context.ParentContext; |
|||
if (context != null) { |
|||
WriteValue(document); |
|||
} |
|||
} |
|||
|
|||
if (context == null) { |
|||
state = BsonWriterState.Done; |
|||
} else { |
|||
state = GetNextState(); |
|||
} |
|||
} |
|||
|
|||
public override void WriteInt32( |
|||
int value |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteInt32 cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(value); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteInt64( |
|||
long value |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteInt64 cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(value); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteJavaScript( |
|||
string code |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteJavaScript cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(new BsonJavaScript(code)); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteJavaScriptWithScope( |
|||
string code |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteJavaScriptWithScope cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
context = new BsonDocumentWriterContext(context, ContextType.JavaScriptWithScope, code); |
|||
state = BsonWriterState.ScopeDocument; |
|||
} |
|||
|
|||
public override void WriteMaxKey() { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteMaxKey cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(BsonMaxKey.Value); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteMinKey() { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteMinKey cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(BsonMinKey.Value); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteName( |
|||
string name |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException(this.GetType().Name); } |
|||
if (state != BsonWriterState.Name) { |
|||
var message = string.Format("WriteName cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
context.Name = name; |
|||
state = BsonWriterState.Value; |
|||
} |
|||
|
|||
public override void WriteNull() { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteNull cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(BsonNull.Value); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteObjectId( |
|||
int timestamp, |
|||
int machine, |
|||
short pid, |
|||
int increment |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteObjectId cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(new ObjectId(timestamp, machine, pid, increment)); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteRegularExpression( |
|||
string pattern, |
|||
string options |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteRegularExpression cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(new BsonRegularExpression(pattern, options)); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteStartArray() { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteStartArray cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
context = new BsonDocumentWriterContext(context, ContextType.Array, new BsonArray()); |
|||
state = BsonWriterState.Value; |
|||
} |
|||
|
|||
public override void WriteStartDocument() { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Initial && state != BsonWriterState.Value && state != BsonWriterState.ScopeDocument && state != BsonWriterState.Done) { |
|||
var message = string.Format("WriteStartDocument cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
switch (state) { |
|||
case BsonWriterState.Initial: |
|||
case BsonWriterState.Done: |
|||
context = new BsonDocumentWriterContext(null, ContextType.Document, topLevelDocument); |
|||
break; |
|||
case BsonWriterState.Value: |
|||
context = new BsonDocumentWriterContext(context, ContextType.Document, new BsonDocument()); |
|||
break; |
|||
case BsonWriterState.ScopeDocument: |
|||
context = new BsonDocumentWriterContext(context, ContextType.ScopeDocument, new BsonDocument()); |
|||
break; |
|||
default: |
|||
throw new BsonInternalException("Unexpected state"); |
|||
} |
|||
|
|||
state = BsonWriterState.Name; |
|||
} |
|||
|
|||
public override void WriteString( |
|||
string value |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteString cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(value); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteSymbol( |
|||
string value |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteSymbol cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(BsonSymbol.Create(value)); |
|||
state = GetNextState(); |
|||
} |
|||
|
|||
public override void WriteTimestamp( |
|||
long value |
|||
) { |
|||
if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } |
|||
if (state != BsonWriterState.Value) { |
|||
var message = string.Format("WriteTimestamp cannot be called when State is: {0}", state); |
|||
throw new InvalidOperationException(message); |
|||
} |
|||
|
|||
WriteValue(new BsonTimestamp(value)); |
|||
state = GetNextState(); |
|||
} |
|||
#endregion
|
|||
|
|||
#region protected methods
|
|||
protected override void Dispose( |
|||
bool disposing |
|||
) { |
|||
if (disposing) { |
|||
Close(); |
|||
} |
|||
} |
|||
#endregion
|
|||
|
|||
#region private methods
|
|||
private BsonWriterState GetNextState() { |
|||
if (context.ContextType == ContextType.Array) { |
|||
return BsonWriterState.Value; |
|||
} else { |
|||
return BsonWriterState.Name; |
|||
} |
|||
} |
|||
|
|||
private void WriteValue( |
|||
BsonValue value |
|||
) { |
|||
if (context.ContextType == ContextType.Array) { |
|||
context.Array.Add(value); |
|||
} else { |
|||
context.Document.Add(context.Name, value); |
|||
} |
|||
} |
|||
#endregion
|
|||
} |
|||
} |
@ -0,0 +1,92 @@ |
|||
/* Copyright 2010 10gen Inc. |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Text; |
|||
|
|||
namespace MongoDB.Bson.IO { |
|||
internal class BsonDocumentWriterContext { |
|||
#region private fields
|
|||
private BsonDocumentWriterContext parentContext; |
|||
private ContextType contextType; |
|||
private BsonDocument document; |
|||
private BsonArray array; |
|||
private string code; |
|||
private string name; |
|||
#endregion
|
|||
|
|||
#region constructors
|
|||
internal BsonDocumentWriterContext( |
|||
BsonDocumentWriterContext parentContext, |
|||
ContextType contextType, |
|||
BsonDocument document |
|||
) { |
|||
this.parentContext = parentContext; |
|||
this.contextType = contextType; |
|||
this.document = document; |
|||
} |
|||
|
|||
internal BsonDocumentWriterContext( |
|||
BsonDocumentWriterContext parentContext, |
|||
ContextType contextType, |
|||
BsonArray array |
|||
) { |
|||
this.parentContext = parentContext; |
|||
this.contextType = contextType; |
|||
this.array = array; |
|||
} |
|||
|
|||
internal BsonDocumentWriterContext( |
|||
BsonDocumentWriterContext parentContext, |
|||
ContextType contextType, |
|||
string code |
|||
) { |
|||
this.parentContext = parentContext; |
|||
this.contextType = contextType; |
|||
this.code = code; |
|||
} |
|||
#endregion
|
|||
|
|||
#region internal properties
|
|||
internal BsonDocumentWriterContext ParentContext { |
|||
get { return parentContext; } |
|||
} |
|||
|
|||
internal string Name { |
|||
get { return name; } |
|||
set { name = value; } |
|||
} |
|||
|
|||
internal ContextType ContextType { |
|||
get { return contextType; } |
|||
} |
|||
|
|||
internal BsonDocument Document { |
|||
get { return document; } |
|||
} |
|||
|
|||
internal BsonArray Array { |
|||
get { return array; } |
|||
} |
|||
|
|||
internal string Code { |
|||
get { return code; } |
|||
} |
|||
#endregion
|
|||
} |
|||
} |
@ -0,0 +1,764 @@ |
|||
/* Copyright 2010 10gen Inc. |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using NUnit.Framework; |
|||
|
|||
using MongoDB.Bson; |
|||
using MongoDB.Bson.IO; |
|||
|
|||
namespace MongoDB.BsonUnitTests.IO { |
|||
[TestFixture] |
|||
public class BsonDocumentWriterTests { |
|||
// Empty Document tests
|
|||
[Test] |
|||
public void TestEmptyDocument() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ }"; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneEmptyDocument() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("a"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedEmptyDocument() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteStartDocument("a"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoEmptyDocuments() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("a"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteStartDocument("b"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { }, 'b' : { } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedEmptyDocuments() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteStartDocument("a"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteStartDocument("b"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { }, 'b' : { } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
// Boolean tests
|
|||
[Test] |
|||
public void TestOneBoolean() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteBoolean("a", true); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : true }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedBoolean() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteBoolean("a", true); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : true } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoBooleans() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteBoolean("a", true); |
|||
writer.WriteBoolean("b", false); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : true, 'b' : false }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedBooleans() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteBoolean("a", true); |
|||
writer.WriteBoolean("b", false); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : true, 'b' : false } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
// Double tests
|
|||
[Test] |
|||
public void TestOneDouble() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteDouble("a", 1.5); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : 1.5 }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedDouble() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteDouble("a", 1.5); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : 1.5 } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoDoubles() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteDouble("a", 1.5); |
|||
writer.WriteDouble("b", 2.5); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : 1.5, 'b' : 2.5 }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedDoubles() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteDouble("a", 1.5); |
|||
writer.WriteDouble("b", 2.5); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : 1.5, 'b' : 2.5 } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
// Int32 tests
|
|||
[Test] |
|||
public void TestOneInt32() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteInt32("a", 1); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : 1 }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedInt32() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteInt32("a", 1); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : 1 } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoInt32s() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteInt32("a", 1); |
|||
writer.WriteInt32("b", 2); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : 1, 'b' : 2 }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedInt32s() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteInt32("a", 1); |
|||
writer.WriteInt32("b", 2); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : 1, 'b' : 2 } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
// Int64 tests
|
|||
[Test] |
|||
public void TestOneInt64() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteInt64("a", 1); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : 1 }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedInt64() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteInt64("a", 1); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : 1 } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoInt64s() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteInt64("a", 1); |
|||
writer.WriteInt64("b", 2); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : 1, 'b' : 2 }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedInt64s() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteInt64("a", 1); |
|||
writer.WriteInt64("b", 2); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : 1, 'b' : 2 } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
// MaxKey tests
|
|||
[Test] |
|||
public void TestOneMaxKey() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteMaxKey("a"); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { '$maxkey' : 1 } }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedMaxKey() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteMaxKey("a"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { '$maxkey' : 1 } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoMaxKeys() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteMaxKey("a"); |
|||
writer.WriteMaxKey("b"); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { '$maxkey' : 1 }, 'b' : { '$maxkey' : 1 } }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedMaxKeys() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteMaxKey("a"); |
|||
writer.WriteMaxKey("b"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { '$maxkey' : 1 }, 'b' : { '$maxkey' : 1 } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
// MinKey tests
|
|||
[Test] |
|||
public void TestOneMinKey() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteMinKey("a"); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { '$minkey' : 1 } }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedMinKey() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteMinKey("a"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { '$minkey' : 1 } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoMinKeys() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteMinKey("a"); |
|||
writer.WriteMinKey("b"); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { '$minkey' : 1 }, 'b' : { '$minkey' : 1 } }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedMinKeys() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteMinKey("a"); |
|||
writer.WriteMinKey("b"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { '$minkey' : 1 }, 'b' : { '$minkey' : 1 } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
// Null tests
|
|||
[Test] |
|||
public void TestOneNull() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteNull("a"); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : null }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedNull() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteNull("a"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : null } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNulls() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteNull("a"); |
|||
writer.WriteNull("b"); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : null, 'b' : null }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedNulls() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteNull("a"); |
|||
writer.WriteNull("b"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : null, 'b' : null } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
// ObjectId tests
|
|||
[Test] |
|||
public void TestOneObjectId() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
var id = ObjectId.Empty; |
|||
writer.WriteObjectId("a", id.Timestamp, id.Machine, id.Pid, id.Increment); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { '$oid' : '000000000000000000000000' } }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedObjectId() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
var id = ObjectId.Empty; |
|||
writer.WriteObjectId("a", id.Timestamp, id.Machine, id.Pid, id.Increment); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { '$oid' : '000000000000000000000000' } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoObjectIds() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
var id = ObjectId.Empty; |
|||
writer.WriteObjectId("a", id.Timestamp, id.Machine, id.Pid, id.Increment); |
|||
writer.WriteObjectId("b", id.Timestamp, id.Machine, id.Pid, id.Increment); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { '$oid' : '000000000000000000000000' }, 'b' : { '$oid' : '000000000000000000000000' } }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedObjectIds() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
var id = ObjectId.Empty; |
|||
writer.WriteObjectId("a", id.Timestamp, id.Machine, id.Pid, id.Increment); |
|||
writer.WriteObjectId("b", id.Timestamp, id.Machine, id.Pid, id.Increment); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { '$oid' : '000000000000000000000000' }, 'b' : { '$oid' : '000000000000000000000000' } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
// RegularExpression tests
|
|||
[Test] |
|||
public void TestOneRegularExpression() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteRegularExpression("a", "p", "o"); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { '$regex' : 'p', '$options' : 'o' } }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedRegularExpression() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteRegularExpression("a", "p", "o"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { '$regex' : 'p', '$options' : 'o' } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoRegularExpressions() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteRegularExpression("a", "p", "o"); |
|||
writer.WriteRegularExpression("b", "q", "r"); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { '$regex' : 'p', '$options' : 'o' }, 'b' : { '$regex' : 'q', '$options' : 'r' } }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedRegularExpressions() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteRegularExpression("a", "p", "o"); |
|||
writer.WriteRegularExpression("b", "q", "r"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { '$regex' : 'p', '$options' : 'o' }, 'b' : { '$regex' : 'q', '$options' : 'r' } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
// String tests
|
|||
[Test] |
|||
public void TestOneString() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteString("a", "x"); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : 'x' }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedString() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteString("a", "x"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : 'x' } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoStrings() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteString("a", "x"); |
|||
writer.WriteString("b", "y"); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : 'x', 'b' : 'y' }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedStrings() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteString("a", "x"); |
|||
writer.WriteString("b", "y"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : 'x', 'b' : 'y' } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
// Symbol tests
|
|||
[Test] |
|||
public void TestOneSymbol() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteSymbol("a", "x"); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { '$symbol' : 'x' } }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedSymbol() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteSymbol("a", "x"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { '$symbol' : 'x' } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoSymbols() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteSymbol("a", "x"); |
|||
writer.WriteSymbol("b", "y"); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { '$symbol' : 'x' }, 'b' : { '$symbol' : 'y' } }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedSymbols() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteSymbol("a", "x"); |
|||
writer.WriteSymbol("b", "y"); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { '$symbol' : 'x' }, 'b' : { '$symbol' : 'y' } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
// Timestamp tests
|
|||
[Test] |
|||
public void TestOneTimestamp() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteTimestamp("a", 1); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { '$timestamp' : 1 } }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestOneNestedTimestamp() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteTimestamp("a", 1); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { '$timestamp' : 1 } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoTimestamps() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteTimestamp("a", 1); |
|||
writer.WriteTimestamp("b", 2); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'a' : { '$timestamp' : 1 }, 'b' : { '$timestamp' : 2 } }".Replace("'", "\""); ; |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
|
|||
[Test] |
|||
public void TestTwoNestedTimestamps() { |
|||
var document = new BsonDocument(); |
|||
var writer = BsonWriter.Create(document); |
|||
writer.WriteStartDocument(); |
|||
writer.WriteStartDocument("nested"); |
|||
writer.WriteTimestamp("a", 1); |
|||
writer.WriteTimestamp("b", 2); |
|||
writer.WriteEndDocument(); |
|||
writer.WriteEndDocument(); |
|||
var json = document.ToJson(); |
|||
var expected = "{ 'nested' : { 'a' : { '$timestamp' : 1 }, 'b' : { '$timestamp' : 2 } } }".Replace("'", "\""); |
|||
Assert.AreEqual(expected, json); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue