def test_rebuild_cache(app, demo_taxonomy): """classifier - test rebuilding cache.""" from invenio_classifier.reader import ( _get_ontology, _get_cache_path, get_regular_expressions ) with app.app_context(): info = _get_ontology(demo_taxonomy) assert info[0] cache = _get_cache_path(info[0]) if os.path.exists(cache): ctime = os.stat(cache)[stat.ST_CTIME] else: ctime = -1 time.sleep(0.5) # sleep a bit for timing issues rex = get_regular_expressions( demo_taxonomy, rebuild=True) assert os.path.exists(cache) ntime = os.stat(cache)[stat.ST_CTIME] assert ntime > ctime assert len(rex[0]) + len(rex[1]) == 63
def test_rebuild_cache(app, demo_taxonomy): """Test rebuilding taxonomy cache.""" from invenio_classifier.reader import ( _get_ontology, _get_cache_path, get_regular_expressions ) with app.app_context(): info = _get_ontology(demo_taxonomy) assert info[0] cache = _get_cache_path(info[0]) if os.path.exists(cache): ctime = os.stat(cache)[stat.ST_CTIME] else: ctime = -1 time.sleep(0.5) # sleep a bit for timing issues rex = get_regular_expressions( demo_taxonomy, rebuild=True) assert os.path.exists(cache) ntime = os.stat(cache)[stat.ST_CTIME] assert ntime > ctime assert len(rex[0]) + len(rex[1]) == 63
def test_rebuild_cache(self): """classifier - test rebuilding cache.""" from invenio_classifier import reader info = reader._get_ontology(self.taxonomy_name) self.assertTrue(info[0]) cache = reader._get_cache_path(info[0]) if os.path.exists(cache): ctime = os.stat(cache)[stat.ST_CTIME] else: ctime = -1 time.sleep(0.5) # sleep a bit for timing issues rex = reader.get_regular_expressions(self.taxonomy_name, rebuild=True) self.assertTrue(os.path.exists(cache)) ntime = os.stat(cache)[stat.ST_CTIME] self.assertTrue((ntime > ctime)) self.assertEqual(len(rex[0]) + len(rex[1]), 63)
def test_rebuild_cache(self): """classifier - test rebuilding cache.""" from invenio_classifier import reader info = reader._get_ontology(self.taxonomy_name) self.assertTrue(info[0]) cache = reader._get_cache_path(info[0]) if os.path.exists(cache): ctime = os.stat(cache)[stat.ST_CTIME] else: ctime = -1 time.sleep(0.5) # sleep a bit for timing issues rex = reader.get_regular_expressions( self.taxonomy_name, rebuild=True) self.assertTrue(os.path.exists(cache)) ntime = os.stat(cache)[stat.ST_CTIME] self.assertTrue((ntime > ctime)) self.assertEqual(len(rex[0]) + len(rex[1]), 63)
def test_cache_accessibility(app, demo_taxonomy): """Test taxonomy cache accessibility/writability.""" from invenio_classifier.reader import ( _get_ontology, get_regular_expressions, _get_cache_path ) assert os.path.exists(demo_taxonomy) with app.app_context(): # we will do tests with a copy of test taxonomy, in case anything goes # wrong... orig_name, orig_taxonomy_path, orig_taxonomy_url = _get_ontology( demo_taxonomy) demo_taxonomy = demo_taxonomy + '.copy.rdf' shutil.copy(orig_taxonomy_path, demo_taxonomy) dummy_name, demo_taxonomy, dummy_url = _get_ontology(demo_taxonomy) cache = _get_cache_path(demo_taxonomy) if os.path.exists(cache): os.remove(cache) get_regular_expressions(demo_taxonomy, rebuild=True, no_cache=False) assert os.path.exists(cache) # set cache unreadable os.chmod(cache, 000) with pytest.raises(TaxonomyError): get_regular_expressions( demo_taxonomy, rebuild=False, no_cache=False ) # set cache unreadable and test writing os.chmod(cache, 000) with pytest.raises(TaxonomyError): get_regular_expressions( demo_taxonomy, rebuild=True, no_cache=False ) # set cache readable and test writing os.chmod(cache, 600) with pytest.raises(TaxonomyError): get_regular_expressions( demo_taxonomy, rebuild=True, no_cache=False ) # set cache writable only os.chmod(cache, 200) get_regular_expressions( demo_taxonomy, rebuild=True, no_cache=False) get_regular_expressions( demo_taxonomy, rebuild=False, no_cache=False) # set cache readable/writable but corrupted (must rebuild itself) os.chmod(cache, 600) os.remove(cache) open(cache, 'w').close() get_regular_expressions( demo_taxonomy, rebuild=False, no_cache=False) # set cache readable/writable but corrupted (must rebuild itself) open(cache, 'w').close() try: os.rename(demo_taxonomy, demo_taxonomy + 'x') open(demo_taxonomy, 'w').close() with pytest.raises(TaxonomyError): get_regular_expressions( demo_taxonomy, rebuild=False, no_cache=False ) finally: os.rename(demo_taxonomy + 'x', demo_taxonomy) # make cache ok, but corrupt source get_regular_expressions( demo_taxonomy, rebuild=True, no_cache=False) try: os.rename(demo_taxonomy, demo_taxonomy + 'x') open(demo_taxonomy, 'w').close() time.sleep(.1) # touch the taxonomy to be older os.utime(cache, (time.time() + 100, time.time() + 100)) get_regular_expressions( demo_taxonomy, rebuild=False, no_cache=False) finally: os.rename(demo_taxonomy + 'x', demo_taxonomy) # make cache ok (but old), and corrupt source get_regular_expressions( demo_taxonomy, rebuild=True, no_cache=False) try: os.rename(demo_taxonomy, demo_taxonomy + 'x') open(demo_taxonomy, 'w').close() with pytest.raises(TaxonomyError): get_regular_expressions( demo_taxonomy, rebuild=False, no_cache=False ) finally: os.rename(demo_taxonomy + 'x', demo_taxonomy) name, demo_taxonomy, taxonomy_url = _get_ontology(demo_taxonomy) cache = _get_cache_path(name) os.remove(demo_taxonomy) os.remove(cache)
def test_cache_accessibility(self): """classifier - test cache accessibility/writability""" from flask import current_app from invenio_classifier.registry import taxonomies from invenio_classifier import reader from invenio_classifier.errors import TaxonomyError # we will do tests with a copy of test taxonomy, in case anything goes # wrong... orig_name, orig_taxonomy_path, orig_taxonomy_url = reader._get_ontology( self.taxonomy_name) taxonomy_name = self.taxonomy_name + '.copy' taxonomy_path = os.path.join( current_app.config['CFG_TMPDIR'], taxonomy_name + '.rdf') shutil.copy(orig_taxonomy_path, taxonomy_path) taxonomies[taxonomy_name] = taxonomy_path assert(os.path.exists(taxonomy_path)) name, taxonomy_path, taxonomy_url = reader._get_ontology( taxonomy_name) cache = reader._get_cache_path( os.path.basename(taxonomy_path)) assert name if os.path.exists(cache): os.remove(cache) reader.get_regular_expressions( taxonomy_name, rebuild=True, no_cache=False) assert(os.path.exists(cache)) # set cache unreadable os.chmod(cache, 000) self.assertRaises( TaxonomyError, reader.get_regular_expressions, taxonomy_name, rebuild=False, no_cache=False ) # set cache unreadable and test writing os.chmod(cache, 000) self.assertRaises( TaxonomyError, reader.get_regular_expressions, taxonomy_name, rebuild=True, no_cache=False ) # set cache readable and test writing os.chmod(cache, 600) self.assertRaises( TaxonomyError, reader.get_regular_expressions, taxonomy_name, rebuild=True, no_cache=False ) # set cache writable only os.chmod(cache, 200) reader.get_regular_expressions( taxonomy_name, rebuild=True, no_cache=False) reader.get_regular_expressions( taxonomy_name, rebuild=False, no_cache=False) # set cache readable/writable but corrupted (must rebuild itself) os.chmod(cache, 600) os.remove(cache) open(cache, 'w').close() reader.get_regular_expressions( taxonomy_name, rebuild=False, no_cache=False) # set cache readable/writable but corrupted (must rebuild itself) open(cache, 'w').close() try: os.rename(taxonomy_path, taxonomy_path + 'x') open(taxonomy_path, 'w').close() self.assertRaises( TaxonomyError, reader.get_regular_expressions, taxonomy_name, rebuild=False, no_cache=False ) finally: os.rename(taxonomy_path + 'x', taxonomy_path) # make cache ok, but corrupt source reader.get_regular_expressions( taxonomy_name, rebuild=True, no_cache=False) try: os.rename(taxonomy_path, taxonomy_path + 'x') open(taxonomy_path, 'w').close() time.sleep(.1) # touch the taxonomy to be older os.utime(cache, (time.time() + 100, time.time() + 100)) reader.get_regular_expressions( taxonomy_name, rebuild=False, no_cache=False) finally: os.rename(taxonomy_path + 'x', taxonomy_path) # make cache ok (but old), and corrupt source reader.get_regular_expressions( taxonomy_name, rebuild=True, no_cache=False) try: os.rename(taxonomy_path, taxonomy_path + 'x') open(taxonomy_path, 'w').close() self.assertRaises( TaxonomyError, reader.get_regular_expressions, taxonomy_name, rebuild=False, no_cache=False ) finally: os.rename(taxonomy_path + 'x', taxonomy_path) name, taxonomy_path, taxonomy_url = reader._get_ontology( taxonomy_name) cache = reader._get_cache_path(name) os.remove(taxonomy_path) os.remove(cache)
def test_cache_accessibility(app, demo_taxonomy): """classifier - test cache accessibility/writability""" from invenio_classifier.reader import ( _get_ontology, get_regular_expressions, _get_cache_path ) from invenio_classifier.errors import TaxonomyError assert os.path.exists(demo_taxonomy) with app.app_context(): # we will do tests with a copy of test taxonomy, in case anything goes # wrong... orig_name, orig_taxonomy_path, orig_taxonomy_url = _get_ontology( demo_taxonomy) demo_taxonomy = demo_taxonomy + '.copy.rdf' shutil.copy(orig_taxonomy_path, demo_taxonomy) dummy_name, demo_taxonomy, dummy_url = _get_ontology(demo_taxonomy) cache = _get_cache_path(demo_taxonomy) if os.path.exists(cache): os.remove(cache) get_regular_expressions(demo_taxonomy, rebuild=True, no_cache=False) assert os.path.exists(cache) # set cache unreadable os.chmod(cache, 000) with pytest.raises(TaxonomyError): get_regular_expressions( demo_taxonomy, rebuild=False, no_cache=False ) # set cache unreadable and test writing os.chmod(cache, 000) with pytest.raises(TaxonomyError): get_regular_expressions( demo_taxonomy, rebuild=True, no_cache=False ) # set cache readable and test writing os.chmod(cache, 600) with pytest.raises(TaxonomyError): get_regular_expressions( demo_taxonomy, rebuild=True, no_cache=False ) # set cache writable only os.chmod(cache, 200) get_regular_expressions( demo_taxonomy, rebuild=True, no_cache=False) get_regular_expressions( demo_taxonomy, rebuild=False, no_cache=False) # set cache readable/writable but corrupted (must rebuild itself) os.chmod(cache, 600) os.remove(cache) open(cache, 'w').close() get_regular_expressions( demo_taxonomy, rebuild=False, no_cache=False) # set cache readable/writable but corrupted (must rebuild itself) open(cache, 'w').close() try: os.rename(demo_taxonomy, demo_taxonomy + 'x') open(demo_taxonomy, 'w').close() with pytest.raises(TaxonomyError): get_regular_expressions( demo_taxonomy, rebuild=False, no_cache=False ) finally: os.rename(demo_taxonomy + 'x', demo_taxonomy) # make cache ok, but corrupt source get_regular_expressions( demo_taxonomy, rebuild=True, no_cache=False) try: os.rename(demo_taxonomy, demo_taxonomy + 'x') open(demo_taxonomy, 'w').close() time.sleep(.1) # touch the taxonomy to be older os.utime(cache, (time.time() + 100, time.time() + 100)) get_regular_expressions( demo_taxonomy, rebuild=False, no_cache=False) finally: os.rename(demo_taxonomy + 'x', demo_taxonomy) # make cache ok (but old), and corrupt source get_regular_expressions( demo_taxonomy, rebuild=True, no_cache=False) try: os.rename(demo_taxonomy, demo_taxonomy + 'x') open(demo_taxonomy, 'w').close() with pytest.raises(TaxonomyError): get_regular_expressions( demo_taxonomy, rebuild=False, no_cache=False ) finally: os.rename(demo_taxonomy + 'x', demo_taxonomy) name, demo_taxonomy, taxonomy_url = _get_ontology(demo_taxonomy) cache = _get_cache_path(name) os.remove(demo_taxonomy) os.remove(cache)