def test16_content_ok(self): """Test content_ok method.""" self.assertTrue( Namaste(0, 'frog').content_ok('tests/testdata/namaste')) self.assertFalse(Namaste().content_ok('tests/testdata/namaste')) self.assertFalse( Namaste(0, 'bison').content_ok('tests/testdata/namaste'))
def test13_tvalue(self): """Test tvalue property.""" # Explicitly set n = Namaste(tvalue='xyz') self.assertEqual(n.tvalue, 'xyz') # Derived from content n = Namaste(content='x y z\n') self.assertEqual(n.tvalue, 'x_y_z')
def test15_check_content(self): """Test check_content method.""" Namaste(0, 'frog').check_content('tests/testdata/namaste') self.assertRaises(NamasteException, Namaste().check_content, 'tests/testdata/namaste') self.assertRaises(NamasteException, Namaste(0, 'a').check_content, 'tests/testdata/namaste/does_not_exist') self.assertRaises(NamasteException, Namaste(0, 'bison').check_content, 'tests/testdata/namaste')
def test11_init(self): """Test initialization.""" n = Namaste() self.assertEqual(n.d, 0) self.assertEqual(n._tr_func, content_to_tvalue) # pylint: disable=protected-access n = Namaste(0, 'myspec') self.assertEqual(n.d, 0) self.assertEqual(n.content, 'myspec') n = Namaste(d=0, content='whatevs', tvalue='myspec_1', tr_func=lambda x: x[:3]) self.assertEqual(n.content, 'whatevs')
def test14_write(self): """Test write method.""" tempdir = tempfile.mkdtemp(prefix='test_namaste') # Plain OS method n = Namaste(0, 'balloon') n.write(tempdir) filepath = os.path.join(tempdir, '0=balloon') self.assertTrue(os.path.isfile(filepath)) with open(filepath, 'r', encoding="utf-8") as fh: self.assertEqual(fh.read(), 'balloon\n') # With fs filesystem tmpfs = fs.tempfs.TempFS() n = Namaste(1, 'jelly') n.write(pyfs=tmpfs) self.assertTrue(tmpfs.isfile('1=jelly')) self.assertEqual(tmpfs.readtext('1=jelly'), 'jelly\n')
def test14_write(self): """Test write method.""" tempdir = tempfile.mkdtemp(prefix='test_namaste') n = Namaste(0, 'balloon') n.write(tempdir) filepath = os.path.join(tempdir, '0=balloon') self.assertTrue(os.path.isfile(filepath)) with open(filepath, 'r') as fh: self.assertEqual(fh.read(), 'balloon\n')
def test15_check_content(self): """Test check_content method.""" Namaste(0, 'frog').check_content('tests/testdata/namaste') self.assertRaises(NamasteException, Namaste().check_content, 'tests/testdata/namaste') self.assertRaises(NamasteException, Namaste(0, 'a').check_content, 'tests/testdata/namaste/does_not_exist') self.assertRaises(NamasteException, Namaste(0, 'bison').check_content, 'tests/testdata/namaste') # Using pyfs... tmpfs = fs.tempfs.TempFS() tmpfs.writetext('9=niner', 'niner\n') tmpfs.writetext('8=smiley', 'FROWNY\n') Namaste(9, 'niner').check_content(pyfs=tmpfs) Namaste(9, 'niner').check_content(dir='', pyfs=tmpfs) self.assertRaises(NamasteException, Namaste(8, 'smiley').check_content, pyfs=tmpfs) self.assertRaises(NamasteException, Namaste(7, 'not-there').check_content, pyfs=tmpfs)
def test12_filename(self): """Test filename property.""" n = Namaste() self.assertEqual(n.filename, '0=') n = Namaste(2, 'wibble') self.assertEqual(n.filename, '2=wibble')