def test_load(owm_project): owm_bundle = p('tests', 'test_data', 'example_bundle.tar.xz') target_bundle = p(owm_project.testdir, 'bundle.tar.xz') shutil.copyfile(owm_bundle, target_bundle) owm_project.sh('owm bundle load ' + target_bundle) assertRegexpMatches(owm_project.sh('owm bundle cache list'), r'example/aBundle@23')
def test_input_file_format_is_unexpected(tempdir): ''' Test the exception when the input file is not in a format we support ''' open(p(tempdir, 'file_name'), 'w').close() with pytest.raises(NotABundlePath, match='file_name.*read.*archive'): Unarchiver(tempdir).unpack(p(tempdir, 'file_name'))
def test_save_classes(self): modpath = p(self.testdir, 'test_module') os.mkdir(modpath) open(p(modpath, '__init__.py'), 'w').close() with open(p(modpath, 'monkey.py'), 'w') as out: print(dedent('''\ from PyOpenWorm.dataObject import DataObject, DatatypeProperty class Monkey(DataObject): class_context = 'http://example.org/primate/monkey' bananas = DatatypeProperty() def identifier_augment(self): return type(self).rdf_namespace['paul'] def defined_augment(self): return True __yarom_mapped_classes__ = (Monkey,) '''), file=out) print(self.sh('pow save test_module.monkey')) assertRegexpMatches(self.sh('pow diff'), r'<[^>]+>')
def test_open_open_close_query(tempdir): bds1 = BundleDependencyStore() bds2 = BundleDependencyStore() store_cache = StoreCache() store = plugin_get('FileStorageZODB', Store)() store.open(p(tempdir, 'db.fs')) trip = (URIRef('http://example.org/a'), URIRef('http://example.org/b'), URIRef('http://example.org/c')) store.add(trip, context=None) store.close() conf = dict(type='FileStorageZODB', conf={ 'read_only': True, 'url': p(tempdir, 'db.fs') }, cache=store_cache) print("OPEN BDS 1") bds1.open(conf) print("OPEN BDS 2") bds2.open(conf) print("CLOSE BDS 1") bds1.close() assert list(bds2.triples((None, None, None)))[0][0] == trip
def test_open_open_close_close_2(tempdir): ''' A cached store, once opened, cannot be closed unilaterally by a BDS holdidng a reference to that store. Consequently, we must prevent closing of the store. However, calling 'close' on the store must remain an allowed operation (i.e., it must not raise an exception) so that the sharing of the store remains transparent to the BDS user. ''' bds1 = BundleDependencyStore() bds2 = BundleDependencyStore() store_cache = StoreCache() store = plugin_get('FileStorageZODB', Store)() store.open(p(tempdir, 'db.fs')) store.close() conf = dict(type='FileStorageZODB', conf={ 'read_only': True, 'url': p(tempdir, 'db.fs') }, cache=store_cache) print("OPEN BDS 1") bds1.open(conf) print("OPEN BDS 2") bds2.open(conf) print("CLOSE BDS 2") bds2.close() print("CLOSE BDS 1") bds1.close()
def make_module(self, module): ''' Create a module directory under `testdir`. Each of the intermediate directories (if there are any) will also be usable as modules (i.e., they'll have __init__.py files in them). Parameters ---------- module : str Path to the module directory. Must be a relative path Returns ------- str The full path to the module directory ''' if isabs(module): raise ValueError('Must use a relative path. Given ' + str(module)) modpath = p(self.testdir, module) os.makedirs(modpath) last_dname = None dname = modpath while last_dname != dname and dname != self.testdir: open(p(dname, '__init__.py'), 'x').close() base = '' while not base and last_dname != dname: last_dname = dname dname, base = split_path(modpath) return modpath
def f(*args, **kwargs): res = _shell_helper(*args, **kwargs) try: default_context_id = 'http://example.org/data' res.sh(f'owm -b init --default-context-id "{default_context_id}"') add_core_bundle = request.node.get_closest_marker('core_bundle') if add_core_bundle: core_bundle = request.getfixturevalue('core_bundle') bundles_directory = p(res.test_homedir, '.owmeta', 'bundles') fetcher = Fetcher(bundles_directory, (core_bundle.remote, )) fetcher.fetch(core_bundle.id, core_bundle.version) res.owmdir = p(res.testdir, DEFAULT_OWM_DIR) res.default_context_id = default_context_id def owm(**kwargs): r = OWM(owmdir=p(res.testdir, '.owm'), **kwargs) r.userdir = p(res.test_homedir, '.owmeta') return r res.owm = owm yield res finally: shutil.rmtree(res.testdir)
def test_manifest_missing_from_archive(tempdir): ''' Test manifest missing from archive file raises exception ''' tarfile.open(p(tempdir, 'file_name'), 'w:xz').close() with pytest.raises(NotABundlePath, match='file_name.*manifest'): Unarchiver(tempdir).unpack(p(tempdir, 'file_name'))
def __call__(self, dirlist=(), flist=()): for d in dirlist: self.dirs[d] = os.makedirs(p(testdir, d)) for f in flist: self.files[f] = open(p(testdir, f), mode='w').close() return self
def test_cache_list_empty_malformed_manifest(tempdir): bdir = p(tempdir, 'bdir', '10') makedirs(bdir) with open(p(bdir, 'manifest'), 'w') as f: f.write('certainly not a manifest') cut = Cache(tempdir) assert len(list(cut.list())) == 0
def test_bundle_directory_lacks_manifest(tempdir): ''' A valid bundle needs a manifest ''' cut = Deployer() os.mkdir(p(tempdir, 'notabundle')) with raises(NotABundlePath): cut.deploy(p(tempdir, 'notabundle'))
def test_init_default_store_config_file_exists_update_store_conf(self): self._init_conf() self.cut.init(update_existing_config=True) with open(p('.owm', 'owm.conf'), 'r') as f: conf = json.load(f) self.assertEqual(conf['rdf.store_conf'], p('$OWM', 'worm.db'))
def test_bundle_directory_manifest_is_a_directory(tempdir): ''' A valid bundle manifest is not a directory ''' cut = Deployer() os.makedirs(p(tempdir, 'notabundle', 'manifest')) with raises(NotABundlePath): cut.deploy(p(tempdir, 'notabundle'))
def test_archive_writen_to_target_file_absolute(tempdir, bundle): s = Archiver(tempdir, bundle.bundles_directory).pack(bundle.descriptor.id, bundle.descriptor.version, target_file_name=p( tempdir, 'targetfilename')) assert exists(p(tempdir, 'targetfilename'))
def test_install_class_registry_load(owm_project, core_bundle): from tests.test_modules.owmbundletest01 import Person modpath = p('tests', 'test_modules') owm_project.make_module(modpath) owm_project.writefile(p(modpath, 'owmbundletest02_defs.py')) owm_project.copy(p(modpath, 'owmbundletest02_query.py'), 'query.py') save_output = owm_project.sh( 'owm save tests.test_modules.owmbundletest02_defs') print("---------vSAVE OUTPUTv----------") print(save_output) print("---------^SAVE OUTPUT^----------") descriptor = f'''\ --- id: person_bundle description: A person in a bundle includes: ["{owm_project.default_context_id}", "{Person.definition_context.identifier}"] dependencies: - id: openworm/owmeta-core version: 1 ''' print(descriptor) add_bundle(owm_project, descriptor) owm_project.sh('owm bundle install person_bundle') owm_project.sh('python query.py')
def test_specified_bundle_does_not_exist(tempdir): bundles_directory = p(tempdir, 'bundles') makedirs(p(bundles_directory, 'example')) b = Bundle('example', bundles_directory=bundles_directory, version=2) with pytest.raises(BundleNotFound, match='at version 2.*specified version'): b._get_bundle_directory()
def test_init_default_store_config_file_exists_no_change(self): self._init_conf() with open(p('.owm', 'owm.conf'), 'r') as f: init = f.read() self.cut.init(default_context_id='http://example.org/') with open(p('.owm', 'owm.conf'), 'r') as f: self.assertEqual(init, f.read())
def test_no_versioned_bundles(tempdir): bundles_directory = p(tempdir, 'bundles') makedirs(p(bundles_directory, 'example')) b = Bundle('example', bundles_directory=bundles_directory) with pytest.raises(BundleNotFound, match='No versioned bundle directories'): b._get_bundle_directory()
def test_cache_list_empty_inconsistent_manifest_1(tempdir): bdir = p(tempdir, 'bdir', '10') makedirs(bdir) with open(p(bdir, 'manifest'), 'w') as f: f.write('{}') cut = Cache(tempdir) assert len(list(cut.list())) == 0
def test_save_diff(self): ''' Change something and make a diff ''' os.mkdir('test_module') open(p('test_module', '__init__.py'), 'w').close() with open(p('test_module', 'command_test_save.py'), 'w') as out: print(r''' from test_module.monkey import Monkey def pow_data(ns): ns.context.add_import(Monkey.definition_context) ns.context(Monkey)(bananas=55) ''', file=out) with open(p('test_module', 'monkey.py'), 'w') as out: print(r''' from PyOpenWorm.dataObject import DataObject, DatatypeProperty class Monkey(DataObject): class_context = 'http://example.org/primate/monkey' bananas = DatatypeProperty() def identifier_augment(self): return type(self).rdf_namespace['paul'] def defined_augment(self): return True __yarom_mapped_classes__ = (Monkey,) ''', file=out) print(listdir('.')) print(self.sh('pow save --module test_module.command_test_save')) self.assertRegexpMatches(self.sh('pow diff'), r'<[^>]+>')
def test_bundle_directory_manifest_has_no_bundle_id(tempdir): cut = Deployer() bdir = p(tempdir, 'notabundle') os.makedirs(bdir) with open(p(bdir, 'manifest'), 'w') as mf: json.dump({'manifest_version': 1, 'version': 1}, mf) with raises(NotABundlePath): cut.deploy(bdir)
def test_latest_bundle_fetched(tempdir): bundles_directory = p(tempdir, 'bundles') makedirs(p(bundles_directory, 'example', '1')) makedirs(p(bundles_directory, 'example', '2')) expected = p(bundles_directory, 'example', '3') makedirs(expected) b = Bundle('example', bundles_directory=bundles_directory) assert expected == b._get_bundle_directory()
def test_save_classes(owm_project): modpath = p(owm_project.testdir, 'test_module') os.mkdir(modpath) open(p(modpath, '__init__.py'), 'w').close() owm_project.writefile(p(modpath, 'monkey.py'), 'tests/test_modules/owmclitest03_monkey.py') print(owm_project.sh('owm save test_module.monkey')) assertRegexpMatches(owm_project.sh('owm diff'), r'<[^>]+>')
def gen_ctx_fname(ident, base_directory, hashfunc=None): hs = (hashfunc or hashlib.sha256)(ident.encode('UTF-8')).hexdigest() fname = p(base_directory, hs + '.nt') i = 1 while exists(fname): fname = p(base_directory, hs + '-' + str(i) + '.nt') i += 1 return fname
def test_specified_version_fetched(tempdir): bundles_directory = p(tempdir, 'bundles') makedirs(p(bundles_directory, 'example', '1')) expected = p(bundles_directory, 'example', '2') makedirs(expected) makedirs(p(bundles_directory, 'example', '3')) b = Bundle('example', version=2, bundles_directory=bundles_directory) assert expected == b._get_bundle_directory()
def setUp(self): xfail_without_db() self.startdir = os.getcwd() self.testdir = tempfile.mkdtemp(prefix=__name__ + '.') shutil.copytree('.pow', p(self.testdir, '.pow'), symlinks=True) shutil.copyfile('README.md', p(self.testdir, 'README.md')) shutil.copyfile('readme.conf', p(self.testdir, 'readme.conf')) os.chdir(self.testdir)
def test_ignore_non_version_number(tempdir): bundles_directory = p(tempdir, 'bundles') b = Bundle('example', bundles_directory=bundles_directory) makedirs(p(bundles_directory, 'example', 'ignore_me')) expected = p(bundles_directory, 'example', '5') makedirs(expected) actual = b._get_bundle_directory() assert actual == expected
def test_remove_lock_file(): ''' Unlinking the lock file early is not allowed generally and probably indicates a logic error, so we give an exception in that case ''' with pytest.raises(OSError): with TemporaryDirectory() as td, lock_file(p(td, 'lock')): unlink(p(td, 'lock'))
def test_dir_removed_load_no_raise(self): ''' The load method doesn't take responsibility for the directory existing, in general ''' os.mkdir(p(self.testdir, 'dir1')) cut = POWDirDataSourceDirLoader(self.testdir) cut.load('dsdid1') os.rmdir(p(self.testdir, 'dir1')) cut.load('dsdid1')
def test_save_is_archive(shell_helper): bundle_dir1 = p(shell_helper.test_homedir, '.owmeta', 'bundles', 'test%2Fmain', '1') makedirs(bundle_dir1) with open(p(bundle_dir1, 'manifest'), 'w') as mf: mf.write( '{"version": 1, "id": "test/main", "description": "Waka waka"}') shell_helper.sh('owm bundle save test/main test-main.tar.xz') assert tarfile.is_tarfile(p(shell_helper.testdir, 'test-main.tar.xz'))
def setUpClass(cls): super(TestCa, cls).setUpClass() if os.access(p('demoCA', 'index.txt'), 0): os.remove(p("demoCA", "index.txt")) open(p("demoCA", "index.txt"), 'w').close() _('engine -c') _('genpkey -algorithm bign -pkeyopt params:bign-curve256v1 -out {outfile}'.format(outfile=cls.PRIV_KEY_FILE)) _('genpkey -algorithm bign -pkeyopt params:bign-curve256v1 -out {outfile}'.format(outfile=cls.CAPRIV_KEY_FILE)) _('req -subj "/CN=www.mydom.com/O=My Dom, Inc./C=US/ST=Oregon/L=Portland" -new -key {key} -out {out}' .format(key=cls.PRIV_KEY_FILE, out=cls.REQ_FILE)) _('req -x509 -subj "/CN={CN}/O=My Dom, Inc./C=US/ST=Oregon/L=Portland" -new -key {key} -out {out}' .format(key=cls.CAPRIV_KEY_FILE, out=cls.CACERT_FILE, CN='www.mydom_%s.com' % random.randint(0, 10000)))
def test_init_default_store_config_file_exists_no_change(self): os.mkdir('.pow') with open(p('.pow', 'pow.conf'), 'w') as f: f.write('{}') self.cut.init() with open('.pow/pow.conf', 'r') as f: self.assertEqual('{}', f.read())
def test_reset_resets_remove(self): self.cut.init() self._add_to_graph() # dirty up the index repo = git.Repo(self.cut.powdir) f = p(self.cut.powdir, 'something') with open(f, 'w'): pass self.cut.commit('Commit Message 1') repo.index.remove([p('graphs', 'index')]) self.cut.commit('Commit Message 2') self.assertIn(p('graphs', 'index'), [x[0] for x in repo.index.entries])
def test_init_default_store_config_file_exists_update_store_conf(self): os.mkdir('.pow') with open('.pow/pow.conf', 'w') as f: f.write('{}') self.cut.init(update_existing_config=True) with open('.pow/pow.conf', 'r') as f: conf = json.load(f) self.assertEqual(conf['rdf.store_conf'], p('.pow', 'worm.db'), msg='path is updated')
def setUp(self): super(NeuronCSVDataTranslatorTest, self).setUp() fname = p(self.testdir, 'mycsv.csv') text = ''' header,row,completely,ignored,x ADAR,Neuropeptide,PDF-1,WormAtlas,http://wormatlas.org/neurons/Individual%20Neurons/ADAmainframe.htm''' with open(fname, 'w') as f: f.write(text.strip()) self.ds.csv_file_name('mycsv.csv')
def setUp(self): super(NeuronCSVDataTranslatorNoEvidenceTest, self).setUp() fname = p(self.testdir, 'mycsv.csv') text = ''' header,row,completely,ignored,x ADAR,Neuropeptide,PDF-1,WormAtlas,''' with open(fname, 'w') as f: f.write(text.strip()) self.ds.csv_file_name('mycsv.csv')
def test_clones_graphs(self): self.cut.basedir = 'r1' self.cut.init() self._add_to_graph() self.cut.commit('Commit Message') pd = self.cut.powdir clone = 'r2' self.cut.basedir = clone self.cut.clone(pd) self.assertTrue(exists(p(self.cut.powdir, 'graphs', 'index')))
def setUp(self): super(NeuronCSVDataTranslatorBibtexTest, self).setUp() fname = p(self.testdir, 'mycsv.csv') text = ''' header,row,completely,ignored,x ADAR,Neuropeptide,PDF-1,WormAtlas,''' with open(fname, 'w') as f: f.write(text.strip()) self.ds.csv_file_name('mycsv.csv') self.ds.bibtex_files(['ignored']) self.patcher = patch('PyOpenWorm.data_trans.neuron_data.parse_bibtex_into_documents') mock = self.patcher.start() def m(a, ctx): return {'WormAtlas': ctx(Document)(key="WormAtlas", title="something")} mock.side_effect = m
def test_reset_resets_add(self): self.cut.init() self._add_to_graph() # dirty up the index repo = git.Repo(self.cut.powdir) f = p(self.cut.powdir, 'something') with open(f, 'w'): pass self.cut.commit('Commit Message 1') repo.index.add(['something']) self.cut.commit('Commit Message 2') self.assertNotIn('something', [x[0] for x in repo.index.entries])
def setUp(self): self.startdir = os.getcwd() self.testdir = tempfile.mkdtemp(prefix=__name__ + '.') shutil.copytree('docs', p(self.testdir, 'docs')) os.chdir(self.testdir)
def execute(self, fname, **kwargs): failure_count, _ = doctest.testfile(p('docs', fname + '.rst'), module_relative=False, optionflags=(ALLOW_UNICODE | doctest.ELLIPSIS), **kwargs) self.assertEqual(failure_count, 0)
def test_init_default_creates_config(self): self.cut.init() self.assertTrue(exists(p('.pow', 'pow.conf')), msg='pow.conf is created')
def test_init_default_creates_store(self): self.cut.init() self.assertTrue(exists(p('.pow', 'worm.db')), msg='worm.db is created')
def test_init_tracks_config(self): """ Test that the config file is tracked """ self.cut.init() p(self.cut.powdir, '.git')
def test_init(self): """ A git directory should be created when we use the .git repository provider """ self.cut.init() self.assertTrue(exists(p(self.cut.powdir, '.git')))
def setUpClass(self): self.testdir = tempfile.mkdtemp(prefix=__name__ + '.') shutil.copytree('.pow', p(self.testdir, '.pow'), symlinks=True) shutil.copytree('examples', p(self.testdir, 'examples'), symlinks=True) self.startdir = os.getcwd() os.chdir(p(self.testdir, 'examples'))