-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_alchemydb.py
executable file
·137 lines (114 loc) · 4.15 KB
/
test_alchemydb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Wessex - A log-reading bot for mordhau.
# Copyright (C) 2021 Morgan Chesser mfreelancedef@gmail.com
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import alchemydb as adb
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import selectinload
import pytest
import os
import logparse as lp
import asyncio
DB_URL = 'postgresql+asyncpg://postgres:HereInMyGarage@localhost:5432/wessextest'
@pytest.fixture()
def db():
return adb.DB(db_url=DB_URL)
@pytest.fixture(autouse=True)
async def setup():
db = adb.DB(db_url=DB_URL)
await db.reset()
@pytest.fixture()
def alice_user():
alice_user = adb.User(playfab_id='386B10B5F82A5456',
discord_id='146559291745107968',
nickname='Alice',
is_admin=True)
return alice_user
@pytest.mark.asyncio
async def test_alice(db, alice_user):
async with db.Session() as session:
#async with session.begin():
session.add(alice_user)
res = await session.execute(
sa.select(adb.User)
.options(selectinload(adb.User.infractions))
.options(selectinload(adb.User.actions))
.filter_by(nickname='Alice'))
our_user = res.scalars().first()
assert our_user is not None
assert our_user is alice_user
assert our_user.actions == []
assert our_user.infractions == []
await session.rollback()
@pytest.mark.asyncio
async def test_persistence(db, alice_user):
async with db.Session() as session:
session.add(alice_user)
await session.commit()
async with db.Session() as session:
res = await session.execute(
sa.select(adb.User)
.options(selectinload(adb.User.infractions))
.options(selectinload(adb.User.actions))
.filter_by(nickname='Alice'))
our_user = res.scalars().first()
assert our_user is not None
await session.delete(our_user)
await session.commit()
async with db.Session() as session:
res = await session.execute(
sa.select(adb.User)
.options(selectinload(adb.User.infractions))
.options(selectinload(adb.User.actions))
.filter_by(nickname='Alice'))
our_user = res.scalars().first()
assert our_user is None
@pytest.mark.asyncio
async def test_adminactions(db, alice_user):
async with db.Session() as session:
session.add(alice_user)
reza_user = adb.User(playfab_id='442291D7DA91C013',
discord_id='583448737314242570',
nickname='Reza',
is_admin=True)
session.add(reza_user)
reza_user.infractions = [adb.AdminAction(admin = alice_user,
action_type='Ban')]
assert alice_user.actions[0].target == reza_user
alice_user.actions.append(adb.AdminAction(target = reza_user,
action_type='Mute'))
assert reza_user.infractions[1].action_type == 'Mute'
@pytest.mark.asyncio
async def test_get_discordid_from_playfabid(db, alice_user):
db = adb.DB(db_url=DB_URL)
async with db.Session() as session:
session.add(alice_user)
await session.commit()
discord_id = await db.get_discordid_from_playfabid(alice_user.playfab_id)
assert discord_id == alice_user.discord_id
@pytest.mark.asyncio
async def test_logparse(db):
with open('test/test_ban.log', 'rb') as fp:
data = fp.read()
full_log = data.decode()
#async def msg_handler(logmsg, msgtype, data):
#assert msgtype == 'ban'
async with db.Session() as session:
async def handle_msg(logmsg, msgtype, data):
await db.handle_msg(logmsg, msgtype, data, session)
await lp.parse_lines_n(full_log, handle_msg)
await lp.parse_lines_n(full_log, handle_msg)
#print(session.dirty)
await session.commit()
#assert False
def test_handle_msg(db):
pass