1
1
import { SingleColumnSyntaxTree } from 'dash-table/syntax-tree' ;
2
2
import { ColumnType } from 'dash-table/components/Table/props' ;
3
3
import { SingleColumnConfig } from 'dash-table/syntax-tree/SingleColumnSyntaxTree' ;
4
+ import { RelationalOperator } from 'dash-table/syntax-tree/lexeme/relational' ;
5
+ import { LexemeType } from 'core/syntax-tree/lexicon' ;
4
6
5
7
const COLUMN_ANY : SingleColumnConfig = {
6
8
id : 'a' ,
7
9
type : ColumnType . Any
8
10
} ;
9
11
12
+ const COLUMN_DATE : SingleColumnConfig = {
13
+ id : 'a' ,
14
+ type : ColumnType . Datetime
15
+ } ;
16
+
10
17
const COLUMN_NUMERIC : SingleColumnConfig = {
11
18
id : 'a' ,
12
19
type : ColumnType . Numeric
@@ -72,7 +79,9 @@ describe('Single Column Syntax Tree', () => {
72
79
const tree = new SingleColumnSyntaxTree ( '1' , COLUMN_UNDEFINED ) ;
73
80
74
81
expect ( tree . isValid ) . to . equal ( true ) ;
75
- expect ( tree . evaluate ( { a : 1 } ) ) . to . equal ( true ) ;
82
+ expect ( tree . evaluate ( { a : '1' } ) ) . to . equal ( true ) ;
83
+ expect ( tree . evaluate ( { a : '2' } ) ) . to . equal ( false ) ;
84
+ expect ( tree . evaluate ( { a : 1 } ) ) . to . equal ( false ) ;
76
85
expect ( tree . evaluate ( { a : 2 } ) ) . to . equal ( false ) ;
77
86
78
87
expect ( tree . toQueryString ( ) ) . to . equal ( '{a} contains 1' ) ;
@@ -110,9 +119,119 @@ describe('Single Column Syntax Tree', () => {
110
119
const tree = new SingleColumnSyntaxTree ( '"1"' , COLUMN_TEXT ) ;
111
120
112
121
expect ( tree . isValid ) . to . equal ( true ) ;
122
+ expect ( tree . evaluate ( { a : 1 } ) ) . to . equal ( true ) ;
123
+ expect ( tree . evaluate ( { a : 2 } ) ) . to . equal ( false ) ;
113
124
expect ( tree . evaluate ( { a : '1' } ) ) . to . equal ( true ) ;
114
125
expect ( tree . evaluate ( { a : '2' } ) ) . to . equal ( false ) ;
115
126
116
127
expect ( tree . toQueryString ( ) ) . to . equal ( '{a} contains "1"' ) ;
117
128
} ) ;
129
+
130
+ [ '1975' , '"1975"' ] . forEach ( value => {
131
+ it ( `can be expression '${ value } ' with datetime column type` , ( ) => {
132
+ const tree = new SingleColumnSyntaxTree ( value , COLUMN_DATE ) ;
133
+
134
+ expect ( tree . evaluate ( { a : 1975 } ) ) . to . equal ( true ) ;
135
+ expect ( tree . evaluate ( { a : '1975' } ) ) . to . equal ( true ) ;
136
+ expect ( tree . evaluate ( { a : '1975-01' } ) ) . to . equal ( true ) ;
137
+ expect ( tree . evaluate ( { a : '1975-01-01' } ) ) . to . equal ( true ) ;
138
+ expect ( tree . evaluate ( { a : '1975-01-01 01:01:01' } ) ) . to . equal ( true ) ;
139
+
140
+ expect ( tree . evaluate ( { a : 1976 } ) ) . to . equal ( false ) ;
141
+ expect ( tree . evaluate ( { a : '1976' } ) ) . to . equal ( false ) ;
142
+ expect ( tree . evaluate ( { a : '1976-01' } ) ) . to . equal ( false ) ;
143
+ expect ( tree . evaluate ( { a : '1976-01-01' } ) ) . to . equal ( false ) ;
144
+ expect ( tree . evaluate ( { a : '1976-01-01 01:01:01' } ) ) . to . equal ( false ) ;
145
+ } ) ;
146
+ } ) ;
147
+
148
+ [
149
+ { type : COLUMN_UNDEFINED , name : 'undefined' } ,
150
+ { type : COLUMN_ANY , name : 'any' } ,
151
+ { type : COLUMN_TEXT , name : 'text' }
152
+ ] . forEach ( ( { type, name } ) => {
153
+ it ( `returns the correct relational operator lexeme for '${ name } ' column type` , ( ) => {
154
+ const tree = new SingleColumnSyntaxTree ( '1' , type ) ;
155
+ const structure = tree . toStructure ( ) ;
156
+
157
+ expect ( tree . toQueryString ( ) ) . to . equal ( '{a} contains 1' ) ;
158
+ expect ( structure ) . to . not . equal ( null ) ;
159
+
160
+ if ( structure ) {
161
+ expect ( structure . value ) . to . equal ( RelationalOperator . Contains ) ;
162
+ expect ( structure . subType ) . to . equal ( RelationalOperator . Contains ) ;
163
+ expect ( structure . type ) . to . equal ( LexemeType . RelationalOperator ) ;
164
+
165
+ expect ( structure . left ) . to . not . equal ( null ) ;
166
+ if ( structure . left ) {
167
+ expect ( structure . left . type ) . to . equal ( LexemeType . Expression ) ;
168
+ expect ( structure . left . subType ) . to . equal ( 'field' ) ;
169
+ expect ( structure . left . value ) . to . equal ( 'a' ) ;
170
+ }
171
+
172
+ expect ( structure . right ) . to . not . equal ( null ) ;
173
+ if ( structure . right ) {
174
+ expect ( structure . right . type ) . to . equal ( LexemeType . Expression ) ;
175
+ expect ( structure . right . subType ) . to . equal ( 'value' ) ;
176
+ expect ( structure . right . value ) . to . equal ( 1 ) ;
177
+ }
178
+ }
179
+ } ) ;
180
+ } ) ;
181
+
182
+ it ( `returns the correct relational operator lexeme for 'date' column type` , ( ) => {
183
+ const tree = new SingleColumnSyntaxTree ( '1975' , COLUMN_DATE ) ;
184
+ const structure = tree . toStructure ( ) ;
185
+
186
+ expect ( tree . toQueryString ( ) ) . to . equal ( '{a} datestartswith 1975' ) ;
187
+ expect ( structure ) . to . not . equal ( null ) ;
188
+
189
+ if ( structure ) {
190
+ expect ( structure . value ) . to . equal ( RelationalOperator . DateStartsWith ) ;
191
+ expect ( structure . subType ) . to . equal ( RelationalOperator . DateStartsWith ) ;
192
+ expect ( structure . type ) . to . equal ( LexemeType . RelationalOperator ) ;
193
+
194
+ expect ( structure . left ) . to . not . equal ( null ) ;
195
+ if ( structure . left ) {
196
+ expect ( structure . left . type ) . to . equal ( LexemeType . Expression ) ;
197
+ expect ( structure . left . subType ) . to . equal ( 'field' ) ;
198
+ expect ( structure . left . value ) . to . equal ( 'a' ) ;
199
+ }
200
+
201
+ expect ( structure . right ) . to . not . equal ( null ) ;
202
+ if ( structure . right ) {
203
+ expect ( structure . right . type ) . to . equal ( LexemeType . Expression ) ;
204
+ expect ( structure . right . subType ) . to . equal ( 'value' ) ;
205
+ expect ( structure . right . value ) . to . equal ( 1975 ) ;
206
+ }
207
+ }
208
+ } ) ;
209
+
210
+ it ( `returns the correct relational operator lexeme for 'numeric' column type` , ( ) => {
211
+ const tree = new SingleColumnSyntaxTree ( '1' , COLUMN_NUMERIC ) ;
212
+ const structure = tree . toStructure ( ) ;
213
+
214
+ expect ( tree . toQueryString ( ) ) . to . equal ( '{a} = 1' ) ;
215
+ expect ( structure ) . to . not . equal ( null ) ;
216
+
217
+ if ( structure ) {
218
+ expect ( structure . value ) . to . equal ( RelationalOperator . Equal ) ;
219
+ expect ( structure . subType ) . to . equal ( RelationalOperator . Equal ) ;
220
+ expect ( structure . type ) . to . equal ( LexemeType . RelationalOperator ) ;
221
+
222
+ expect ( structure . left ) . to . not . equal ( null ) ;
223
+ if ( structure . left ) {
224
+ expect ( structure . left . type ) . to . equal ( LexemeType . Expression ) ;
225
+ expect ( structure . left . subType ) . to . equal ( 'field' ) ;
226
+ expect ( structure . left . value ) . to . equal ( 'a' ) ;
227
+ }
228
+
229
+ expect ( structure . right ) . to . not . equal ( null ) ;
230
+ if ( structure . right ) {
231
+ expect ( structure . right . type ) . to . equal ( LexemeType . Expression ) ;
232
+ expect ( structure . right . subType ) . to . equal ( 'value' ) ;
233
+ expect ( structure . right . value ) . to . equal ( 1 ) ;
234
+ }
235
+ }
236
+ } ) ;
118
237
} ) ;
0 commit comments