import copy from pathlib import Path import tempfile import unittest from development_plan import plan_source, validate_extension from release import Refused, file_digest class PlanTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.root = Path(self.temp.name) self.source = self.root/'source' (self.source/'PlotLine/Sql').mkdir(parents=True) self.old = self.source/'PlotLine/Sql/339_Existing.sql' self.old.write_text('CREATE TABLE dbo.Existing(Id int);') step = dict(id='339_Existing',file=self.old.name,sha256=file_digest(self.old), requires=[],mode='execute',prerequisites=[],postconditions=[dict(kind='object',name='dbo.Existing',type='U')]) self.baseline = dict(starting_schema_sha256='a'*64,resulting_schema_sha256='b'*64, migrations=[step],omitted_migrations=[],prerequisites=[],postconditions=step['postconditions'], qualification=dict(status='passed',evidence='evidence/old.json')) def tearDown(self): self.temp.cleanup() def test_code_only_reuses_resulting_schema_without_qualification(self): plan, pending = plan_source(self.source,self.baseline,self.root/'sql') self.assertFalse(pending) self.assertEqual(plan['starting_schema_sha256'],'b'*64) validate_extension(plan,self.baseline) def test_procedure_schema_function_and_data_migrations_append_in_order(self): for name,sql in [('340_Procedure.sql','CREATE OR ALTER PROCEDURE dbo.ReadNew AS SELECT 1;'), ('341_Table.sql','CREATE TABLE [dbo].[NewTable](Id int);'), ('342_Data.sql','UPDATE dbo.Existing SET Id=Id+1;'), ('343_Function.sql','CREATE OR ALTER FUNCTION dbo.ReadValue() RETURNS int AS BEGIN RETURN 1 END;')]: (self.old.parent/name).write_text(sql) plan,pending = plan_source(self.source,self.baseline,self.root/'sql') self.assertEqual(len(pending),4) self.assertEqual(plan['migrations'][1]['requires'],['339_Existing']) self.assertEqual(plan['migrations'][2]['requires'],['340_Procedure']) validate_extension(plan,self.baseline) def test_changed_historical_sql_and_number_collisions_refused(self): self.old.write_text('ALTER TABLE dbo.Existing ADD Surprise int;') with self.assertRaises(Refused): plan_source(self.source,self.baseline,self.root/'sql') self.old.write_text('CREATE TABLE dbo.Existing(Id int);') (self.old.parent/'339_Collision.sql').write_text('SELECT 1;') with self.assertRaises(Refused): plan_source(self.source,self.baseline,self.root/'sql2') def test_broker_rejects_history_changes_and_path_substitution(self): for field,value in [('mode','baseline'),('file','../outside.sql'),('requires',[])]: plan=copy.deepcopy(self.baseline) plan['starting_schema_sha256']='b'*64 plan['migrations'].append(dict(plan['migrations'][0],id='340_New',file='340_New.sql',requires=['339_Existing'])) plan['migrations'][-1][field]=value with self.subTest(field=field),self.assertRaises(Refused):validate_extension(plan,self.baseline) if __name__ == '__main__': unittest.main()