コード例 #1
0
ファイル: FlickrTest.cs プロジェクト: ARLM-Attic/linq2flickr
        public void DoPopularTagTest()
        {
            MockManager.Init();

            Mock restBuilderMock = MockManager.Mock <CollectionBuilder <PopularTag> >(Constructor.NotMocked);

            // set the expectation.
            restBuilderMock.ExpectAndReturn("GetElement", MockElement(ResourceNs + ".HotTagGetList.xml"));

            var query = from tag in context.Tags
                        where tag.Period == TagPeriod.Day && tag.Count == 6
                        orderby tag.Text ascending
                        select tag;

            int count = query.Count();

            // see if the expected and returned value are same.
            Assert.IsTrue(count == 6);

            PopularTag firstTag = query.First();

            Assert.IsTrue(firstTag.Score == 4);

            PopularTag lastTag = query.Last();

            Assert.IsTrue(lastTag.Score == 10);

            MockManager.Verify();
        }
コード例 #2
0
ファイル: FlickrTest.cs プロジェクト: ARLM-Attic/linq2flickr
        public void DoPeopleTest()
        {
            MockManager.Init();

            string id = "12037949754@N01";

            Mock photoMock  = MockManager.Mock <PhotoRepository>(Constructor.NotMocked);
            Mock peopleMock = MockManager.Mock <PeopleRepository>(Constructor.NotMocked);

            Mock restBuilderMock = MockManager.Mock <CollectionBuilder <People> >(Constructor.NotMocked);

            // this also ensures that GetNsid should be called from photo repo only.
            photoMock.ExpectAndReturn("GetNsid", id);
            peopleMock.ExpectAndReturn("GetSignature", "yyyy");
            restBuilderMock.ExpectAndReturn("GetElement", MockElement(ResourceNs + ".PeopleInfo.xml"));

            var query = from p in context.Peoples
                        where p.Username == "bees"
                        select p;

            People people = query.Single();

            Assert.IsTrue(people.Id == id && people.Username == "bees");

            MockManager.Verify();
        }
コード例 #3
0
        public void SearchPhotos()
        {
            Mock photoMock = MockManager.Mock <PhotoRepository>(Constructor.NotMocked);

            string searchUrl = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=" + API_KEY + "&SearchMode=1&tags=microsoft&page=1&per_page=100";

            photoMock.ExpectAndReturn("GetElement", MockElement("Linq.Flickr.Test.Responses.Search.xml")).Args(searchUrl);


            var query = from photo in _context.Photos
                        where photo.SearchMode == SearchMode.TagsOnly && photo.SearchText == "microsoft"
                        select photo;

            int count = query.Count();

            Photo first = query.First();

            Assert.IsTrue(first.SharedProperty.Perpage == count);
            Assert.IsTrue(first.Title == "Mug Shot" && first.Id == "2428052817");

            Photo last = query.Last();

            Assert.IsTrue(last.SharedProperty.Page == 1);
            Assert.IsTrue(last.SharedProperty.Total == 105714);
            Assert.IsTrue(last.Title == "attendee event" && last.Id == "2423378493");
        }
コード例 #4
0
        public void AuthicatedGet()
        {
            MockManager.Init();

            Mock photoMock = MockManager.Mock <PhotoRepository>(Constructor.NotMocked);

            string searchUrl = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=" + API_KEY + "&privacy_filter=0&user_id=xUser&api_sig=yyyy&page=1&per_page=100&auth_token=xyz";

            photoMock.ExpectAndReturn("GetFrob", "1");
            photoMock.ExpectAndReturn("GetSignature", "yyyy");
            photoMock.ExpectAndReturn("CreateDesktopToken", "xyz").Args("1", true, Permission.Delete.ToString().ToLower());
            photoMock.ExpectAndReturn("GetNSID", "xUser").Args("flickr.people.findByUsername", "username", "neetulee");
            photoMock.ExpectAndReturn("GetElement", MockElement("Linq.Flickr.Test.Responses.Owner.xml")).Args(searchUrl);


            var query = from photo in _context.Photos
                        where photo.ViewMode == ViewMode.Owner && photo.User == "neetulee"
                        select photo;


            Photo lastPhoto = query.Last();

            Assert.IsTrue(lastPhoto.ViewMode == ViewMode.Private);

            MockManager.Verify();
        }
コード例 #5
0
        public void ImageDownloadExceptionTest()
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[]       contentAsBytes       = encoding.GetBytes("This is not image data");
            MemoryStream sourceResponseStream = new MemoryStream();

            sourceResponseStream.Write(contentAsBytes, 0, contentAsBytes.Length);
            sourceResponseStream.Position = 0;

            Mock <HttpWebRequest>        mockRequest  = MockManager.Mock <HttpWebRequest>(Constructor.NotMocked);
            MockObject <HttpWebResponse> mockResponse = MockManager.MockObject <HttpWebResponse>(Constructor.Mocked);

            mockResponse.ExpectAndReturn("GetResponseStream", sourceResponseStream);
            mockRequest.ExpectAndReturn("GetResponse", mockResponse.Object);

            HttpWebService service = new HttpWebService();

            try
            {
                service.DownloadImage("http://www.battleclinic.com");
                Assert.Fail("Expected exception was not thrown");
            }
            catch (HttpWebServiceException ex)
            {
                Assert.AreEqual(HttpWebServiceExceptionStatus.ImageException, ex.Status);
            }
            finally
            {
                sourceResponseStream.Close();
            }
        }
コード例 #6
0
        public void StringAsyncDownloadTest()
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[]       contentAsBytes       = encoding.GetBytes(TestResources.CharacterSheet);
            MemoryStream sourceResponseStream = new MemoryStream();

            sourceResponseStream.Write(contentAsBytes, 0, contentAsBytes.Length);
            sourceResponseStream.Position = 0;
            Mock <HttpWebRequest>        mockRequest  = MockManager.Mock <HttpWebRequest>(Constructor.NotMocked);
            MockObject <HttpWebResponse> mockResponse = MockManager.MockObject <HttpWebResponse>(Constructor.Mocked);

            mockResponse.ExpectAndReturn("GetResponseStream", sourceResponseStream);
            mockRequest.ExpectAndReturn("GetResponse", mockResponse.Object);

            _stringAsyncCompletedTrigger = new AutoResetEvent(false);
            HttpWebService service = new HttpWebService();

            service.DownloadStringAsync("http://www.battleclinic.com", StringAysncDownloadTestCompleted, null);
            _stringAsyncCompletedTrigger.WaitOne();
            if (_stringAsyncDownloadResult.Error != null)
            {
                Assert.Fail(_stringAsyncDownloadResult.Error.Message);
            }
            Assert.AreEqual(TestResources.CharacterSheet, _stringAsyncDownloadResult.Result);
            sourceResponseStream.Close();
            _stringAsyncCompletedTrigger = null;
            _stringAsyncDownloadResult   = null;
        }
コード例 #7
0
        public void ImageAsyncDownloadTest()
        {
            MemoryStream sourceResponseStream = new MemoryStream();

            TestResources.testimage.Save(sourceResponseStream, ImageFormat.Png);
            sourceResponseStream.Position = 0;

            Mock <HttpWebRequest>        mockRequest  = MockManager.Mock <HttpWebRequest>(Constructor.NotMocked);
            MockObject <HttpWebResponse> mockResponse = MockManager.MockObject <HttpWebResponse>(Constructor.Mocked);

            mockResponse.ExpectAndReturn("GetResponseStream", sourceResponseStream);
            mockRequest.ExpectAndReturn("GetResponse", mockResponse.Object);

            _imageAsyncCompletedTrigger = new AutoResetEvent(false);
            HttpWebService service = new HttpWebService();

            service.DownloadImageAsync("http://www.battleclinic.com", ImageAysncDownloadTestCompleted, null);
            _imageAsyncCompletedTrigger.WaitOne();
            if (_imageAsyncDownloadResult.Error != null)
            {
                Assert.Fail(_imageAsyncDownloadResult.Error.Message);
            }
            Assert.IsNotNull(_imageAsyncDownloadResult.Result, "No image retrieved");
            _imageAsyncCompletedTrigger = null;
            _imageAsyncDownloadResult   = null;
        }
コード例 #8
0
        public void XmlDownloadTest()
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[]       contentAsBytes       = encoding.GetBytes(TestResources.CharacterSheet);
            MemoryStream sourceResponseStream = new MemoryStream();

            sourceResponseStream.Write(contentAsBytes, 0, contentAsBytes.Length);
            sourceResponseStream.Position = 0;
            XmlDocument contentAsXml = new XmlDocument();

            contentAsXml.Load(new StringReader(TestResources.CharacterSheet));

            Mock <HttpWebRequest>        mockRequest  = MockManager.Mock <HttpWebRequest>(Constructor.NotMocked);
            MockObject <HttpWebResponse> mockResponse = MockManager.MockObject <HttpWebResponse>(Constructor.Mocked);

            mockResponse.ExpectAndReturn("GetResponseStream", sourceResponseStream);
            mockRequest.ExpectAndReturn("GetResponse", mockResponse.Object);

            HttpWebService service = new HttpWebService();
            XmlDocument    result  = service.DownloadXml("http://www.battleclinic.com");

            StringAssert.AreEqualIgnoringCase(contentAsXml.ToString(), result.ToString());

            sourceResponseStream.Close();
        }
コード例 #9
0
        public Mock FakeHttpRequestObject(Stream stream)
        {
            httpRequestMock = MockManager.Mock(typeof(HttpWebRequest));

            httpRequestMock.ExpectSet("ContentType");
            httpRequestMock.ExpectAndReturn("GetRequestStream", stream);

            return(httpRequestMock);
        }
コード例 #10
0
        public void Photo_Comment()
        {
            MockManager.Init();

            #region Add comment
            Mock photoCommentAdd = MockManager.Mock <CommentRepository>(Constructor.NotMocked);

            photoCommentAdd.ExpectAndReturn("Authenticate", "1234").Args(Permission.Delete.ToString());
            photoCommentAdd.ExpectAndReturn("GetSignature", "yyyy");
            photoCommentAdd.ExpectAndReturn("DoHTTPPost", MockElement(RESOURCE_NS + ".AddComment.xml").ToString());

            Comment comment = new Comment();

            comment.PhotoId = COMMENT_PHOTO_ID;
            comment.Text    = "Testing comment add [LINQ.Flickr]";

            _context.Photos.Comments.Add(comment);
            _context.SubmitChanges();

            Assert.IsTrue(comment.Id == "1");
            #endregion

            #region Get added comment
            Mock photoCommentMock = MockManager.Mock <CommentRepository>(Constructor.NotMocked);
            Mock httpCallBase     = MockManager.Mock <RestToCollectionBuilder <Comment> >(Constructor.NotMocked);

            photoCommentMock.ExpectAndReturn("GetSignature", "yyyy");
            httpCallBase.ExpectAndReturn("GetElement", MockElement(RESOURCE_NS + ".GetComment.xml"));

            var query = from c in _context.Photos.Comments
                        where c.PhotoId == COMMENT_PHOTO_ID && c.Id == comment.Id
                        select c;

            Comment commentGet = query.Single();

            Assert.IsTrue(commentGet.Author == "11" && commentGet.PhotoId == COMMENT_PHOTO_ID && commentGet.AuthorName == "John Doe");
            #endregion

            #region Delete added
            Mock photoCommentDelete = MockManager.Mock <CommentRepository>(Constructor.NotMocked);

            photoCommentDelete.ExpectAndReturn("Authenticate", "1234").Args(Permission.Delete.ToString());
            photoCommentDelete.ExpectAndReturn("GetSignature", "yyyy");
            photoCommentDelete.ExpectAndReturn("DoHTTPPost", MockElement(RESOURCE_NS + ".DeleteComment.xml").ToString());

            _context.Photos.Comments.Remove(comment);
            _context.SubmitChanges();
            #endregion

            MockManager.Verify();
        }
コード例 #11
0
        public void Init()
        {
            _context = Isolate.Fake.Instance <ActionExecutingContext>();
            _result  = Isolate.Fake.Instance <ActionExecutedContext>();

            _session     = Isolate.Fake.Instance <ISession>();
            _transaction = Isolate.Fake.Instance <ITransaction>();
            Isolate.WhenCalled(() => _session.Transaction).WillReturn(_transaction);
            Isolate.WhenCalled(() => _transaction.IsActive).WillReturn(true);

            _mock = MockManager.Mock <TransactionAttribute>();
            _mock.CallBase.ExpectCall("OnActionExecuted");

            _attr = new TransactionAttribute();
            Isolate.NonPublic.Property.WhenGetCalled(_attr, "Session").WillReturn(_session);

            _loginController = ObjectFactory.GetInstance <LoginController>();
            _homeController  = ObjectFactory.GetInstance <HomeController>();
        }
コード例 #12
0
        public void PhotoComment()
        {
            MockManager.Init();
            Mock photoCommentMock = MockManager.Mock <CommentRepository>(Constructor.NotMocked);

            photoCommentMock.ExpectAndReturn("Authenticate", "1234").Args(Permission.Delete.ToString());
            photoCommentMock.ExpectAndReturn("GetSignature", "yyyy");
            photoCommentMock.ExpectAndReturn("DoHTTPPost", MockElement(RESOURCE_NS + ".AddComment.xml").ToString());

            Comment comment = new Comment();

            comment.PhotoId = "1x";
            comment.Text    = "Testing comment add [LINQ.Flickr]";

            _context.Photos.Comments.Add(comment);
            _context.SubmitChanges();

            Assert.IsTrue(comment.Id == "1");

            MockManager.Verify();
        }
コード例 #13
0
        public void ImageDownloadTest()
        {
            MemoryStream sourceResponseStream = new MemoryStream();

            TestResources.testimage.Save(sourceResponseStream, ImageFormat.Png);
            sourceResponseStream.Position = 0;

            Mock <HttpWebRequest>        mockRequest  = MockManager.Mock <HttpWebRequest>(Constructor.NotMocked);
            MockObject <HttpWebResponse> mockResponse = MockManager.MockObject <HttpWebResponse>(Constructor.Mocked);

            mockResponse.ExpectAndReturn("GetResponseStream", sourceResponseStream);
            mockRequest.ExpectAndReturn("GetResponse", mockResponse.Object);

            HttpWebService service = new HttpWebService();

            try
            {
                service.DownloadImage("http://www.battleclinic.com");
            }
            finally
            {
                sourceResponseStream.Close();
            }
        }
コード例 #14
0
 public FakeFlickrRepository()
 {
     mockRepository = MockManager.Mock <T>(Constructor.NotMocked);
 }
コード例 #15
0
 public void MockRESTBuilderGetElement(string resource)
 {
     httpCallBase = MockManager.Mock <CollectionBuilder <TItem> >(Constructor.NotMocked);
     httpCallBase.ExpectAndReturn("GetElement", MockElement(resource));
 }