public async Task CollectionExists_CollectionDoesNotExists_DoesNotThrow() { // Arrange var db = new MongoDb(ConnectionString, DatabaseId); var collectionId = Guid.NewGuid().ToString(); var exception = await Record.ExceptionAsync(() => db.CollectionExists(collectionId)); //Assert exception.Should().BeNull(); }
public async Task CollectionExists_CollectionDoesNotExists_ReturnsFalse() { // Arrange var db = new MongoDb(ConnectionString, DatabaseId); var collectionId = Guid.NewGuid().ToString(); // Act var exists = await db.CollectionExists(collectionId); //Assert exists.Should().BeFalse(); }
public async Task CollectionExists_CollectionExists_ReturnsTrue() { // Arrange var db = new MongoDb(ConnectionString, DatabaseId); // Act // Collection with CollectionId gets created durint ctor setup var exists = await db.CollectionExists(CollectionId); //Assert exists.Should().BeTrue(); }
public async Task CollectionExists_WithInvalidId_ThrowsException() { // Arrange var db = new MongoDb(ConnectionString, DatabaseId); // Act var exception = await Record.ExceptionAsync(() => db.CollectionExists(null)); //Assert exception.Should().NotBeNull(); exception.Should().BeOfType <ArgumentNullException>(); }
public async Task DeleteCollection_WithExistingCollection_CollectionShouldNotExistAfterDeletion() { // Arrange var db = new MongoDb(ConnectionString, DatabaseId); // Act await db.DeleteCollection(CollectionId); // Assert var exists = await db.CollectionExists(CollectionId); exists.Should().BeFalse(); }
public async Task CreateCollection_NewCollectionNameProvided_NewCollectionCreated() { // Arrange var db = new MongoDb(ConnectionString, DatabaseId); // Act await db.CreateCollection(NewCollectionId); // Assert var exists = await db.CollectionExists(NewCollectionId); exists.Should().BeTrue(); }
public void ConvertTables(IEnumerable <TableItem> tableItems) { TableItems = tableItems.ToList(); var coll = MongoDb.GetCollection <BsonDocument>("test"); int i = 0; foreach (var tableItem in tableItems) { var tableName = tableItem.TableName; var columnName = tableItem.CollectionName; var keyColumnName = tableItem.KeyColumnName; var intOffset = tableItem.IntegerOffset; using (var conn = new SqlConnection(SqlConnectionString)) { string query = "select * from [" + tableName + "]"; using (var cmd = new SqlCommand(query, conn)) { // Delete the MongoDb Collection first to proceed with data insertion if (MongoDb.CollectionExists(tableItem.CollectionName)) { var collection = MongoDb.GetCollection <BsonDocument>(columnName); collection.Drop(); } conn.Open(); var reader = cmd.ExecuteReader(); var bsonlist = new List <BsonDocument>(1000); while (reader.Read()) { if (i == 1000) { using (MongoSvr.RequestStart(MongoDb)) { //MongoCollection<MongoDB.Bson.BsonDocument> coll = MongoDb.GetCollection <BsonDocument>(columnName); coll.InsertBatch(bsonlist); bsonlist.RemoveRange(0, bsonlist.Count); } i = 0; } ++i; var bson = new BsonDocument(); for (int j = 0; j < reader.FieldCount; j++) { var aType = reader[j].GetType(); var propName = reader.GetName(j); if (aType == typeof(String)) { bson.Add(new BsonElement(propName, reader[j].ToString())); } else if (aType == typeof(Int32)) { bson.Add(new BsonElement(propName, BsonValue.Create(reader.GetInt32(j)))); } else if (aType == typeof(Int16)) { bson.Add(new BsonElement(propName, BsonValue.Create(reader.GetInt16(j)))); } else if (aType == typeof(Int64)) { bson.Add(new BsonElement(propName, BsonValue.Create(reader.GetInt64(j)))); } else if (aType == typeof(float)) { bson.Add(new BsonElement(propName, BsonValue.Create(reader.GetFloat(j)))); } else if (aType == typeof(Decimal)) { var val = (double)reader.GetDecimal(j); bson.Add(new BsonElement(propName, BsonValue.Create(val))); } else if (aType == typeof(Double)) { bson.Add(new BsonElement(propName, BsonValue.Create(reader.GetDouble(j)))); } else if (aType == typeof(DateTime)) { bson.Add(new BsonElement(propName, BsonValue.Create(reader.GetDateTime(j)))); } else if (aType == typeof(DateTimeOffset)) { var val = reader.GetDateTimeOffset(j).DateTime; bson.Add(new BsonElement(propName, BsonValue.Create(val))); } else if (aType == typeof(Guid)) { bson.Add(new BsonElement(propName, BsonValue.Create(reader.GetGuid(j)))); } else if (aType == typeof(Boolean)) { bson.Add(new BsonElement(propName, BsonValue.Create(reader.GetBoolean(j)))); } else if (aType == typeof(DBNull)) { // do nothing // bson.Add(new BsonElement(propName, BsonNull.Value)); } else if (aType == typeof(Byte)) { bson.Add(new BsonElement(propName, BsonValue.Create(reader.GetByte(j)))); } else if (aType == typeof(Byte[])) { bson.Add(new BsonElement(propName, BsonValue.Create(reader[j] as Byte[]))); } else if (aType == typeof(TimeSpan)) { var ts = reader.GetTimeSpan(j); var val = XmlConvert.ToString(ts); bson.Add(new BsonElement(propName, val)); } else { throw new Exception("Unable to convert: " + aType); } } if (keyColumnName != null) { var keyValue = bson.GetElement(keyColumnName).Value; if (intOffset != 0) { keyValue = BsonValue.Create(keyValue.AsInt32 + intOffset); } bson.Add(new BsonElement("_id", keyValue)); } bsonlist.Add(bson); } if (i > 0) { using (MongoSvr.RequestStart(MongoDb)) { coll = MongoDb.GetCollection <BsonDocument>(columnName); coll.InsertBatch(bsonlist); bsonlist.RemoveRange(0, bsonlist.Count); } i = 0; } } } } }