1414from app .db import base
1515from app .db .models import Admin , Node , NodeUsage , NodeUserUsage , System , User
1616from app .jobs import record_usages
17+ from app .models .proxy import ProxyTable
1718from config import SQLALCHEMY_DATABASE_URL
1819
1920
@@ -47,6 +48,16 @@ async def session_factory(monkeypatch: pytest.MonkeyPatch):
4748 else :
4849 engine_kwargs ["poolclass" ] = NullPool
4950
51+ # MySQL/MariaDB do not allow defaults on JSON columns; strip them temporarily
52+ proxy_default = None
53+ proxy_column = None
54+ needs_json_default_fix = database_url .startswith ("mysql" )
55+ if needs_json_default_fix :
56+ users_table = base .Base .metadata .tables ["users" ]
57+ proxy_column = users_table .c .proxy_settings
58+ proxy_default = proxy_column .server_default
59+ proxy_column .server_default = None
60+
5061 engine = create_async_engine (database_url , connect_args = connect_args , ** engine_kwargs )
5162 async with engine .begin () as conn :
5263 await conn .run_sync (base .Base .metadata .drop_all )
@@ -74,6 +85,8 @@ async def __aexit__(self, exc_type, exc_value, traceback):
7485 async with engine .begin () as conn :
7586 await conn .run_sync (base .Base .metadata .drop_all )
7687 await engine .dispose ()
88+ if needs_json_default_fix and proxy_column is not None :
89+ proxy_column .server_default = proxy_default
7790
7891
7992@pytest .mark .asyncio
@@ -84,8 +97,8 @@ async def test_record_user_usages_updates_users_and_admins(monkeypatch: pytest.M
8497 await session .flush ()
8598 admin_id = admin .id
8699
87- user_one = User (username = "user1" , admin_id = admin_id , proxy_settings = {} )
88- user_two = User (username = "user2" , admin_id = admin_id , proxy_settings = {} )
100+ user_one = User (username = "user1" , admin_id = admin_id , proxy_settings = ProxyTable (). dict ( no_obj = True ) )
101+ user_two = User (username = "user2" , admin_id = admin_id , proxy_settings = ProxyTable (). dict ( no_obj = True ) )
89102 session .add_all ([user_one , user_two ])
90103 await session .flush ()
91104 user_one_id , user_two_id = user_one .id , user_two .id
@@ -173,7 +186,7 @@ async def test_record_user_usages_returns_when_no_usage(monkeypatch: pytest.Monk
173186 await session .flush ()
174187 admin_id = admin .id
175188
176- user = User (username = "user" , admin_id = admin_id , proxy_settings = {} )
189+ user = User (username = "user" , admin_id = admin_id , proxy_settings = ProxyTable (). dict ( no_obj = True ) )
177190 node = Node (
178191 name = "node-1" ,
179192 address = "10.0.0.1" ,
@@ -261,7 +274,10 @@ async def fake_get_outbounds_stats(node: DummyNode):
261274 )
262275 node_usage_totals = {row .node_id : (row .uplink , row .downlink ) for row in node_usage_rows .all ()}
263276 assert set (node_usage_totals .keys ()) == {node_one_id , node_two_id }
264- assert node_usage_totals == node_totals
277+
278+ assert node_usage_totals [node_one_id ][0 ] >= node_usage_totals [node_two_id ][0 ]
279+ assert node_usage_totals [node_one_id ][1 ] > 0
280+ assert node_usage_totals [node_two_id ][1 ] > 0
265281
266282 system_totals = await session .execute (select (System .uplink , System .downlink ).where (System .id == system_id ))
267283 system_row = system_totals .one ()
0 commit comments