|
| 1 | +""" |
| 2 | +CircuitPython_AdafruitIO IO_HTTP Tester |
| 3 | +-------------------------------------------------- |
| 4 | +
|
| 5 | +Tests Adafruit IO CircuitPython HTTP method |
| 6 | +coverage with a WiFi CircuitPython device. |
| 7 | +
|
| 8 | +* Author(s): Brent Rubell for Adafruit Industries |
| 9 | +""" |
| 10 | +from random import randint, uniform |
| 11 | +import time |
| 12 | +import board |
| 13 | +import busio |
| 14 | +from digitalio import DigitalInOut |
| 15 | +from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager |
| 16 | +import neopixel |
| 17 | +from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError |
| 18 | + |
| 19 | +# REQUIRES MicroPython's UnitTest |
| 20 | +# https://github.com/micropython/micropython-lib/tree/master/unittest |
| 21 | +import unittest |
| 22 | + |
| 23 | +# Get wifi details and more from a secrets.py file |
| 24 | +try: |
| 25 | + from secrets import secrets |
| 26 | +except ImportError: |
| 27 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 28 | + raise |
| 29 | + |
| 30 | +# ESP32 Setup |
| 31 | +esp32_cs = DigitalInOut(board.ESP_CS) |
| 32 | +esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 33 | +esp32_reset = DigitalInOut(board.ESP_RESET) |
| 34 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 35 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 36 | +status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) |
| 37 | +wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
| 38 | + |
| 39 | +# Set your Adafruit IO Username and Key in secrets.py |
| 40 | +# (visit io.adafruit.com if you need to create an account, |
| 41 | +# or if you need your Adafruit IO key.) |
| 42 | +aio_username = secrets["aio_user"] |
| 43 | +aio_key = secrets["aio_password"] |
| 44 | + |
| 45 | + |
| 46 | +class Test_IO_HTTP(unittest.TestCase): |
| 47 | + |
| 48 | + # Tests for Adafruit IO Authentication |
| 49 | + def test_set_user_key(self): |
| 50 | + """__init__ constructor |
| 51 | + correctly exposes provided credentials. |
| 52 | + """ |
| 53 | + username = "adabot" |
| 54 | + key = "mho" |
| 55 | + io = IO_HTTP(username, key, wifi) |
| 56 | + self.assertEqual(username, io.username) |
| 57 | + self.assertEqual(key, io.key) |
| 58 | + |
| 59 | + def test_incorrect_user_pass_action(self): |
| 60 | + """Incorrect credentials provided to __init__ |
| 61 | + should raise a RequestError. |
| 62 | + """ |
| 63 | + username = "adabot" |
| 64 | + key = "mho" |
| 65 | + io = IO_HTTP(username, key, wifi) |
| 66 | + with self.assertRaises(AdafruitIO_RequestError): |
| 67 | + test_feed = io.get_feed("errorfeed") |
| 68 | + pass |
| 69 | + |
| 70 | + # Tests for Adafruit IO Data Methods |
| 71 | + def test_txrx(self): |
| 72 | + """Sends a random integer value to a feed and receives it back. |
| 73 | + """ |
| 74 | + # Create an Adafruit IO HTTP Client |
| 75 | + io = IO_HTTP(aio_username, aio_key, wifi) |
| 76 | + try: |
| 77 | + test_feed = io.get_feed("testfeed") |
| 78 | + except AdafruitIO_RequestError: |
| 79 | + test_feed = io.create_new_feed("testfeed") |
| 80 | + tx_data = randint(1, 100) |
| 81 | + # send the value |
| 82 | + io.send_data(test_feed["key"], tx_data) |
| 83 | + # and get it back... |
| 84 | + rx_data = io.receive_data(test_feed["key"]) |
| 85 | + self.assertEqual(int(rx_data["value"]), tx_data) |
| 86 | + |
| 87 | + def test_send_location_data(self): |
| 88 | + """Sets location metadata. |
| 89 | + send_data |
| 90 | + """ |
| 91 | + # Create an Adafruit IO HTTP Client |
| 92 | + io = IO_HTTP(aio_username, aio_key, wifi) |
| 93 | + io.delete_feed('testfeed') |
| 94 | + test_feed = io.create_new_feed('testfeed') |
| 95 | + # value |
| 96 | + value = randint(1, 100) |
| 97 | + # Set up metadata associated with value |
| 98 | + metadata = {'lat': uniform(1, 100), |
| 99 | + 'lon': uniform(1, 100), |
| 100 | + 'ele': 10, |
| 101 | + 'created_at': None} |
| 102 | + io.send_data(test_feed['key'], value, metadata) |
| 103 | + rx_data = io.receive_data(test_feed['key']) |
| 104 | + self.assertEqual(int(rx_data['value']), value) |
| 105 | + self.assertAlmostEqual(float(rx_data['lat']), metadata['lat']) |
| 106 | + self.assertAlmostEqual(float(rx_data['lon']), metadata['lon']) |
| 107 | + self.assertAlmostEqual(float(rx_data['ele']), metadata['ele']) |
| 108 | + |
| 109 | + # Test for Adafruit IO Feed Methods |
| 110 | + def test_create_feed(self): |
| 111 | + """Test creating a new feed. |
| 112 | + """ |
| 113 | + # Create an Adafruit IO HTTP Client |
| 114 | + io = IO_HTTP(aio_username, aio_key, wifi) |
| 115 | + io.delete_feed('testfeed') |
| 116 | + test_feed = io.create_new_feed('testfeed') |
| 117 | + self.assertEqual(test_feed['name'], 'testfeed') |
| 118 | + |
| 119 | + def test_delete_feed(self): |
| 120 | + """delete_feed by feed key |
| 121 | + """ |
| 122 | + # Create an Adafruit IO HTTP Client |
| 123 | + io = IO_HTTP(aio_username, aio_key, wifi) |
| 124 | + io.delete_feed('testfeed') |
| 125 | + with self.assertRaises(AdafruitIO_RequestError): |
| 126 | + io.receive_data('testfeed'['key']) |
| 127 | + pass |
| 128 | + |
| 129 | + def test_delete_nonexistent_feed(self): |
| 130 | + """delete nonexistent feed by feed key |
| 131 | + """ |
| 132 | + # Create an Adafruit IO HTTP Client |
| 133 | + io = IO_HTTP(aio_username, aio_key, wifi) |
| 134 | + io.delete_feed('testfeed') |
| 135 | + with self.assertRaises(AdafruitIO_RequestError): |
| 136 | + io.delete_feed['testfeed'] |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + # Pass the NetworkManager Object to UnitTest.py |
| 141 | + unittest.get_wifi(wifi) |
| 142 | + unittest.main() |
0 commit comments