def test_act_direct_message(self): moxer = Mox() api = _create_default_mocks(moxer) _build_standard_config(moxer) bundle = _create_actor_and_delegates(api, moxer) actor = bundle.actor direct_id = 1 direct = moxer.CreateMock(twitter.DirectMessage) user = moxer.CreateMock(twitter.User) direct.id = direct_id direct.sender_screen_name = "mikemattozzi" direct.text = "why is blood spattered all over your car?" _return_direct_messages(api, [direct]) post = moxer.CreateMockAnything() post.id = 101 _return_replies(api, ()) TwitterResponseAccessor.get_by_message_id(str(direct_id)) ArtifactAccessor.search("spattered").AndReturn(create_content_list(10)) # response api.PostDirectMessage(direct.sender_screen_name, IgnoreArg()).AndReturn(post) TwitterResponseAccessor.create(str(direct.id), response_id=str(post.id), user=direct.sender_screen_name) post.AsDict().AndReturn({}) moxer.ReplayAll() actor.act() moxer.VerifyAll()
class BlogCommentTest(TestCase): def setUp(self): initialize(rdb_path='sqlite:///:memory:') self.mocker = Mox() model = KMBlogComment() model.save() # id = 1のデータを登録 def tearDown(self): pass def test_set_data(self): model = KMBlogComment() test_article_id = 'test_article_id' test_comment = 'test_comment' data = self.mocker.CreateMock(KMData) data.get_request_parameter('article_id', default='', decode=True).AndReturn(test_article_id) data.get_request_parameter('comment', default='', decode=True).AndReturn(test_comment) self.mocker.ReplayAll() model.set_data(data); # KMDataで指定した値が設定されること eq_(model.article_id, test_article_id) eq_(model.comment, test_comment) eq_(model.error, None) self.mocker.UnsetStubs() self.mocker.VerifyAll()
class SeeAlsoFinderTest(unittest.TestCase): def setUp(self): self.finder = SeeAlsoFinder() self.mox = Mox() debian.services.RES_BASEURI = "base" debian.services.FROM_GRAPH = None def test__fetch_seealso_uris(self): sourcename = "pkg" unversionedsourceuri = "base/source/%s" % sourcename mock = self.mox.CreateMock(SPARQLQueryProcessor) expectedarg = \ r".+SELECT\s*\?uri.+\<%s\>\sa\sdeb:UnversionedSource\s*;\s*rdfs:seeAlso\s\?uri" % \ re.escape(unversionedsourceuri) mock.execute_query(Regex(expectedarg, flags=re.DOTALL)) binding1 = {'uri': {'value': "http://example.org/1"}} binding2 = {'uri': {'value': "http://example.org/2"}} bindings = [binding1, binding2] fakeresults = {'results': {'bindings': bindings}} self.finder.processor = mock self.mox.ReplayAll() self.finder.processor.results = fakeresults uris = self.finder._fetch_seealso_uris(unversionedsourceuri) self.mox.VerifyAll() self.assertEqual(2, len(uris)) self.assertTrue("http://example.org/1" in uris) self.assertTrue("http://example.org/2" in uris) def test_find_forbidden_characters(self): self.assertRaises(SPARQLQueryBuilderPackageNameSchemeError, self.finder.find, "{}@") def test_find(self): srcpkgname = "source" srcpkguri = "base/source/%s" % srcpkgname self.mox.StubOutWithMock(self.finder, "_fetch_seealso_uris") returnvals = ["http://example.org/1", "http://example.org/2"] self.finder._fetch_seealso_uris(srcpkguri).AndReturn(returnvals) self.mox.ReplayAll() uris = self.finder.find(srcpkgname) self.mox.VerifyAll() self.assertEqual(2, len(uris)) self.assertTrue("http://example.org/1" in uris) self.assertTrue("http://example.org/2" in uris) def test_find_escape(self): srcpkgname = "source.+-" srcpkguri = "base/source/%s" % "source.%2B-" self.mox.StubOutWithMock(self.finder, "_fetch_seealso_uris") returnvals = ["http://example.org/1", "http://example.org/2"] self.finder._fetch_seealso_uris(srcpkguri).AndReturn(returnvals) self.mox.ReplayAll() uris = self.finder.find(srcpkgname) self.mox.VerifyAll()
def test_get_friends(self): m = Mox() api = m.CreateMock(Api) ro_api = ReadOnlyTwitterApi(api) api.GetFriends() m.ReplayAll() ro_api.GetFriends() m.VerifyAll()
def test_get_user(self): m = Mox() api = m.CreateMock(Api) ro_api = ReadOnlyTwitterApi(api) api.GetUser(IgnoreArg()) m.ReplayAll() ro_api.GetUser("mhawthorne") m.VerifyAll()
def test_after_each_all_is_executed_before_each_all(): "terrain.before.each_all and terrain.after.each_all decorators" import lettuce from lettuce.fs import FeatureLoader world.all_steps = [] mox = Mox() loader_mock = mox.CreateMock(FeatureLoader) mox.StubOutWithMock(lettuce.sys, 'path') mox.StubOutWithMock(lettuce, 'fs') mox.StubOutWithMock(lettuce.fs, 'FileSystem') mox.StubOutWithMock(lettuce, 'Feature') lettuce.fs.FeatureLoader('some_basepath').AndReturn(loader_mock) lettuce.sys.path.insert(0, 'some_basepath') lettuce.sys.path.remove('some_basepath') loader_mock.find_and_load_step_definitions() loader_mock.find_feature_files().AndReturn(['some_basepath/foo.feature']) lettuce.Feature.from_file('some_basepath/foo.feature'). \ AndReturn(Feature.from_string(FEATURE2)) mox.ReplayAll() runner = lettuce.Runner('some_basepath') CALLBACK_REGISTRY.clear() @before.all def set_state_to_before(): world.all_steps.append('before') @step('append "during" to states') def append_during_to_all_steps(step): world.all_steps.append("during") @after.all def set_state_to_after(total): world.all_steps.append('after') isinstance(total, TotalResult) runner.run() mox.VerifyAll() assert_equals( world.all_steps, ['before', 'during', 'during', 'after'], ) mox.UnsetStubs()
def test_act_when_is_tweeting_is_0(self): moxer = Mox() config = moxer.CreateMock(Configuration) config.is_tweeting = "0" self.__mock_config(config, moxer) api = _create_api(moxer) actor = _create_actor_and_delegates(api, moxer).actor moxer.ReplayAll() actor.act() moxer.VerifyAll()
class MockTest(unittest.TestCase): def setUp(self): # Mocks self.mox = Mox() self.gh = self.mox.CreateMock(GHManager) self.user = self.mox.CreateMock(AuthenticatedUser) self.org = self.mox.CreateMock(AuthenticatedUser) self.repo = self.mox.CreateMock(Repository) self.repo.organization = None self.gh.get_user("mock").AndReturn(self.user) self.user.get_repo("mock").AndReturn(self.repo) self.mox.ReplayAll() self.gh_repo = GitHubRepository(self.gh, "mock", "mock") def tearDown(self): try: self.mox.VerifyAll() finally: self.mox.UnsetStubs()
def test_act_when_is_tweeting_invalid(self): moxer = Mox() config = moxer.CreateMock(Configuration) config.is_tweeting = "hello" self.__mock_config(config, moxer) api = _create_api(moxer) bundle = _create_actor_and_delegates(api, moxer) actor, selector = bundle.actor, bundle.selector moxer.ReplayAll() actor.act() moxer.VerifyAll()
def test_read_only(self): m = Mox() config = m.CreateMock(Configuration) config.twitter_access_token = "oauth_token=1234&oauth_token_secret=5678" config.twitter_read_only = "1" m.StubOutWithMock(ConfigurationAccessor, "get_or_create") ConfigurationAccessor.get_or_create().AndReturn(config) m.ReplayAll() api = TwitterConnector.new_api() self.__assert_is_instance(api, ReadOnlyTwitterApi) m.VerifyAll()
def test_act_when_is_tweeting_is_1(self): moxer = Mox() config = moxer.CreateMock(Configuration) config.is_tweeting = "1" self.__mock_config(config, moxer) api = _create_api(moxer) _act_no_messages(api, moxer) bundle = _create_actor_and_delegates(api, moxer) actor, selector = bundle.actor, bundle.selector _should_act(selector, True) _random_tweet(selector) moxer.ReplayAll() actor.act() moxer.VerifyAll()
def test_safe_twitter_actor_handler(self): moxer = Mox() request, response = new_mock_request_response(moxer) moxer.StubOutWithMock(TwitterActor, "__new__") actor = moxer.CreateMock(TwitterActor) TwitterActor.__new__(TwitterActor).AndReturn(actor) actor.act() moxer.ReplayAll() handler = SafeTwitterActorHandler() handler.initialize(request, response) result = handler.post() moxer.VerifyAll() if result: exception, msg = result self.fail("exception occurred: %s" % msg)
def test_post_with_user(self): moxer = Mox() request, response = new_mock_request_response(moxer) moxer.StubOutWithMock(users, "get_current_user", use_mock_anything=True) # moxer.StubOutWithMock(ArtifactInfo, "delete_oldest_by_source", use_mock_anything=True) moxer.StubOutWithMock(ArtifactContent, "delete_oldest_by_source") moxer.StubOutWithMock(Counters, "source_counter") moxer.StubOutWithMock(Feed, "get_by_source_name", use_mock_anything=True) moxer.StubOutWithMock(model, "ingest_feed_entries") source_name = "hi" username = Services.API_USER user = MockEntity(key_name=username, email=lambda: username) users.get_current_user().AndReturn(user) handler = IngestHandler() users.get_current_user().AndReturn(user) handler.initialize(request, response) request.get("keep").AndReturn(None) counter = moxer.CreateMock(Counter) Counters.source_counter(source_name).AndReturn(counter) counter.decrement(IgnoreArg()) source = MockEntity(key_name=source_name, name=source_name) feed = MockEntity(key_name=source_name, url="no", artifact_source=source) # ArtifactInfo.delete_oldest_by_source(source, IgnoreArg()).AndReturn([]) ArtifactContent.delete_oldest_by_source(source, IgnoreArg(), pre_call=IgnoreArg()).AndReturn([]) Feed.get_by_source_name(source_name, return_none=True).AndReturn(feed) model.ingest_feed_entries(feed, user, error_call=IgnoreArg()).AndReturn(()) moxer.ReplayAll() handler.post(source_name) moxer.VerifyAll()
class TestSchedulerProxyInjection(unittest.TestCase): def setUp(self): self.mox = Mox() self.mox.StubOutClassWithMocks(AuroraAdmin, 'Client') self.mox.StubOutClassWithMocks(scheduler_client, 'SchedulerClient') self.mock_scheduler_client = self.mox.CreateMock(scheduler_client.SchedulerClient) self.mock_thrift_client = self.mox.CreateMock(AuroraAdmin.Client) scheduler_client.SchedulerClient.get(IgnoreArg(), verbose=IgnoreArg()).AndReturn( self.mock_scheduler_client) self.mock_scheduler_client.get_thrift_client().AndReturn(self.mock_thrift_client) def tearDown(self): self.mox.UnsetStubs() self.mox.VerifyAll() def make_scheduler_proxy(self): return scheduler_client.SchedulerProxy(Cluster(name='local')) def test_startCronJob(self): self.mock_thrift_client.startCronJob(IsA(JobKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().startCronJob(JOB_KEY) def test_createJob(self): self.mock_thrift_client.createJob( IsA(JobConfiguration)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().createJob(JobConfiguration()) def test_replaceCronTemplate(self): self.mock_thrift_client.replaceCronTemplate( IsA(JobConfiguration), IsA(Lock)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().replaceCronTemplate(JobConfiguration(), Lock()) def test_scheduleCronJob(self): self.mock_thrift_client.scheduleCronJob( IsA(JobConfiguration)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().scheduleCronJob(JobConfiguration()) def test_descheduleCronJob(self): self.mock_thrift_client.descheduleCronJob( IsA(JobKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().descheduleCronJob(JOB_KEY) def test_populateJobConfig(self): self.mock_thrift_client.populateJobConfig(IsA(JobConfiguration)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().populateJobConfig(JobConfiguration()) def test_restartShards(self): self.mock_thrift_client.restartShards( IsA(JobKey), IgnoreArg()).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().restartShards(JOB_KEY, set([0])) def test_getTasksStatus(self): self.mock_thrift_client.getTasksStatus(IsA(TaskQuery)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().getTasksStatus(TaskQuery()) def test_getJobs(self): self.mock_thrift_client.getJobs(IgnoreArg()).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().getJobs(ROLE) def test_killTasks(self): self.mock_thrift_client.killTasks( IgnoreArg(), IgnoreArg(), IsA(JobKey), IgnoreArg()).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().killTasks(None, None, JobKey(), set([0])) def test_getQuota(self): self.mock_thrift_client.getQuota(IgnoreArg()).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().getQuota(ROLE) def test_addInstances(self): self.mock_thrift_client.addInstances( IsA(JobKey), IgnoreArg(), IsA(Lock)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().addInstances(JobKey(), {}, Lock()) def test_acquireLock(self): self.mock_thrift_client.acquireLock(IsA(Lock)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().acquireLock(Lock()) def test_releaseLock(self): self.mock_thrift_client.releaseLock( IsA(Lock), IsA(LockValidation)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().releaseLock(Lock(), LockValidation()) def test_getJobUpdateSummaries(self): self.mock_thrift_client.getJobUpdateSummaries(IsA(JobUpdateQuery)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().getJobUpdateSummaries(JobUpdateQuery()) def test_getJobUpdateDetails(self): self.mock_thrift_client.getJobUpdateDetails('update_id').AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().getJobUpdateDetails('update_id') def test_startJobUpdate(self): self.mock_thrift_client.startJobUpdate( IsA(JobUpdateRequest)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().startJobUpdate(JobUpdateRequest()) def test_pauseJobUpdate(self): self.mock_thrift_client.pauseJobUpdate('update_id').AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().pauseJobUpdate('update_id') def test_resumeJobUpdate(self): self.mock_thrift_client.resumeJobUpdate( 'update_id').AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().resumeJobUpdate('update_id') def test_abortJobUpdate(self): self.mock_thrift_client.abortJobUpdate('update_id').AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().abortJobUpdate('update_id') def test_pulseJobUpdate(self): self.mock_thrift_client.pulseJobUpdate('update_id').AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().pulseJobUpdate('update_id') def test_raise_auth_error(self): self.mock_thrift_client.killTasks(TaskQuery(), None, None).AndRaise( TRequestsTransport.AuthError()) self.mock_scheduler_client.get_failed_auth_message().AndReturn('failed auth') self.mox.ReplayAll() with pytest.raises(scheduler_client.SchedulerProxy.AuthError): self.make_scheduler_proxy().killTasks(TaskQuery(), None, None)
class BlogArticleTest(TestCase): test_info_id = 'test_info_id' test_category_id = 'test_category_id' test_title = 'test_title' test_article = 'test_article' def setUp(self): initialize(rdb_path='sqlite:///:memory:') self.mocker = Mox() model = KMBlogArticle() model.save() # id = 1のデータを登録 def tearDown(self): pass def test_set_data(self): data = self.mocker.CreateMock(KMData) data.get_request_parameter('info_id').AndReturn(self.test_info_id) data.get_request_parameter('category_id').AndReturn(self.test_category_id) data.get_request_parameter('title', default='', decode=True).AndReturn(self.test_title) data.get_request_parameter('article', default='', decode=True).AndReturn(self.test_article) self.mocker.ReplayAll() model = KMBlogArticle() model.set_data(data); # KMDataで指定した値が設定されること eq_(model.info_id, self.test_info_id) eq_(model.category_id, self.test_category_id) eq_(model.title, self.test_title) eq_(model.error, None) self.mocker.UnsetStubs() self.mocker.VerifyAll() def test_validate(self): model = KMBlogArticle() # 各フィールドに値が設定されていない場合はFalseが返され、エラーが取得できること eq_(model.validate(), False) eq_(model.error.get('title')['message'], '記事名は必須です。') # 各フィールドに値が設定されている場合はTrueが返されること model = KMBlogArticle() model.title = self.test_title eq_(model.validate(), True) def test_get(self): model = KMBlogArticle.get(None) # id = Noneの場合は新規モデルが取得できること eq_(model.id, None) model = KMBlogArticle.get(1) # id = 1の場合は登録済みのモデルが取得できること eq_(model.id, 1) def test_save_data_error(self): data = self.mocker.CreateMock(KMData) data.get_request_parameter('info_id').AndReturn('') data.get_request_parameter('category_id').AndReturn('') data.get_request_parameter('title', default='', decode=True).AndReturn('') data.get_request_parameter('article', default='', decode=True).AndReturn('') self.mocker.ReplayAll() model = KMBlogArticle.save_data(None, data) # フィールドに値が設定されていない場合はエラーが返ること eq_(model.error.size(), 1) self.mocker.VerifyAll() def test_save_data_id_none(self): data = self.mocker.CreateMock(KMData) data.get_request_parameter('info_id').AndReturn(self.test_info_id) data.get_request_parameter('category_id').AndReturn(self.test_category_id) data.get_request_parameter('title', default='', decode=True).AndReturn(self.test_title) data.get_request_parameter('article', default='', decode=True).AndReturn(self.test_article) self.mocker.ReplayAll() model = KMBlogArticle.save_data(None, data) # idが指定されていない場合は新規で登録されること(id=2) eq_(model.error.size(), 0) eq_(model.id, 2) eq_(model.info_id, self.test_info_id) eq_(model.category_id, self.test_category_id) eq_(model.title, self.test_title) eq_(model.article, self.test_article) self.mocker.VerifyAll() def test_save_data(self): model = KMBlogArticle.get(1) eq_(model.id, 1) eq_(model.info_id, None) eq_(model.category_id, None) eq_(model.title, '') eq_(model.article, '') data = self.mocker.CreateMock(KMData) data.get_request_parameter('info_id').AndReturn(self.test_info_id) data.get_request_parameter('category_id').AndReturn(self.test_category_id) data.get_request_parameter('title', default='', decode=True).AndReturn(self.test_title) data.get_request_parameter('article', default='', decode=True).AndReturn(self.test_article) self.mocker.ReplayAll() model = KMBlogArticle.save_data(1, data) # 既存データのidが指定された場合は値が上書きされること eq_(model.error.size(), 0) eq_(model.id, 1) eq_(model.info_id, self.test_info_id) eq_(model.category_id, self.test_category_id) eq_(model.title, self.test_title) eq_(model.article, self.test_article) self.mocker.VerifyAll()
class TestDataStore(TestCase): def setUp(self): self.mox = Mox() def tearDown(self): self.mox.UnsetStubs() def test_it_queries_the_filesystem_for_the_latest_version(self): fs = self.mox.CreateMock(DataStoreFS) ds = DataStore(fs) pj = os.path.join fs.listfiles('my-id').AndReturn([ pj('my-id', 'item-0.json'), pj('my-id', 'item-15.json'), pj('my-id', 'item-1.json'), pj('my-id', 'item-6.json'), pj('my-id', 'readme.txt'), ]) fs.open_for_reading(os.path.join('my-id', 'item-15.json')).AndReturn( closing(BytesIO('{"name":"the_name"}')) ) self.mox.ReplayAll() item = ds.get('my-id') self.assertEquals(dict(name='the_name', version=15), item) self.mox.VerifyAll() def test_it_bails_out_when_no_item_is_found(self): fs = self.mox.CreateMock(DataStoreFS) ds = DataStore(fs) fs.listfiles('my-id').AndReturn([ os.path.join('my-id', 'readme.txt') ]) self.mox.ReplayAll() self.assertRaises(DataStoreException, ds.get, 'my-id') self.mox.VerifyAll() def test_it_bails_out_when_item_cannot_be_read(self): fs = self.mox.CreateMock(DataStoreFS) ds = DataStore(fs) fs.listfiles('my-id').AndReturn([ os.path.join('my-id', 'item-0.json') ]) fs.open_for_reading(os.path.join('my-id', 'item-0.txt')).AndRaise( Exception() ) self.mox.ReplayAll() self.assertRaises(DataStoreException, ds.get, 'my-id') self.mox.VerifyAll() def test_it_creates_a_new_version_on_put(self): fs = self.mox.CreateMock(DataStoreFS) def clock_now(): return datetime(1970, 1, 1) ds = DataStore(fs, clock_now) first_version = os.path.join('my-id', 'item-0.json') fs.listfiles('my-id').AndReturn([ first_version ]) fs.open_for_reading(first_version).AndReturn( closing(BytesIO('{"name":"the name"}')) ) new_version = os.path.join('my-id', 'item-1.json') new_content = BytesIO() @contextmanager def not_closing_content(): yield new_content fs.open_new_file(new_version).AndReturn( not_closing_content() ) self.mox.ReplayAll() item = ds.get('my-id') item['name'] = 'the new name' ds.put('item', 'my-id', item) self.assertEquals( json.loads(new_content.getvalue()), dict( name='the new name', version=1, creation_date={ '__datetime__': True, 'iso8601': '19700101T000000' } ) ) self.mox.VerifyAll() def test_it_fails_when_two_concurrent_puts_happen(self): fs = self.mox.CreateMock(DataStoreFS) ds = DataStore(fs) fp = os.path.join('my-id', 'item-32.json') fs.open_new_file(fp).AndReturn(closing(BytesIO())) fs.open_new_file(fp).AndRaise(FileAlreadyExistsException('file already exists')) self.mox.ReplayAll() ds.put('item', 'my-id', dict(name='hello', version=31)) self.assertRaises( ConcurrentModificationException, ds.put, 'item', 'my-id', dict(name='hello', version=31) ) self.mox.VerifyAll() def test_it_can_list_all_ids(self): fs = self.mox.CreateMock(DataStoreFS) ds = DataStore(fs) items = [ os.path.join('item1', 'item-1.json'), os.path.join('item2', 'item-5.json') ] fs.listdirs().AndReturn(['__temp', 'item1', 'item2']) fs.listfiles('item1').AndReturn([items[0]]) fs.open_for_reading(items[0]).AndReturn( closing(BytesIO('{"name":"a"}')) ) fs.listfiles('item2').AndReturn([items[1]]) fs.open_for_reading(items[1]).AndReturn( closing(BytesIO('{"name":"b"}')) ) self.mox.ReplayAll() self.assertEquals(set(['item1', 'item2']), set(ds.all_ids())) self.mox.VerifyAll()
class AlternativesTest(unittest.TestCase): def setUp(self): self.mox = Mox() self.fakedsource = "http://rdf.debian.net/source/src/1.0" def fake_get_sources_linked_to_homepage(uri, endpoint, graph): return [self.fakedsource] self.gslth = io._get_sources_linked_to_homepage io._get_sources_linked_to_homepage = \ fake_get_sources_linked_to_homepage def tearDown(self): io._get_sources_linked_to_homepage = self.gslth def test_no_matches(self): processor = self.mox.CreateMock(TripleProcessor) endpoint = self.mox.CreateMock(SPARQLWrapper2) uri = "http://example.org" expected = ["http://example.org"] self.mox.ReplayAll() self.assertEqual(expected, _alternatives(uri, processor, endpoint, "graph")) self.mox.VerifyAll() def test_match_sourceforge(self): processor = self.mox.CreateMock(TripleProcessor) endpoint = self.mox.CreateMock(SPARQLWrapper2) uri = "http://src.sourceforge.net" processor.push_homepage(self.fakedsource, "http://sourceforge.net/projects/src") expected = [ "http://src.sourceforge.net", "http://sourceforge.net/projects/src" ] self.mox.ReplayAll() self.assertEqual(expected, _alternatives(uri, processor, endpoint, "graph")) self.mox.VerifyAll() def test_match_sourceforge_sf(self): processor = self.mox.CreateMock(TripleProcessor) endpoint = self.mox.CreateMock(SPARQLWrapper2) uri = "http://src.sf.net" processor.push_homepage(self.fakedsource, "http://sourceforge.net/projects/src") expected = ["http://src.sf.net", "http://sourceforge.net/projects/src"] self.mox.ReplayAll() self.assertEqual(expected, _alternatives(uri, processor, endpoint, "graph")) self.mox.VerifyAll() def test_match_sourceforge_alioth(self): processor = self.mox.CreateMock(TripleProcessor) endpoint = self.mox.CreateMock(SPARQLWrapper2) uri = "http://src.alioth.debian.org" processor.push_homepage(self.fakedsource, "http://alioth.debian.org/projects/src") expected = [ "http://src.alioth.debian.org", "http://alioth.debian.org/projects/src" ] self.mox.ReplayAll() self.assertEqual(expected, _alternatives(uri, processor, endpoint, "graph")) self.mox.VerifyAll()
class WSGIRapperTestCase(TestCase): @classmethod def setup_class(clazz): pass @classmethod def teardown_class(clazz): pass def setUp(self): self.mocker = Mox() plugins.clear() def teardown(self): plugins.clear() def test_add_route(self): ''' ルーティングのテスト nameが指定された場合 :return: ''' plugin = WSGI_Bottle() # テスト用にプラグインを生成 plugins['test'] = plugin app_mock = self.mocker.CreateMock(Bottle) def test_method(): pass app_mock.route(path='/test', method='GET', callback=test_method, name='test') # routeが呼び出された際の振る舞いを設定 plugin.app = app_mock # プラグインのappを差し替え params = { 'rule': '/test', 'method': 'GET', 'target': test_method, 'name': 'test' } self.mocker.ReplayAll() add_route('test', params) self.mocker.VerifyAll() def test_add_route_noname(self): ''' ルーティングのテスト nameが指定されない場合 :return: ''' plugin = WSGI_Bottle() plugins['test'] = plugin app_mock = self.mocker.CreateMock(Bottle) def test_method(): pass app_mock.route(path='/test', method='GET', callback=test_method, name=None) plugin.app = app_mock params = {'rule': '/test', 'method': 'GET', 'target': test_method} self.mocker.ReplayAll() add_route('test', params) self.mocker.VerifyAll()
class TestSchedulerProxyInjection(unittest.TestCase): def setUp(self): self.mox = Mox() self.mox.StubOutClassWithMocks(AuroraAdmin, 'Client') self.mox.StubOutClassWithMocks(scheduler_client, 'SchedulerClient') self.mock_scheduler_client = self.mox.CreateMock( scheduler_client.SchedulerClient) self.mock_thrift_client = self.mox.CreateMock(AuroraAdmin.Client) scheduler_client.SchedulerClient.get(IgnoreArg(), verbose=IgnoreArg()).AndReturn( self.mock_scheduler_client) self.mock_scheduler_client.get_thrift_client().AndReturn( self.mock_thrift_client) version_resp = Response(responseCode=ResponseCode.OK) version_resp.result = Result(getVersionResult=CURRENT_API_VERSION) self.mock_thrift_client.getVersion().AndReturn(version_resp) def tearDown(self): self.mox.UnsetStubs() self.mox.VerifyAll() def make_scheduler_proxy(self): return TestSchedulerProxy('local') def test_startCronJob(self): self.mock_thrift_client.startCronJob(IsA(JobKey), IsA(SessionKey)) self.mox.ReplayAll() self.make_scheduler_proxy().startCronJob(JOB_KEY) def test_createJob(self): self.mock_thrift_client.createJob(IsA(JobConfiguration), IsA(SessionKey)) self.mox.ReplayAll() self.make_scheduler_proxy().createJob(JobConfiguration()) def test_replaceCronTemplate(self): self.mock_thrift_client.replaceCronTemplate(IsA(JobConfiguration), IsA(Lock), IsA(SessionKey)) self.mox.ReplayAll() self.make_scheduler_proxy().replaceCronTemplate( JobConfiguration(), Lock()) def test_populateJobConfig(self): self.mock_thrift_client.populateJobConfig(IsA(JobConfiguration)) self.mox.ReplayAll() self.make_scheduler_proxy().populateJobConfig(JobConfiguration()) def test_restartShards(self): self.mock_thrift_client.restartShards(IsA(JobKey), IgnoreArg(), IsA(SessionKey)) self.mox.ReplayAll() self.make_scheduler_proxy().restartShards(JOB_KEY, set([0])) def test_getTasksStatus(self): self.mock_thrift_client.getTasksStatus(IsA(TaskQuery)) self.mox.ReplayAll() self.make_scheduler_proxy().getTasksStatus(TaskQuery()) def test_getJobs(self): self.mock_thrift_client.getJobs(IgnoreArg()) self.mox.ReplayAll() self.make_scheduler_proxy().getJobs(ROLE) def test_killTasks(self): self.mock_thrift_client.killTasks(IsA(TaskQuery), IsA(SessionKey)) self.mox.ReplayAll() self.make_scheduler_proxy().killTasks(TaskQuery()) def test_getQuota(self): self.mock_thrift_client.getQuota(IgnoreArg()) self.mox.ReplayAll() self.make_scheduler_proxy().getQuota(ROLE) def test_startMaintenance(self): self.mock_thrift_client.startMaintenance(IsA(Hosts), IsA(SessionKey)) self.mox.ReplayAll() self.make_scheduler_proxy().startMaintenance(Hosts()) def test_drainHosts(self): self.mock_thrift_client.drainHosts(IsA(Hosts), IsA(SessionKey)) self.mox.ReplayAll() self.make_scheduler_proxy().drainHosts(Hosts()) def test_maintenanceStatus(self): self.mock_thrift_client.maintenanceStatus(IsA(Hosts), IsA(SessionKey)) self.mox.ReplayAll() self.make_scheduler_proxy().maintenanceStatus(Hosts()) def test_endMaintenance(self): self.mock_thrift_client.endMaintenance(IsA(Hosts), IsA(SessionKey)) self.mox.ReplayAll() self.make_scheduler_proxy().endMaintenance(Hosts()) def test_getVersion(self): self.mock_thrift_client.getVersion() self.mox.ReplayAll() self.make_scheduler_proxy().getVersion() def test_addInstances(self): self.mock_thrift_client.addInstances(IsA(JobKey), IgnoreArg(), IsA(Lock), IsA(SessionKey)) self.mox.ReplayAll() self.make_scheduler_proxy().addInstances(JobKey(), {}, Lock()) def test_acquireLock(self): self.mock_thrift_client.acquireLock(IsA(Lock), IsA(SessionKey)) self.mox.ReplayAll() self.make_scheduler_proxy().acquireLock(Lock()) def test_releaseLock(self): self.mock_thrift_client.releaseLock(IsA(Lock), IsA(LockValidation), IsA(SessionKey)) self.mox.ReplayAll() self.make_scheduler_proxy().releaseLock(Lock(), LockValidation())
class BlogInfoTest(TestCase): test_name = 'test_name' test_url = 'test_url' test_description = 'test_description' def setUp(self): initialize(rdb_path='sqlite:///:memory:') self.mocker = Mox() model = KMBlogInfo() model.save() # id = 1のデータを登録 def tearDown(self): pass def test_set_data(self): model = KMBlogInfo() data = self.mocker.CreateMock(KMData) data.get_request_parameter('name', default='', decode=True).AndReturn(self.test_name) data.get_request_parameter('url', default='').AndReturn(self.test_url) data.get_request_parameter('description', default='', decode=True).AndReturn( self.test_description) self.mocker.ReplayAll() model.set_data(data) # KMDataで指定した値が設定されること eq_(model.name, self.test_name) eq_(model.url, self.test_url) eq_(model.description, self.test_description) eq_(model.error, None) self.mocker.UnsetStubs() self.mocker.VerifyAll() def test_validate(self): model = KMBlogInfo() # 各フィールドに値が設定されていない場合はFalseが返され、エラーが取得できること eq_(model.validate(), False) eq_(model.error.get('name')['message'], 'ブログ名は必須です。') eq_(model.error.get('url')['message'], 'URLは必須です。') # 各フィールドに値が設定されている場合はTrueが返されること model = KMBlogInfo() model.name = self.test_name model.url = self.test_url eq_(model.validate(), True) def test_get(self): model = KMBlogInfo.get(None) # id = Noneの場合は新規モデルが取得できること eq_(model.id, None) model = KMBlogInfo.get(1) # id = 1の場合は登録済みのモデルが取得できること eq_(model.id, 1) def test_save_data_error(self): data = self.mocker.CreateMock(KMData) data.get_request_parameter('name', default='', decode=True).AndReturn('') data.get_request_parameter('url', default='').AndReturn('') data.get_request_parameter('description', default='', decode=True).AndReturn('') self.mocker.ReplayAll() model = KMBlogInfo.save_data(None, data) # フィールドに値が設定されていない場合はエラーが返ること eq_(model.error.size(), 2) self.mocker.VerifyAll() def test_save_data_id_none(self): data = self.mocker.CreateMock(KMData) data.get_request_parameter('name', default='', decode=True).AndReturn(self.test_name) data.get_request_parameter('url', default='').AndReturn(self.test_url) data.get_request_parameter('description', default='', decode=True).AndReturn( self.test_description) self.mocker.ReplayAll() model = KMBlogInfo.save_data(None, data) # idが指定されていない場合は新規で登録されること(id=2) eq_(model.error.size(), 0) eq_(model.id, 2) eq_(model.name, self.test_name) eq_(model.url, self.test_url) eq_(model.description, self.test_description) self.mocker.VerifyAll() def test_save_data(self): model = KMBlogInfo.get(1) eq_(model.id, 1) eq_(model.name, '') eq_(model.url, '') eq_(model.description, '') data = self.mocker.CreateMock(KMData) data.get_request_parameter('name', default='', decode=True).AndReturn(self.test_name) data.get_request_parameter('url', default='').AndReturn(self.test_url) data.get_request_parameter('description', default='', decode=True).AndReturn( self.test_description) self.mocker.ReplayAll() model = KMBlogInfo.save_data(1, data) # 既存データのidが指定された場合は値が上書きされること eq_(model.error.size(), 0) eq_(model.id, 1) eq_(model.name, self.test_name) eq_(model.url, self.test_url) eq_(model.description, self.test_description) self.mocker.VerifyAll()
class FeedFinderTest(unittest.TestCase): def setUp(self): self.finder = FeedFinder() self.mox = Mox() debian.services.RES_BASEURI = "base" def test__fetch_feeduris(self): unversionedsourceuri = "http://example.org/p" mock = self.mox.CreateMock(SPARQLQueryProcessor) expectedarg = r"SELECT.+\<%s\>.+" % re.escape(unversionedsourceuri) mock.execute_query(Regex(expectedarg, flags=re.DOTALL)) binding1 = {'feeduri': {'value': "feed1"}} binding2 = {'feeduri': {'value': "feed2"}} bindings = [binding1, binding2] fakeresults = {'results': {'bindings': bindings}} self.finder.processor = mock self.mox.ReplayAll() self.finder.processor.results = fakeresults feeds = self.finder._fetch_feeduris(unversionedsourceuri) self.mox.VerifyAll() self.assertEqual(2, len(feeds)) self.assertEqual("feed1", feeds[0].feeduri) self.assertEqual("feed2", feeds[1].feeduri) # This cannot never happen, homepages without alternatives # won't match SPARQL patterns and won't be in the result set def test__fetch_feeduris_no_feeduris_in_bindings(self): unversionedsourceuri = "http://example.org/p" mock = self.mox.CreateMock(SPARQLQueryProcessor) expectedarg = r"SELECT.+\<%s\>.+" % re.escape(unversionedsourceuri) mock.execute_query(Regex(expectedarg, flags=re.DOTALL)) binding1 = {} binding2 = {} bindings = [binding1, binding2] fakeresults = {'results': {'bindings': bindings}} self.finder.processor = mock self.mox.ReplayAll() self.finder.processor.results = fakeresults feeds = self.finder._fetch_feeduris(unversionedsourceuri) self.mox.VerifyAll() self.assertEqual(0, len(feeds)) def test__fetch_feeduris_no_bindings(self): unversionedsourceuri = "http://example.org/p" mock = self.mox.CreateMock(SPARQLQueryProcessor) expectedarg = r"SELECT.+\<%s\>.+" % re.escape(unversionedsourceuri) mock.execute_query(Regex(expectedarg, flags=re.DOTALL)) bindings = [] fakeresults = {'results': {'bindings': bindings}} self.finder.processor = mock self.mox.ReplayAll() self.finder.processor.results = fakeresults feeds = self.finder._fetch_feeduris(unversionedsourceuri) self.mox.VerifyAll() self.assertEqual(0, len(feeds)) def test__fetch_feeditems(self): feeduri = "http://example.org/p" mock = self.mox.CreateMock(SPARQLQueryProcessor) expectedarg = r"SELECT.+\<%s\>.+" % re.escape(feeduri) mock.execute_query(Regex(expectedarg, flags=re.DOTALL)) binding1 = {'title': {'value': "title1"}, 'link': {'value': "link1"}} binding2 = {'title': {'value': "title2"}} bindings = [binding1, binding2] fakeresults = {'results': {'bindings': bindings}} self.finder.processor = mock self.mox.ReplayAll() self.finder.processor.results = fakeresults items = self.finder._fetch_feeditems(feeduri) self.mox.VerifyAll() self.assertEqual(2, len(items)) self.assertEqual("title1", items[0]['title']) self.assertEqual("link1", items[0]['link']) self.assertEqual("title2", items[1]['title']) self.assertEqual(None, items[1]['link']) def test__fetch_feeditems_no_bindings(self): feeduri = "http://example.org/p" mock = self.mox.CreateMock(SPARQLQueryProcessor) expectedarg = r"SELECT.+\<%s\>.+" % re.escape(feeduri) mock.execute_query(Regex(expectedarg, flags=re.DOTALL)) bindings = [] fakeresults = {'results': {'bindings': bindings}} self.finder.processor = mock self.mox.ReplayAll() self.finder.processor.results = fakeresults items = self.finder._fetch_feeditems(feeduri) self.mox.VerifyAll() self.assertEqual(0, len(items)) def test__fill_feeds(self): input = [RSSFeed("uri1"), RSSFeed("uri2")] self.mox.StubOutWithMock(self.finder, "_fetch_feeditems") self.mox.StubOutWithMock(self.finder, "_fetch_feed_channel_information") uri1items = [] uri2items = [{'title': "title21"}, {'title': "title22"}] uri1channel = {'title': "title1"} uri2channel = {'title': "title2"} self.finder._fetch_feed_channel_information("uri1").AndReturn( uri1channel) self.finder._fetch_feeditems("uri1").AndReturn(uri1items) self.finder._fetch_feed_channel_information("uri2").AndReturn( uri2channel) self.finder._fetch_feeditems("uri2").AndReturn(uri2items) self.mox.ReplayAll() feeds = self.finder._fill_feeds(input) self.assertEqual("uri1", feeds[0].feeduri) self.assertEqual("uri2", feeds[1].feeduri) self.assertEqual([], feeds[0].items) self.assertEqual(2, len(feeds[1].items)) self.assertEqual("title21", feeds[1].items[0]['title']) self.assertEqual("title22", feeds[1].items[1]['title']) self.assertEqual("title1", feeds[0].channel['title']) self.assertEqual("title2", feeds[1].channel['title']) def test__fetch_feed_channel_information(self): feeduri = "http://example.org/p" mock = self.mox.CreateMock(SPARQLQueryProcessor) expectedarg = r"SELECT.+\<%s\>.+" % re.escape(feeduri) mock.execute_query(Regex(expectedarg, flags=re.DOTALL)) binding1 = {'title': {'value': "title1"}} bindings = [binding1] fakeresults = {'results': {'bindings': bindings}} self.finder.processor = mock self.mox.ReplayAll() self.finder.processor.results = fakeresults channel = self.finder._fetch_feed_channel_information(feeduri) self.mox.VerifyAll() self.assertEqual("title1", channel['title']) def test__fetch_feed_channel_information_channel_but_no_title(self): feeduri = "http://example.org/p" mock = self.mox.CreateMock(SPARQLQueryProcessor) expectedarg = r"SELECT.+\<%s\>.+" % re.escape(feeduri) mock.execute_query(Regex(expectedarg, flags=re.DOTALL)) binding1 = {} bindings = [binding1] fakeresults = {'results': {'bindings': bindings}} self.finder.processor = mock self.mox.ReplayAll() self.finder.processor.results = fakeresults channel = self.finder._fetch_feed_channel_information(feeduri) self.mox.VerifyAll() self.assertEqual(None, channel['title']) def test__fetch_feed_channel_information_no_channel(self): feeduri = "http://example.org/p" mock = self.mox.CreateMock(SPARQLQueryProcessor) expectedarg = r"SELECT.+\<%s\>.+" % re.escape(feeduri) mock.execute_query(Regex(expectedarg, flags=re.DOTALL)) bindings = [] fakeresults = {'results': {'bindings': bindings}} self.finder.processor = mock self.mox.ReplayAll() self.finder.processor.results = fakeresults channel = self.finder._fetch_feed_channel_information(feeduri) self.mox.VerifyAll() self.assertEqual(None, channel) def test_populate_feeds_forbidden_characters(self): self.assertRaises(SPARQLQueryBuilderPackageNameSchemeError, self.finder.populate_feeds, "{}@") def test_populate_feeds_escape(self): srcpkgname = "source.+-" srcpkguri = "base/source/%s" % "source.%2B-" self.mox.StubOutWithMock(self.finder, "_fetch_feeduris") self.finder._fetch_feeduris(srcpkguri).AndReturn([]) self.mox.StubOutWithMock(self.finder, "_fill_feeds") self.finder._fill_feeds([]).AndReturn([]) self.mox.ReplayAll() data = self.finder.populate_feeds(srcpkgname) self.mox.VerifyAll() self.assertEqual(0, len(data))
class SPARQLQueryBuilderTest(unittest.TestCase): def setUp(self): self.builder = SPARQLQueryBuilder() self.builder.params = {} self.mox = Mox() # No optional calls in PyMox :( def mock_binary_search(self): self.builder.binary_search = lambda: True self.builder._extended_binary_search = lambda: False self.builder.source_search = lambda: False def mock_source_search(self): self.builder.binary_search = lambda: False self.builder._extended_binary_search = lambda: False self.builder.source_search = lambda: True def mock_extended_binary_search(self): self.builder.binary_search = lambda: True self.builder._extended_binary_search = lambda: True self.builder.source_search = lambda: False def test__searchtype_source(self): self.builder.params['searchtype'] = "SOURCE" self.assertTrue(self.builder.source_search()) self.assertFalse(self.builder.binary_search()) def test__searchtype_binary(self): self.builder.params['searchtype'] = "BINARY" self.assertTrue(self.builder.binary_search()) self.assertFalse(self.builder.source_search()) def test__searchtype_binary_description(self): self.builder.params['searchtype'] = "BINARYEXT" self.assertTrue(self.builder.binary_search()) self.assertFalse(self.builder.source_search()) def test__searchtype_unexpected(self): self.builder.params['searchtype'] = "FAIL" self.assertRaises(SPARQLQueryBuilderUnexpectedFieldValueError, \ self.builder.binary_search) self.assertRaises(SPARQLQueryBuilderUnexpectedFieldValueError, \ self.builder.source_search) self.builder.params.pop('searchtype') self.assertRaises(SPARQLQueryBuilderUnexpectedFieldValueError, \ self.builder.binary_search) self.assertRaises(SPARQLQueryBuilderUnexpectedFieldValueError, \ self.builder.source_search) def test__consume_homepage_true(self): self.builder.params['homepage'] = True mock = self.mox.CreateMock(SelectQueryHelper) mock.add_variable(Variable("homepage")) triple = Triple(\ Variable("source"), FOAF.page, Variable("homepage")) mock.add_optional(triple) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_homepage() self.mox.VerifyAll() def test__consume_homepage_false(self): self.builder.params['homepage'] = False mock = self.mox.CreateMock(SelectQueryHelper) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_homepage() self.mox.VerifyAll() def test__consume_filter_empty(self): self.builder.params['filter'] = "" mock = self.mox.CreateMock(SelectQueryHelper) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_filter() self.mox.VerifyAll() def test__consume_filter_source_not_exact(self): self.mock_source_search() self.builder.params['filter'] = "keyword" self.builder.params['exactmatch'] = False mock = self.mox.CreateMock(SelectQueryHelper) mock.add_or_filter_regex({Variable("sourcename"): "keyword"}) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_filter() self.mox.VerifyAll() def test__consume_filter_source_exact(self): self.mock_source_search() self.builder.params['filter'] = "keyword" self.builder.params['exactmatch'] = True mock = self.mox.CreateMock(SelectQueryHelper) mock.add_or_filter_regex({Variable("sourcename"): "^keyword$"}) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_filter() def test__consume_filter_source_escapes(self): self.mock_source_search() self.builder.params['filter'] = "-+." self.builder.params['exactmatch'] = False mock = self.mox.CreateMock(SelectQueryHelper) mock.add_or_filter_regex({Variable("sourcename"): "\\\\-\\\\+\\\\."}) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_filter() def test__consume_filter_binary_not_exact(self): self.mock_binary_search() self.builder.params['filter'] = "keyword" self.builder.params['exactmatch'] = False mock = self.mox.CreateMock(SelectQueryHelper) mock.add_or_filter_regex({Variable("binaryname"): "keyword"}) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_filter() self.mox.VerifyAll() def test__consume_filter_binary_exact(self): self.mock_binary_search() self.builder.params['filter'] = "keyword" self.builder.params['exactmatch'] = True mock = self.mox.CreateMock(SelectQueryHelper) mock.add_or_filter_regex({Variable("binaryname"): "^keyword$"}) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_filter() self.mox.VerifyAll() def test__consume_filter_binary_desc_not_exact(self): self.mock_extended_binary_search() self.builder.params['filter'] = "keyword" self.builder.params['exactmatch'] = False mock = self.mox.CreateMock(SelectQueryHelper) mock.push_triple(Variable("binary"), DEB.extendedDescription,\ Variable("desc")) mock.add_or_filter_regex({Variable("binaryname"): "keyword",\ Variable("desc"): "keyword"}) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_filter() self.mox.VerifyAll() def test__consume_filter_binary_desc_exact(self): self.mock_extended_binary_search() self.builder.params['filter'] = "keyword" self.builder.params['exactmatch'] = True mock = self.mox.CreateMock(SelectQueryHelper) mock.push_triple(Variable("binary"), DEB.extendedDescription,\ Variable("desc")) mock.add_or_filter_regex({Variable("binaryname"): "^keyword$",\ Variable("desc"): "^keyword$"}) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_filter() self.mox.VerifyAll() def test__consume_distribution_any(self): self.builder.params['distribution'] = "ANY" mock = self.mox.CreateMock(SelectQueryHelper) mock.push_triple_variables(Variable("source"), DEB.distribution,\ Variable("distribution")) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_distribution() self.mox.VerifyAll() def test__consume_distribution_selected(self): self.builder.params['distribution'] = \ "http://rdf.debian.net/distributions/distribution" mock = self.mox.CreateMock(SelectQueryHelper) mock.push_triple(Variable("source"), DEB.distribution,\ URIRef(self.builder.params['distribution'])) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_distribution() self.mox.VerifyAll() def test__consume_priority_source_any(self): self.mock_source_search() self.builder.params['priority'] = "ANY" mock = self.mox.CreateMock(SelectQueryHelper) mock.add_variable("priority") triple = Triple(Variable("source"), DEB.priority, Variable("priority")) mock.add_optional(triple) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_priority() self.mox.VerifyAll() def test__consume_priority_binary_any(self): self.mock_binary_search() self.builder.params['priority'] = "ANY" mock = self.mox.CreateMock(SelectQueryHelper) mock.add_variable("priority") triple = Triple(Variable("binary"), DEB.priority, Variable("priority")) mock.add_optional(triple) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_priority() self.mox.VerifyAll() def test__consume_priority_source_selected(self): self.mock_source_search() self.builder.params['priority'] = "http://example.org/p" mock = self.mox.CreateMock(SelectQueryHelper) mock.push_triple(Variable("source"), DEB.priority, URIRef("http://example.org/p")) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_priority() self.mox.VerifyAll() def test__consume_priority_binary_selected(self): self.mock_binary_search() self.builder.params['priority'] = "http://example.org/p" mock = self.mox.CreateMock(SelectQueryHelper) mock.push_triple(Variable("binary"), DEB.priority, URIRef("http://example.org/p")) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_priority() self.mox.VerifyAll() def test__consume_vcs_empty(self): self.builder.params['vcs'] = [] mock = self.mox.CreateMock(SelectQueryHelper) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_vcs() self.mox.VerifyAll() def test__consume_vcs_one(self): self.builder.params['vcs'] = ["SVN"] mock = self.mox.CreateMock(SelectQueryHelper) mock.push_triple(Variable("source"), DEB.repository, Variable("repobnode")) triple = Triple(Variable("repobnode"), RDF.type, DOAP.SVNRepository) mock.add_triple(triple) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_vcs() self.mox.VerifyAll() def test__consume_vcs_several(self): self.builder.params['vcs'] = ["SVN", "GIT"] mock = self.mox.CreateMock(SelectQueryHelper) mock.push_triple(Variable("source"), DEB.repository, Variable("repobnode")) triple1 = Triple(Variable("repobnode"), RDF.type, DOAP.SVNRepository) triple2 = Triple(Variable("repobnode"), RDF.type, DOAP.GitRepository) mock.add_union([triple1], [triple2]) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_vcs() self.mox.VerifyAll() def test__consume_area_any(self): self.builder.params['area'] = "ANY" mock = self.mox.CreateMock(SelectQueryHelper) mock.push_triple_variables(Variable("source"), DEB.area,\ Variable("area")) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_area() self.mox.VerifyAll() def test__consume_area_selected(self): self.builder.params['area'] = "http://example.org/c" mock = self.mox.CreateMock(SelectQueryHelper) mock.push_triple(Variable("source"), DEB.area,\ URIRef(self.builder.params['area'])) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_area() self.mox.VerifyAll() def test__consume_sort(self): self.builder.params['sort'] = "MAINTMAIL" self.mock_source_search() mock = self.mox.CreateMock(SelectQueryHelper) mock.set_orderby("maintmail") self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_sort() self.mox.VerifyAll() self.mock_binary_search() mock = self.mox.CreateMock(SelectQueryHelper) mock.set_orderby("maintmail") self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_sort() self.mox.VerifyAll() def test__consume_sort_error(self): self.builder.params['sort'] = "FAIL" self.assertRaises(SPARQLQueryBuilderUnexpectedFieldValueError, self.builder._consume_sort) def test__consume_sort_package_source(self): self.mock_source_search() self.builder.params['sort'] = "PACKAGE" mock = self.mox.CreateMock(SelectQueryHelper) mock.set_orderby("sourcename") self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_sort() self.mox.VerifyAll() def test__consume_sort_package_binary(self): self.mock_binary_search() self.builder.params['sort'] = "PACKAGE" mock = self.mox.CreateMock(SelectQueryHelper) mock.set_orderby("binaryname") self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_sort() self.mox.VerifyAll() def test__consume_maintainer_error(self): self.builder.params['maintainer'] = "FAIL" self.assertRaises(SPARQLQueryBuilderUnexpectedFieldValueError, self.builder._consume_maintainer) def test__consume_maintainer_all(self): self.builder.params['maintainer'] = "ALL" mock = self.mox.CreateMock(SelectQueryHelper) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_maintainer() self.mox.VerifyAll() def test__consume_maintainer_custom(self): self.builder.params['maintainer'] = "CUSTOM" mock = self.mox.CreateMock(SelectQueryHelper) self.mox.StubOutWithMock(self.builder, "_consume_maintainer_filter") self.builder._consume_maintainer_filter() self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_maintainer() self.mox.VerifyAll() def test__consume_maintainer_qa(self): self.builder.params['maintainer'] = "QA" mock = self.mox.CreateMock(SelectQueryHelper) debian.services.RES_BASEURI = "base" qaref = URIRef("base/team/packages%40qa.debian.org") mock.push_triple(Variable("source"), DEB.maintainer, qaref) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_maintainer() self.mox.VerifyAll() # TODO: Test DEBIAN, TEAM def test__consume_comaintainer_error(self): self.builder.params['comaintainer'] = "FAIL" self.assertRaises(SPARQLQueryBuilderUnexpectedFieldValueError, self.builder._consume_comaintainer) def test__consume_comaintainer_all(self): self.builder.params['comaintainer'] = "ALL" mock = self.mox.CreateMock(SelectQueryHelper) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_comaintainer() self.mox.VerifyAll() def test__consume_essential_source(self): self.builder.params['essential'] = True self.mock_source_search() mock = self.mox.CreateMock(SelectQueryHelper) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_essential() self.mox.VerifyAll() def test__consume_essential_binary_disabled(self): self.builder.params['essential'] = False self.mock_binary_search() mock = self.mox.CreateMock(SelectQueryHelper) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_essential() self.mox.VerifyAll() def test__consume_essential_binary_enabled(self): self.builder.params['essential'] = True self.mock_binary_search() mock = self.mox.CreateMock(SelectQueryHelper) mock.push_triple(Variable("binary"), RDF.type, DEB.EssentialBinary) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_essential() self.mox.VerifyAll() def test_create_binaries_query_bad_source(self): source = "{" version = "1.0" self.assertRaises(SPARQLQueryBuilderError, self.builder.create_binaries_query,\ source, version) def test_wants_json(self): self.assertRaises(SPARQLQueryBuilderUnexpectedFieldValueError, self.builder.wants_json) self.assertRaises(SPARQLQueryBuilderUnexpectedFieldValueError, self.builder.wants_html) self.builder.params['tojson'] = True self.assertTrue(self.builder.wants_json()) self.assertFalse(self.builder.wants_html()) self.builder.params['tojson'] = False self.assertFalse(self.builder.wants_json()) self.assertTrue(self.builder.wants_html()) def test__consume_maintainer_filter_empty(self): self.builder.params['maintainerfilter'] = "" mock = self.mox.CreateMock(SelectQueryHelper) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_maintainer_filter() self.mox.VerifyAll() def test__consume_maintainer_filter(self): self.builder.params['maintainerfilter'] = "keyword" mock = self.mox.CreateMock(SelectQueryHelper) mock.push_triple(\ Variable("maint"), FOAF.name, Variable("maintname")) mock.add_or_filter_regex({Variable("maintmail"): "keyword",\ Variable("maintname"): "keyword"}) self.builder.helper = mock self.mox.ReplayAll() self.builder._consume_maintainer_filter() self.mox.VerifyAll()
class TestSchedulerProxyInjection(unittest.TestCase): def setUp(self): self.mox = Mox() self.mox.StubOutClassWithMocks(AuroraAdmin, 'Client') self.mox.StubOutClassWithMocks(scheduler_client, 'SchedulerClient') self.mock_scheduler_client = self.mox.CreateMock( scheduler_client.SchedulerClient) self.mock_thrift_client = self.mox.CreateMock(AuroraAdmin.Client) scheduler_client.SchedulerClient.get(IgnoreArg(), verbose=IgnoreArg()).AndReturn( self.mock_scheduler_client) self.mock_scheduler_client.get_thrift_client().AndReturn( self.mock_thrift_client) def tearDown(self): self.mox.UnsetStubs() self.mox.VerifyAll() def make_scheduler_proxy(self): return TestSchedulerProxy(Cluster(name='local')) def test_startCronJob(self): self.mock_thrift_client.startCronJob( IsA(JobKey), IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().startCronJob(JOB_KEY) def test_createJob(self): self.mock_thrift_client.createJob( IsA(JobConfiguration), IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().createJob(JobConfiguration()) def test_replaceCronTemplate(self): self.mock_thrift_client.replaceCronTemplate( IsA(JobConfiguration), IsA(Lock), IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().replaceCronTemplate( JobConfiguration(), Lock()) def test_scheduleCronJob(self): self.mock_thrift_client.scheduleCronJob( IsA(JobConfiguration), IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().scheduleCronJob(JobConfiguration()) def test_descheduleCronJob(self): self.mock_thrift_client.descheduleCronJob( IsA(JobKey), IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().descheduleCronJob(JOB_KEY) def test_populateJobConfig(self): self.mock_thrift_client.populateJobConfig( IsA(JobConfiguration)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().populateJobConfig(JobConfiguration()) def test_restartShards(self): self.mock_thrift_client.restartShards( IsA(JobKey), IgnoreArg(), IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().restartShards(JOB_KEY, set([0])) def test_getTasksStatus(self): self.mock_thrift_client.getTasksStatus( IsA(TaskQuery)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().getTasksStatus(TaskQuery()) def test_getJobs(self): self.mock_thrift_client.getJobs( IgnoreArg()).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().getJobs(ROLE) def test_killTasks(self): self.mock_thrift_client.killTasks( IsA(TaskQuery), IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().killTasks(TaskQuery()) def test_getQuota(self): self.mock_thrift_client.getQuota( IgnoreArg()).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().getQuota(ROLE) def test_api_version_mismatch(self): resp = Response(serverInfo=ServerInfo( thriftAPIVersion=THRIFT_API_VERSION + 1)) self.mock_thrift_client.getQuota(IgnoreArg()).AndReturn(resp) self.mox.ReplayAll() with pytest.raises( scheduler_client.SchedulerProxy.ThriftInternalError): self.make_scheduler_proxy().getQuota(ROLE) def test_addInstances(self): self.mock_thrift_client.addInstances( IsA(JobKey), IgnoreArg(), IsA(Lock), IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().addInstances(JobKey(), {}, Lock()) def test_acquireLock(self): self.mock_thrift_client.acquireLock( IsA(Lock), IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().acquireLock(Lock()) def test_releaseLock(self): self.mock_thrift_client.releaseLock( IsA(Lock), IsA(LockValidation), IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().releaseLock(Lock(), LockValidation()) def test_getJobUpdateSummaries(self): self.mock_thrift_client.getJobUpdateSummaries( IsA(JobUpdateQuery)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().getJobUpdateSummaries(JobUpdateQuery()) def test_getJobUpdateDetails(self): self.mock_thrift_client.getJobUpdateDetails('update_id').AndReturn( DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().getJobUpdateDetails('update_id') def test_startJobUpdate(self): self.mock_thrift_client.startJobUpdate( IsA(JobUpdateRequest), IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().startJobUpdate(JobUpdateRequest()) def test_pauseJobUpdate(self): self.mock_thrift_client.pauseJobUpdate( 'update_id', IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().pauseJobUpdate('update_id') def test_resumeJobUpdate(self): self.mock_thrift_client.resumeJobUpdate( 'update_id', IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().resumeJobUpdate('update_id') def test_abortJobUpdate(self): self.mock_thrift_client.abortJobUpdate( 'update_id', IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().abortJobUpdate('update_id') def test_pulseJobUpdate(self): self.mock_thrift_client.pulseJobUpdate( 'update_id', IsA(SessionKey)).AndReturn(DEFAULT_RESPONSE) self.mox.ReplayAll() self.make_scheduler_proxy().pulseJobUpdate('update_id')
class PluginManagerTestCase(TestCase): @classmethod def setup_class(clazz): pass @classmethod def teardown_class(clazz): pass def setUp(self): self.mocker = Mox() plugins.clear() def tearDown(self): plugins.clear() def test_create_base_plugin(self): ''' プラグイン生成のテスト ・プラグインが生成されることの確認 :return: ''' ok_(len(plugins) == 0) # 初期プラグイン数 result = create_base_plugin('test') # testという名前のプラグインを生成 ok_(isinstance(result, WSGI_Bottle)) # クラスを確認 ok_(len(plugins) == 1) # プラグインが追加されたことを確認 ok_(result is plugins['test']) # 取得できたプラグインと一覧に保持されたものが同一か確認 def test_add_route(self): ''' ルーティングのテスト ・pluginsの対象のプラグインのrouteが呼び出されることを確認 :return: ''' plugin = WSGI_Bottle() # テスト用にプラグインを生成 plugins['test'] = plugin app_mock = self.mocker.CreateMock(Bottle) def test_method(): pass app_mock.route(path='/test', method='GET', callback=test_method, name='test') # routeが呼び出された際の振る舞いを設定 plugin.app = app_mock # プラグインのappを差し替え params = { 'rule': '/test', 'method': 'GET', 'target': test_method, 'name': 'test' } self.mocker.ReplayAll() add_route('test', params) self.mocker.VerifyAll() def test_get_root_plugin(self): ''' クラスメソッドの呼び出しだけなので不要 :return: ''' pass def test_set_root_plugin(self): ''' クラスメソッドの呼び出しだけなので不要 :return: ''' pass def test_mount(self): ''' クラスメソッドの呼び出しだけなので不要 :return: ''' pass def test_run(self): ''' クラスメソッドの呼び出しだけなので不要 :return: ''' pass def test_get_plugin(self): ''' プラグイン取得のテスト ・pluginsに追加したプラグインが、指定した名前で取得できることを確認 :return: ''' plugin = create_base_plugin('test') plugins['test'] = plugin ok_(isinstance(plugin, get_plugin('test'))) @raises(Exception) def test_get_plugin(self): ''' プラグイン取得のテスト ・pluginsに存在しない名前を指定すると例外が発生する事を確認 :return: ''' plugin = create_base_plugin('test') plugins['test'] = plugin ok_(isinstance(plugin, get_plugin('hoge')))