Esempio n. 1
0
 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)
class Test_SqlVm_locate(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.db = MockStorageCoordinator(LOCATE_RESULT_SAMPLE_STR, TABLE_DEFINITION_SAMPLE, [])
        self.vm = SqlVm()

    def test_locate_sample1(self):
        sample_condition = [
            [
                ('sno', '>', 10),
                ('name', '=', 'JackSon Li')
            ],
            [
                ('cno', '<>', 3),
                ('name', '<>', '')
            ]
        ]
        self.vm.locate(sample_condition, self.db)
        self.assertEqual([1, 3, 5, 7, 9], self.vm.reg_selector)

    def test_locate_sample2(self):
        sample_condition = [
            [
                ('sno', '>', 10),
                ('cno', '<>', 3)
            ]
        ]
        self.vm.locate(sample_condition, self.db)
        self.assertEqual([1, 3, 5], self.vm.reg_selector)

    def test_locate_sample3(self):
        sample_condition = [
            [
                ('sno', '>', 10)
            ],
            [
                ('cno', '<>', 3),
                ('name', '<>', '')
            ]
        ]
        self.vm.locate(sample_condition, self.db)
        self.assertEqual([1, 2, 3, 4, 5, 7, 9], self.vm.reg_selector)
 def setUp(self):
     self.db = MockStorageCoordinator(LOCATE_RESULT_SAMPLE_INT, TABLE_DEFINITION_SAMPLE, TABLE_CONTENT_SAMPLE)
     self.vm = SqlVm()
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_SqlVm_project(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.db = MockStorageCoordinator(LOCATE_RESULT_SAMPLE_INT, TABLE_DEFINITION_SAMPLE, TABLE_CONTENT_SAMPLE)
        self.vm = SqlVm()

    def setUp(self):
        self.vm.reg_selector = []
        self.vm.reg_table = []

    def test_project_sample1(self):
        expect_tbl = [
            (3, 'Alice', 80),
            (1, 'JackSon Li', 83),
            (3, 'Alpha', 85),
            (1, 'Beta', 88),
            (3, 'Gamma', 90),
            (2, 'Delta', 93),
            (3, 'Epsilon', 96),
            (2, 'Zeta', 97),
            (3, 'Iota', 99),
            (2, 'Kappa', 77),
            (3, 'Omega', 79)
        ]
        self.vm.reg_table = TABLE_CONTENT_SAMPLE
        self.vm.project([1, 2, 3])
        self.assertEqual(expect_tbl, self.vm.reg_table)

    def test_project_sample2(self):
        expect_tbl = [
            (3, 3, 'Alice', 80),
            (1, 1, 'JackSon Li', 83),
            (3, 3, 'Alpha', 85),
            (1, 1, 'Beta', 88),
            (3, 3, 'Gamma', 90),
            (2, 2, 'Delta', 93),
            (3, 3, 'Epsilon', 96),
            (2, 2, 'Zeta', 97),
            (3, 3, 'Iota', 99),
            (2, 2, 'Kappa', 77),
            (3, 3, 'Omega', 79)
        ]
        self.vm.reg_table = TABLE_CONTENT_SAMPLE
        self.vm.project([1, 1, 2, 3])
        self.assertEqual(expect_tbl, self.vm.reg_table)

    def test_project_sample3(self):
        expect_tbl = [
            (3, 'Alice', 80, 'Alice', 80),
            (1, 'JackSon Li', 83, 'JackSon Li', 83),
            (3, 'Alpha', 85, 'Alpha', 85),
            (1, 'Beta', 88, 'Beta', 88),
            (3, 'Gamma', 90, 'Gamma', 90),
            (2, 'Delta', 93, 'Delta', 93),
            (3, 'Epsilon', 96, 'Epsilon', 96),
            (2, 'Zeta', 97, 'Zeta', 97),
            (3, 'Iota', 99, 'Iota', 99),
            (2, 'Kappa', 77, 'Kappa', 77),
            (3, 'Omega', 79, 'Omega', 79)
        ]
        self.vm.reg_table = TABLE_CONTENT_SAMPLE
        self.vm.project([1, 2, 3, 2, 3])
        self.assertEqual(expect_tbl, self.vm.reg_table)
 def setUpClass(self):
     self.db = MockStorageCoordinator(LOCATE_RESULT_SAMPLE_STR, TABLE_DEFINITION_SAMPLE, [])
     self.vm = SqlVm()
Esempio n. 7
0
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])