|
1 | | -import { getNested, isCorrectParenthesis } from './hiveTypeRules'; |
| 1 | +import { |
| 2 | + backtickedType, |
| 3 | + checkValidDataType, |
| 4 | + getNested, |
| 5 | + isCorrectParenthesis, |
| 6 | + isValidColName, |
| 7 | + makeCompact, |
| 8 | + makePretty, |
| 9 | + tokenize, |
| 10 | +} from './hiveTypeRules'; |
2 | 11 |
|
3 | 12 | describe('Test isCorrectParenthesis function', () => { |
4 | 13 | /* TRUE */ |
@@ -29,3 +38,179 @@ describe('Test getNested function', () => { |
29 | 38 | expect(getNested('struct<tag:array<int>>')).toBe('tag:array<int>'); |
30 | 39 | }); |
31 | 40 | }); |
| 41 | + |
| 42 | +describe('Test tokenize function', () => { |
| 43 | + it('괄호가 없는 케이스', () => { |
| 44 | + expect(tokenize('int, string, char', ',')).toStrictEqual(['int', 'string', 'char']); |
| 45 | + }); |
| 46 | + |
| 47 | + it('괄호 안에 구분자가 있는 케이스', () => { |
| 48 | + expect(tokenize('tag:array<struct<code:string>>', ':')).toStrictEqual(['tag', 'array<struct<code:string>>']); |
| 49 | + expect(tokenize('int, map<int, decimal(5, 5)>', ',')).toStrictEqual(['int', 'map<int,decimal(5,5)>']); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe('Test isValidColName function', () => { |
| 54 | + /* TRUE */ |
| 55 | + it('단순 문자열', () => { |
| 56 | + expect(isValidColName('colname')).toBe(true); |
| 57 | + }); |
| 58 | + |
| 59 | + it('연속된 백틱(`)이 짝수개가 있는 문자열', () => { |
| 60 | + expect(isValidColName('`col``name`')).toBe(true); |
| 61 | + expect(isValidColName('`col````name`')).toBe(true); |
| 62 | + expect(isValidColName('`col``````name`')).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + /* FALSE */ |
| 66 | + it('비어 있는 문자열', () => { |
| 67 | + expect(isValidColName('')).toBe(false); |
| 68 | + expect(isValidColName('``')).toBe(false); |
| 69 | + }); |
| 70 | + |
| 71 | + it('공백이 있는 문자열', () => { |
| 72 | + expect(isValidColName('colname ')).toBe(false); |
| 73 | + expect(isValidColName('col name')).toBe(false); |
| 74 | + }); |
| 75 | + |
| 76 | + it('캐럿 문자(‸)가 있는 문자열', () => { |
| 77 | + expect(isValidColName('col‸name')).toBe(false); |
| 78 | + expect(isValidColName('colname‸')).toBe(false); |
| 79 | + }); |
| 80 | + |
| 81 | + it('타입 구분 문자(쉼표[,], 콜론[:], 괄호[(), <>])가 있는 문자열', () => { |
| 82 | + expect(isValidColName('col,name')).toBe(false); |
| 83 | + expect(isValidColName('col:name')).toBe(false); |
| 84 | + expect(isValidColName('col(name')).toBe(false); |
| 85 | + expect(isValidColName('colname)')).toBe(false); |
| 86 | + expect(isValidColName('col<name')).toBe(false); |
| 87 | + expect(isValidColName('colname>')).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it('연속된 백틱(`)이 홀수개가 있는 문자열', () => { |
| 91 | + expect(isValidColName('`col`name`')).toBe(false); |
| 92 | + expect(isValidColName('`col```name`')).toBe(false); |
| 93 | + expect(isValidColName('`col`````name`')).toBe(false); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe('Test checkValidDataType function', () => { |
| 98 | + /* TRUE */ |
| 99 | + it('올바른 Primitive Type', () => { |
| 100 | + expect(checkValidDataType('string')).toBe(true); |
| 101 | + expect(checkValidDataType('int')).toBe(true); |
| 102 | + expect(checkValidDataType('char(10)')).toBe(true); |
| 103 | + expect(checkValidDataType('varchar(10)')).toBe(true); |
| 104 | + expect(checkValidDataType('decimal')).toBe(true); |
| 105 | + expect(checkValidDataType('decimal(10, 10)')).toBe(true); |
| 106 | + }); |
| 107 | + |
| 108 | + it('대소문자는 결과에 영향을 주지 않음', () => { |
| 109 | + expect(checkValidDataType('STRING')).toBe(true); |
| 110 | + expect(checkValidDataType('INT')).toBe(true); |
| 111 | + expect(checkValidDataType('CHAR(10)')).toBe(true); |
| 112 | + }); |
| 113 | + |
| 114 | + it('올바른 Complex Type', () => { |
| 115 | + expect(checkValidDataType('array<int>')).toBe(true); |
| 116 | + expect(checkValidDataType('map<int, array<int>>')).toBe(true); |
| 117 | + expect(checkValidDataType('struct<code:string, text:string>')).toBe(true); |
| 118 | + expect(checkValidDataType('array<struct<code:string, text:string>>')).toBe(true); |
| 119 | + expect(checkValidDataType('uniontype<int, array<int>, map<int, string>, struct<c:string, t:string>>')).toBe(true); |
| 120 | + }); |
| 121 | + |
| 122 | + /* FALSE */ |
| 123 | + it('올바르지 않은 array 타입', () => { |
| 124 | + expect(checkValidDataType('arr<int>')).toBe(false); // arr는 올바르지 않음. |
| 125 | + expect(checkValidDataType('array<str>')).toBe(false); // 괄호 내부의 타입이 올바르지 않음. |
| 126 | + }); |
| 127 | + |
| 128 | + it('올바르지 않은 map 타입', () => { |
| 129 | + expect(checkValidDataType('mapp<int>')).toBe(false); // mapp은 올바르지 않음. |
| 130 | + expect(checkValidDataType('map<array<int>, int>')).toBe(false); // 괄호 내부의 첫 번째 타입은 primitive type이어야 함. |
| 131 | + expect(checkValidDataType('map<int, str>')).toBe(false); // 괄호 내부의 두 번째 타입이 올바르지 않음. |
| 132 | + expect(checkValidDataType('map<int>')).toBe(false); // 괄호 내부의 타입 개수가 올바르지 않음. |
| 133 | + expect(checkValidDataType('map<int, int, int>')).toBe(false); // 괄호 내부의 타입 개수가 올바르지 않음. |
| 134 | + }); |
| 135 | + |
| 136 | + it('올바르지 않은 struct 타입', () => { |
| 137 | + expect(checkValidDataType('struc<int>')).toBe(false); // struc은 올바르지 않음. |
| 138 | + expect(checkValidDataType('struct<co de:string')).toBe(false); // 괄호 내부의 컬럼 이름이 올바르지 않음(공백이 있음). |
| 139 | + expect(checkValidDataType('struct<code:str>')).toBe(false); // 괄호 내부의 컬럼 타입이 올바르지 않음. |
| 140 | + expect(checkValidDataType('struct<int>')).toBe(false); // 괄호 내부가 이름-타입 쌍이 아님. |
| 141 | + }); |
| 142 | + |
| 143 | + it('올바르지 않은 uniontype 타입', () => { |
| 144 | + expect(checkValidDataType('union<int>')).toBe(false); // union은 올바르지 않음. |
| 145 | + expect(checkValidDataType('uniontype<string, int, str>')).toBe(false); // 괄호 내부에 올바르지 않은 타입이 있음. |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +describe('Test makeCompact function', () => { |
| 150 | + it('타입의 대문자화', () => { |
| 151 | + expect(makeCompact('string')).toBe('STRING'); |
| 152 | + expect(makeCompact('int')).toBe('INT'); |
| 153 | + expect(makeCompact('char(10)')).toBe('CHAR(10)'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('STRUCT 타입 내의 컬럼 이름은 대문자화를 수행하지 않음', () => { |
| 157 | + expect(makeCompact('struct<code:string,text:string>')).toBe('STRUCT<code:STRING,text:STRING>'); |
| 158 | + }); |
| 159 | + |
| 160 | + it('공백 제거', () => { |
| 161 | + expect(makeCompact('array < int > ')).toBe('ARRAY<INT>'); |
| 162 | + expect(makeCompact(' map < int , array < int > > ')).toBe('MAP<INT,ARRAY<INT>>'); |
| 163 | + expect( |
| 164 | + makeCompact(` struct < |
| 165 | + code : string , |
| 166 | + text : string > `) |
| 167 | + ).toBe('STRUCT<code:STRING,text:STRING>'); |
| 168 | + }); |
| 169 | +}); |
| 170 | + |
| 171 | +describe('Test makePretty function', () => { |
| 172 | + it('띄어쓰기', () => { |
| 173 | + expect(makePretty('DECIMAL(10,10)')).toStrictEqual(['DECIMAL(10, 10)', null]); // 둥근 괄호('(', ')') 내 ',' 뒤 띄어쓰기 |
| 174 | + }); |
| 175 | + |
| 176 | + it('개행', () => { |
| 177 | + expect(makePretty('UNIONTYPE<INT,INT,INT,INT,INT>')).toStrictEqual([ |
| 178 | + `UNIONTYPE< |
| 179 | + INT, |
| 180 | + INT, |
| 181 | + INT, |
| 182 | + INT, |
| 183 | + INT |
| 184 | +>`, |
| 185 | + null, |
| 186 | + ]); |
| 187 | + }); |
| 188 | + |
| 189 | + it('개행 및 띄어쓰기', () => { |
| 190 | + // struct 타입 내 컬럼 이름-타입 쌍이 단독 라인에 존재하게 되는 경우, ':' 뒤에 한 칸 띄어쓰기 |
| 191 | + expect(makePretty('STRUCT<code:STRING,text:STRING>')).toStrictEqual([ |
| 192 | + `STRUCT< |
| 193 | + code: STRING, |
| 194 | + text: STRING |
| 195 | +>`, |
| 196 | + null, |
| 197 | + ]); |
| 198 | + }); |
| 199 | + |
| 200 | + it('커서 위치 반환', () => { |
| 201 | + // 캐럿(‸) 문자가 있는 경우, 포맷팅 된 이후 문자열에서 문자 간 상대적 위치가 동일하도록 계산된 커서 위치 반환 |
| 202 | + expect(makePretty('STRUCT<code:STRING,‸text:STRING>')).toStrictEqual([ |
| 203 | + `STRUCT< |
| 204 | + code: STRING, |
| 205 | + text: STRING |
| 206 | +>`, |
| 207 | + 23, |
| 208 | + ]); |
| 209 | + }); |
| 210 | +}); |
| 211 | + |
| 212 | +describe('Test backtickedType function', () => { |
| 213 | + it('STRUCT 타입 내 컬럼 이름 백틱 조작', () => { |
| 214 | + expect(backtickedType('STRUCT<code:STRING,te`xt:STRING>')).toBe('STRUCT<`code`:STRING,`te``xt`:STRING>'); |
| 215 | + }); |
| 216 | +}); |
0 commit comments