def _test_post_no_user(self): moxer = Mox() request, response = new_mock_request_response(moxer) moxer.StubOutWithMock(users, "get_current_user", use_mock_anything=True) moxer.StubOutWithMock(User, "__init__", use_mock_anything=True) 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) # passes auth (via cron) users.get_current_user().AndReturn(user) handler = IngestHandler() # no logged in user users.get_current_user() User.__init__(username) handler.initialize(request, response) feed = MockEntity(key_name=source_name, url="no") Feed.get_by_source_name(source_name, return_none=True).AndReturn(feed) model.ingest_feed_entries(feed, None, error_call=IgnoreArg()).AndReturn(()) moxer.ReplayAll() handler.post(source_name) moxer.VerifyAll()
def create_content_list(count, phrase_length=8): source_name = "source-%s" key_str = str(count) return [ MockEntity(key_name=key_str, guid=key_str, source=MockEntity(key_name=source_name), source_name=source_name % count, body=generate_phrase(phrase_length)) for i in xrange(count) ]
def test_delete_by_name_deletes_source_with_referencing_feed(self): # self.m.StubOutWithMock(antonym_model, 'ArtifactSource') self.m.StubOutWithMock(ArtifactSource, "get_by_name") self.m.StubOutWithMock(FeedAccessor, "get_by_source_name") name = "mhawthorne" source = MockEntity(key_name=name) ArtifactSource.get_by_name(name).AndReturn(source) FeedAccessor.get_by_source_name(name, return_none=True).AndReturn( MockEntity(key=name, url="http://real.ly")) self.m.ReplayAll() self.assertRaises(ConflictingDataException, ArtifactSourceAccessor.delete_by_name, name) self.m.VerifyAll()
def test_mix_sources_2(self): moxer = mox.Mox() moxer.StubOutWithMock(ArtifactSource, "get_multiple_by_name") moxer.StubOutWithMock(ArtifactContent, "all") source_names = ("source/1", "source/2") sources = [ MockEntity(key_name=name, name=name) for name in source_names ] def _content(id): # toggles between sources source = sources[id % 2] id_str = str(id) return MockEntity(key_name=id_str, guid=id_str, body=generate_phrase(5), source=source, source_name=source.name) ArtifactSource.get_multiple_by_name(source_names).AndReturn(sources) # performs ArtifactContent query for each source ArtifactContent.all().AndReturn( MockQuery(xrange(12), create_call=_content)) ArtifactContent.all().AndReturn( MockQuery(xrange(12), create_call=_content)) moxer.ReplayAll() mixed_sources, text = new_default_mixer().mix_sources(source_names) self.assertEquals(len(mixed_sources), len(source_names)) moxer.VerifyAll()
def _content(id): id_str = str(id) return MockEntity(key_name=id_str, guid=id_str, body=generate_phrase(5), source=source, source_name=source.name)
def test_mix_sources_1(self): moxer = mox.Mox() moxer.StubOutWithMock(ArtifactSource, "get_multiple_by_name") moxer.StubOutWithMock(ArtifactContent, "all") source_name = "source/1" source = MockEntity(key_name=source_name, name=source_name) def _content(id): id_str = str(id) return MockEntity(key_name=id_str, guid=id_str, body=generate_phrase(5), source=source, source_name=source.name) ArtifactSource.get_multiple_by_name(source_name).AndReturn((source, )) ArtifactContent.all().AndReturn( MockQuery(xrange(12), create_call=_content)) moxer.ReplayAll() sources, text = new_default_mixer().mix_sources(source_name) self.assertEquals(len(sources), 1) moxer.VerifyAll()
def _content(include_word): source = _source(0) return MockEntity(key_name=include_word, guid=include_word, body=_inject(include_word, generate_phrase(5)), source=source, source_name=source.name)
def _test_delete(self): self.moxer.StubOutWithMock(ArtifactInfo, "get_by_guid", use_mock_anything=True) self.moxer.StubOutWithMock(ArtifactContent, "get_by_guid", use_mock_anything=True) guid = 'blah' ArtifactInfo.get_by_guid(guid).AndReturn(MockEntity( MockKey(name=guid))) ArtifactContent.get_by_guid(guid).AndReturn( MockEntity(MockKey(name=guid))) self.moxer.ReplayAll() ArtifactAccessor.delete(guid) self.moxer.VerifyAll()
def _mix_random(api, moxer): moxer.StubOutWithMock(Mixer, "mix_random_limit_sources") sources = [ MockEntity(key_name=str(i), name="source/%d" % i) for i in xrange(2) ] content = "so, wtf is going on here" Mixer.mix_random_limit_sources(2, degrade=True).AndReturn( (sources, content))
def _content(id, length=8): source = _source(0) id_str = str(id) return MockEntity(key_name=id_str, guid=id_str, body=generate_phrase(length), source=source, source_name=source.name)
def _content(id): # toggles between sources source = sources[id % 2] id_str = str(id) return MockEntity(key_name=id_str, guid=id_str, body=generate_phrase(5), source=source, source_name=source.name)
def test_create(self): accessor_save_kw = self.__keywords() source_name = accessor_save_kw['source'] content_type = accessor_save_kw['content_type'] body = accessor_save_kw['body'] self.moxer.StubOutWithMock(ArtifactInfo, "all", use_mock_anything=True) self.moxer.StubOutWithMock(ArtifactSource, "get_or_create", use_mock_anything=True) self.moxer.StubOutWithMock(Counters, "source_counter", use_mock_anything=True) self.moxer.StubOutWithMock(ArtifactInfo, "create", use_mock_anything=True) self.moxer.StubOutWithMock(ArtifactContent, "create", use_mock_anything=True) source = MockEntity(key_name=source_name) ArtifactInfo.all(keys_only=True).AndReturn( MockQuery(None, keys_only=True)) ArtifactSource.get_or_create(source_name).AndReturn(source) counter = self.moxer.CreateMockAnything() Counters.source_counter(source_name).AndReturn(counter) counter.increment() # TODO: I wish I could ignore keywords md5 = ArtifactAccessor._content_md5(source_name, content_type, body) info_save_kw = dict(source=source, source_name=source_name, content_type=content_type, content_md5=md5) info_key = MockKey(name=self.test_id) ArtifactInfo.create(**info_save_kw).AndReturn(info_key) content_save_kw = dict(source=source, source_name=source_name, info=info_key, body=body) ArtifactContent.create(info_key.name(), **content_save_kw).AndReturn( MockKey(name=self.test_id)) self.moxer.ReplayAll() info, content, source = ArtifactAccessor.create(**accessor_save_kw) print 'info:%s, content:%s, source:%s' % (info, content, source) self.moxer.VerifyAll()
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()
def test_post_unauthorized(self): moxer = Mox() request, response = new_mock_request_response(moxer) moxer.StubOutWithMock(users, "get_current_user", use_mock_anything=True) handler = IngestHandler() handler.initialize(request, response) username = "******" users.get_current_user().AndReturn(MockEntity(key_name=username, email=lambda: username)) response.set_status(403) response.clear() moxer.ReplayAll() handler.post("hi") moxer.VerifyAll()
def test_delete_by_name_deletes_source_with_no_referencing_feed(self): self.m.StubOutWithMock(ArtifactSource, "get_by_name") self.m.StubOutWithMock(FeedAccessor, "get_by_source_name") self.m.StubOutWithMock(ArtifactInfo, "find_by_source") self.m.StubOutWithMock(ArtifactContent, "find_by_source") self.m.StubOutWithMock(db, "delete") self.m.StubOutWithMock(memcache, "delete") name = "mhawthorne" source = MockEntity(key_name=name) ArtifactSource.get_by_name(name).AndReturn(source) FeedAccessor.get_by_source_name(name, return_none=True) ArtifactInfo.find_by_source(source, keys_only=True).AndReturn( MockQuery(range(0, 0))) ArtifactContent.find_by_source(source).AndReturn(MockQuery(range(0, 0))) db.delete(source) memcache.delete(IsA(str)).AndReturn(1) self.m.ReplayAll() ArtifactSourceAccessor.delete_by_name(name) self.m.VerifyAll()
def create_call(i): source_name = "source-%i" % i source = MockEntity(key_name=source_name, name=source_name) return MockEntity(key_name="feed-%i" % i, artifact_source=source, url="hi")
def set_current_user(moxer, username): moxer.StubOutWithMock(users, "get_current_user", use_mock_anything=True) users.get_current_user().AndReturn( MockEntity(key_name=username, email=lambda: username))
def _source(id): return MockEntity(key_name=str(id), name="source/%d" % id)