41 lines
1.9 KiB
Python
41 lines
1.9 KiB
Python
import importlib.util
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
MODULE_PATH=Path(__file__).resolve().parents[2]/'tools/wiki/export_public_tables.py'
|
|
spec=importlib.util.spec_from_file_location('export_wiki',MODULE_PATH)
|
|
module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
|
|
|
|
|
|
class ExportTests(unittest.TestCase):
|
|
def test_only_public_inputs_and_readonly_query(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
base=Path(folder);conf=base/'server/share/conf';conf.mkdir(parents=True)
|
|
for name in ('item_proto.txt','item_names.txt','mob_proto.txt','mob_names.txt'):
|
|
(conf/name).write_text('public\n')
|
|
(conf/'CONFIG').write_text('DO NOT EXPORT')
|
|
def run(args,stdout,check):
|
|
self.assertTrue(check)
|
|
self.assertTrue(args[-1].startswith('SELECT id,cost,prob,'))
|
|
self.assertNotIn('account',args[-1])
|
|
stdout.write(b'1\t600\t90\n')
|
|
with patch.object(module.subprocess,'run',side_effect=run):
|
|
result=module.export(base/'server',base/'out','player','mysql')
|
|
self.assertEqual(len(result),5)
|
|
self.assertFalse((base/'out/CONFIG').exists())
|
|
self.assertTrue((base/'out/source-manifest.json').exists())
|
|
with self.assertRaises(ValueError):module.export(base/'server',base/'out','player','mysql')
|
|
|
|
def test_invalid_database_and_incomplete_source(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
base=Path(folder)
|
|
with self.assertRaises(ValueError):module.export(base,base/'out','player;DROP TABLE','mysql')
|
|
self.assertFalse((base/'out').exists())
|
|
with self.assertRaises(FileNotFoundError):module.export(base,base/'out','player','mysql')
|
|
self.assertFalse((base/'out').exists())
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|