Skip to content

Commit 9bf4323

Browse files
committed
Add a full test coverage test suite
1 parent d8e0c64 commit 9bf4323

File tree

4 files changed

+351
-24
lines changed

4 files changed

+351
-24
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ connectors:
1515
databases:
1616
matrix:
1717
default_room: 'main'
18-
state_key: 'opsdroid.database'
18+
single_state_key: 'opsdroid.database'
1919
```

matrix.py

+47-23
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,83 @@ def __init__(self, config, opsdroid=None):
1616
super().__init__(config, opsdroid=opsdroid)
1717
self.name = "matrix"
1818
self.room = config.get("default_room", "main")
19-
self._state_key = config.get("state_key","opsdroid.database")
20-
_LOGGER.debug('Loaded matrix database connector')
19+
self._event_type = "opsdroid.database"
20+
self._single_state_key = config.get("single_state_key", True)
21+
_LOGGER.debug("Loaded matrix database connector")
2122

2223
async def connect(self):
2324
"""Connect to the database."""
24-
# Currently can't actually get connectors when this runs, so just store opsdroid instead
25-
_LOGGER.info(self.opsdroid)
26-
_LOGGER.info("Plugged into the matrix")
25+
_LOGGER.info("Matrix Database connector initialised.")
2726

2827
async def put(self, key, value):
2928
"""Insert or replace an object into the database for a given key."""
30-
room = self.room or 'main'
31-
room_id = room if room[0] == '!' else self.connector.room_ids[room]
3229

33-
_LOGGER.debug(f"===== Putting {key} into matrix room {room_id}")
30+
# If the single state key flag is set then use that else use state key.
31+
state_key = "" if self._single_state_key is True else key
32+
33+
room = self.room or "main"
34+
room_id = room if room[0] == "!" else self.connector.room_ids[room]
35+
36+
_LOGGER.debug(f"Putting {key} into matrix room {room_id}.")
37+
38+
ori_data = await self.get_state_event(room_id, state_key)
39+
40+
if self._single_state_key:
41+
value = {key: value}
42+
43+
elif not isinstance(value, dict):
44+
raise ValueError("When single_state_key is False value must be a dict.")
45+
46+
data = {**ori_data, **value}
3447

35-
ori_data = await self.get_state_event(room_id, key)
36-
data = {key: value}
37-
data = {**ori_data, **data}
3848
if data == ori_data:
3949
_LOGGER.debug("Not updating matrix state, as content hasn't changed.")
4050
return
51+
4152
_LOGGER.debug(f"===== Putting {key} into matrix room {room_id} with {data}")
4253

43-
await self.opsdroid.send(MatrixStateEvent(key=self._state_key,
44-
content=data,
45-
target=room_id,
46-
connector=self.connector,
47-
state_key=key))
54+
await self.opsdroid.send(
55+
MatrixStateEvent(
56+
key=self._event_type,
57+
content=data,
58+
target=room_id,
59+
connector=self.connector,
60+
state_key=state_key,
61+
)
62+
)
4863

4964
async def get(self, key):
5065
"""Get a document from the database for a given key."""
51-
room = self.room or 'main'
52-
room_id = room if room.startswith('!') else self.connector.room_ids[room]
66+
room = self.room or "main"
67+
room_id = room if room.startswith("!") else self.connector.room_ids[room]
5368

5469
_LOGGER.debug(f"Getting {key} from matrix room {room_id}")
5570

5671
try:
5772
data = await self.get_state_event(room_id, key)
58-
data = data.get(key)
59-
except MatrixRequestError:
73+
except MatrixRequestError as e:
74+
_LOGGER.info(f"Failed to get state event with state_key={key}: {e}")
6075
data = None
6176

77+
if not data:
78+
return
79+
80+
if self._single_state_key:
81+
data = data.get(key)
82+
6283
return data
6384

6485
@property
6586
def connector(self):
66-
return self.opsdroid._connector_names['matrix']
87+
return self.opsdroid._connector_names["matrix"]
6788

6889
async def get_state_event(self, room_id, key):
90+
91+
url = f"/rooms/{room_id}/state/{self._event_type}"
92+
if not self._single_state_key:
93+
url += f"/{key}"
6994
try:
70-
return await self.connector.connection._send("GET",
71-
quote(f"/rooms/{room_id}/state/{self._state_key}/{key}"))
95+
return await self.connector.connection._send("GET", quote(url))
7296
except MatrixRequestError as e:
7397
if e.code != 404:
7498
raise e

setup.cfg

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[tool:pytest]
2+
filterwarnings =
3+
error
4+
ignore: "@coroutine" decorator is deprecated
5+
ignore: Using or importing the ABCs from 'collections' instead

0 commit comments

Comments
 (0)