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 test_mix_random_limit_sources_1(self): moxer = mox.Mox() moxer.StubOutWithMock(ArtifactSource, "all") moxer.StubOutWithMock(ArtifactContent, "all") ArtifactSource.all().AndReturn( MockQuery(xrange(4), create_call=_source)) ArtifactContent.all().AndReturn( MockQuery(xrange(12), create_call=_content)) moxer.ReplayAll() sources, text = new_default_mixer().mix_random_limit_sources(1) self.assertEquals(len(sources), 1) moxer.VerifyAll()
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 test_get_registers_appropriate_tasks(self): moxer = Mox() request, response = new_mock_request_response(moxer) _stub_taskqueue(moxer) moxer.StubOutWithMock(Feed, "find_active", use_mock_anything=True) 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") q_range = xrange(0, 5) Feed.find_active().AndReturn( MockQuery(q_range, create_call=create_call)) # expects queued tasks for each feed for i in q_range: taskqueue.add(name=IgnoreArg(), url=IgnoreArg()) moxer.ReplayAll() handler = CronIngestDriverHandler() handler.initialize(request, response) handler.get() moxer.VerifyAll()
def test_create_duplicate(self): self.moxer.StubOutWithMock(ArtifactInfo, "all", use_mock_anything=True) ArtifactInfo.all(keys_only=True).AndReturn( MockQuery(range(1), keys_only=True)) self.moxer.ReplayAll() try: ArtifactAccessor.create(**self.__keywords()) self.fail("exception expected") except DuplicateDataException, ex: pass
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 test_words(words, text_call): for i in xrange(len(words)): moxer = mox.Mox() moxer.StubOutWithMock(ArtifactSource, "all") moxer.StubOutWithMock(ArtifactContent, "all") ArtifactSource.all().AndReturn( MockQuery(xrange(4), create_call=_source)) word = words[i] source_content = _content(word) source_text = source_content.body ArtifactContent.all().AndReturn( MockQuery(xrange(1), create_call=lambda id: source_content)) moxer.ReplayAll() mixer = new_default_mixer() sources, mixed_text = mixer.mix_random_limit_sources(1) print "('%s') '%s' -> '%s'" % (word, source_text, mixed_text) text_call(word, source_text, mixed_text) moxer.VerifyAll()
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_mix_response_mix_contents_fails(self): m = mox.Mox() m.StubOutWithMock(ArtifactAccessor, "search") # m.StubOutWithMock(Mixer, "random_resource") m.StubOutWithMock(Mixer, "mix_random_limit_sources") inquiry = "elephants" # content should be short enough to make SentenceSpeaker fail ArtifactAccessor.search("elephants").AndReturn( tuple(e for e in MockQuery(xrange(1), create_call=lambda id: _content(id, 0)))) Mixer.mix_random_limit_sources(1).AndReturn( self.__default_mix_results()) #Mixer.random_resource().AndReturn(MockEntity(key_name="failblog", url="http://failblog.org")) m.ReplayAll() sources, text = new_default_mixer().mix_response(inquiry) m.VerifyAll()