def test_read_values(mock_put, mock_get): mock_get.return_value.json.return_value = {'api_lock': '12345'} mock_get.return_value.status_code = 201 with serial(url='http://localhost:5005') as client: mock_get.return_value.json.return_value = {'data': 'Read Value'} mock_get.return_value.status_code = 201 response = client.read() assert response == 'Read Value' mock_get.return_value.json.return_value = {'api_lock': '12345'} mock_get.return_value.status_code = 201 with serial(url='http://localhost:5005') as client: mock_get.return_value.json.return_value = {'data': ''} mock_get.return_value.status_code = 404 response = client.read() assert response == ''
def test_write_values(mock_put, mock_get): mock_get.return_value.json.return_value = {'api_lock': '12345'} mock_get.return_value.status_code = 201 with serial(url='http://localhost:5005') as client: mock_put.return_value.json.return_value = "{'status': 'ok'}" mock_put.return_value.status_code = 201 response = client.write('failed messaage') assert response == "{'status': 'ok'}" mock_get.return_value.json.return_value = {'api_lock': '12345'} mock_get.return_value.status_code = 201 with serial(url='http://localhost:5005') as client: mock_put.return_value.json.return_value = {'status': 'connected'} mock_put.return_value.status_code = 404 response = client.write('failed messaage') assert response == {'status': ERROR_NO_ENDPOINT}
def test_port_list(mock_put, mock_get): mock_put.return_value.json.return_value = { 'ports': ['/dev/tty0', '/dev/tty1']} mock_put.return_value.status_code = 201 with serial(url='http://localhost:5005') as client: for port in client.ports(): assert port.startswith('/dev/')
def test_open_port_values(mock_put, mock_get): mock_get.return_value.json.return_value = {'api_lock': '12345'} mock_get.return_value.status_code = 201 with serial(url='http://localhost:5005') as client: mock_put.return_value.json.return_value = {'status': 'connected'} mock_put.return_value.status_code = 201 response = client.open('/dev/faketty') assert response == {'status': 'connected'} mock_get.return_value.json.return_value = {'api_lock': '12345'} mock_get.return_value.status_code = 201 with serial(url='http://localhost:5005') as client: mock_put.return_value.json.return_value = {'status': ERROR_NO_ENDPOINT} mock_put.return_value.status_code = 404 response = client.open('/dev/faketty') assert response == {'status': ERROR_NO_ENDPOINT}
def test_is_connected(mock_get): """Test that we can get an api_lock key""" mock_get.return_value.json.return_value = {'api_lock': '12345'} mock_get.return_value.status_code = 201 with serial(url='http://localhost:5005') as client: mock_get.return_value.json.return_value = {'connected': True} mock_get.return_value.status_code = 201 response = client.isOpen() assert response is True mock_get.return_value.json.return_value = {'api_lock': '12345'} mock_get.return_value.status_code = 201 with serial(url='http://localhost:5005') as client: mock_get.return_value.json.return_value = {'connected': False} mock_get.return_value.status_code = 404 response = client.isOpen() assert response is False
def test_port_list(mock_put, mock_get): mock_put.return_value.json.return_value = { 'ports': ['/dev/tty0', '/dev/tty1'] } mock_put.return_value.status_code = 201 with serial(url='http://localhost:5005') as client: for port in client.ports(): assert port.startswith('/dev/')
def test_switch_port(mock_put, mock_get): mock_get.return_value.json.return_value = {'api_lock': '12345'} mock_get.return_value.status_code = 201 with serial(url='http://localhost:5005') as client: mock_put.return_value.json.return_value = {'status': 'connected'} mock_put.return_value.status_code = 201 response = client.open('/dev/faketty1') assert response == {'status': 'connected'} response = client.open('/dev/faketty2') assert response == {'status': 'connected'}
def test_connection_to_invalid_url(): with serial(url='http://localdoesnotexist:5005') as client: response = client.open('/dev/fakeport') assert response == {'status': ERROR_NOT_CONNECTED}
# Example program connecting to the server and printing # this just dumps the responses to the command line import os import pty from serjax.client import serial master_pty, slave_pty = pty.openpty() slave_port = os.ttyname(slave_pty) master_port = os.ttyname(master_pty) with serial(url='http://localhost:5005') as client: print('List ports') print("\t%s" % client.ports()) print('Open port') print("\t%s" % client.open(slave_port)) print('Client isOpen') # print("\t%d" % client.isOpen()) print('Client status') print("\t%s" % client.status()) # you should see this pop up in the echo server logs a = client.write('test first message') print(a) print('Read text on serial buffer') os.write(master_pty, 'Read text on serial buffer') print("\t%s" % client.read()) os.write(master_pty, 'test second message') print("\t%s" % client.read())