def test_ifneeded_parse(self): @ifneeded("parse") def testfunc(repo, basefile): repo.called = True # mockdoc = Mock() # mockdoc.basefile="1234" mockbasefile = "1234" mockrepo = Mock() mockrepo.store.needed = DocumentStore(datadir='fake').needed mockrepo.called = False mockrepo.config.force = False # test 1: Outfile is newer - the ifneeded decorator should # make sure the actual testfunc code is never reached with patch('ferenda.util.outfile_is_newer', return_value=True): testfunc(mockrepo, mockbasefile) self.assertFalse(mockrepo.called) mockrepo.called = False # test 2: Outfile is older with patch('ferenda.util.outfile_is_newer', return_value=False): testfunc(mockrepo, mockbasefile) self.assertTrue(mockrepo.called) mockrepo.called = False # test 3: Outfile is newer, but the global force option was set mockrepo.config.force = True with patch('ferenda.util.outfile_is_newer', return_value=True): testfunc(mockrepo, mockbasefile) self.assertTrue(mockrepo.called) mockrepo.config.force = None mockrepo.called = False
def test_ifneeded_relate(self): @ifneeded("relate") def testfunc(repo, basefile, needed): repo.called = True repo.needed = needed try: datadir = tempfile.mkdtemp() mockbasefile = "1234" mockrepo = Mock() mockrepo.store = DocumentStore(datadir=datadir) mockrepo.called = False mockrepo.config.force = False # create some docentry file in a good place de = DocumentEntry(mockrepo.store.documententry_path("1234")) now = datetime.datetime.now() de.indexed_ts = now + datetime.timedelta(seconds=3600) de.indexed_ft = now + datetime.timedelta(seconds=-3600) de.indexed_dep = now + datetime.timedelta(seconds=-3600) de.save() # test 1: Outfile is newer - the ifneeded decorator should # make sure the actual testfunc code is never reached # NOTE: the "relate" branch of DocumentStore.needed # doesn't use outfile_is_newer, so we can't patch that, we # have to create actual files parsedpath = mockrepo.store.parsed_path("1234") util.writefile(parsedpath, "dummy") os.utime(parsedpath, (now.timestamp(), now.timestamp() - 7200)) testfunc(mockrepo, mockbasefile) self.assertFalse(mockrepo.called) mockrepo.called = False # test 2: Outfile is older than the information in the documententry file os.utime(parsedpath, (now.timestamp(), now.timestamp())) testfunc(mockrepo, mockbasefile) self.assertTrue(mockrepo.called) self.assertTrue(mockrepo.needed) self.assertFalse(mockrepo.needed.triples) self.assertFalse(mockrepo.needed.dependencies) self.assertTrue(mockrepo.needed.fulltext) mockrepo.called = False # test 3: Outfile is newer, but the global force option was set os.utime(parsedpath, (now.timestamp(), now.timestamp() - 7200)) mockrepo.config.force = True testfunc(mockrepo, mockbasefile) self.assertTrue(mockrepo.called) mockrepo.config.force = None mockrepo.called = False finally: if os.path.exists(datadir): shutil.rmtree(datadir)
def test_ifneeded_generate(self): @ifneeded("generate") def testfunc(repo, basefile): repo.called = True mockbasefile = "1234" mockrepo = Mock() mockrepo.store.needed = DocumentStore(datadir='fake').needed mockrepo.called = False mockrepo.config.force = False # test 1: Outfile is newer - the ifneeded decorator should # make sure the actual testfunc code is never reached with patch('ferenda.util.outfile_is_newer', return_value=True): testfunc(mockrepo, mockbasefile) self.assertFalse(mockrepo.called) mockrepo.called = False # test 2: Outfile is older than source file with patch('ferenda.util.outfile_is_newer', return_value=False): testfunc(mockrepo, mockbasefile) self.assertTrue(mockrepo.called) mockrepo.called = False # FIXME: we could add more tests, eg create a dependency file # and make sure an arbitrary file named in that depfile is # newer then outfile. but the tests in testDocStore.Needed # should cover that pretty well. # test 3: Outfile is newer, but the global force option was set mockrepo.config.force = True with patch('ferenda.util.outfile_is_newer', return_value=True): testfunc(mockrepo, mockbasefile) self.assertTrue(mockrepo.called) mockrepo.config.force = None mockrepo.called = False
def setUp(self): self.datadir = tempfile.mkdtemp() self.basefile = "1" self.store = DocumentStore(self.datadir + "/base") self.d = Devel() self.globalconf = LayeredConfig(Defaults({ 'datadir': self.datadir, 'patchdir': self.datadir, 'download_text': None, 'intermediate_text': None, 'devel': { 'class': 'ferenda.Devel' }, 'base': { 'class': 'testDevel.MockRepo' }, 'koi8': { 'class': 'testDevel.Koi8Repo' } }), cascade=True) self.d.config = self.globalconf.devel self.d.config.download_text self.d.config.download_text = "what"
def setUp(self): self.datadir = tempfile.mkdtemp() self.store = DocumentStore(self.datadir)
def setUp(self): self.datadir = tempfile.mkdtemp() self.store = DocumentStore(self.datadir, compression=self.compression)