public void FromCore_should_convert_from_core_exception_with_a_write_concern_error_when_original_models_do_not_exist()
        {
            var exception = new MongoBulkWriteOperationException(
                __connectionId,
                result: new BulkWriteOperationResult.Acknowledged(
                    requestCount: 1,
                    matchedCount: 1,
                    deletedCount: 0,
                    insertedCount: 0,
                    modifiedCount: 0,
                    processedRequests: new[] { new InsertRequest(new BsonDocumentWrapper(new BsonDocument("b", 1)))
                                               {
                                                   CorrelationId = 1
                                               } },
                    upserts: new List <BulkWriteOperationUpsert>()),
                writeErrors: new[] { new BulkWriteOperationError(10, 1, "blah", new BsonDocument("a", 1)) },
                writeConcernError: new BulkWriteConcernError(11, "funny", new BsonDocument("c", 1)),
                unprocessedRequests: new[] { new InsertRequest(new BsonDocumentWrapper(new BsonDocument("a", 1)))
                                             {
                                                 CorrelationId = 0
                                             } });

            var mapped = MongoBulkWriteException <BsonDocument> .FromCore(exception);

            mapped.Result.ProcessedRequests.Count.Should().Be(1);
            mapped.Result.ProcessedRequests[0].Should().BeOfType <InsertOneModel <BsonDocument> >();
            ((InsertOneModel <BsonDocument>)mapped.Result.ProcessedRequests[0]).Document.Should().Be("{b:1}");
            mapped.WriteConcernError.Should().NotBeNull();
            mapped.WriteErrors.Count.Should().Be(1);
            mapped.WriteErrors[0].Should().NotBeNull();
            mapped.UnprocessedRequests.Count.Should().Be(1);
            mapped.UnprocessedRequests[0].Should().BeOfType <InsertOneModel <BsonDocument> >();
            ((InsertOneModel <BsonDocument>)mapped.UnprocessedRequests[0]).Document.Should().Be("{a:1}");
        }
示例#2
0
        public void constructor_should_initialize_subject()
        {
            var subject = new MongoBulkWriteException<BsonDocument>(_connectionId, _bulkWriteResult, _writeErrors, _writeConcernError, _unprocessedRequests);

            subject.ConnectionId.Should().BeSameAs(_connectionId);
            subject.Message.Should().Contain("bulk write operation");
            subject.Result.Should().BeSameAs(_bulkWriteResult);
            subject.UnprocessedRequests.Should().Equal(_unprocessedRequests);
            subject.WriteConcernError.Should().BeSameAs(_writeConcernError);
            subject.WriteErrors.Should().Equal(_writeErrors);
        }
        /// <summary>Parses an exception stack.</summary>
        /// <param name="exceptionObject">The exception object.</param>
        /// <returns>A list of <see cref="CosmosError"/>s.</returns>
        public static IList <CosmosError> ParseBulkWriteException(MongoBulkWriteException exceptionObject)
        {
            List <CosmosError> result = new List <CosmosError>();

            foreach (var we in exceptionObject.WriteErrors)
            {
                result.Add(ParseBulkWriteError(we));
            }

            return(result);
        }
示例#4
0
        public void constructor_should_initialize_subject()
        {
            var subject = new MongoBulkWriteException <BsonDocument>(__connectionId, __bulkWriteResult, __writeErrors, __writeConcernError, __unprocessedRequests);

            subject.ConnectionId.Should().BeSameAs(__connectionId);
            subject.ErrorLabels.Should().BeEquivalentTo(__writeConcernError.ErrorLabels);
            subject.Message.Should().Be("A bulk write operation resulted in one or more errors. WriteErrors: [ { Category : \"Uncategorized\", Code : 1, Message : \"blah\", Details : \"{ \"a\" : 1 }\" } ]. WriteConcernError: { Code : \"11\", Message : \"funny\", Details : \"{ \"c\" : 1 }\", ErrorLabels : [ \"RetryableWriteError\" ] }.");
            subject.Result.Should().BeSameAs(__bulkWriteResult);
            subject.UnprocessedRequests.Should().Equal(__unprocessedRequests);
            subject.WriteConcernError.Should().BeSameAs(__writeConcernError);
            subject.WriteErrors.Should().Equal(__writeErrors);
        }
        public void FromBulkWriteException_should_return_expected_result()
        {
            var processedRequests = new[] { new InsertOneModel<BsonDocument>(new BsonDocument("_id", 1)) };
            var upserts = new List<BulkWriteUpsert>();
            var bulkWriteResult = new BulkWriteResult<BsonDocument>.Acknowledged(1, 1, 0, 0, 0, processedRequests, upserts);
            var writeErrors = new[] { new BulkWriteError(1, ServerErrorCategory.Uncategorized, 2, "message", new BsonDocument("details", 1)) };
            var writeConcernError = new WriteConcernError(1, "message", new BsonDocument("details", 1));
            var unprocessedRequests = new List<WriteModel<BsonDocument>>();
            var bulkWriteException = new MongoBulkWriteException<BsonDocument>(_connectionId, bulkWriteResult, writeErrors, writeConcernError, unprocessedRequests);

            var result = MongoWriteException.FromBulkWriteException(bulkWriteException);

            result.ConnectionId.Should().Be(_connectionId);
            result.InnerException.Should().BeSameAs(bulkWriteException);
            result.Message.Should().Be("A write operation resulted in an error." + Environment.NewLine + "  message" + Environment.NewLine + "  message");
            result.WriteConcernError.Should().Be(writeConcernError);
            result.WriteError.Should().Be(writeErrors[0]);
        }
        public void FromBulkWriteException_should_return_expected_result()
        {
            var processedRequests = new[] { new InsertOneModel <BsonDocument>(new BsonDocument("_id", 1)) };
            var upserts           = new List <BulkWriteUpsert>();
            var bulkWriteResult   = new BulkWriteResult <BsonDocument> .Acknowledged(1, 1, 0, 0, 0, processedRequests, upserts);

            var writeErrors         = new[] { new BulkWriteError(1, ServerErrorCategory.Uncategorized, 2, "message", new BsonDocument("details", 1)) };
            var writeConcernError   = new WriteConcernError(1, "message", new BsonDocument("details", 1));
            var unprocessedRequests = new List <WriteModel <BsonDocument> >();
            var bulkWriteException  = new MongoBulkWriteException <BsonDocument>(__connectionId, bulkWriteResult, writeErrors, writeConcernError, unprocessedRequests);

            var result = MongoWriteException.FromBulkWriteException(bulkWriteException);

            result.ConnectionId.Should().Be(__connectionId);
            result.InnerException.Should().BeSameAs(bulkWriteException);
            result.Message.Should().Be("A write operation resulted in an error." + Environment.NewLine + "  message" + Environment.NewLine + "  message");
            result.WriteConcernError.Should().Be(writeConcernError);
            result.WriteError.Should().Be(writeErrors[0]);
        }
示例#7
0
        ///// <summary>
        ///// 得到数据库实例
        ///// </summary>
        ///// <returns></returns>
        //public IMongoDatabase GetDataBase()
        //{
        //    MongoClientSettings mongoSetting = new MongoClientSettings();
        //    //设置连接超时时间
        //    mongoSetting.ConnectTimeout = new TimeSpan(CONNECT_TIME_OUT * TimeSpan.TicksPerSecond);
        //    //设置数据库服务器
        //    mongoSetting.Server = new MongoServerAddress(MONGO_CONN_HOST, MONGO_CONN_PORT);
        //    //创建Mongo的客户端
        //    MongoClient client = new MongoClient(CONN_STR);

        //    //得到服务器端并且生成数据库实例
        //    return client.GetDatabase(DB_NAME);
        //}


        #region 插入数据
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <typeparam name="T">插入类型</typeparam>
        /// <param name="collectionName">集合名</param>
        /// <param name="document">插入实例</param>
        public void Insert <T>(string collectionName, T document)
        {
            try
            {
                _DB.GetCollection <T>(collectionName).InsertOne(document);
            }
            catch (MongoWriteException me)
            {
                MongoBulkWriteException mbe = me.InnerException as MongoBulkWriteException;
                if (mbe != null && mbe.HResult == -2146233088)
                {
                    throw new Exception("插入重复的键!");
                }
                throw new Exception(mbe.Message);
            }
            catch (Exception ep)
            {
                throw ep;
            }
        }
示例#8
0
        public void Serialization_should_work()
        {
            var subject = new MongoBulkWriteException<BsonDocument>(_connectionId, _bulkWriteResult, _writeErrors, _writeConcernError, _unprocessedRequests);

            var formatter = new BinaryFormatter();
            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, subject);
                stream.Position = 0;
                var rehydrated = (MongoBulkWriteException<BsonDocument>)formatter.Deserialize(stream);

                rehydrated.ConnectionId.Should().Be(subject.ConnectionId);
                rehydrated.Message.Should().Be(subject.Message);
                rehydrated.Result.Should().BeUsing(subject.Result, EqualityComparerRegistry.Default);
                rehydrated.UnprocessedRequests.Should().EqualUsing(subject.UnprocessedRequests, EqualityComparerRegistry.Default);
                rehydrated.WriteConcernError.Should().BeUsing(subject.WriteConcernError, EqualityComparerRegistry.Default);
                rehydrated.WriteErrors.Should().EqualUsing(subject.WriteErrors, EqualityComparerRegistry.Default);
            }
        }