public void BypassCacheForRecentResults()
        {
            // Setup the cache
            var cachedResults = new TwitterSearchResultCollection();
            cachedResults.Add(new TwitterSearchResult() { Id = 1, Text = "This is just a #test" });
            _localCache.Stub(c => c.PageResults).Return(cachedResults);
            _localCache.Stub(c => c.PageNumber).Return(1);

            // Setup the Twitterizer response
            TwitterResponse<TwitterSearchResultCollection> response = new TwitterResponse<TwitterSearchResultCollection>();
            response.ResponseObject = new TwitterSearchResultCollection();
            response.ResponseObject.Add(new TwitterSearchResult() { Id = 1, Text = "This is just a #test" });
            response.ResponseObject.Add(new TwitterSearchResult() { Id = 2, Text = "This is just another #test" });
            _searchService
                .Expect(s => s.Search(Arg<string>.Is.Anything, Arg<SearchOptions>.Is.Anything))
                .Return(response);

            TwitterSearchClient client = new TwitterSearchClient(_localCache, _searchService);
            var results = client.Search("#test", 1, 10, true); //newSearch means ignore the cache
            Assert.IsFalse(AreEquivalent(cachedResults, results));
            Assert.IsTrue(AreEquivalent(response.ResponseObject, results));

            _searchService.VerifyAllExpectations();
            _localCache.VerifyAllExpectations();
        }
        public void MaintainPaginationConsistency()
        {
            // Setup the cache
            _localCache.Expect(c => c.PageNumber).Return(10);
            _localCache.Expect(c => c.MaxId).Return(999);

            // Setup the Twitterizer response
            TwitterResponse<TwitterSearchResultCollection> response = new TwitterResponse<TwitterSearchResultCollection>();
            response.ResponseObject = new TwitterSearchResultCollection();
            response.ResponseObject.Add(new TwitterSearchResult() { Id = 1, Text = "This is just a #test" });
            response.ResponseObject.Add(new TwitterSearchResult() { Id = 2, Text = "This is just another #test" });
            _searchService
                .Expect(s => s.Search(Arg<string>.Is.Anything, Arg<SearchOptions>.Matches(so => so.MaxId == 999 && so.PageNumber == 2)))
                .Return(response);

            TwitterSearchClient client = new TwitterSearchClient(_localCache, _searchService);
            var results = client.Search("#test", 11, 10, false);
            Assert.IsTrue(AreEquivalent(response.ResponseObject, results));

            _searchService.VerifyAllExpectations();
            _localCache.VerifyAllExpectations();
        }
 private void PopulateSearchResultModel(TwitterSearchModel model)
 {
     // Retrieve data and populate the search result model for display
     model.SearchResultModel = new TwitterSearchResultModel();
     try
     {
         TwitterSearchClient searchClient = new TwitterSearchClient(
             new SessionStateCache(this.HttpContext.Session),
             new TwitterizerSearchService());
         model.SearchResultModel.TweetResults = searchClient.Search(
             model.SearchText,
             model.ClientPage,
             model.ClientPageSize,
             model.NewSearch);
     }
     catch (TwitterSearchException twitterSearchException)
     {
         model.SearchResultModel.ErrorMessage = "An error occurred when attempting to search Twitter's records, please try again later.";
     }
     catch (Exception exception)
     {
         model.SearchResultModel.ErrorMessage = "An unexpected error occurred, yikes!";
     }
 }
        public void RetrieveTweetsFromCache()
        {
            var cachedResults = new TwitterSearchResultCollection();
            cachedResults.Add(new TwitterSearchResult() { Id = 1, Text = "This is just a #test" });

            _searchService.AssertWasNotCalled(s => s.Search(Arg<string>.Is.Anything, Arg<SearchOptions>.Is.Anything));

            _localCache.Expect(c => c.PageResults).Return(cachedResults);
            _localCache.Expect(c => c.PageNumber).Return(1);

            TwitterSearchClient client = new TwitterSearchClient(_localCache, _searchService);
            var results = client.Search("#test", 1, 10, false);
            Assert.IsTrue(AreEquivalent(cachedResults, results));

            _searchService.VerifyAllExpectations();
            _localCache.VerifyAllExpectations();
        }
 public void ClientPage_CannotBeZero()
 {
     TwitterSearchClient client = new TwitterSearchClient(_localCache, _searchService);
     client.Search("#test", 0, 10, true);
 }
        public void StoreTweetsLocally()
        {
            TwitterResponse<TwitterSearchResultCollection> response = new TwitterResponse<TwitterSearchResultCollection>();
            response.ResponseObject = new TwitterSearchResultCollection();
            response.ResponseObject.Add(new TwitterSearchResult() { Id = 1, Text = "This is just a #test" });

            _searchService
                .Expect(s => s.Search(Arg<string>.Is.Anything, Arg<SearchOptions>.Is.Anything))
                .Return(response);

            _localCache.Expect(c => c.PageResults = response.ResponseObject);

            TwitterSearchClient client = new TwitterSearchClient(_localCache, _searchService);
            client.Search("#test", 1, 10, true);

            _searchService.VerifyAllExpectations();
            _localCache.VerifyAllExpectations();
        }