public void ReadConcern_get_and_set_should_work( [Values(null, ReadConcernLevel.Linearizable, ReadConcernLevel.Local)] ReadConcernLevel?level) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); var value = level.HasValue ? new ReadConcern(level.Value) : ReadConcern.Default; subject.ReadConcern = value; var result = subject.ReadConcern; result.Should().Be(value); }
public void MaxAwaitTime_get_and_set_should_work( [Values(null, 1)] int?seconds) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); var value = seconds == null ? (TimeSpan?)null : TimeSpan.FromSeconds(seconds.Value); subject.MaxAwaitTime = value; var result = subject.MaxAwaitTime; result.Should().Be(value); }
public void MaxTime_set_should_throw_when_value_is_invalid( [Values(-10001, -9999, -1)] long maxTimeTicks) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); var value = TimeSpan.FromTicks(maxTimeTicks); var exception = Record.Exception(() => subject.MaxTime = value); var e = exception.Should().BeOfType <ArgumentOutOfRangeException>().Subject; e.ParamName.Should().Be("value"); }
public void CreateFindOpcodeOperation_should_return_expected_result_when_singleBatch_is_true() { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings) { Limit = 1, SingleBatch = true }; var result = subject.CreateFindOpcodeOperation(); result.Limit.Should().Be(-subject.Limit); }
public void FirstBatchSize_set_should_throw_when_value_is_invalid( [Values(-1)] int value) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); var exception = Record.Exception(() => { subject.FirstBatchSize = value; }); var argumentOutOfRangeException = exception.Should().BeOfType <ArgumentOutOfRangeException>().Subject; argumentOutOfRangeException.ParamName.Should().Be("value"); }
public void Filter_get_and_set_should_work( [Values(null, "{ a : 1 }", "{ b : 2 }")] string valueString) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); var value = valueString == null ? null : BsonDocument.Parse(valueString); subject.Filter = value; var result = subject.Filter; result.Should().Be(value); }
public void CreateFindOpcodeOperation_should_return_expected_result() { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); subject.AllowPartialResults = true; subject.BatchSize = 1; subject.Comment = "comment"; subject.CursorType = CursorType.Tailable; subject.Filter = new BsonDocument("filter", 1); subject.FirstBatchSize = 2; subject.Hint = "x_1"; subject.Limit = 3; subject.Max = new BsonDocument("max", 1); subject.MaxScan = 4; subject.MaxTime = TimeSpan.FromSeconds(1); subject.Min = new BsonDocument("min", 1); subject.NoCursorTimeout = true; subject.OplogReplay = true; subject.Projection = new BsonDocument("projection", 1); subject.ReadConcern = ReadConcern.Local; subject.ReturnKey = true; subject.ShowRecordId = true; subject.SingleBatch = false; subject.Skip = 6; subject.Snapshot = true; subject.Sort = new BsonDocument("sort", 1); var result = subject.CreateFindOpcodeOperation(); result.AllowPartialResults.Should().Be(subject.AllowPartialResults); result.BatchSize.Should().Be(subject.BatchSize); result.CollectionNamespace.Should().Be(subject.CollectionNamespace); result.Comment.Should().Be(subject.Comment); result.CursorType.Should().Be(subject.CursorType); result.Filter.Should().Be(subject.Filter); result.FirstBatchSize.Should().Be(subject.FirstBatchSize); result.Hint.Should().Be(subject.Hint); result.Limit.Should().Be(subject.Limit); result.Max.Should().Be(subject.Max); result.MaxScan.Should().Be(subject.MaxScan); result.MaxTime.Should().Be(subject.MaxTime); result.MessageEncoderSettings.Should().BeSameAs(subject.MessageEncoderSettings); result.Min.Should().Be(subject.Min); result.Modifiers.Should().Be(subject.Modifiers); result.NoCursorTimeout.Should().Be(subject.NoCursorTimeout); result.OplogReplay.Should().Be(subject.OplogReplay); result.Projection.Should().Be(subject.Projection); result.ResultSerializer.Should().Be(subject.ResultSerializer); result.ShowRecordId.Should().Be(subject.ShowRecordId); result.Skip.Should().Be(subject.Skip); result.Snapshot.Should().Be(subject.Snapshot); result.Sort.Should().Be(subject.Sort); }
public void CollectionNamespace_get_should_return_expected_result( [Values("a", "b")] string collectionName) { var databaseNamespace = new DatabaseNamespace("test"); var collectionNamespace = new CollectionNamespace(databaseNamespace, collectionName); var subject = new FindOperation <BsonDocument>(collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); var result = subject.CollectionNamespace; result.Should().Be(collectionNamespace); }
public void Hint_get_and_set_should_work( [Values(null, "{ hint : \"b_1\" }", "{ hint : { b : 1 } }")] string valueString) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); var value = valueString == null ? null : BsonDocument.Parse(valueString)["hint"]; subject.Hint = value; var result = subject.Hint; result.Should().Be(value); }
public void Collation_get_and_set_should_work( [Values(null, "en_US", "fr_CA")] string locale) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); var value = locale == null ? null : new Collation(locale); subject.Collation = value; var result = subject.Collation; result.Should().Be(value); }
public void MaxScan_get_and_set_should_work( [Values(null, 1)] int?value) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); #pragma warning disable 618 subject.MaxScan = value; var result = subject.MaxScan; #pragma warning restore result.Should().Be(value); }
private async Task <IAsyncCursor <BsonDocument> > ExecuteUsingQueryAsync(IChannelSourceHandle channelSource, ReadPreference readPreference, CancellationToken cancellationToken) { var indexes = new List <BsonDocument>(); var systemIndexesCollection = _collectionNamespace.DatabaseNamespace.SystemIndexesCollection; var filter = new BsonDocument("ns", _collectionNamespace.FullName); var operation = new FindOperation <BsonDocument>(systemIndexesCollection, BsonDocumentSerializer.Instance, _messageEncoderSettings) { Filter = filter }; return(await operation.ExecuteAsync(channelSource, readPreference, cancellationToken).ConfigureAwait(false)); }
public async Task ExecuteAsync_should_find_all_the_documents_matching_the_query_when_split_across_batches() { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings) { BatchSize = 2 }; var cursor = await ExecuteOperationAsync(subject); var result = await ReadCursorToEndAsync(cursor); result.Should().HaveCount(5); }
public void OplogReplay_get_and_set_should_work( [Values(null, false, true)] bool?value) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); #pragma warning disable 618 subject.OplogReplay = value; var result = subject.OplogReplay; #pragma warning restore 618 result.Should().Be(value); }
public void MessageEncoderSettings_get_should_return_expected_result( [Values(GuidRepresentation.CSharpLegacy, GuidRepresentation.Standard)] GuidRepresentation guidRepresentation) { var messageEncoderSettings = new MessageEncoderSettings { { "GuidRepresentation", guidRepresentation } }; var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, messageEncoderSettings); var result = subject.MessageEncoderSettings; result.Should().BeEquivalentTo(messageEncoderSettings); }
public void Execute_should_raise_an_error_when_an_unsupported_read_concern_is_specified( [Values(false, true)] bool async) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings) { ReadConcern = ReadConcern.Majority }; Action act = () => ExecuteOperation(subject, async); act.ShouldThrow <MongoClientException>(); }
public void Execute_should_find_all_the_documents_matching_the_query_when_read_preference_is_used( [Values(false, true)] bool async) { RequireServer.Check(); EnsureTestData(); var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); var cursor = ExecuteOperation(subject, ReadPreference.PrimaryPreferred, async); // note: SecondaryPreferred doesn't test $readPreference because it is encoded as slaveOk = true var result = ReadCursorToEnd(cursor, async); result.Should().HaveCount(5); }
public void Execute_should_find_all_the_documents_matching_the_query( [Values(false, true)] bool async) { RequireServer.Check(); EnsureTestData(); var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); var cursor = ExecuteOperation(subject, async); var result = ReadCursorToEnd(cursor, async); result.Should().HaveCount(5); }
public void Execute_should_find_all_the_documents_matching_the_query_when_split_across_batches( [Values(false, true)] bool async) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings) { BatchSize = 2 }; var cursor = ExecuteOperation(subject, async); var result = ReadCursorToEnd(cursor, async); result.Should().HaveCount(5); }
public void Modifiers_get_and_set_should_work( [Values(null, "{ a : 1 }", "{ b : 2 }")] string valueString) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); var value = valueString == null ? null : BsonDocument.Parse(valueString); #pragma warning disable 618 subject.Modifiers = value; var result = subject.Modifiers; #pragma warning restore 618 result.Should().Be(value); }
public void Execute_should_throw_when_ReadConcern_is_set_but_not_supported( [Values(false, true)] bool async) { RequireServer.Check().DoesNotSupport(Feature.ReadConcern); EnsureTestData(); var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings) { ReadConcern = ReadConcern.Majority }; var exception = Record.Exception(() => { ExecuteOperation(subject, async); }); exception.Should().BeOfType <MongoClientException>(); }
public void Execute_should_throw_when_maxTime_is_exceeded( [Values(false, true)] bool async) { RequireServer.Check().ClusterTypes(ClusterType.Standalone, ClusterType.ReplicaSet); var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); subject.MaxTime = TimeSpan.FromSeconds(9001); using (var failPoint = FailPoint.ConfigureAlwaysOn(_cluster, _session, FailPointName.MaxTimeAlwaysTimeout)) { var exception = Record.Exception(() => ExecuteOperation(subject, failPoint.Binding, async)); exception.Should().BeOfType <MongoExecutionTimeoutException>(); } }
private async Task <IReadOnlyList <BsonDocument> > ExecuteUsingQueryAsync(IChannelSourceHandle channelSource, ReadPreference readPreference, CancellationToken cancellationToken) { // if the filter includes a comparison to the "name" we must convert the value to a full namespace var filter = _filter; if (filter != null && filter.Contains("name")) { var value = filter["name"]; if (!value.IsString) { throw new NotSupportedException("Name filter must be a plain string when connected to a server version less than 2.8."); } filter = (BsonDocument)filter.Clone(); // shallow clone filter["name"] = _databaseNamespace.DatabaseName + "." + value; } var operation = new FindOperation <BsonDocument>(_databaseNamespace.SystemNamespacesCollection, BsonDocumentSerializer.Instance, _messageEncoderSettings) { Filter = filter }; var cursor = await operation.ExecuteAsync(channelSource, readPreference, cancellationToken).ConfigureAwait(false); var collections = new List <BsonDocument>(); var prefix = _databaseNamespace + "."; while (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false)) { var batch = cursor.Current; foreach (var collection in batch) { var name = (string)collection["name"]; if (name.StartsWith(prefix)) { var collectionName = name.Substring(prefix.Length); if (!collectionName.Contains('$')) { collection["name"] = collectionName; // replace the full namespace with just the collection name collections.Add(collection); } } } } return(collections); }
public void Execute_should_return_expected_result_when_Collation_is_set( [Values(false, true)] bool async) { RequireServer.Check().Supports(Feature.FindCommand, Feature.Collation); EnsureTestData(); var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings) { Collation = new Collation("en_US", caseLevel: false, strength: CollationStrength.Primary), Filter = BsonDocument.Parse("{ x : 'd' }") }; var cursor = ExecuteOperation(subject, async); var result = ReadCursorToEnd(cursor); result.Should().HaveCount(2); }
public void constructor_should_initialize_instance() { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); subject.CollectionNamespace.Should().BeSameAs(_collectionNamespace); subject.ResultSerializer.Should().BeSameAs(BsonDocumentSerializer.Instance); subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings); subject.AllowDiskUse.Should().NotHaveValue(); subject.AllowPartialResults.Should().NotHaveValue(); subject.BatchSize.Should().NotHaveValue(); subject.Collation.Should().BeNull(); subject.Comment.Should().BeNull(); subject.CursorType.Should().Be(CursorType.NonTailable); subject.Filter.Should().BeNull(); subject.FirstBatchSize.Should().NotHaveValue(); subject.Hint.Should().BeNull(); subject.Limit.Should().NotHaveValue(); subject.Max.Should().BeNull(); subject.MaxAwaitTime.Should().NotHaveValue(); #pragma warning disable 618 subject.MaxScan.Should().NotHaveValue(); #pragma warning restore subject.MaxTime.Should().NotHaveValue(); subject.Min.Should().BeNull(); #pragma warning disable 618 subject.Modifiers.Should().BeNull(); #pragma warning restore 618 subject.NoCursorTimeout.Should().NotHaveValue(); #pragma warning disable 618 subject.OplogReplay.Should().NotHaveValue(); #pragma warning restore 618 subject.Projection.Should().BeNull(); subject.ReadConcern.Should().Be(ReadConcern.Default); subject.RetryRequested.Should().BeFalse(); subject.ReturnKey.Should().NotHaveValue(); subject.ShowRecordId.Should().NotHaveValue(); subject.SingleBatch.Should().NotHaveValue(); subject.Skip.Should().NotHaveValue(); #pragma warning disable 618 subject.Snapshot.Should().NotHaveValue(); #pragma warning restore subject.Sort.Should().BeNull(); }
public async Task ExecuteAsync_should_find_documents_matching_options() { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings) { Comment = "funny", Filter = BsonDocument.Parse("{y: 1}"), Limit = 4, MaxTime = TimeSpan.FromSeconds(20), Projection = BsonDocument.Parse("{y: 1}"), Skip = 1, Sort = BsonDocument.Parse("{_id: -1}") }; var cursor = await ExecuteOperationAsync(subject); var result = await ReadCursorToEndAsync(cursor); result.Should().HaveCount(1); }
public void CreateFindCommandOperation_should_prefer_top_level_fields_over_modifiers() { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings) { Hint = "x_2", Max = new BsonDocument("max", 5), #pragma warning disable 618 MaxScan = 7, #pragma warning restore 618 Min = new BsonDocument("min", 3), ReturnKey = true, ShowRecordId = true, #pragma warning disable 618 Snapshot = true, #pragma warning restore 618 #pragma warning disable 618 Modifiers = new BsonDocument { { "$hint", "x_1" }, { "$max", new BsonDocument("max", 1) }, { "$maxScan", 1 }, { "$min", new BsonDocument("min", 1) }, { "$returnKey", false }, { "$showDiskLoc", false }, { "$snapshot", false } } #pragma warning restore 618 }; var result = subject.CreateFindCommandOperation(); result.Hint.Should().Be(subject.Hint); result.Max.Should().Be(subject.Max); #pragma warning disable 618 result.MaxScan.Should().Be(subject.MaxScan); #pragma warning restore 618 result.Min.Should().Be(subject.Min); result.ReturnKey.Should().Be(subject.ReturnKey); result.ShowRecordId.Should().Be(subject.ShowRecordId); #pragma warning disable 618 result.Snapshot.Should().Be(subject.Snapshot); #pragma warning restore 618 }
public void Execute_should_find_documents_matching_options( [Values(false, true)] bool async) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings) { Comment = "funny", Filter = BsonDocument.Parse("{ y : 1 }"), Limit = 4, MaxTime = TimeSpan.FromSeconds(20), Projection = BsonDocument.Parse("{ y : 1 }"), Skip = 1, Sort = BsonDocument.Parse("{ _id : -1 }") }; var cursor = ExecuteOperation(subject, async); var result = ReadCursorToEnd(cursor, async); result.Should().HaveCount(1); }
// constructors public Builder(FindOperation <TDocument> other) { _additionalOptions = other._additionalOptions; _awaitData = other._awaitData; _batchSize = other._batchSize; _collectionName = other._collectionName; _comment = other._comment; _databaseName = other._databaseName; _fields = other._fields; _hint = other._hint; _limit = other._limit; _noCursorTimeout = other._noCursorTimeout; _partialOk = other._partialOk; _query = other._query; _serializer = other._serializer; _skip = other._skip; _snapshot = other._snapshot; _sort = other._sort; _tailableCursor = other._tailableCursor; }
public void Execute_should_throw_when_binding_is_null( [Values(false, true)] bool async) { var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings); var exception = Record.Exception(() => { if (async) { subject.ExecuteAsync(null, CancellationToken.None).GetAwaiter().GetResult(); } else { subject.Execute(null, CancellationToken.None); } }); var argumentNullException = exception.Should().BeOfType <ArgumentNullException>().Subject; argumentNullException.ParamName.Should().Be("binding"); }