예제 #1
0
        public void adds_item_to_cache()
        {
            MongoDbApiOutputCache.Add(_user.Id.ToString(), _user, DateTime.Now.AddSeconds(60));

            var item = MongoCollection.FindAllAs <CachedItem>().FirstOrDefault();

            Assert.That(item, Is.Not.Null);
        }
예제 #2
0
        public void does_not_return_item_that_has_expired()
        {
            //add an item that expires 1 hour ago
            MongoCollection.Insert(new CachedItem("expired-item", _user, DateTime.Now.AddHours(-1)));

            var result = MongoDbApiOutputCache.Get <UserFixture>("expired-item");

            Assert.That(result, Is.Null);
        }
예제 #3
0
        public void added_item_stored_with_supplied_key()
        {
            var key = _user.Id.ToString();

            MongoDbApiOutputCache.Add(key, _user, DateTime.Now.AddSeconds(60));

            var item = MongoCollection.FindAllAs <CachedItem>().FirstOrDefault();

            Assert.That(item.Key, Is.EqualTo(key));
        }
예제 #4
0
        public void retrieves_item_from_cache()
        {
            var result = MongoDbApiOutputCache.Get <UserFixture>(_user.Id.ToString());

            Assert.That(result, Is.InstanceOf <UserFixture>());
            Assert.That(result.Id, Is.EqualTo(_user.Id));
            Assert.That(result.Name, Is.EqualTo(_user.Name));
            Assert.That(result.DateOfBirth.Day, Is.EqualTo(_user.DateOfBirth.Day));
            Assert.That(result.DateOfBirth.Month, Is.EqualTo(_user.DateOfBirth.Month));
            Assert.That(result.DateOfBirth.Year, Is.EqualTo(_user.DateOfBirth.Year));
        }
        public void retrieves_item_from_cache()
        {
            var instance = MongoDbApiOutputCache.Get(_user.Id.ToString()) as UserFixture;

            Assert.That(instance, Is.Not.Null);
            Assert.That(instance.Id, Is.EqualTo(_user.Id));
            Assert.That(instance.Name, Is.EqualTo(_user.Name));
            Assert.That(instance.DateOfBirth.Day, Is.EqualTo(_user.DateOfBirth.Day));
            Assert.That(instance.DateOfBirth.Month, Is.EqualTo(_user.DateOfBirth.Month));
            Assert.That(instance.DateOfBirth.Year, Is.EqualTo(_user.DateOfBirth.Year));
        }
예제 #6
0
        public void item_is_deleted_from_database_if_expired()
        {
            //add an item that expires 1 hour ago
            MongoCollection.Insert(new CachedItem("expired-item", _user, DateTime.Now.AddHours(-1)));

            var result          = MongoDbApiOutputCache.Get <UserFixture>("expired-item");
            var resultFromMongo = MongoCollection.FindOneAs <CachedItem>(Query.EQ("_id", new BsonString("expired-item")));

            Assert.That(result, Is.Null);
            Assert.That(resultFromMongo, Is.Null);
        }
        public MongoDbApiOutputCacheTestsBase()
        {
            var mongoUrl = new MongoUrl("mongodb://localhost/MongoDbApiOutputCache_Test");
            var client   = new MongoClient(mongoUrl);
            var server   = client.GetServer();

            MongoDatabase   = server.GetDatabase(mongoUrl.DatabaseName);
            MongoCollection = MongoDatabase.GetCollection("cache");

            MongoDbApiOutputCache = new MongoDbApiOutputCache(MongoDatabase);
        }
        public MongoDbApiOutputCacheTestsBase()
        {
            var mongoUrl = new MongoUrl("mongodb://localhost/MongoDbApiOutputCache_Test");
            var client = new MongoClient(mongoUrl);
            var server = client.GetServer();

            MongoDatabase = server.GetDatabase(mongoUrl.DatabaseName);
            MongoCollection = MongoDatabase.GetCollection("cache");

            MongoDbApiOutputCache = new MongoDbApiOutputCache(MongoDatabase);
        }
예제 #9
0
        public void adding_item_with_long_key_throws_exception()
        {
            var key = "";

            for (var i = 0; i < 100; i++)
            {
                key += Guid.NewGuid().ToString();
            }

            var exception = Assert.Throws <KeyTooLongException>(() => MongoDbApiOutputCache.Add(key, _user, DateTime.Now.AddSeconds(60)));

            Assert.That(exception.Message, Is.EqualTo("The key provided was over the 1024 bytes maximum for an indexed MongoDb field"));
        }
        public void removes_item_from_cache()
        {
            MongoDbApiOutputCache.Remove(_user1.Id.ToString());

            Assert.That(MongoCollection.Count(), Is.EqualTo(1));

            var users = MongoCollection
                        .FindAllAs <CachedItem>()
                        .Select(cachedItem => JsonSerializer.DeserializeFromString <UserFixture>(cachedItem.Value))
                        .ToList();

            Assert.That(users.First().Id, Is.Not.EqualTo(_user1.Id));
        }
예제 #11
0
        public void removes_keys_starting_with_given_string()
        {
            Assert.That(MongoCollection.Count(), Is.EqualTo(3));

            MongoDbApiOutputCache.RemoveStartsWith("apples");

            Assert.That(MongoCollection.Count(), Is.EqualTo(1));

            var result = MongoCollection.FindAllAs <CachedItem>();

            Assert.That(result.Any(x => x.Key.Equals("apples-1")), Is.False);
            Assert.That(result.Any(x => x.Key.Equals("apples-2")), Is.False);
        }
        public void items_are_deleted_from_database_automatically_when_expired()
        {
            var mongoDbApiOutputCache = new MongoDbApiOutputCache(MongoDatabase);

            mongoDbApiOutputCache.Add("something", "a value", DateTime.Now.AddMilliseconds(250));

            //wait just over a minute....
            Thread.Sleep(61000);

            //get the thing directly from mongo
            var resultDirectlyFromMongo = MongoCollection.FindOneAs<CachedItem>(Query.EQ("_id", "something"));

            Assert.That(resultDirectlyFromMongo, Is.Null);
        }
        public void items_are_deleted_from_database_automatically_when_expired()
        {
            var mongoDbApiOutputCache = new MongoDbApiOutputCache(MongoDatabase);

            mongoDbApiOutputCache.Add("something", "a value", DateTime.Now.AddMilliseconds(250));

            //wait just over a minute....
            Thread.Sleep(61000);

            //get the thing directly from mongo
            var resultDirectlyFromMongo = MongoCollection.FindOneAs <CachedItem>(Query.EQ("_id", "something"));

            Assert.That(resultDirectlyFromMongo, Is.Null);
        }
예제 #14
0
        public void adding_item_with_duplicate_key_updates_original()
        {
            MongoDbApiOutputCache.Add("user", _user, DateTime.Now.AddSeconds(60));

            var differentUser = new UserFixture {
                Name = "Simon"
            };

            MongoDbApiOutputCache.Add("user", differentUser, DateTime.Now.AddSeconds(60));

            Assert.That(MongoCollection.Count(), Is.EqualTo(1));

            var item = JsonSerializer.DeserializeFromString <UserFixture>(MongoCollection.FindOneAs <CachedItem>().Value);

            Assert.That(item, Is.Not.Null);
            Assert.That(item.Id, Is.EqualTo(differentUser.Id));
            Assert.That(item.Name, Is.EqualTo(differentUser.Name));
        }
예제 #15
0
        public void added_item_stored_with_expiry()
        {
            var expiration = DateTime.Now.AddSeconds(60);

            MongoDbApiOutputCache.Add(_user.Id.ToString(), _user, expiration);

            var item         = MongoCollection.FindOneAs <CachedItem>();
            var itemExpireAt = item.ExpireAt;

            //todo: would be good to check they are the same value.. without this rubbish!
            //something like:
            //Assert.That(DateTime.Compare(item.ExpireAt, new DateTime(expiration.Ticks)), Is.EqualTo(0));

            Assert.That(expiration.Day, Is.EqualTo(itemExpireAt.Day));
            Assert.That(expiration.Month, Is.EqualTo(itemExpireAt.Month));
            Assert.That(expiration.Year, Is.EqualTo(itemExpireAt.Year));
            Assert.That(expiration.Hour, Is.EqualTo(itemExpireAt.Hour));
            Assert.That(expiration.Minute, Is.EqualTo(itemExpireAt.Minute));
            Assert.That(expiration.Second, Is.EqualTo(itemExpireAt.Second));
            Assert.That(expiration.Millisecond, Is.EqualTo(itemExpireAt.Millisecond));
        }
 public void collection_name_is_cache_by_default()
 {
     var mongoDbApiOutputCache = new MongoDbApiOutputCache(MongoDatabase);
     Assert.That(mongoDbApiOutputCache.MongoCollection.Name, Is.EqualTo("cache"));
 }
 public void can_specify_collection_name()
 {
     var mongoDbApiOutputCache = new MongoDbApiOutputCache(MongoDatabase, "myCache");
     Assert.That(mongoDbApiOutputCache.MongoCollection.Name, Is.EqualTo("myCache"));
 }
        public void returns_true_if_item_is_in_collection()
        {
            var result = MongoDbApiOutputCache.Contains(_user1.Id.ToString());

            Assert.That(result, Is.True);
        }
예제 #19
0
        public void returns_null_if_item_not_in_collection()
        {
            var result = MongoDbApiOutputCache.Get <UserFixture>("unknown key");

            Assert.That(result, Is.Null);
        }
        public void can_specify_collection_name()
        {
            var mongoDbApiOutputCache = new MongoDbApiOutputCache(MongoDatabase, "myCache");

            Assert.That(mongoDbApiOutputCache.MongoCollection.Name, Is.EqualTo("myCache"));
        }
        public void returns_false_if_item_is_not_in_collection()
        {
            var result = MongoDbApiOutputCache.Contains("i know this won't be there");

            Assert.That(result, Is.False);
        }
        public void collection_name_is_cache_by_default()
        {
            var mongoDbApiOutputCache = new MongoDbApiOutputCache(MongoDatabase);

            Assert.That(mongoDbApiOutputCache.MongoCollection.Name, Is.EqualTo("cache"));
        }