def test_read_from_file_env_val_fail_name_2(self): tf = self.tempfile() print('{"z": "$1StillNoGood"}', file=tf) tf.close() try: with self.assertRaises(ValueError): Configure.open(tf.name) finally: os.unlink(tf.name)
def test_init_config_no_Data(self): """ Should fail if given a non-Data configuration """ # XXX: This test touches some machinery in # PyOpenWorm/__init__.py. Feel like it's a bad test tmp = Configureable.default Configureable.default = Configure() with self.assertRaises(BadConf): DataUser() Configureable.default = tmp
def test_namespace_manager(self): c = Configure() c['rdf.source'] = 'default' c['rdf.store'] = 'default' Configureable.default = c d = Data() d.openDatabase() self.assertIsInstance(d['rdf.namespace_manager'], R.namespace.NamespaceManager)
def test_ConfigValue(self): """ Assign a ConfigValue""" c = Configure() class pipe(ConfigValue): def get(self): return "sign" c['seven'] = pipe() self.assertEqual("sign", c['seven'])
def test_init_no_rdf_store(self): """ Should be able to init without these values """ c = Configure() Configureable.default = c d = Data() try: d.openDatabase() except Exception: self.fail("Bad state")
def test_getter_no_ConfigValue(self): """ Assign a method with a "get". Should return a the object rather than calling its get method """ c = Configure() class pipe: def get(self): return "sign" c['seven'] = pipe() self.assertIsInstance(c['seven'], pipe)
def test_read_from_file_env_val_multi_empty(self): with patch.dict('os.environ', (), clear=True): tf = self.tempfile() print('{"z": "$V1$V2"}', file=tf) tf.close() try: c = Configure.open(tf.name) self.assertIsNone(c['z']) finally: os.unlink(tf.name)
def test_read_from_file_env_val_empty_string(self): with patch.dict('os.environ', {'ENV_VAR': ''}): tf = self.tempfile() print('{"z": "$ENV_VAR"}', file=tf) tf.close() try: c = Configure.open(tf.name) self.assertIsNone(c['z']) finally: os.unlink(tf.name)
def test_read_from_file_env_val_success(self): with patch.dict('os.environ', {'ENV_VAR': 'myapikey'}): tf = self.tempfile() print('{"z": "$ENV_VAR"}', file=tf) tf.close() try: c = Configure.open(tf.name) self.assertEqual(c['z'], 'myapikey') finally: os.unlink(tf.name)
def test_read_from_file_env_val_multi_embedded_success(self): with patch.dict('os.environ', {'USER': '******', 'IS_SUPER': 'normal'}, clear=True): tf = self.tempfile() print('{"greeting": "Hello, $IS_SUPER $USER"}', file=tf) tf.close() try: c = Configure.open(tf.name) self.assertEqual(c['greeting'], 'Hello, normal dave') finally: os.unlink(tf.name)
def test_read_from_file_env_val_embedded_success(self): with patch.dict('os.environ', {'USER': '******'}, clear=True): tf = self.tempfile() print('{"greeting": "Hello, $USER"}', file=tf) tf.close() try: c = Configure.open(tf.name) self.assertEqual(c['greeting'], 'Hello, dave') finally: os.unlink(tf.name)
def test_base_varname_override(self): with patch('PyOpenWorm.configure.resource_filename') as rf: with patch.dict('os.environ', {'BASE': 'cars'}, clear=True): rf.return_value = 'moosh' tf = self.tempfile() print('{"z": "$BASE/car"}', file=tf) tf.close() try: c = Configure.open(tf.name) self.assertEqual(c['z'], 'cars/car') finally: os.unlink(tf.name)
def test_late_get(self): """ "get" shouldn't be called until the value is *dereferenced* """ c = Configure() a = {'t': False} class pipe(ConfigValue): def get(self): a['t'] = True return "sign" c['seven'] = pipe() self.assertFalse(a['t']) self.assertEqual(c['seven'], "sign") self.assertTrue(a['t'])
def test_here_varname(self): ''' By default, $HERE refers to the directory the config file sits in, or to the current working directory ''' with patch.dict('os.environ', (), clear=True): tf = self.tempfile() dname = os.path.dirname(tf.name) print('{"z": "$HERE/car"}', file=tf) tf.close() try: c = Configure.open(tf.name) self.assertEqual(c['z'], dname + '/car') finally: os.unlink(tf.name)
def test_ZODB_persistence(self): """ Should be able to init without these values """ c = Configure() fname = 'ZODB.fs' c['rdf.source'] = 'ZODB' c['rdf.store_conf'] = fname Configureable.default = c d = Data() try: d.openDatabase() g = make_graph(20) for x in g: d['rdf.graph'].add(x) d.closeDatabase() d.openDatabase() self.assertEqual(20, len(list(d['rdf.graph']))) d.closeDatabase() except Exception: traceback.print_exc() self.fail("Bad state") delete_zodb_data_store(fname)
def test_Sleepycat_persistence(self): """ Should be able to init without these values """ c = Configure() fname = 'Sleepycat_store' c['rdf.source'] = 'Sleepycat' c['rdf.store_conf'] = fname Configureable.default = c d = Data() try: d.openDatabase() g = make_graph(20) for x in g: d['rdf.graph'].add(x) d.closeDatabase() d.openDatabase() self.assertEqual(20, len(list(d['rdf.graph']))) d.closeDatabase() except Exception: traceback.print_exc() self.fail("Bad state") subprocess.call("rm -rf "+fname, shell=True)
def test_contains(self): c = Configure(x=2, y=1) self.assertIn('x', c)
def test_copy_dict(self): c = Configure() c.copy({'a': 1}) self.assertEqual(c['a'], 1)
def test_copy_non_string_key(self): c = Configure() c.copy({5: 1}) self.assertEqual(c[5], 1)
def test_here_varname_override(self): with patch.dict('os.environ', {'HERE': 'there'}, clear=True): c = Configure.process_config({'configure.file_location': '/blah.file', 'z': '$HERE/car'}) self.assertEqual(c['z'], 'there/car')
def test_fake_config(self): """ Try to retrieve a config value that hasn't been set """ with self.assertRaises(KeyError): c = Configure() c['not_a_valid_config']
def test_literal(self): """ Assign a literal rather than a ConfigValue""" c = Configure() c['seven'] = "coke" self.assertEqual(c['seven'], "coke")
def test_dict_init(self): c = Configure(x=4, y=3) self.assertEqual(4, c['x'])
def test_iter(self): c = Configure(x=2, y=1) self.assertEqual({'x', 'y'}, {s for s in c})
def test_here_varname_no_value(self): with patch.dict('os.environ', (), clear=True): c = Configure.process_config({'z': '$HERE/car'}) self.assertEqual(c['z'], '/car')