def test_unknownarrayordertype(self): with tempdir() as dirname: dar = create_array(path=dirname, shape=(2,4), fill=0, dtype='int64', overwrite=True) dar._update_arrayinfo({'arrayorder': 'X'}) self.assertRaises(ValueError, numtypedescriptiontxt, dar) self.assertRaises(ValueError, Array, dirname)
def test_writejsondictincorrectinput(self): with tempdir() as dirname: bd = DataDir(dirname) with self.assertRaises(TypeError): bd.write_jsondict('test1.json', 3) with self.assertRaises(TypeError): bd.write_jsondict('test1.json', 'a')
def test_readjsondictrequiredkeypresent(self): with tempdir() as dirname: bd = DataDir(dirname) wd = {'a': 1, 'b': [1, 2, 3], 'c': 'k'} bd.write_jsondict('test1.json', wd) rd = bd.read_jsondict('test1.json', requiredkeys=('a', 'c')) self.assertDictEqual(wd, rd)
def test_openfile(self): with tempdir() as dirname: dar = create_array(path=dirname, shape=(0, 2), dtype='int64', overwrite=True, accessmode='r+') with dar._datadir.open_file('notes.txt', 'a') as f: f.write('test\n') path = dar.path / 'notes.txt' self.assertEqual(path.exists(), True)
def test_pathexistsdonotoverwrite(self): with tempdir() as dirname: filepath = dirname / "test" open(filepath, 'w').close() self.assertRaises(OSError, write_jsonfile, path=filepath, data={'a': 1})
def test_openfileprotectedfiles(self): with tempdir() as dirname: dar = create_array(path=dirname, shape=(0, 2), dtype='int64', overwrite=True, accessmode='r+') for fn in dar._protectedfiles: with self.assertRaises(OSError): with dar._datadir.open_file(fn, 'a') as f: f.write('test\n')
def test_readjsondictrequiredkeynotpresent(self): with tempdir() as dirname: bd = DataDir(dirname) wd = {'a': 1, 'b': [1, 2, 3], 'c': 'k'} bd.write_jsondict('test1.json', wd) self.assertRaises(ValueError, bd.read_jsondict, 'test1.json', requiredkeys=('a', 'd'))
def create_testbasedatadir(filename='test.json', datadict=None): if datadict is None: datadict = {'a': 1} with tempdir() as dirname: bdddirname = Path(dirname) / 'data.bd' bdddirname.mkdir() bdd = DataDir(bdddirname) bdd._write_jsondict(filename, datadict) yield bdd
def test_protectedfiles(self): with tempdir() as dirname: bd = DataDir(dirname, protectedpaths=('test.dat', )) self.assertEqual(bd.protectedfiles, set(('test.dat', )))
def test_updatejsondictcorrect(self): with tempdir() as dirname: bd = DataDir(dirname) bd.write_jsondict('test1.json', {'a': 1}) bd.update_jsondict('test1.json', {'a': 2, 'b': 3})
def test_report(self): with tempdir(report=True) as td: pass # see at least if this doesn't fail
def test_writejsondictcorrectinput(self): with tempdir() as dirname: bd = DataDir(dirname) bd.write_jsondict('test1.json', {'a': 1})
def test_keep(self): with tempdir(keep=True) as td: pass self.assertTrue(td.exists()) shutil.rmtree(td)
def test_isdeleted(self): with tempdir() as td: pass self.assertFalse(td.exists())
def test_writetxtoverwrite(self): with tempdir() as dirname: bd = DataDir(dirname) bd.write_txt('test1.txt', 'hello') bd.write_txt('test1.txt', 'hello', overwrite=True)
def test_writetxt(self): with tempdir() as dirname: bd = DataDir(dirname) bd.write_txt('test1.txt', 'hello') self.assertEqual(bd.read_txt('test1.txt'), 'hello')
def test_readjsondictnotdict(self): with tempdir() as dirname: bd = DataDir(dirname) bd.write_jsonfile('test1.json', [1, 2, 3]) self.assertRaises(TypeError, bd.read_jsondict, 'test1.json')
def test_deletefiles(self): with tempdir() as dirname: bd = DataDir(dirname) bd.write_txt('test1.txt', 'hello') bd.write_txt('test2.txt', 'hello') bd.delete_files(('test1.txt', 'test2.txt', 'test3.txt'))
def test_deleteprotectedfile(self): with tempdir() as dirname: bd = DataDir(dirname, protectedpaths=('test.dat', )) self.assertRaises(OSError, bd.delete_files, (('test.dat', )))
def test_pathexistsnotoverwrite(self): with tempdir() as dirname: self.assertRaises(OSError, create_datadir, dirname)
def test_writetxtdonotoverwrite(self): with tempdir() as dirname: bd = DataDir(dirname) bd.write_txt('test1.txt', 'hello') self.assertRaises(OSError, bd._write_txt, 'test1.txt', 'hello')
def test_pathexistsoverwrite(self): with tempdir() as dirname: create_datadir(dirname, overwrite=True)
def test_futurewarningdeprecation(self): with tempdir() as dirname: self.assertWarns(FutureWarning, create_basedatadir, dirname, True)
def test_ispath(self): with tempdir() as td: self.assertIsInstance(td, Path) self.assertTrue(td.exists()) self.assertTrue(td.is_dir())