70 lines
3.7 KiB
Python
70 lines
3.7 KiB
Python
import copy
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import sqlite3
|
|
import unittest
|
|
|
|
spec=importlib.util.spec_from_file_location('spawn_export',Path(__file__).resolve().parents[1]/'ops/editor/spawn_export.py')
|
|
m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
|
|
|
|
|
|
def state():
|
|
return {'format':1,'database':'tw_world','tables':{
|
|
'creature':{'columns':['guid','id','position_x','orientation'],'rows':{'1':['1','100','1.23456789','0'],'2':['2','200','5','1']}},
|
|
'gameobject':{'columns':['guid','id','rotation2','rotation3'],'rows':{'8':['8','900','0','1']}}}}
|
|
|
|
|
|
class Export(unittest.TestCase):
|
|
def test_sql_round_trip(self):
|
|
old=state();new=copy.deepcopy(old)
|
|
new['tables']['creature']['rows']['1'][2]='-12.3456789'
|
|
del new['tables']['creature']['rows']['2']
|
|
new['tables']['gameobject']['rows']['9']=['9','901','0.70710678','0.70710678']
|
|
sql,counts=m.diff(old,new)
|
|
self.assertEqual(counts,{'insert':1,'update':1,'delete':1})
|
|
self.assertIn('-12.3456789',sql)
|
|
self.assertNotIn('REPLACE INTO',sql)
|
|
db=sqlite3.connect(':memory:')
|
|
for table,data in old['tables'].items():
|
|
db.execute('CREATE TABLE '+table+' ('+','.join(c+' NUMERIC' for c in data['columns'])+')')
|
|
for row in data['rows'].values():db.execute('INSERT INTO '+table+' VALUES ('+','.join('?' for _ in row)+')',row)
|
|
db.executescript(sql.replace('<=>','IS'))
|
|
for table,data in new['tables'].items():
|
|
got=list(db.execute('SELECT * FROM '+table+' ORDER BY guid'))
|
|
expected=[tuple(float(v) if v is not None else None for v in row) for _,row in sorted(data['rows'].items(),key=lambda kv:int(kv[0]))]
|
|
self.assertEqual(got,expected)
|
|
self.assertEqual(m.diff(new,new)[1],{'insert':0,'update':0,'delete':0})
|
|
|
|
def test_refuses_partial_or_changed_schema(self):
|
|
bad=state();del bad['tables']['gameobject']
|
|
with self.assertRaises(ValueError):m.diff(state(),bad)
|
|
bad=state();bad['tables']['creature']['columns'][-1]='other'
|
|
with self.assertRaises(ValueError):m.diff(state(),bad)
|
|
for value in ['1; DROP TABLE creature','NaN','Infinity',{},'']:
|
|
with self.assertRaises(ValueError):m.literal(value)
|
|
|
|
def test_old_value_guard(self):
|
|
old=state();new=copy.deepcopy(old);new['tables']['creature']['rows']['1'][2]='9'
|
|
sql,_=m.diff(old,new)
|
|
self.assertIn('`position_x` <=> 1.23456789',sql)
|
|
self.assertIn('`orientation` <=> 0',sql)
|
|
|
|
def test_snapshot_boundaries_null_and_precision(self):
|
|
schema=''.join(t+'\t'+c+'\t'+k+'\t'+key+'\n' for t in m.TABLES for c,k,key in [('guid','int','PRI'),('id','int','MUL'),('position_x','float','')])
|
|
def hexrow(values):return '\t'.join('NULL' if v is None else v.encode().hex().upper() for v in values)+'\n'
|
|
rows='__EDITOR_TABLE__creature\n'+hexrow(['1','100','-1.23456789'])+'__EDITOR_TABLE__gameobject\n'+hexrow(['2','200',None])+'__EDITOR_END__\n'
|
|
calls=[]
|
|
def reader(sql):
|
|
calls.append(sql)
|
|
return schema if 'information_schema' in sql else rows
|
|
got=m.snapshot(reader)
|
|
self.assertEqual(got['tables']['creature']['rows']['1'][2],'-1.23456789')
|
|
self.assertIsNone(got['tables']['gameobject']['rows']['2'][2])
|
|
self.assertTrue(calls[1].startswith('LOCK TABLES `creature` READ, `gameobject` READ;'))
|
|
self.assertTrue(calls[1].endswith('UNLOCK TABLES;'))
|
|
for damaged in [rows.replace('__EDITOR_END__\n',''),rows.replace('__EDITOR_TABLE__gameobject\n',''),rows+'junk\n']:
|
|
with self.assertRaises(ValueError):m.snapshot(lambda sql:schema if 'information_schema' in sql else damaged)
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|