def test_read_file(self): """Reading should proceed block-by-block.""" s = MockSerial([b"", # Close existing file b"file.close()\r\n", b"file.close()\r\n", # Check for existance of the file b"=file.list()['test.txt']\r\n", b"=file.list()['test.txt']\r\n3\r\n", # Open the file for read b"=file.open('test.txt', 'r')\r\n", b"=file.open('test.txt', 'r')\r\ntrue\r\n", # Read a block b"uart.write(0, file.read(2))\r\n", b"uart.write(0, file.read(2))\r\n\x01\x02", # Read last block b"uart.write(0, file.read(1))\r\n", b"uart.write(0, file.read(1))\r\n\x03", # Close the file b"file.close()\r\n", b"file.close()\r\n"]) n = NodeMCU(s) assert n.read_file("test.txt", 2) == b"\x01\x02\x03" assert s.finished
def test_flush(self, junk): """Flush should read all available data.""" s = MockSerial([junk]) n = NodeMCU(s) n.flush() assert s.expected_sequence[0] == b""
def test_read_verbose(self): """When verbose channel provided, reads are echoed.""" s = Mock(read=Mock(return_value=b"passes")) verb = Mock() n = NodeMCU(s, verb) assert n.read(6) == b"passes" verb.write.assert_called_once_with(b"passes")
def test_read_file(self): """Reading should proceed block-by-block.""" s = MockSerial([ b"", # Close existing file b"file.close()\r\n", b"file.close()\r\n", # Check for existance of the file b"=file.list()['test.txt']\r\n", b"=file.list()['test.txt']\r\n3\r\n", # Open the file for read b"=file.open('test.txt', 'r')\r\n", b"=file.open('test.txt', 'r')\r\ntrue\r\n", # Read a block b"uart.write(0, file.read(2))\r\n", b"uart.write(0, file.read(2))\r\n\x01\x02", # Read last block b"uart.write(0, file.read(1))\r\n", b"uart.write(0, file.read(1))\r\n\x03", # Close the file b"file.close()\r\n", b"file.close()\r\n" ]) n = NodeMCU(s) assert n.read_file("test.txt", 2) == b"\x01\x02\x03" assert s.finished
def test_write_file(self): """Writing should succeed in blocks of some predetermined size.""" s = MockSerial([ b"", # Close existing file b"file.close()\r\n", b"file.close()\r\n", # Open file b"=file.open('test.txt', 'w')\r\n", b"=file.open('test.txt', 'w')\r\ntrue\r\n", # Write part 1 b"=file.write('12')\r\n", b"=file.write('12')\r\ntrue\r\n", # Write part 2 b"=file.write('3')\r\n", b"=file.write('3')\r\ntrue\r\n", # Close file b"file.close()\r\n", b"file.close()\r\n" ]) n = NodeMCU(s) # Write two bytes at a time n.write_file("test.txt", b"123", 2) assert s.finished
def test_format(self): """Format should just work...""" s = MockSerial([b"", b"file.format()\r\n", b"file.format()\r\n"]) n = NodeMCU(s) n.format() assert s.finished
def test_read_line(self): """Line up to the line-ending should be absorbed.""" s = MockSerial([b"ba-ba-black sheep bar baz"]) n = NodeMCU(s) line = n.read_line(b"bar") # Ending should be stripped off assert line == b"ba-ba-black sheep " # Remainder should say in the buffer assert s.expected_sequence[0] == b" baz"
def test_rename_file(self): """Remove file should work.""" s = MockSerial([b"", # Rename b"=file.rename('old.txt', 'new.txt')\r\n", b"=file.rename('old.txt', 'new.txt')\r\ntrue\r\n"]) n = NodeMCU(s) n.rename_file("old.txt", "new.txt") assert s.finished
def test_dofile_no_file(self): """If no file, dofile should fail.""" s = MockSerial([b"", # Check existance b"=file.list()['test.lua']\r\n", b"=file.list()['test.lua']\r\nnil\r\n"]) n = NodeMCU(s) with pytest.raises(IOError): n.dofile("test.lua") assert s.finished
def test_rename_file_fails(self): """Remove file can fail.""" s = MockSerial([b"", # Rename b"=file.rename('old.txt', 'new.txt')\r\n", b"=file.rename('old.txt', 'new.txt')\r\nnil\r\n"]) n = NodeMCU(s) with pytest.raises(IOError): n.rename_file("old.txt", "new.txt") assert s.finished
def test_remove_file_no_file(self): """Make sure a file which doesn't exist doesn't get removed.""" s = MockSerial([b"", # Check file existance b"=file.list()['test.txt']\r\n", b"=file.list()['test.txt']\r\nnil\r\n"]) n = NodeMCU(s) with pytest.raises(IOError): n.remove_file("test.txt") assert s.finished
def test_rename_file(self): """Remove file should work.""" s = MockSerial([ b"", # Rename b"=file.rename('old.txt', 'new.txt')\r\n", b"=file.rename('old.txt', 'new.txt')\r\ntrue\r\n" ]) n = NodeMCU(s) n.rename_file("old.txt", "new.txt") assert s.finished
def test_get_version(self): """Make sure versions are correctly decoded.""" s = MockSerial([b"", # Nothing to read in buffer b"=node.info()\r\n", # Command should be sent b"=node.info()\r\n" # Echo-back and response b"1\t4\t1234\t4321\r\n"]) # Node info n = NodeMCU(s) major, minor = n.get_version() assert major == 1 assert minor == 4 assert s.finished
def test_dofile(self): """Dofile should return command's output.""" s = MockSerial([b"", # Check existance b"=file.list()['test.lua']\r\n", b"=file.list()['test.lua']\r\n123\r\n", # Execute and await prompt b"dofile('test.lua')\r\n", b"dofile('test.lua')\r\nhello!\r\n> "]) n = NodeMCU(s) assert n.dofile("test.lua") == b"hello!\r\n" assert s.finished
def test_dofile_no_file(self): """If no file, dofile should fail.""" s = MockSerial([ b"", # Check existance b"=file.list()['test.lua']\r\n", b"=file.list()['test.lua']\r\nnil\r\n" ]) n = NodeMCU(s) with pytest.raises(IOError): n.dofile("test.lua") assert s.finished
def test_remove_file(self): """Remove file should work.""" s = MockSerial([b"", # Check file existance b"=file.list()['test.txt']\r\n", b"=file.list()['test.txt']\r\n123\r\n", # Remove file b"file.remove('test.txt')\r\n", b"file.remove('test.txt')\r\n"]) n = NodeMCU(s) n.remove_file("test.txt") assert s.finished
def test_list_files_with_no_files(self): """Special case: list files when none present""" s = MockSerial([b"", # Get file count self.COUNT_FILES_SNIPPET + b"\r\n", self.COUNT_FILES_SNIPPET + b"\r\n0\r\n", # Enumerate file info self.LIST_FILES_SNIPPET + b"\r\n", self.LIST_FILES_SNIPPET + b"\r\n"]) n = NodeMCU(s) assert n.list_files() == {} assert s.finished
def test_read_file_not_exists(self): """Files which can't be opened for read cause an error.""" s = MockSerial([b"", # Close existing file b"file.close()\r\n", b"file.close()\r\n", # Check for existance of the file b"=file.list()['test.txt']\r\n", b"=file.list()['test.txt']\r\nnil\r\n"]) n = NodeMCU(s) with pytest.raises(IOError): n.read_file("test.txt") assert s.finished
def test_write_file_unopenable(self): """Files which can't be opened for write cause an error.""" s = MockSerial([b"", # Close existing file b"file.close()\r\n", b"file.close()\r\n", # Open file b"=file.open('test.txt', 'w')\r\n", b"=file.open('test.txt', 'w')\r\nnil\r\n"]) n = NodeMCU(s) with pytest.raises(IOError): n.write_file("test.txt", b"1234") assert s.finished
def test_remove_file_no_file(self): """Make sure a file which doesn't exist doesn't get removed.""" s = MockSerial([ b"", # Check file existance b"=file.list()['test.txt']\r\n", b"=file.list()['test.txt']\r\nnil\r\n" ]) n = NodeMCU(s) with pytest.raises(IOError): n.remove_file("test.txt") assert s.finished
def test_rename_file_fails(self): """Remove file can fail.""" s = MockSerial([ b"", # Rename b"=file.rename('old.txt', 'new.txt')\r\n", b"=file.rename('old.txt', 'new.txt')\r\nnil\r\n" ]) n = NodeMCU(s) with pytest.raises(IOError): n.rename_file("old.txt", "new.txt") assert s.finished
def test_get_version(self): """Make sure versions are correctly decoded.""" s = MockSerial([ b"", # Nothing to read in buffer b"=node.info()\r\n", # Command should be sent b"=node.info()\r\n" # Echo-back and response b"1\t4\t1234\t4321\r\n" ]) # Node info n = NodeMCU(s) major, minor = n.get_version() assert major == 1 assert minor == 4 assert s.finished
def test_send_command(self, prompt, response): """Make sure commands are sent and the echo-back absorbed.""" s = MockSerial([b"", # Nothing to read in buffer b"foo()\r\n", # Command should be sent (prompt + b"foo()\r\n" + response)]) # Echo-back and response n = NodeMCU(s) n.send_command(b"foo()") # Should have sent the command as expected assert len(s.expected_sequence) == 1 # Should have read in the echo-back but left the response assert s.expected_sequence[0] == response
def test_send_command(self, prompt, response): """Make sure commands are sent and the echo-back absorbed.""" s = MockSerial([ b"", # Nothing to read in buffer b"foo()\r\n", # Command should be sent (prompt + b"foo()\r\n" + response) ]) # Echo-back and response n = NodeMCU(s) n.send_command(b"foo()") # Should have sent the command as expected assert len(s.expected_sequence) == 1 # Should have read in the echo-back but left the response assert s.expected_sequence[0] == response
def test_remove_file(self): """Remove file should work.""" s = MockSerial([ b"", # Check file existance b"=file.list()['test.txt']\r\n", b"=file.list()['test.txt']\r\n123\r\n", # Remove file b"file.remove('test.txt')\r\n", b"file.remove('test.txt')\r\n" ]) n = NodeMCU(s) n.remove_file("test.txt") assert s.finished
def test_dofile(self): """Dofile should return command's output.""" s = MockSerial([ b"", # Check existance b"=file.list()['test.lua']\r\n", b"=file.list()['test.lua']\r\n123\r\n", # Execute and await prompt b"dofile('test.lua')\r\n", b"dofile('test.lua')\r\nhello!\r\n> " ]) n = NodeMCU(s) assert n.dofile("test.lua") == b"hello!\r\n" assert s.finished
def test_list_files_with_no_files(self): """Special case: list files when none present""" s = MockSerial([ b"", # Get file count self.COUNT_FILES_SNIPPET + b"\r\n", self.COUNT_FILES_SNIPPET + b"\r\n0\r\n", # Enumerate file info self.LIST_FILES_SNIPPET + b"\r\n", self.LIST_FILES_SNIPPET + b"\r\n" ]) n = NodeMCU(s) assert n.list_files() == {} assert s.finished
def test_write_file_unopenable(self): """Files which can't be opened for write cause an error.""" s = MockSerial([ b"", # Close existing file b"file.close()\r\n", b"file.close()\r\n", # Open file b"=file.open('test.txt', 'w')\r\n", b"=file.open('test.txt', 'w')\r\nnil\r\n" ]) n = NodeMCU(s) with pytest.raises(IOError): n.write_file("test.txt", b"1234") assert s.finished
def test_read_file_not_exists(self): """Files which can't be opened for read cause an error.""" s = MockSerial([ b"", # Close existing file b"file.close()\r\n", b"file.close()\r\n", # Check for existance of the file b"=file.list()['test.txt']\r\n", b"=file.list()['test.txt']\r\nnil\r\n" ]) n = NodeMCU(s) with pytest.raises(IOError): n.read_file("test.txt") assert s.finished
def test_write_file_unwriteable(self): """Files which can't be be written to cause an error.""" s = MockSerial([b"", # Close existing file b"file.close()\r\n", b"file.close()\r\n", # Open file b"=file.open('test.txt', 'w')\r\n", b"=file.open('test.txt', 'w')\r\ntrue\r\n", # Write fails b"=file.write('1234')\r\n", b"=file.write('1234')\r\nnil\r\n"]) n = NodeMCU(s) with pytest.raises(IOError): n.write_file("test.txt", b"1234") assert s.finished
def test_context_manager_wrapper(self): """Make sure the context manager passes through to the serial port.""" s = MockSerial() n = NodeMCU(s) assert s.context_manager_state == [] with n: assert s.context_manager_state == ["enter"] assert s.context_manager_state == [ "enter", ("exit", (None, None, None), {}) ]
def test_write_file_unwriteable(self): """Files which can't be be written to cause an error.""" s = MockSerial([ b"", # Close existing file b"file.close()\r\n", b"file.close()\r\n", # Open file b"=file.open('test.txt', 'w')\r\n", b"=file.open('test.txt', 'w')\r\ntrue\r\n", # Write fails b"=file.write('1234')\r\n", b"=file.write('1234')\r\nnil\r\n" ]) n = NodeMCU(s) with pytest.raises(IOError): n.write_file("test.txt", b"1234") assert s.finished
def test_list_files(self): """Should send a suitable file-listing command.""" s = MockSerial([b"", # Get file count self.COUNT_FILES_SNIPPET + b"\r\n", self.COUNT_FILES_SNIPPET + b"\r\n2\r\n", # Enumerate file info self.LIST_FILES_SNIPPET + b"\r\n", self.LIST_FILES_SNIPPET + b"\r\n" + b"7\r\nfoo.txt123\r\n" b"5\r\n\t.tab0\r\n"]) n = NodeMCU(s) assert n.list_files() == { "foo.txt": 123, "\t.tab": 0, } assert s.finished
def test_list_files(self): """Should send a suitable file-listing command.""" s = MockSerial([ b"", # Get file count self.COUNT_FILES_SNIPPET + b"\r\n", self.COUNT_FILES_SNIPPET + b"\r\n2\r\n", # Enumerate file info self.LIST_FILES_SNIPPET + b"\r\n", self.LIST_FILES_SNIPPET + b"\r\n" + b"7\r\nfoo.txt123\r\n" b"5\r\n\t.tab0\r\n" ]) n = NodeMCU(s) assert n.list_files() == { "foo.txt": 123, "\t.tab": 0, } assert s.finished
def test_restart(self): """Should be able to restart, absorbing any garbage.""" s = MockSerial([b"", # Send restart command, get echoback, a prompt and # some garbage b"node.restart()\r\n", (b"node.restart()\r\n" b"> " b"\xDE\xAD\xBE\xEF\xFF" # Garbage b"\r\n\r\n" b"NodeMCU [some version]\r\n" # Banner b" some info: here\r\n" b" build built on: 1990-12-11 19:56\r\n" b" powered by Lua 5.1.4 on SDK 1.4.0\r\n" b"> ")]) n = NodeMCU(s) n.restart() assert s.finished
def test_write_file(self): """Writing should succeed in blocks of some predetermined size.""" s = MockSerial([b"", # Close existing file b"file.close()\r\n", b"file.close()\r\n", # Open file b"=file.open('test.txt', 'w')\r\n", b"=file.open('test.txt', 'w')\r\ntrue\r\n", # Write part 1 b"=file.write('12')\r\n", b"=file.write('12')\r\ntrue\r\n", # Write part 2 b"=file.write('3')\r\n", b"=file.write('3')\r\ntrue\r\n", # Close file b"file.close()\r\n", b"file.close()\r\n"]) n = NodeMCU(s) # Write two bytes at a time n.write_file("test.txt", b"123", 2) assert s.finished
def test_restart(self): """Should be able to restart, absorbing any garbage.""" s = MockSerial([ b"", # Send restart command, get echoback, a prompt and # some garbage b"node.restart()\r\n", ( b"node.restart()\r\n" b"> " b"\xDE\xAD\xBE\xEF\xFF" # Garbage b"\r\n\r\n" b"NodeMCU [some version]\r\n" # Banner b" some info: here\r\n" b" build built on: 1990-12-11 19:56\r\n" b" powered by Lua 5.1.4 on SDK 1.4.0\r\n" b"> ") ]) n = NodeMCU(s) n.restart() assert s.finished
def test_read(self): """Read wrapper should work as expected...""" s = Mock(read=Mock(return_value=b"passes")) n = NodeMCU(s) assert n.read(6) == b"passes"
def test_write(self): """Write wrapper should work as expected...""" s = Mock(write=Mock(return_value=6)) n = NodeMCU(s) assert n.write(b"passes") == 6
def test_read_timeout(self): """Make sure read fails when wrong response length received.""" s = Mock(read=Mock(return_value=b"fails")) n = NodeMCU(s) with pytest.raises(IOError): n.read(6)
def test_write_timeout(self): """Make sure write fails when wrong response length received.""" s = Mock(write=Mock(return_value=1)) n = NodeMCU(s) with pytest.raises(IOError): n.write(b"fails")