|
3 | 3 | import time |
4 | 4 | import unittest |
5 | 5 |
|
6 | | -from Adafruit_IO import Client, Data, Feed, Group, RequestError |
| 6 | +from Adafruit_IO import Client, Data, Feed, Group, Dashboard, Block, Layout, RequestError |
7 | 7 |
|
8 | 8 | import base |
9 | 9 |
|
@@ -46,6 +46,22 @@ def ensure_group_deleted(self, client, group): |
46 | 46 | # Swallow the error if the group doesn't exist. |
47 | 47 | pass |
48 | 48 |
|
| 49 | + def ensure_dashboard_deleted(self, client, dashboard): |
| 50 | + # Delete the specified dashboard if it exists. |
| 51 | + try: |
| 52 | + client.delete_dashboard(dashboard) |
| 53 | + except RequestError: |
| 54 | + # Swallow the error if the dashboard doesn't exist. |
| 55 | + pass |
| 56 | + |
| 57 | + def ensure_block_deleted(self, client, dashboard, block): |
| 58 | + # Delete the specified block if it exists. |
| 59 | + try: |
| 60 | + client.delete_block(dashboard, block) |
| 61 | + except RequestError: |
| 62 | + # Swallow the error if the block doesn't exist. |
| 63 | + pass |
| 64 | + |
49 | 65 | def empty_feed(self, client, feed): |
50 | 66 | # Remove all the data from a specified feed (but don't delete the feed). |
51 | 67 | data = client.data(feed) |
@@ -269,3 +285,90 @@ def test_receive_group_by_key(self): |
269 | 285 | group = io.create_group(Group(name='grouprx')) |
270 | 286 | response = io.groups(group.key) |
271 | 287 | self.assertEqual(response.key, 'grouprx') |
| 288 | + |
| 289 | + # Test Dashboard Functionality |
| 290 | + def test_dashboard_create_dashboard(self): |
| 291 | + io = self.get_client() |
| 292 | + self.ensure_dashboard_deleted(io, 'dashtest') |
| 293 | + response = io.create_dashboard(Dashboard(name='dashtest')) |
| 294 | + self.assertEqual(response.name, 'dashtest') |
| 295 | + |
| 296 | + def test_dashboard_returns_all_dashboards(self): |
| 297 | + io = self.get_client() |
| 298 | + self.ensure_dashboard_deleted(io, 'dashtest') |
| 299 | + dashboard = io.create_dashboard(Dashboard(name='dashtest')) |
| 300 | + response = io.dashboards() |
| 301 | + self.assertGreaterEqual(len(response), 1) |
| 302 | + |
| 303 | + def test_dashboard_returns_requested_feed(self): |
| 304 | + io = self.get_client() |
| 305 | + self.ensure_dashboard_deleted(io, 'dashtest') |
| 306 | + dashboard = io.create_dashboard(Dashboard(name='dashtest')) |
| 307 | + response = io.dashboards('dashtest') |
| 308 | + self.assertEqual(response.name, 'dashtest') |
| 309 | + |
| 310 | + # Test Block Functionality |
| 311 | + def test_block_create_block(self): |
| 312 | + io = self.get_client() |
| 313 | + self.ensure_block_deleted(io, 'dashtest', 'blocktest') |
| 314 | + self.ensure_dashboard_deleted(io, 'dashtest') |
| 315 | + dash = io.create_dashboard(Dashboard(name='dashtest')) |
| 316 | + block = io.create_block(dash.key, Block(name='blocktest', |
| 317 | + visual_type = 'line_chart')) |
| 318 | + self.assertEqual(block.name, 'blocktest') |
| 319 | + io.delete_block(dash.key, block.id) |
| 320 | + io.delete_dashboard(dash.key) |
| 321 | + |
| 322 | + def test_dashboard_returns_all_blocks(self): |
| 323 | + io = self.get_client() |
| 324 | + self.ensure_block_deleted(io, 'dashtest', 'blocktest') |
| 325 | + self.ensure_dashboard_deleted(io, 'dashtest') |
| 326 | + dash = io.create_dashboard(Dashboard(name='dashtest')) |
| 327 | + block = io.create_block(dash.key, Block(name='blocktest', |
| 328 | + visual_type = 'line_chart')) |
| 329 | + response = io.blocks(dash.key) |
| 330 | + self.assertEqual(len(response), 1) |
| 331 | + io.delete_block(dash.key, block.id) |
| 332 | + io.delete_dashboard(dash.key) |
| 333 | + |
| 334 | + def test_dashboard_returns_requested_block(self): |
| 335 | + io = self.get_client() |
| 336 | + self.ensure_block_deleted(io, 'dashtest', 'blocktest') |
| 337 | + self.ensure_dashboard_deleted(io, 'dashtest') |
| 338 | + dash = io.create_dashboard(Dashboard(name='dashtest')) |
| 339 | + block = io.create_block(dash.key, Block(name='blocktest', |
| 340 | + visual_type = 'line_chart')) |
| 341 | + response = io.blocks(dash.key, block.id) |
| 342 | + self.assertEqual(response.name, 'blocktest') |
| 343 | + io.delete_block(dash.key, block.id) |
| 344 | + io.delete_dashboard(dash.key) |
| 345 | + |
| 346 | + # Test Layout Functionality |
| 347 | + def test_layout_returns_all_layouts(self): |
| 348 | + io = self.get_client() |
| 349 | + self.ensure_block_deleted(io, 'dashtest', 'blocktest') |
| 350 | + self.ensure_dashboard_deleted(io, 'dashtest') |
| 351 | + dash = io.create_dashboard(Dashboard(name='dashtest')) |
| 352 | + block = io.create_block(dash.key, Block(name='blocktest', |
| 353 | + visual_type = 'line_chart')) |
| 354 | + response = io.layouts(dash.key) |
| 355 | + self.assertEqual(len(response), 5) # 5 layouts: xs, sm, md, lg, xl |
| 356 | + self.assertEqual(len(response.lg), 1) |
| 357 | + io.delete_block(dash.key, block.id) |
| 358 | + io.delete_dashboard(dash.key) |
| 359 | + |
| 360 | + def test_layout_update_layout(self): |
| 361 | + io = self.get_client() |
| 362 | + self.ensure_block_deleted(io, 'dashtest', 'blocktest') |
| 363 | + self.ensure_dashboard_deleted(io, 'dashtest') |
| 364 | + dash = io.create_dashboard(Dashboard(name='dashtest')) |
| 365 | + block = io.create_block(dash.key, Block(name='blocktest', |
| 366 | + visual_type = 'line_chart')) |
| 367 | + layout = Layout(lg = [ |
| 368 | + {'x': 0, 'y': 0, 'w': 16, 'h': 4, 'i': str(block.id)}]) |
| 369 | + io.update_layout(dash.key, layout) |
| 370 | + response = io.layouts(dash.key) |
| 371 | + self.assertEqual(len(response.lg), 1) |
| 372 | + self.assertEqual(response.lg[0]['w'], 16) |
| 373 | + io.delete_block(dash.key, block.id) |
| 374 | + io.delete_dashboard(dash.key) |
0 commit comments