Files
WoW-Hybrid-gateway/tests/octo_spell_aura_restrictions_test.py

64 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""Source spell conditions must survive the existing native-definition importer."""
import builtins
import contextlib
import importlib.util
import io
import json
from pathlib import Path
import tempfile
import sys
import types
import unittest
from unittest import mock
ROOT=Path(__file__).resolve().parents[1]
spec=importlib.util.spec_from_file_location('octo_spell_presentation',ROOT/'ops/octo_spell_presentation.py')
module=importlib.util.module_from_spec(spec)
with mock.patch.dict(sys.modules, {'octo_mpq':types.ModuleType('octo_mpq')}):
spec.loader.exec_module(module)
class AuraRestrictions(unittest.TestCase):
def test_source_conditions_and_existing_rows(self):
def spell(ident,caster,target):
row=[0]*173;row[0]=ident;row[16]=caster;row[17]=target;row[120]=1;return row
spells=[spell(2,0,2),spell(3,1,0),spell(4,0,4),spell(77,12,0),spell(9,0,2),spell(6,6,0)]
def dbc(name,signed=False):
return (spells if name=='Spell.dbc' else []),lambda offset:'Fixture' if offset else ''
original_open=builtins.open
def opened(path,*args,**kwargs):
if str(path)=='/mnt/Games/@project/listfiles/community-listfile.csv':return io.StringIO('')
return original_open(path,*args,**kwargs)
with tempfile.TemporaryDirectory(prefix='spell-aura-restrictions-') as temp:
base=Path(temp);csv=base/'csv';csv.mkdir();out=base/'out'
(csv/'SpellName.csv').write_text('77\tName_lang=Fixture\n9\tName_lang=Fixture\n')
for table in ['SpellDuration','SpellCastTimes','SpellRange']:(csv/(table+'.csv')).write_text('')
(csv/'SpellAuraRestrictions.csv').write_text('2600005\tID=2600005\tDifficultyID=0\tCasterAuraState=0\tTargetAuraState=2\tSpellID=9\n')
with mock.patch.object(module,'dbc',side_effect=dbc),mock.patch.object(module,'open',side_effect=opened,create=True),contextlib.redirect_stdout(io.StringIO()):
module.main(csv,out)
path=out/'SpellAuraRestrictions.json'
self.assertTrue(path.exists(),'source aura conditions are missing from the imported native tables')
rows=json.loads(path.read_text());by_spell={r['SpellID']:r for r in rows}
self.assertEqual(set(by_spell),{2,3,4,77})
self.assertEqual([(by_spell[i]['CasterAuraState'],by_spell[i]['TargetAuraState']) for i in [2,3,4,77]],[(0,2),(1,0),(0,4),(13,0)])
self.assertEqual(len({r['ID'] for r in rows}),4)
self.assertTrue(all(r['ID']>2600005 for r in rows))
self.assertTrue(all(r['DifficultyID']==0 for r in rows))
for row in rows:
for field in ['ExcludeCasterAuraState','ExcludeTargetAuraState','CasterAuraSpell','TargetAuraSpell','ExcludeCasterAuraSpell','ExcludeTargetAuraSpell','CasterAuraType','TargetAuraType','ExcludeCasterAuraType','ExcludeTargetAuraType']:
self.assertEqual(row[field],0)
holds=json.loads((out/'SpellAuraRestrictions.holds.json').read_text())
self.assertEqual([(r['SpellID'],r['CasterAuraState'],r['TargetAuraState']) for r in holds],[(6,6,0)])
self.assertNotIn(9,by_spell,'existing native restriction rows must not be overwritten')
self.assertIn(77,by_spell,'previously imported spell names still need missing restrictions')
self.assertEqual(len(json.loads((out/'SpellName.json').read_text())),4)
with (csv/'SpellAuraRestrictions.csv').open('a') as stream:
for row in rows:
stream.write(str(row['ID'])+'\t'+'\t'.join(f'{k}={v}' for k,v in row.items())+'\n')
with mock.patch.object(module,'dbc',side_effect=dbc),mock.patch.object(module,'open',side_effect=opened,create=True),contextlib.redirect_stdout(io.StringIO()):
module.main(csv,base/'again')
self.assertEqual(json.loads((base/'again/SpellAuraRestrictions.json').read_text()),[])
if __name__=='__main__':unittest.main()