from contextlib import closing import sqlite3 import tempfile import time import unittest from pathlib import Path import rankings from test_http_contract import namespace class BalanceBoardTests(unittest.TestCase): def test_multiple_keys_and_duplicate_names_do_not_repeat_one_owner(self): with tempfile.TemporaryDirectory() as directory: bot, gateway = Path(directory)/'bot.db', Path(directory)/'gateway.db' with closing(sqlite3.connect(bot)) as con, con: con.executescript('CREATE TABLE users(tg_id INTEGER,username TEXT);' 'CREATE TABLE econ_tx(src INTEGER,ts INTEGER,type TEXT);' "INSERT INTO users VALUES(42,'alice'),(43,NULL);") with closing(sqlite3.connect(gateway)) as con, con: con.execute('CREATE TABLE tokens(name TEXT,remain_quota INTEGER,status INTEGER,deleted_at INTEGER)') con.executemany('INSERT INTO tokens VALUES(?,?,?,?)', [ ('tg42',500_000,1,0), ('tg42',200_000,1,0), ('pm42-k1',1_000_000,4,None), ('sol42-k2',500_000,1,0), ('tg43',1_000_000,1,0), ('tg42evil',99_000_000,1,0), ('tg042',99_000_000,1,0), ('tg42-k0',99_000_000,1,0), ('tg42-k3',99_000_000,2,0), ('pm42-k4',99_000_000,1,100)]) def connect(path): con = sqlite3.connect(path) con.row_factory = sqlite3.Row return con env = namespace(['_boards_sync'], _con=connect, rankings=rankings, time=time, QUOTA_PER_USD=500_000) env.update(BOT_DB=str(bot), NEWAPI_DB=str(gateway)) result, code = env['_boards_sync']() self.assertEqual(code,200) self.assertEqual(len(result['rich']),2) self.assertEqual(result['rich'][0],{'pos':1,'user':'@alice','usd':4.0}) self.assertTrue(result['rich'][1]['user'].startswith('user-')) self.assertEqual(result['casino'],[]) self.assertFalse(any('uid' in row or 'tg_id' in row for row in result['rich'])) if __name__ == '__main__': unittest.main(verbosity=2)