def test_led(self): engine = mocks.MockATEngine() controller = app.controller.LightController(engine) controller.led('STATE 123') self.assertEqual(len(engine.calls), 1) self.assertEqual(engine.calls[0]['cmd'], 'LED') self.assertEqual(engine.calls[0]['data'], 'STATE 123')
def test_ping(self): engine = mocks.MockATEngine() controller = app.controller.LightController(engine) want = ['PING RESPONSE1', 'PING RESPONSE2'] engine.results.append(want) got = controller.ping('PING REQUEST') self.assertEqual(got, ','.join(want)) self.assertEqual(len(engine.calls), 1) self.assertEqual(engine.calls[0]['cmd'], 'PING') self.assertEqual(engine.calls[0]['data'], 'PING REQUEST')
def test_set_pin(self): engine = mocks.MockATEngine() controller = app.controller.LightController(engine) want_pin = random.randint(10, 50) want_state = random.randint(50, 100) controller.set_pin(want_pin, want_state) self.assertEqual(len(engine.calls), 1) self.assertEqual(engine.calls[0]['cmd'], 'PIN') self.assertEqual(engine.calls[0]['data'], "%d,%d" % (want_pin, want_state))
def test_get_pin_bad_state(self): engine = mocks.MockATEngine() controller = app.controller.LightController(engine) engine.results.append(['bad1', 'bad2']) err = None try: controller.get_pin(123) except at.ATException as e: err = e self.assertIsNotNone(err) self.assertEqual(err.args[0], 'Unexpected response')
def test_get_pin(self): engine = mocks.MockATEngine() controller = app.controller.LightController(engine) want_pin = random.randint(10, 50) want_state = random.randint(50, 100) engine.results.append([str(want_state)]) got_state = controller.get_pin(want_pin) self.assertEqual(len(engine.calls), 1) self.assertEqual(engine.calls[0]['cmd'], 'PIN?') self.assertEqual(engine.calls[0]['data'], str(want_pin)) self.assertEqual(got_state, want_state)
def test_handle(self): state = 'NEXT-STATE-123' payload = '{ "state": "%s" }' % state engine = mocks.MockATEngine() controller = app.controller.LightController(engine) handler = app.routes.create_led_handler(controller) req = mocks.MockReq(payload=payload) w = mocks.MockWriter() handler(w, req) self.assertEqual(engine.calls[0]['cmd'], 'LED') self.assertEqual(engine.calls[0]['data'], state) self.assertEqual(w.written_status, uhttp.HTTP_STATUS_NO_CONTENT)
def test_handle(self): engine = mocks.MockATEngine() controller = app.controller.LightController(engine) handler = app.routes.create_ping_handler(controller) req = mocks.MockReq() w = mocks.MockWriter() engine.results.append(['PONG']) handler(w, req) self.assertEqual(engine.calls[0]['cmd'], 'PING') self.assertEqual(engine.calls[0]['data'], 'PONG') self.assertEqual(w.written_status, uhttp.HTTP_STATUS_OK) self.assertEqual( w.written_body, ujson.dumps({ 'service': 'PONG', 'controller': 'PONG' }))
def test_handle(self): pinNumber = random.randint(10, 1000) wantState = random.randint(10, 1000) uri = '/v1/light/pins/%d' % pinNumber engine = mocks.MockATEngine() engine.results.append([str(wantState)]) controller = app.controller.LightController(engine) handler = app.routes.create_get_pin_handler(controller) req = mocks.MockReq(uri=uri) w = mocks.MockWriter() re = ure.compile(r'.+\/([0-9]+)') handler(w, req, re.match(uri)) self.assertEqual(engine.calls[0]['cmd'], 'PIN?') self.assertEqual(engine.calls[0]['data'], str(pinNumber)) self.assertEqual(w.written_status, uhttp.HTTP_STATUS_OK) self.assertEqual(ujson.loads(w.written_body), {"state": wantState})
def test_handle(self): pinNumber = random.randint(10, 1000) wantState = random.randint(10, 1000) uri = '/v1/light/pins/%d' % pinNumber payload = '{ "state": %d }' % wantState engine = mocks.MockATEngine() controller = app.controller.LightController(engine) handler = app.routes.create_set_pin_handler(controller) req = mocks.MockReq(uri=uri, payload=payload) w = mocks.MockWriter() re = ure.compile(r'.+\/([0-9]+)') handler(w, req, re.match(uri)) self.assertEqual(engine.calls[0]['cmd'], 'PIN') self.assertEqual(engine.calls[0]['data'], "%d,%d" % (pinNumber, wantState)) self.assertEqual(w.written_status, uhttp.HTTP_STATUS_NO_CONTENT)