Best Python code snippet using molecule_python
test_fileio.py
Source:test_fileio.py
1import asyncio2import inspect3import os4import sys5import pytest6from async_files import FileIO7from async_files.fileobj import FileObj8from tests.utils import get_event_loop9from tests.utils import TempDirTest10@pytest.fixture11def event_loop():12 yield get_event_loop()13class TestFileIO(TempDirTest):14 def setup_method(self):15 self.test_read_file = os.path.join(self.tempdir, "test_read_file")16 with open(self.test_read_file, "w") as f:17 f.writelines(["Hello\n", "world!"])18 def test_signature(self):19 assert inspect.signature(FileIO) == inspect.signature(open)20 @pytest.mark.asyncio21 async def test_async_attrs(self):22 async with FileIO(self.test_read_file) as f:23 # Assert that new fileobject is instance of the FileObj24 assert isinstance(f, FileObj)25 async_attrs = [26 "close",27 "flush",28 "isatty",29 "read",30 "readline",31 "readlines",32 # "reconfigure",33 "seek",34 "tell",35 "truncate",36 "write",37 "writelines",38 ]39 for attr in async_attrs:40 coro = getattr(f, attr)41 if not coro:42 pytest.fail(43 msg=f"{attr} haven't been attached to FileObj!")44 # Make sure all IO methods are converted into coroutines.45 # Only coroutine or an awaitable can be converted to future.46 try:47 future = asyncio.ensure_future(coro())48 future.cancel()49 except TypeError:50 pytest.fail(51 msg=52 f"{attr} exists but haven't been converted to coroutine!"53 )54 @pytest.mark.asyncio55 async def test_basics(self):56 async with FileIO(self.test_read_file) as f:57 assert all([f.readable, f.seekable, f.writable])58class TestCRUD(TempDirTest):59 def setup_method(self):60 self.test_read_file = os.path.join(self.tempdir, "test_read_file")61 with open(self.test_read_file, "w") as f:62 f.writelines(["Hello\n", "world!"])63 @pytest.mark.asyncio64 async def test_seek(self):65 async with FileIO(self.test_read_file) as f:66 assert (await f.tell()) == 067 s = await f.seek(4)68 assert s == 4 == (await f.tell())69 @pytest.mark.asyncio70 async def test_read(self):71 async with FileIO(self.test_read_file) as f:72 # Test Read whole file73 s = await f.read()74 assert s == "Hello\nworld!"75 # Test Read n characters76 await f.seek(0)77 s = await f.read(4)78 assert s == "Hell"79 expected_lines = ["Hello\n", "world!"]80 # Iterate lines81 await f.seek(0)82 lines = [line async for line in f]83 assert lines == expected_lines84 # make sure readlines are same as expected_lines85 await f.seek(0)86 assert expected_lines == await f.readlines()87 @pytest.mark.asyncio88 async def test_write(self):89 test_write_file = os.path.join(self.tempdir, "test_write_file")90 async with FileIO(test_write_file, "w") as f:91 await f.write("Hello\n")92 await f.writelines(["World\n", "This is amazing!"])93 with open(test_write_file, "r") as f:94 assert f.read() == "Hello\nWorld\nThis is amazing!"95 @pytest.mark.asyncio96 async def test_bytes_write(self):97 test_write_file = os.path.join(self.tempdir, "test_write_file")98 f = await FileIO(test_write_file, "wb")()99 await f.write(b"Hello\n")100 await f.writelines([b"World\n", b"This is amazing!"])101 await f.close()102 with open(test_write_file, "rb") as f:103 assert f.read() == b"Hello\nWorld\nThis is amazing!"104 @pytest.mark.asyncio105 async def test_bytes_read(self):106 async with FileIO(self.test_read_file, "rb") as f:107 # Test Read whole file108 lines = await f.read()109 if sys.platform.startswith("win"):110 assert lines == b"Hello\r\nworld!"111 else:...
MyClient.py
Source:MyClient.py
...23 filestore = FileStore.Client(protocol)24 transport.open()25 def __init__(self):26 pass27 def test_write_file(self, rfile):28 node = self.filestore.findSucc(rfile.meta.contentHash)29 print "node id: ", node30 assert node.id == "162f2ef78020a93545457290a21d4ea634d4bca22aff8530e2011209be88ff82", "Test_Write_file - Node returned is wrong"31 print "Test case passed 1 -- get successor node for key: ", rfile.meta.contentHash32 file_store = make_socket(node.ip, node.port)33 file_store.writeFile(rfile)34 read_file = file_store.readFile(rfile.meta.filename, rfile.meta.owner)35 assert str(read_file.meta.filename) == str(rfile.meta.filename), "Test_Write_file - Filename mismatch"36 assert str(read_file.meta.version) == str(rfile.meta.version), "Test_Write_file - version mismatch"37 assert str(read_file.meta.owner) == str(rfile.meta.owner), "Test_Write_file - owner mismatch"38 assert str(read_file.meta.contentHash) == str(rfile.meta.contentHash), "Test_Write_file - contentHash mismatch"39 assert str(read_file.content) == str(rfile.content), "Test_Write_file - Content mismatch"40 print "Test case passed 2 -- Write file: file name - %s, owner - %s" % (rfile.meta.filename, rfile.meta.owner)41if __name__ == '__main__':42 rf_metadata = RFileMetadata()43 rf_metadata.filename = "file_name"44 rf_metadata.version = 045 rf_metadata.owner = "owner_name"46 rf_metadata.contentHash = sha256(rf_metadata.owner + ":" + rf_metadata.filename).hexdigest()47 rfile = RFile()48 rfile.meta = rf_metadata49 rfile.content = "This is my first apache thrift programming experience"50 try:51 t = TestChord()52 t.test_write_file(rfile)53 # t.test_read_file(rf_metadata.filename, rf_metadata.owner)54 #t.test_negative_cases(make_socket('128.226.180.166', 9095), rfile.meta.contentHash)55 except Thrift.TException as tx:...
test_write.py
Source:test_write.py
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3"""4[tests/stdlib/test_write.py]5Test the write command.6"""7import os8import unittest9from ergonomica import ergo10class TestWrite(unittest.TestCase):11 """Tests the write command."""12 def test_write(self):13 """14 Tests the write command.15 """16 # first write to the file17 ergo("print oq4ij 4ojioj1iorj oo4joijoi12 | write -a test_write_file ${0}")18 # then check that the contents are correct19 with open("test_write_file") as f:20 self.assertEqual(f.read(), "oq4ij\n4ojioj1iorj\noo4joijoi12\n")21 # then cleanup22 os.remove("test_write_file")23 # first write to the file24 ergo("print oq4ij 4ojioj1iorj oo4joijoi12 | write test_write_file ${0}")25 # then check that the contents are correct26 with open("test_write_file") as f:27 self.assertEqual(f.read(), "oo4joijoi12\n")28 # then cleanup...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!