def test_exists_path(self): self.assertFalse(Utils.exists_path(test_file_path, True)) with self.assertRaises(TypeError): Utils.exists_path() with self.assertRaises(TypeError): Utils.exists_path(isFolder=True) self.assertFalse(Utils.exists_path(self.base_path)) self.assertTrue(Utils.exists_path(test_file_path)) self.assertTrue(Utils.exists_path(self.base_path, True))
def test_write_to_file(self): test_filename = 'empty_file.txt' message = 'file content' file_path = Utils.join_path(os.getcwd(), test_filename) self.assertFalse(Utils.exists_path(file_path)) Utils.write_to_file(file_path, message) self.assertTrue(os.path.exists(file_path)) with open(file_path, 'rb') as f: self.assertEqual(f.read().decode('utf-8').find(message), 0) os.remove(file_path) self.assertFalse(os.path.exists(file_path)) file_path = Utils.join_path(os.getcwd(), 'some_path', 'sub_path', test_filename) Utils.write_to_file(file_path, message) self.assertTrue(os.path.exists(file_path)) with open(file_path, 'rb') as f: self.assertEqual(f.read().decode('utf-8').find(message), 0) os.remove(file_path) self.assertFalse(os.path.exists(file_path)) shutil.rmtree(Utils.join_path(os.getcwd(), 'some_path')) os.makedirs(Utils.join_path(os.getcwd(), 'some_path')) file_path = Utils.join_path(os.getcwd(), 'some_path', 'sub_path', test_filename) Utils.write_to_file(file_path, message) self.assertTrue(os.path.exists(file_path)) with open(file_path, 'rb') as f: self.assertEqual(f.read().decode('utf-8').find(message), 0) os.remove(file_path) self.assertFalse(os.path.exists(file_path)) Utils.write_to_file(file_path, {"some": "content"}, action='a+', as_json=True) self.assertTrue(os.path.exists(file_path)) with open(file_path, 'rb') as f: self.assertEqual(f.read().decode('utf-8'), '{"some": "content"}') os.remove(file_path) self.assertFalse(os.path.exists(file_path)) shutil.rmtree(Utils.join_path(os.getcwd(), 'some_path'))
def test_write_to_file(self): test_filename = 'empty_file.txt' message = 'file content' file_path = Utils.join_path(os.getcwd(), test_filename) self.assertFalse(Utils.exists_path(file_path)) Utils.write_to_file(file_path, message) self.assertTrue(os.path.exists(file_path)) with open(file_path, 'rb') as f: self.assertEqual(f.read().decode('utf-8').find(message), 0) os.remove(file_path) self.assertFalse(os.path.exists(file_path)) file_path = Utils.join_path(os.getcwd(), 'some_path', 'sub_path', test_filename) Utils.write_to_file(file_path, message) self.assertTrue(os.path.exists(file_path)) with open(file_path, 'rb') as f: self.assertEqual(f.read().decode('utf-8').find(message), 0) os.remove(file_path) self.assertFalse(os.path.exists(file_path)) self.create_folder(Utils.join_path(os.getcwd(), 'some_path')) file_path = Utils.join_path(os.getcwd(), 'some_path', 'sub_path', test_filename) Utils.write_to_file(file_path, message) self.assertTrue(os.path.exists(file_path)) with open(file_path, 'rb') as f: self.assertEqual(f.read().decode('utf-8').find(message), 0) os.remove(file_path) self.assertFalse(os.path.exists(file_path)) Utils.write_to_file(file_path, {"some": "content"}, action='a+', as_json=True) self.assertTrue(os.path.exists(file_path)) with open(file_path, 'rb') as f: self.assertEqual(f.read().decode('utf-8'), '{"some": "content"}') os.remove(file_path) self.assertFalse(os.path.exists(file_path)) self.delete_folder(Utils.join_path(os.getcwd(), 'some_path'))