-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a script to use multiple Spond
get_
functions, as a simple end-…
…to-end manual assurance test.
- Loading branch information
1 parent
ece9410
commit 9578428
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"""Use Spond 'get' functions to summarise available data. | ||
Intended as a simple end-to-end test for assurance when making changes. | ||
Uses all existing group, event, message `get_` methods. | ||
Doesn't yet use `get_person(id)` or any `send_`, `update_` methods.""" | ||
|
||
import asyncio | ||
import random | ||
|
||
from config import password, username | ||
from spond import spond | ||
|
||
|
||
async def main() -> None: | ||
s = spond.Spond(username=username, password=password) | ||
|
||
print("Getting all groups...") | ||
groups = await s.get_groups() | ||
print(f"{len(groups)} groups:") | ||
for i, group in enumerate(groups): | ||
print(f"[{i}] {_group_summary(group)}") | ||
|
||
print("Getting a random group by id...") | ||
random_group_id = random.choice(groups)["id"] | ||
group = await s.get_group(random_group_id) | ||
print(f"{_group_summary(group)}") | ||
|
||
print("\nGetting up to 10 events...") | ||
events = await s.get_events(max_events=10) | ||
print(f"{len(events)} events:") | ||
for i, event in enumerate(events): | ||
print(f"[{i}] {_event_summary(event)}") | ||
|
||
print("Getting a random event by id...") | ||
random_event_id = random.choice(events)["id"] | ||
event = await s.get_event(random_event_id) | ||
print(f"{_event_summary(event)}") | ||
|
||
print("\nGetting up to 10 messages...") | ||
messages = await s.get_messages() | ||
print(f"{len(messages)} messages:") | ||
for i, message in enumerate(messages): | ||
print(f"[{i}] {_message_summary(message)}") | ||
|
||
# No `get_message(id)` function | ||
|
||
await s.clientsession.close() | ||
|
||
|
||
def _group_summary(group) -> str: | ||
return f"id: {group['id']}, " f"name: {group['name']}" | ||
|
||
|
||
def _event_summary(event) -> str: | ||
return ( | ||
f"id: {event['id']}, " | ||
f"name: {event['heading']}, " | ||
f"startTimestamp: {event['startTimestamp']}" | ||
) | ||
|
||
|
||
def _message_summary(message) -> str: | ||
return ( | ||
f"id: {message['id']}, " | ||
f"timestamp: {message['message']['timestamp']}, " | ||
f"text: {_abbreviate(message['message']['text'], length=64)}, " | ||
) | ||
|
||
|
||
def _abbreviate(text, length) -> str: | ||
"""Abbreviate long text, normalising line endings to escape characters.""" | ||
escaped_text = repr(text) | ||
if len(text) > length: | ||
return f"{escaped_text[0:length]}[…]" | ||
return f"{escaped_text}" | ||
|
||
|
||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
asyncio.run(main()) |