class Test_SqlVm(unittest.TestCase): def setUp(self): self.db = MockStorageCoordinator(LOCATE_RESULT_SAMPLE_INT, TABLE_DEFINITION_SAMPLE, TABLE_CONTENT_SAMPLE) self.vm = SqlVm() def test_run_sample1(self): codes = [Code(opc='locate', opr=None), Code(opc='query', opr=None), Code(opc='project', opr=None)] codes[0].opr = [ [ (2, '<>', '') ], [ (1, '<>', 3), (2, '=', 'JackSon Li'), (0, '>', 10) ] ] codes[2].opr = [0, 1, 2, 3] expect_tbl = [ (0, 3, 'Alice', 80), (11, 1, 'JackSon Li', 83), (12, 3, 'Alpha', 85), (13, 1, 'Beta', 88), (14, 3, 'Gamma', 90), (15, 2, 'Delta', 93), (1, 3, 'Epsilon', 96), (2, 2, 'Zeta', 97), (3, 3, 'Iota', 99), (4, 2, 'Kappa', 77), (5, 3, 'Omega', 79) ] vmResult = self.vm.run(codes, self.db) self.assertTrue(vmResult['is_success']) self.assertEqual(expect_tbl, vmResult['content']) def test_run_sample2(self): codes = [Code(opc='locate', opr=None), Code(opc='query', opr=None), Code(opc='project', opr=None)] codes[0].opr = [ [ (2, '=', 'JackSon Li'), (2, '<>', '') ], [ (1, '<>', 3), (0, '>', 10) ] ] codes[2].opr = [0, 1, 2, 3] expect_tbl = [ (11, 1, 'JackSon Li', 83), (13, 1, 'Beta', 88), (15, 2, 'Delta', 93) ] vmResult = self.vm.run(codes, self.db) self.assertTrue(vmResult['is_success']) self.assertEqual(expect_tbl, vmResult['content']) def test_run_sample3(self): codes = [Code(opc='insert', opr=None)] codes[0].opr = (99, 9, 'Lucas', 90) vmResult = self.vm.run(codes, self.db) self.assertTrue(vmResult['is_success']) self.assertEqual(1, len(self.db.call_seq)) self.assertEqual(('insert', codes[0].opr), self.db.call_seq[0]) def test_run_sample4(self): codes = [Code(opc='locate', opr=None), Code(opc='delete', opr=None)] codes[0].opr = [[(2, '=', 'JackSon Li')]] vmResult = self.vm.run(codes, self.db) self.assertTrue(vmResult['is_success']) self.assertEqual(3, len(self.db.call_seq)) self.assertEqual(('locate_all'), self.db.call_seq[0]) self.assertEqual(('locate', 2, '=', 'JackSon Li'), self.db.call_seq[1]) self.assertEqual(('delete', [1]), self.db.call_seq[2]) def test_run_sample5(self): codes = [Code(opc='locate', opr=None), Code(opc='update', opr=None)] codes[0].opr = [[(2, '=', 'JackSon Li'), (2, '<>', '')], [(1, '<>', 3)]] codes[1].opr = {3: 95} vmResult = self.vm.run(codes, self.db) self.assertTrue(vmResult['is_success']) seq = self.db.call_seq self.assertEqual(5, len(seq)) self.assertEqual(('locate_all'), seq[0]) self.assertEqual(('locate', 2, '=', 'JackSon Li'), seq[1]) self.assertEqual(('locate', 2, '<>', ''), seq[2]) self.assertEqual(('locate', 1, '<>', 3), seq[3]) self.assertEqual(('update', {3: 95}, [1, 3, 5, 7, 9]), self.db.call_seq[4])
class Test_Core_SqlEngine_integration1(unittest.TestCase): def setUp(self): self.db = MockStorageCoordinator(LOCATE_RESULT_SAMPLE, TABLE_DEFINITION_SAMPLE, TABLE_CONTENT_SAMPLE) self.engine = SqlEngine(TABLE_DEFINITION_SAMPLE) self.vm = SqlVm() lexer, tokens = self.engine.gen_lex() self.parser = self.engine.gen_yacc(lexer, tokens) def test_runSql_sample1(self): sql_expr = "SELECT * WHERE sno > 10 and name ='JackSon Li' and cno <> 3 OR name <> ''" # codes = [Code(opc='locate', opr=None), # Code(opc='query', opr=None), # Code(opc='project', opr=None)] # codes[0].opr = [ # [ # (2, '<>', '') # ], # [ # (1, '<>', 3), # (2, '=', 'JackSon Li'), # (0, '>', 10) # ] # ] # codes[2].opr = [0, 1, 2, 3] expect_tbl = [(0, 3, 'Alice', 80), (11, 1, 'JackSon Li', 83), (12, 3, 'Alpha', 85), (13, 1, 'Beta', 88), (14, 3, 'Gamma', 90), (15, 2, 'Delta', 93), (1, 3, 'Epsilon', 96), (2, 2, 'Zeta', 97), (3, 3, 'Iota', 99), (4, 2, 'Kappa', 77), (5, 3, 'Omega', 79)] codes = self.parser.parse(sql_expr) vmResult = self.vm.run(codes, self.db) self.assertTrue(vmResult['is_success']) self.assertEqual(expect_tbl, vmResult['content']) def test_runSql_sample2(self): sql_expr = "SELECT * WHERE sno > 10 and cno <> 3 OR name ='JackSon Li' and name <> ''" # codes = [Code(opc='locate', opr=None), # Code(opc='query', opr=None), # Code(opc='project', opr=None)] # codes[0].opr = [ # [ # (2, '=', 'JackSon Li'), # (2, '<>', '') # ], # [ # (1, '<>', 3), # (0, '>', 10) # ] # ] # codes[2].opr = [0, 1, 2, 3] expect_tbl = [(11, 1, 'JackSon Li', 83), (13, 1, 'Beta', 88), (15, 2, 'Delta', 93)] codes = self.parser.parse(sql_expr) vmResult = self.vm.run(codes, self.db) self.assertTrue(vmResult['is_success']) self.assertEqual(expect_tbl, vmResult['content']) def test_run_sample3(self): sql_expr = "INSERT grade, sno, cno, name VALUES 90, 99, 9, 'Lucas'" # codes = [Code(opc='insert', opr=None)] # codes[0].opr = (99, 9, 'Lucas', 90) expect_insertion = (99, 9, 'Lucas', 90) codes = self.parser.parse(sql_expr) vmResult = self.vm.run(codes, self.db) self.assertTrue(vmResult['is_success']) self.assertEqual(1, len(self.db.call_seq)) self.assertEqual(('insert', expect_insertion), self.db.call_seq[0]) def test_run_sample4(self): sql_expr = "DELETE WHERE name = 'JackSon Li'" # codes = [Code(opc='locate', opr=None), # Code(opc='delete', opr=None)] # codes[0].opr = [[(2, '=', 'JackSon Li')]] codes = self.parser.parse(sql_expr) vmResult = self.vm.run(codes, self.db) self.assertTrue(vmResult['is_success']) self.assertEqual(3, len(self.db.call_seq)) self.assertEqual(('locate_all'), self.db.call_seq[0]) self.assertEqual(('locate', 2, '=', 'JackSon Li'), self.db.call_seq[1]) self.assertEqual(('delete', [1]), self.db.call_seq[2]) def test_run_sample5(self): sql_expr = "UPDATE SET grade=95 WHERE name = 'JackSon Li' and name <> '' OR cno <> 3" # codes = [Code(opc='locate', opr=None), # Code(opc='update', opr=None)] # codes[0].opr = [[(2, '=', 'JackSon Li'), (2, '<>', '')], # [(1, '<>', 3)]] # codes[1].opr = {3: '95'} codes = self.parser.parse(sql_expr) vmResult = self.vm.run(codes, self.db) self.assertTrue(vmResult['is_success']) seq = self.db.call_seq self.assertEqual(5, len(seq)) self.assertEqual(('locate_all'), seq[0]) self.assertEqual(('update', { 3: 95 }, [1, 3, 5, 7, 9]), self.db.call_seq[4])