mirror of
https://github.com/matrix-org/pantalaimon.git
synced 2024-10-01 03:35:38 -04:00
154 lines
4.9 KiB
Python
154 lines
4.9 KiB
Python
import pdb
|
|
|
|
from nio import RoomMessage
|
|
|
|
from conftest import faker
|
|
from pantalaimon.index import Index
|
|
from pantalaimon.store import FetchTask
|
|
|
|
TEST_ROOM = "!SVkFJHzfwvuaIEawgC:localhost"
|
|
TEST_ROOM2 = "!testroom:localhost"
|
|
|
|
|
|
class TestClass(object):
|
|
@property
|
|
def test_event(self):
|
|
return RoomMessage.parse_event(
|
|
{
|
|
"content": {"body": "Test message", "msgtype": "m.text"},
|
|
"event_id": "$15163622445EBvZJ:localhost",
|
|
"origin_server_ts": 1516362244026,
|
|
"room_id": "!SVkFJHzfwvuaIEawgC:localhost",
|
|
"sender": "@example2:localhost",
|
|
"type": "m.room.message",
|
|
"unsigned": {"age": 43289803095},
|
|
"user_id": "@example2:localhost",
|
|
"age": 43289803095
|
|
}
|
|
)
|
|
|
|
@property
|
|
def another_event(self):
|
|
return RoomMessage.parse_event(
|
|
{
|
|
"content": {"body": "Another message", "msgtype": "m.text"},
|
|
"event_id": "$15163622445EBvZJ:localhost",
|
|
"origin_server_ts": 1516362244026,
|
|
"room_id": "!SVkFJHzfwvuaIEawgC:localhost",
|
|
"sender": "@example2:localhost",
|
|
"type": "m.room.message",
|
|
"unsigned": {"age": 43289803095},
|
|
"user_id": "@example2:localhost",
|
|
"age": 43289803095
|
|
}
|
|
)
|
|
|
|
def test_account_loading(self, panstore):
|
|
accounts = panstore.load_all_users()
|
|
# pdb.set_trace()
|
|
assert len(accounts) == 10
|
|
|
|
def test_token_saving(self, panstore, access_token):
|
|
accounts = panstore.load_all_users()
|
|
user_id = accounts[0][0]
|
|
device_id = accounts[0][1]
|
|
|
|
panstore.save_access_token(user_id, device_id, access_token)
|
|
|
|
token = panstore.load_access_token(user_id, device_id)
|
|
access_token == token
|
|
|
|
def test_event_storing(self, panstore_with_users):
|
|
panstore = panstore_with_users
|
|
accounts = panstore.load_all_users()
|
|
user, _ = accounts[0]
|
|
|
|
event = self.test_event
|
|
|
|
event_id = panstore.save_event("example", user, event, TEST_ROOM,
|
|
"Example2", None)
|
|
|
|
assert event_id == 1
|
|
|
|
event_id = panstore.save_event("example", user, event, TEST_ROOM,
|
|
"Example2", None)
|
|
assert event_id is None
|
|
|
|
event_dict = panstore.load_event_by_columns("example", user, 1)
|
|
assert event.source == event_dict["result"]
|
|
|
|
event_source = panstore.load_event_by_columns("example", user, 1, True)
|
|
|
|
assert event_source["context"]["profile_info"] == {
|
|
"@example2:localhost": {
|
|
"display_name": "Example2",
|
|
"avatar_url": None
|
|
}
|
|
}
|
|
|
|
def test_index(self, panstore_with_users):
|
|
panstore = panstore_with_users
|
|
accounts = panstore.load_all_users()
|
|
user, _ = accounts[0]
|
|
|
|
event = self.test_event
|
|
another_event = self.another_event
|
|
|
|
index = Index(panstore.store_path)
|
|
|
|
event_id = panstore.save_event("example", user, event, TEST_ROOM,
|
|
"Example2", None)
|
|
assert event_id == 1
|
|
index.add_event(event_id, event, TEST_ROOM)
|
|
|
|
event_id = panstore.save_event("example", user, another_event,
|
|
TEST_ROOM2, "Example2", None)
|
|
assert event_id == 2
|
|
index.add_event(event_id, another_event, TEST_ROOM2)
|
|
|
|
index.commit()
|
|
|
|
searcher = index.searcher()
|
|
|
|
searched_events = searcher.search("message", TEST_ROOM)
|
|
|
|
_, found_id = searched_events[0]
|
|
|
|
event_dict = panstore.load_event_by_columns("example", user, found_id)
|
|
|
|
assert event_dict["result"] == event.source
|
|
|
|
def test_token_storing(self, panstore_with_users):
|
|
panstore = panstore_with_users
|
|
accounts = panstore.load_all_users()
|
|
user, _ = accounts[0]
|
|
|
|
assert not panstore.load_token("example", user)
|
|
panstore.save_token("example", user, "abc123")
|
|
|
|
assert panstore.load_token("example", user) == "abc123"
|
|
|
|
def test_fetcher_tasks(self, panstore_with_users):
|
|
panstore = panstore_with_users
|
|
accounts = panstore.load_all_users()
|
|
user, _ = accounts[0]
|
|
|
|
task = FetchTask(TEST_ROOM, "abc1234")
|
|
task2 = FetchTask(TEST_ROOM2, "abc1234")
|
|
|
|
assert not panstore.load_fetcher_tasks("example", user)
|
|
|
|
panstore.save_fetcher_task("example", user, task)
|
|
panstore.save_fetcher_task("example", user, task2)
|
|
|
|
tasks = panstore.load_fetcher_tasks("example", user)
|
|
|
|
assert task in tasks
|
|
assert task2 in tasks
|
|
|
|
panstore.delete_fetcher_task("example", user, task)
|
|
tasks = panstore.load_fetcher_tasks("example", user)
|
|
|
|
assert task not in tasks
|
|
assert task2 in tasks
|