public void RemoveGivenId_StateUnderTest_ExpectedBehavior1() { // Arrange var service = this.CreateService(); BsonObjectId id = ObjectId.GenerateNewId(); User user = new User(); user.Username = id.ToString(); user.Id = id.ToString(); service.Create(user); var existsBeforeRemove = service.UserExistsByUsername(id.ToString()); // Act service.Remove(id.ToString()); var existsAfterRemove = service.UserExistsByUsername(id.ToString()); // Assert Assert.IsTrue(existsBeforeRemove); Assert.IsFalse(existsAfterRemove); }
public void Update_StateUnderTest_ExpectedBehavior() { // Arrange var service = this.CreateService(); BsonObjectId id = ObjectId.GenerateNewId(); User user = new User(); user.Username = id.ToString(); user.Id = id.ToString(); service.Create(user); BsonObjectId id2 = ObjectId.GenerateNewId(); var result1 = service.UserExistsByUsername(id2.ToString()); user.Username = id2.ToString(); // Act service.Update( user.Id, user); var result2 = service.UserExistsByUsername(id2.ToString()); // Assert Assert.IsFalse(result1); Assert.IsTrue(result2); }
private static void WriteObjectId(object result, BsonDocument document) { PropertyInfo objectIdProperty = result.GetType().GetProperty("ObjectId"); BsonObjectId bsonObjectId = (BsonObjectId)document.GetValue("_id"); objectIdProperty.SetValue(result, bsonObjectId.ToString(), null); }
public void TestDoesNotThrowStackOverflowExceptionWhenConvertingToBsonString() { var id1 = new BsonObjectId(ObjectId.GenerateNewId()); var id2 = (BsonString)((IConvertible)id1).ToType(typeof(BsonString), null); Assert.Equal(id1.ToString(), id2.AsString); }
private void UpdateIdIfNone(Id id) { if (id == Id.None) { var objectId = ObjectId.GenerateNewId(); var bsonObjectId = new BsonObjectId(objectId); id.SetIfNone(bsonObjectId.ToString()); } }
public void TestDoesNotThrowStackOverflowExceptionWhenConvertingToBsonString() { BsonObjectId id1 = BsonObjectId.GenerateNewId(); BsonString id2 = null; Assert.DoesNotThrow(() => { id2 = (BsonString)((IConvertible)id1).ToType(typeof(BsonString), null); }); Assert.AreEqual(id1.ToString(), id2.AsString); }
public void TestByteArrayConstructor() { byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; var objectId = new BsonObjectId(bytes); Assert.AreEqual(0x01020304, objectId.Timestamp); Assert.AreEqual(0x050607, objectId.Machine); Assert.AreEqual(0x0809, objectId.Pid); Assert.AreEqual(0x0a0b0c, objectId.Increment); Assert.AreEqual(0x05060708090a0b0c, objectId.MachinePidIncrement); Assert.AreEqual(Bson.UnixEpoch.AddSeconds(0x01020304), objectId.CreationTime); Assert.AreEqual("0102030405060708090a0b0c", objectId.ToString()); Assert.IsTrue(bytes.SequenceEqual(objectId.ToByteArray())); }
public void Get_StateUnderTest_ExpectedBehavior1() { // Arrange var service = this.CreateService(); BsonObjectId id = ObjectId.GenerateNewId(); // Act var result = service.Get( id.ToString()); // Only checking if the get function doesnt cause an error // Assert Assert.Pass(); }
public void TestStringConstructor() { byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; var objectId = new BsonObjectId("0102030405060708090a0b0c"); Assert.AreEqual(0x01020304, objectId.Timestamp); Assert.AreEqual(0x050607, objectId.Machine); Assert.AreEqual(0x0809, objectId.Pid); Assert.AreEqual(0x0a0b0c, objectId.Increment); Assert.AreEqual(0x050607, objectId.Machine); Assert.AreEqual(0x0809, objectId.Pid); Assert.AreEqual(0x0a0b0c, objectId.Increment); Assert.AreEqual(BsonConstants.UnixEpoch.AddSeconds(0x01020304), objectId.CreationTime); Assert.AreEqual("0102030405060708090a0b0c", objectId.ToString()); Assert.IsTrue(bytes.SequenceEqual(objectId.ToByteArray())); }
public void TestIntIntShortIntConstructor() { #pragma warning disable 618 byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; var objectId = new BsonObjectId(0x01020304, 0x050607, 0x0809, 0x0a0b0c); Assert.AreEqual(0x01020304, objectId.Timestamp); Assert.AreEqual(0x050607, objectId.Machine); Assert.AreEqual(0x0809, objectId.Pid); Assert.AreEqual(0x0a0b0c, objectId.Increment); Assert.AreEqual(0x050607, objectId.Machine); Assert.AreEqual(0x0809, objectId.Pid); Assert.AreEqual(0x0a0b0c, objectId.Increment); Assert.AreEqual(BsonConstants.UnixEpoch.AddSeconds(0x01020304), objectId.CreationTime); Assert.AreEqual("0102030405060708090a0b0c", objectId.ToString()); Assert.IsTrue(bytes.SequenceEqual(objectId.ToByteArray())); #pragma warning restore }
public void TestIntIntShortIntConstructor() { #pragma warning disable 618 byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; var objectId = new BsonObjectId(0x01020304, 0x050607, 0x0809, 0x0a0b0c); Assert.Equal(0x01020304, objectId.Timestamp); Assert.Equal(0x050607, objectId.Machine); Assert.Equal(0x0809, objectId.Pid); Assert.Equal(0x0a0b0c, objectId.Increment); Assert.Equal(0x050607, objectId.Machine); Assert.Equal(0x0809, objectId.Pid); Assert.Equal(0x0a0b0c, objectId.Increment); Assert.Equal(BsonConstants.UnixEpoch.AddSeconds(0x01020304), objectId.CreationTime); Assert.Equal("0102030405060708090a0b0c", objectId.ToString()); Assert.True(bytes.SequenceEqual(objectId.ToByteArray())); #pragma warning restore }
public Dictionary <string, int> GetArticlesCommentsAmount() { string stringCommand = @" function rec(comments) { var res = comments.length; for(var i=0;i<comments.length; i++) { var comment = comments[i]; if(comment.Replies.length > 0) { res += rec(comment.Replies); } } return res; } function map(article) { return { id: article._id , count: rec(article.Comments) }; } db.Articles.find().map(map)"; BsonDocument command = new BsonDocument("eval", stringCommand); dynamic res = db.RunCommand <dynamic>(command); Dictionary <string, int> dicitionary = new Dictionary <string, int>(); foreach (dynamic r in res) { foreach (var l in r.Value) { BsonObjectId articleId = l.id; int count = Convert.ToInt32(l.count); dicitionary.Add(articleId.ToString(), count); } return(dicitionary); } return(dicitionary); }
public void UserExistsByUsername_StateUnderTest_ExpectedBehavior() { // Arrange var service = this.CreateService(); BsonObjectId id = ObjectId.GenerateNewId(); string username = id.ToString(); User user = new User(); user.Username = username; service.Create(user); // Act var result = service.UserExistsByUsername( username); // Assert Assert.IsTrue(result); }
public void GetByUsername_StateUnderTest_ExpectedBehavior() { // Arrange var service = this.CreateService(); BsonObjectId id = ObjectId.GenerateNewId(); string username = id.ToString(); User user = new User(); user.Username = username; service.Create(user); // Act var result = service.GetByUsername(username); var result2 = service.GetByUsername("notaUser"); // Assert Assert.AreEqual(username, result.Username); Assert.IsNull(result2); }
public void FindPlayerById() { //Arrange var plainObjectId = new ObjectId(); var bsonObjectId = new BsonObjectId(plainObjectId); var convertedId = bsonObjectId.ToString(); var player = new Player() { Id = bsonObjectId, Name = "Lorem Ipsum", Losses = 4, Wins = 8 }; var mockService = new Mock <IPlayerService>(); mockService.Setup(x => x.SearchById(bsonObjectId)).Returns(player); var playerController = new PlayerController(mockService.Object); //Act var returnedPlayer = playerController.FindPlayerById(convertedId); //Assert returnedPlayer.ToJson().Should().Contain(convertedId); }
public static void EnsureSeedData(this TrulliContext db) { BsonObjectId donAntonio = ObjectId.GenerateNewId(); BsonObjectId cinquenoci = ObjectId.GenerateNewId(); BsonObjectId carbonaio = ObjectId.GenerateNewId(); if (db.Properties.CountDocuments(new BsonDocument()) == 0 || db.Trulli.CountDocuments(new BsonDocument()) == 0) { var properties = new List <Property> { new Property { _id = donAntonio.ToString(), Name = "Trulli Don Antonio", City = "Locorotondo", Street = "S.C. 21 C.da Crocifisso n. 9", Spa = true, SwimmingPool = false }, new Property { _id = cinquenoci.ToString(), Name = "Trulli Cinquenoci", City = "Locorotondo", Street = "S.C. 126 C.da Cinquenoci n. 6", Spa = false, SwimmingPool = true }, new Property { _id = carbonaio.ToString(), Name = "Casa del Carbonaio", City = "Locorotondo", Street = "Via Garibaldi n. 17", Spa = false, SwimmingPool = false } }; var trulli = new List <Trullo> { new Trullo { _id = ObjectId.GenerateNewId().ToString(), Property_id = donAntonio.ToString(), Name = "Trullo Panoramico", Description = "Trullo con soppalco con 2 camere da letto e 2 bagni", Capacity = 4, Price = 100 }, new Trullo { _id = ObjectId.GenerateNewId().ToString(), Property_id = donAntonio.ToString(), Name = "Trullo dell'Interprete", Description = "Trullo in ambiente unico con 1 camera da letto e 1 bagno", Capacity = 3, Price = 80 }, new Trullo { _id = ObjectId.GenerateNewId().ToString(), Property_id = donAntonio.ToString(), Name = "Trullo dell'Arco", Description = "Trullo ampio con 2 camere da letto e 1 bagno", Capacity = 4, Price = 120 }, new Trullo { _id = ObjectId.GenerateNewId().ToString(), Property_id = cinquenoci.ToString(), Name = "Trullo Mille Volte", Description = "Trullo a volte in pietra con 3 camere da letto e 2 bagni", Capacity = 7, Price = 180 }, new Trullo { _id = ObjectId.GenerateNewId().ToString(), Property_id = cinquenoci.ToString(), Name = "Trullo Romantico", Description = "Trullo in ambiente unico con 1 camera da letto e 1 bagno", Capacity = 2, Price = 60 }, new Trullo { _id = ObjectId.GenerateNewId().ToString(), Property_id = cinquenoci.ToString(), Name = "Trullo Nuovo", Description = "Trullo di nuova costruzione con 2 camere da letto e 1 bagno", Capacity = 4, Price = 120 }, new Trullo { _id = ObjectId.GenerateNewId().ToString(), Property_id = carbonaio.ToString(), Name = "Trullo del Carbonaio", Description = "Trullo a volte in pietra nel centro storico con 1 camera da letto e 2 bagni", Capacity = 7, Price = 110 }, }; db.Properties.InsertManyAsync(properties); db.Trulli.InsertManyAsync(trulli); } }
public override string ToString() { return(BsonObjectId.ToString()); }