def test_getClientPenalty(self): tmp = Penalty( type='Kick', clientId=54, inactive=1, adminId=4, reason='respect the rules', keyword='rules', data='foo', timeAdd=123, timeEdit=1234, timeExpire=654, duration=87, ) tmp_id = self.storage.setClientPenalty(tmp) penalty = self.storage.getClientPenalty(Penalty(type="Kick", id=tmp_id)) self.assertIsInstance(penalty, Penalty) self.assertEqual(penalty.type, 'Kick') self.assertEqual(penalty.clientId, 54) self.assertEqual(penalty.inactive, 1) self.assertEqual(penalty.adminId, 4) self.assertEqual(penalty.reason, 'respect the rules') self.assertEqual(penalty.keyword, 'rules') self.assertEqual(penalty.data, 'foo') self.assertEqual(penalty.timeAdd, 123) self.assertEqual(penalty.timeEdit, 1234) self.assertEqual(penalty.timeExpire, 654) self.assertEqual(penalty.duration, 87) self.assertRaises(KeyError, self.storage.getClientPenalty, Penalty(id=654654))
def test_setClientPenalty_bad_keyword(self): id1 = self.storage.setClientPenalty(Penalty(keyword='!=+', clientId=1, adminId=0)) self.assertIsNotNone(id1) p1 = self.storage.getClientPenalty(Penalty(id=id1, type='foo')) self.assertIsInstance(p1, Penalty) self.assertIs(type(p1), Penalty) self.assertEquals('', p1.keyword)
def test_setClientPenalty(self): id1 = self.storage.setClientPenalty(Penalty(type='Ban', clientId=1, adminId=0)) self.assertIsNotNone(id1) id2 = self.storage.setClientPenalty(Penalty(type='Kick', clientId=1, adminId=0)) self.assertEqual(id1 + 1, id2) id3 = self.storage.setClientPenalty(Penalty(id=id1, type='Warning', clientId=1, adminId=0)) self.assertEqual(id1, id3)
def test_getLastPenalties(self): c1 = Mock() c1.id = 15 c2 = Mock() c2.id = 18 Penalty(clientId=c1.id, adminId=0, inactive=1, type='Ban', timeExpire=-1, data=u'pA').save(self.console) Penalty(clientId=c1.id, adminId=0, inactive=0, type='Ban', timeExpire=self.console.time()+10, data=u'pB').save(self.console) Penalty(clientId=c1.id, adminId=0, inactive=0, type='Warning', timeExpire=self.console.time()+10, data=u'pC').save(self.console) Penalty(clientId=c1.id, adminId=0, inactive=0, type='Kick', timeExpire=self.console.time()-10, data=u'pD').save(self.console) Penalty(clientId=c1.id, adminId=0, inactive=0, type='Ban', timeExpire=self.console.time()-10, data=u'pE').save(self.console) Penalty(clientId=c2.id, adminId=0, inactive=0, type='Warning', timeExpire=-1, data=u'pF').save(self.console) Penalty(clientId=c2.id, adminId=0, inactive=0, type='TempBan', timeExpire=-1, data=u'pG').save(self.console) Penalty(clientId=c2.id, adminId=0, inactive=0, type='Ban', timeExpire=-1, data=u'pH').save(self.console) def getLastPenalties(types, num): p_datas = [] for p in self.storage.getLastPenalties(types=types, num=num): p_datas.append(p.data) self.assertTrue(p.inactive == 0) self.assertTrue(p.timeExpire == -1 or p.timeExpire > self.console.time()) self.assertGreaterEqual(num, len(p_datas)) return p_datas self.assertListEqual([u'pH', u'pG', u'pF', u'pC', u'pB'], getLastPenalties(types=('Ban', 'TempBan', 'Kick', 'Warning', 'Notice'), num=5)) self.assertListEqual([u'pH', u'pG', u'pF', u'pC'], getLastPenalties(types=('Ban', 'TempBan', 'Kick', 'Warning', 'Notice'), num=4)) self.assertListEqual([u'pH', u'pG', u'pB'], getLastPenalties(types=('Ban', 'TempBan'), num=5))
def test_truncateTables_multiple_tables(self): c1 = Client(guid="aaaaaaaaa") c2 = Client(guid="bbbbbbbbb") self.storage.setClient(c1) self.storage.setClient(c2) self.storage.setClientPenalty(Penalty(clientId=c1.id, adminId=0, type='Kick')) self.storage.setClientPenalty(Penalty(clientId=c2.id, adminId=0, type='Kick')) cursor = self.storage.query("SELECT * FROM clients") self.assertFalse(cursor.EOF) cursor = self.storage.query("SELECT * FROM penalties") self.assertFalse(cursor.EOF) self.storage.truncateTable(['clients', 'penalties']) cursor = self.storage.query("SELECT * FROM clients") self.assertTrue(cursor.EOF) cursor = self.storage.query("SELECT * FROM penalties") self.assertTrue(cursor.EOF)
def test_getCounts(self): c1 = Client(guid="aaaaaaaaa") self.assertEqual({'Kicks': 0, 'TempBans': 0, 'clients': 0, 'Bans': 0, 'Warnings': 0}, self.storage.getCounts()) self.storage.setClient(c1) self.assertEqual({'Kicks': 0, 'TempBans': 0, 'clients': 1, 'Bans': 0, 'Warnings': 0}, self.storage.getCounts()) self.storage.setClient(Client(guid="bbbbbbbbbb")) self.assertEqual({'Kicks': 0, 'TempBans': 0, 'clients': 2, 'Bans': 0, 'Warnings': 0}, self.storage.getCounts()) Penalty(clientId=c1.id, adminId=0, type='Ban').save(self.console) self.assertEqual({'Kicks': 0, 'TempBans': 0, 'clients': 2, 'Bans': 1, 'Warnings': 0}, self.storage.getCounts()) Penalty(clientId=c1.id, adminId=0, type='Ban').save(self.console) self.assertEqual({'Kicks': 0, 'TempBans': 0, 'clients': 2, 'Bans': 2, 'Warnings': 0}, self.storage.getCounts()) Penalty(clientId=c1.id, adminId=0, type='TempBan').save(self.console) self.assertEqual({'Kicks': 0, 'TempBans': 1, 'clients': 2, 'Bans': 2, 'Warnings': 0}, self.storage.getCounts()) Penalty(clientId=c1.id, adminId=0, type='Kick').save(self.console) self.assertEqual({'Kicks': 1, 'TempBans': 1, 'clients': 2, 'Bans': 2, 'Warnings': 0}, self.storage.getCounts()) Penalty(clientId=c1.id, adminId=0, type='Warning').save(self.console) self.assertEqual({'Kicks': 1, 'TempBans': 1, 'clients': 2, 'Bans': 2, 'Warnings': 1}, self.storage.getCounts())
def test_getClientPenalties(self): c1 = Mock() c1.id = 15 c2 = Mock() c2.id = 18 Penalty(clientId=c1.id, adminId=0, inactive=1, type='Ban', timeExpire=-1, data='pA').save(self.console) Penalty(clientId=c1.id, adminId=0, inactive=0, type='Ban', timeExpire=self.console.time()+10, data='pB').save(self.console) Penalty(clientId=c1.id, adminId=0, inactive=0, type='Warning', timeExpire=self.console.time()+10, data='pC').save(self.console) Penalty(clientId=c1.id, adminId=0, inactive=0, type='Kick', timeExpire=self.console.time()-10, data='pD').save(self.console) Penalty(clientId=c1.id, adminId=0, inactive=0, type='Ban', timeExpire=self.console.time()-10, data='pE').save(self.console) Penalty(clientId=c2.id, adminId=0, inactive=0, type='Warning', timeExpire=-1, data='pF').save(self.console) Penalty(clientId=c2.id, adminId=0, inactive=0, type='TempBan', timeExpire=-1, data='pG').save(self.console) def assertPenalties(client, types, penalties_in=[], penalties_notin=[]): penalties = self.storage.getClientPenalties(client=client, type=types) self.assertIsInstance(penalties, list) bucket = [] for i in penalties: self.assertIsInstance(i, Penalty) self.assertEqual(i.clientId, client.id) bucket.append(i.data) for i in penalties_in: self.assertIn(i, bucket) for i in penalties_notin: self.assertNotIn(i, bucket) assertPenalties(client=c1, types=('Ban', 'TempBan', 'Kick', 'Warning', 'Notice'), penalties_in=('pB','pC'), penalties_notin=('pA','pD','pE','pF','pG')) assertPenalties(client=c2, types=('Ban', 'TempBan', 'Kick', 'Warning', 'Notice'), penalties_in=('pF','pG'), penalties_notin=('pA','pB','pC','pD','pE'))
def test_getClientFirstPenalty(self): client = Mock() client.id = 15 self.storage.setClientPenalty( Penalty(clientId=client.id, adminId=0, timeAdd=2, timeExpire=-1, type='Ban', data='pA')) self.storage.setClientPenalty( Penalty(clientId=client.id, adminId=0, timeAdd=4, timeExpire=-1, type='Kick', data='pB')) self.storage.setClientPenalty( Penalty(clientId=client.id, adminId=0, timeAdd=1, timeExpire=-1, type='Notice', data='pC')) self.storage.setClientPenalty( Penalty(clientId=client.id, adminId=0, timeAdd=3, timeExpire=-1, type='Ban', data='pD')) self.storage.setClientPenalty( Penalty(clientId=client.id, adminId=0, timeAdd=6, timeExpire=-1, type='Kick', data='pE')) self.storage.setClientPenalty( Penalty(clientId=client.id, adminId=0, timeAdd=5, timeExpire=-1, type='Ban', data='pF')) penalty = self.storage.getClientFirstPenalty(client=client) self.assertIsInstance(penalty, Penalty) self.assertEqual(penalty.data, 'pA') penalty = self.storage.getClientFirstPenalty(client=client, type=('Ban', 'Kick')) self.assertIsInstance(penalty, Penalty) self.assertEqual(penalty.data, 'pA') penalty = self.storage.getClientFirstPenalty(client=client, type=('Ban', 'Kick', 'Notice')) self.assertIsInstance(penalty, Penalty) self.assertEqual(penalty.data, 'pC') self.assertIsNone(self.storage.getClientFirstPenalty(Penalty(clientId=3231)))
def test_numPenalties(self): c1 = Mock() c1.id = 15 c2 = Mock() c2.id = 18 Penalty(clientId=c1.id, adminId=0, timeExpire=-1, type='Ban', inactive=1, data='pA').save(self.console) Penalty(clientId=c1.id, adminId=0, timeExpire=-1, type='Ban', inactive=0, data='pB').save(self.console) Penalty(clientId=c1.id, adminId=0, timeExpire=-1, type='Warning', inactive=0, data='pC').save(self.console) Penalty(clientId=c1.id, adminId=0, timeExpire=-1, type='Kick', inactive=0, data='pD').save(self.console) Penalty(clientId=c2.id, adminId=0, timeExpire=-1, type='Notice', inactive=0, data='pE').save(self.console) Penalty(clientId=c2.id, adminId=0, timeExpire=-1, type='Warning', inactive=0, data='pF').save(self.console) Penalty(clientId=c1.id, adminId=0, timeExpire=-1, type='TempBan', inactive=0, data='pG').save(self.console) self.assertEqual(1, self.storage.numPenalties(client=c1)) self.assertEqual( 4, self.storage.numPenalties(client=c1, type=('Ban', 'TempBan', 'Kick', 'Warning', 'Notice'))) self.assertEqual(0, self.storage.numPenalties(client=c2)) self.assertEqual( 2, self.storage.numPenalties(client=c2, type=('Ban', 'TempBan', 'Kick', 'Warning', 'Notice')))
def _createPenaltyFromRow(row): """ Create a Penalty object given a result set row. :param row: The result set row """ constructors = { 'Warning': ClientWarning, 'TempBan': ClientTempBan, 'Kick': ClientKick, 'Ban': ClientBan, 'Notice': ClientNotice } try: constructor = constructors[row['type']] penalty = constructor() except KeyError: penalty = Penalty() penalty.id = int(row['id']) penalty.type = row['type'] penalty.keyword = row['keyword'] penalty.reason = row['reason'] penalty.data = row['data'] penalty.inactive = int(row['inactive']) penalty.timeAdd = int(row['time_add']) penalty.timeEdit = int(row['time_edit']) penalty.timeExpire = int(row['time_expire']) penalty.clientId = int(row['client_id']) penalty.adminId = int(row['admin_id']) penalty.duration = int(row['duration']) return penalty