-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
110 lines (93 loc) · 3.82 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python3.10.6
# -*- coding: utf-8 -*-
#
# @Time : 2024/4/24 下午10:11
# @Author : ASXE
"""
this is a test file.
"""
from common import log, OutputTable, Logger, curdir
from core import Table, createDatabase
from parser import parser
logger = Logger(f'{curdir}/logs/test')
def testLog():
"""测试日志输出"""
log.warning('test warning')
log.info('test info')
log.error('test error', 'test')
def testLexParser():
"""测试词法分析器"""
codes = ["create database a", "create table a (a int , b str ,)",
"select a,b from t where (c='asxe' or d=1) and e=10",
"insert into t values (1,2,3), (2,3,4)",
"update t set a=1,b=2 where c='6'",
"delete from t where a=1",
"use test",
"drop database test",
'drop table test'
]
for code in codes:
lexParser = parser.LexParser(code, logger)
lexParser.getNextToken()
while lexParser.curToken.tokenType != parser.TokenType.TOKEN_END:
print((lexParser.curToken.value, lexParser.curToken.tokenType, lexParser.preToken.tokenType))
lexParser.getNextToken()
print()
def testSyntaxParser():
"""测试语法分析器"""
sparser = parser.SyntaxParser("create database a", logger)
print(sparser.parse())
sparser = parser.SyntaxParser("create table a (a int , b str ,)", logger)
print(sparser.parse())
sparser = parser.SyntaxParser("select a,b from t where (c='asxe' or d=1) and e=10", logger)
print(sparser.parse())
sparser = parser.SyntaxParser("insert into t values (1,2,3), (2,3,4)", logger)
print(sparser.parse())
sparser = parser.SyntaxParser("update t set a=1,b=2 where c='6'", logger)
print(sparser.parse())
sparser = parser.SyntaxParser("delete from t where a=1", logger)
print(sparser.parse())
sparser = parser.SyntaxParser("use test", logger)
print(sparser.parse())
sparser = parser.SyntaxParser("drop database test", logger)
print(sparser.parse())
sparser = parser.SyntaxParser("drop table test", logger)
print(sparser.parse())
def testCore():
"""测试内核"""
table = Table("a", [("a", "int"), ("b", "int"), ("c", "str"), ("d", "float")])
table.insert(['a', 'b', 'c'], [[1, 2, 'test'], [2, 2, 'example'], [1, 3, 'sample']])
table.insert(['a', 'b', 'c'], [[1, 2, 'test'], [2, 2, 'example'], [1, 3, 'sample']])
table.insert(['a', 'b', 'c'], [[1, 2, 'test'], [2, 2, 'example'], [1, 3, 'sample']])
table.insert(['a', 'b', 'c'], [[1, 2, 'test'], [2, 2, 'example'], [1, 3, 'sample']])
table.insert(['a', 'b', 'c', 'd'], [[10, 23, 'asxe', 12.1]])
selectQuery = {'SELECT': {'columns': ['a', 'b'], 'from': 'a',
'where': [('a', '=', 10), 'or', ('b', '=', 2)]}}
selectedRows = table.select(selectQuery['SELECT'])
for row in selectedRows:
print(f'select: {row}')
updateQuery = {'UPDATE': {'table': 'a', 'set': [('b', 10), ('c', 'updated')],
'where': [('a', '=', 1), 'and', ('b', '=', 2)]}}
table.update(updateQuery['UPDATE'])
selectedRows = table.select({'columns': ['*'], 'from': 'a'})
for row in selectedRows:
print(f'update: {row}')
deleteQuery = {'DELETE': {'table': 'a', 'where': [('a', '=', 1), 'or', ('b', '=', 10)]}}
table.delete(deleteQuery['DELETE'])
selectedRows = table.select({'columns': ['*'], 'from': 'a'})
for row in selectedRows:
print(f'delete: {row}')
def testOutPut():
data = [
{"Name": "1", "Age": 19, "City": "city1"},
{"Name": "2", "Age": 20, "City": "city2"},
{"Name": "3", "Age": 21, "City": "city3"}
]
table = OutputTable(data)
print(table)
if __name__ == '__main__':
testLog()
testLexParser()
testSyntaxParser()
testCore()
testOutPut()