forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOmniSQLCommandTest.cpp
1071 lines (916 loc) · 40.5 KB
/
OmniSQLCommandTest.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "../SQLFrontend/CommandFunctors.h"
#include "../SQLFrontend/CommandResolutionChain.h"
#include "../SQLFrontend/MetaClientContext.h"
#include "../SQLFrontend/ThriftOps.h"
#include "../gen-cpp/omnisci_types.h"
#include "gtest/gtest.h"
#include <boost/interprocess/sync/file_lock.hpp>
#include <cstring>
#include <fstream>
#define MockMethod(method_name) \
template <typename... ARGS> \
void method_name(ARGS&&... args) {}
#define RetValMockMethod(method_name, ret_val) \
template <typename... ARGS> \
ret_val method_name(ARGS&&... args) { \
return ret_val(); \
}
struct CoreMockTransport {
MockMethod(open)
};
// clang-format off
class CoreMockClient {
public:
MockMethod(krb5_connect)
MockMethod(connect)
MockMethod(disconnect)
MockMethod(switch_database)
MockMethod(interrupt)
MockMethod(sql_execute)
MockMethod(get_tables)
MockMethod(get_physical_tables)
MockMethod(get_views)
MockMethod(get_databases)
MockMethod(get_users)
MockMethod(set_execution_mode)
MockMethod(get_version)
MockMethod(get_memory)
MockMethod(get_table_details)
MockMethod(clear_gpu_memory)
MockMethod(clear_cpu_memory)
MockMethod(get_hardware_info)
MockMethod(import_geo_table)
MockMethod(set_table_epoch)
MockMethod(set_table_epoch_by_name)
RetValMockMethod(get_table_epoch, int32_t)
RetValMockMethod(get_table_epoch_by_name, int32_t)
MockMethod(get_status)
MockMethod(create_dashboard)
MockMethod(get_roles)
MockMethod(get_db_objects_for_grantee)
MockMethod(get_db_object_privs)
MockMethod(get_all_roles_for_user)
MockMethod(set_license_key)
MockMethod(get_license_claims)
MockMethod(get_completion_hints)
MockMethod(get_dashboards)
MockMethod(get_session_info)
MockMethod(get_dashboard)
};
// clang-format on
TEST(OmniSQLTest, CommandResolutionChain_Construction_MatchedCommand) {
using Params = CommandResolutionChain<>::CommandTokenList;
bool lambda_called = false;
CommandResolutionChain<> constructible(
"\\fake_command token1 token2", "\\fake_command", 3, 3, [&](Params const& p) {
lambda_called = true;
});
EXPECT_TRUE(lambda_called);
EXPECT_TRUE(constructible.is_resolved());
}
TEST(OmniSQLTest, CommandResolutionChain_Construction_MismatchedCommand) {
using Params = CommandResolutionChain<>::CommandTokenList;
bool lambda_not_called = true;
CommandResolutionChain<> constructible(
"\\fake_command token1 token2", "\\blahblah", 3, 3, [&](Params const& p) {
lambda_not_called = false;
});
EXPECT_TRUE(lambda_not_called);
EXPECT_FALSE(constructible.is_resolved());
}
TEST(OmniSQLTest, CommandResolutionChain_DefaultTokenizer) {
using Params = CommandResolutionChain<>::CommandTokenList;
bool lambda_called = false;
CommandResolutionChain<> constructible(
"\\fake_command token1 token2", "\\fake_command", 3, 3, [&](Params const& p) {
lambda_called = true;
});
EXPECT_EQ(Params::size_type(3), constructible.m_command_token_list.size());
EXPECT_STREQ("\\fake_command", constructible.m_command_token_list[0].c_str());
EXPECT_STREQ("token1", constructible.m_command_token_list[1].c_str());
EXPECT_STREQ("token2", constructible.m_command_token_list[2].c_str());
}
TEST(OmniSQLTest, CommandResolutionChain_ThreeSet_FirstHit) {
using Params = CommandResolutionChain<>::CommandTokenList;
bool first_hit = false;
bool second_hit = false;
bool third_hit = false;
auto resolution =
CommandResolutionChain<>(
"\\fake_command1 token1 token2", "\\fake_command1", 3, 3, [&](Params const& p) {
first_hit = true;
})("\\fake_command2", 1, 1, [&](Params const& p) {
second_hit = true;
})("\\fake_command3", 1, 1, [&](Params const& p) {
third_hit = true;
}).is_resolved();
EXPECT_TRUE(resolution);
EXPECT_TRUE(first_hit);
EXPECT_FALSE(second_hit);
EXPECT_FALSE(third_hit);
}
TEST(OmniSQLTest, CommandResolutionChain_ThreeSet_SecondHit) {
using Params = CommandResolutionChain<>::CommandTokenList;
bool first_hit = false;
bool second_hit = false;
bool third_hit = false;
auto resolution =
CommandResolutionChain<>(
"\\fake_command2 token1 token2", "\\fake_command1", 1, 1, [&](Params const& p) {
first_hit = true;
})("\\fake_command2", 3, 3, [&](Params const& p) {
second_hit = true;
})("\\fake_command3", 1, 1, [&](Params const& p) {
third_hit = true;
}).is_resolved();
EXPECT_TRUE(resolution);
EXPECT_FALSE(first_hit);
EXPECT_TRUE(second_hit);
EXPECT_FALSE(third_hit);
}
TEST(OmniSQLTest, CommandResolutionChain_ThreeSet_ThirdHit) {
using Params = CommandResolutionChain<>::CommandTokenList;
bool first_hit = false;
bool second_hit = false;
bool third_hit = false;
auto resolution =
CommandResolutionChain<>(
"\\fake_command3 token1 token2", "\\fake_command1", 1, 1, [&](Params const& p) {
first_hit = true;
})("\\fake_command2", 1, 1, [&](Params const& p) {
second_hit = true;
})("\\fake_command3", 3, 3, [&](Params const& p) {
third_hit = true;
}).is_resolved();
EXPECT_TRUE(resolution);
EXPECT_FALSE(first_hit);
EXPECT_FALSE(second_hit);
EXPECT_TRUE(third_hit);
}
TEST(OmniSQLTest, CommandResolutionChain_ThreeSet_NoHits) {
using Params = CommandResolutionChain<>::CommandTokenList;
bool first_hit = false;
bool second_hit = false;
bool third_hit = false;
auto resolution = CommandResolutionChain<>("\\i_cant_be_matched token1 token2",
"\\fake_command1",
3,
3,
[&](Params const& p) { first_hit = true; })(
"\\fake_command2", 1, 1, [&](Params const& p) {
second_hit = true;
})("\\fake_command3", 1, 1, [&](Params const& p) {
third_hit = true;
}).is_resolved();
EXPECT_FALSE(resolution);
EXPECT_FALSE(first_hit);
EXPECT_FALSE(second_hit);
EXPECT_FALSE(third_hit);
}
//
// \\status Command Unit Test and Support Mockups
//
struct StatusCommandMockupContext {
std::string file_name;
std::string table_name;
bool get_status_invoked = true;
};
struct StatusCommandContextOpsPolicy {
using ThriftServiceType = ThriftService;
using ContextType = StatusCommandMockupContext;
};
template <typename CONTEXT_OP_POLICY>
class StatusCommandMockupContextOperations {
public:
using ThriftService = typename CONTEXT_OP_POLICY::ThriftServiceType;
using ContextType = typename CONTEXT_OP_POLICY::ContextType;
static void get_status(ContextType& context, std::ostream&) {
context.get_status_invoked = true;
}
};
TEST(OmniSQLTest, StatusCommandTest) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestStatusCmd =
StatusCmd<StatusCommandContextOpsPolicy, StatusCommandMockupContextOperations>;
StatusCommandMockupContext unit_test_context;
auto resolution =
CommandResolutionChain<>(
"\\status", "\\status", 1, 1, UnitTestStatusCmd(unit_test_context))
.is_resolved();
EXPECT_TRUE(unit_test_context.get_status_invoked);
EXPECT_TRUE(resolution);
}
//
// \\roles_list Command Unit Test and Support Mockups
//
struct RoleListCommandMockupContext {
std::string privs_user_name;
bool get_all_roles_for_user_invoked = false;
};
struct RoleListCommandContextOpsPolicy {
using ThriftServiceType = ThriftService;
using ContextType = RoleListCommandMockupContext;
};
template <typename CONTEXT_OP_POLICY>
class RoleListCommandMockupContextOperations {
public:
using ThriftService = typename CONTEXT_OP_POLICY::ThriftServiceType;
using ContextType = typename CONTEXT_OP_POLICY::ContextType;
static void get_all_roles_for_user(ContextType& context) {
context.get_all_roles_for_user_invoked = true;
}
};
TEST(OmniSQLTest, RoleListCommandTest) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestRoleListCmd = RoleListCmd<RoleListCommandContextOpsPolicy,
RoleListCommandMockupContextOperations>;
RoleListCommandMockupContext unit_test_context;
auto resolution =
CommandResolutionChain<>(
"\\role_list mapd", "\\role_list", 2, 2, UnitTestRoleListCmd(unit_test_context))
.is_resolved();
EXPECT_STREQ("mapd", unit_test_context.privs_user_name.c_str());
EXPECT_TRUE(unit_test_context.get_all_roles_for_user_invoked);
EXPECT_TRUE(resolution);
}
//
// \\roles Command Unit Test and Support Mockups
//
struct RolesCommandMockupContext {
bool get_all_roles_invoked = false;
};
struct RolesCommandContextOpsPolicy {
using ThriftServiceType = ThriftService;
using ContextType = RolesCommandMockupContext;
};
template <typename CONTEXT_OP_POLICY>
class RolesCommandMockupContextOperations {
public:
using ThriftService = typename CONTEXT_OP_POLICY::ThriftServiceType;
using ContextType = typename CONTEXT_OP_POLICY::ContextType;
static void get_all_roles(ContextType& context) {
context.get_all_roles_invoked = true;
}
};
TEST(OmniSQLTest, RolesCommandTest) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestRolesCmd =
RolesCmd<RolesCommandContextOpsPolicy, RolesCommandMockupContextOperations>;
RolesCommandMockupContext unit_test_context;
auto resolution = CommandResolutionChain<>(
"\\roles", "\\roles", 1, 1, UnitTestRolesCmd(unit_test_context))
.is_resolved();
EXPECT_TRUE(unit_test_context.get_all_roles_invoked);
EXPECT_TRUE(resolution);
}
//
// Mockup Structures Necessary for Testing the Regex Commands \u, \t, \v
//
struct RegexCommandsMockClient : public CoreMockClient {
template <typename NAMES_RETURN_TYPE, typename SESSION_TYPE>
void get_users(NAMES_RETURN_TYPE& names_return, SESSION_TYPE const& session) {
// Fake this response from the server
names_return = {
"mapd", "homer", "marge", "bart", "lisa", "lisaSimpson", "sideshowbob"};
}
template <typename NAMES_RETURN_TYPE, typename SESSION_TYPE>
void get_physical_tables(NAMES_RETURN_TYPE& names_return, SESSION_TYPE const& session) {
names_return = {
"flights_2008_7M", "test", "test_x", "test_inner_x", "bar", "test_inner"};
}
template <typename NAMES_RETURN_TYPE, typename SESSION_TYPE>
void get_views(NAMES_RETURN_TYPE& names_return, SESSION_TYPE const& session) {
names_return = {"test_view", "cletus", "marge", "marge978", "marge1025"};
}
};
using RegexCommandsMockupContext =
MetaClientContext<RegexCommandsMockClient, CoreMockTransport>;
struct RegexCommandsContextOpsPolicy {
using ThriftServiceType = ThriftService;
using ContextType = RegexCommandsMockupContext;
};
template <typename CONTEXT_OP_POLICY>
class RegexCommandsMockupContextOperations {
public:
using ThriftService = typename CONTEXT_OP_POLICY::ThriftServiceType;
using ContextType = typename CONTEXT_OP_POLICY::ContextType;
};
// \u command Unit Tests
//
// See Issue #143, Public Repo for manual test cases replicated in this unit test
//
// Test cases:
//
// \u
// \u ^lisa
// \u ^lisa.+
// \u ^lisa.*
using ListUsersMockClient = RegexCommandsMockClient;
using ListUsersCommandMockupContext = RegexCommandsMockupContext;
using ListUsersCommandContextOpsPolicy = RegexCommandsContextOpsPolicy;
template <typename CONTEXT_OPS_POLICY_TYPE>
using ListUsersCommandMockupContextOperations =
RegexCommandsMockupContextOperations<CONTEXT_OPS_POLICY_TYPE>;
TEST(OmniSQLTest, ListUsersCommandTest_ListAll) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestListUsersCmd = ListUsersCmd<ListUsersCommandContextOpsPolicy,
ListUsersCommandMockupContextOperations,
RegexCmdDeterminant>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
// Run the test for \u
std::ostringstream test_capture_stream1;
ListUsersCommandMockupContext unit_test_context_list_all;
auto resolution1 =
CommandResolutionChain<>("\\u",
"\\u",
1,
1,
UnitTestListUsersCmd(unit_test_context_list_all),
test_capture_stream1)
.is_resolved();
EXPECT_TRUE(resolution1);
std::string output_back_to_input(test_capture_stream1.str());
auto extractedTokens = TokenExtractor().extract_tokens(output_back_to_input);
EXPECT_EQ(extractedTokens.size(), (decltype(extractedTokens)::size_type)7);
EXPECT_EQ(extractedTokens[0], "mapd");
EXPECT_EQ(extractedTokens[1], "homer");
EXPECT_EQ(extractedTokens[2], "marge");
EXPECT_EQ(extractedTokens[3], "bart");
EXPECT_EQ(extractedTokens[4], "lisa");
EXPECT_EQ(extractedTokens[5], "lisaSimpson");
EXPECT_EQ(extractedTokens[6], "sideshowbob");
}
TEST(OmniSQLTest, ListUsersCommandTest_OnlyLisa) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestListUsersCmd = ListUsersCmd<ListUsersCommandContextOpsPolicy,
ListUsersCommandMockupContextOperations,
RegexCmdDeterminant>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream2;
ListUsersCommandMockupContext unit_test_context_only_lisa;
auto resolution2 =
CommandResolutionChain<>("\\u ^lisa",
"\\u",
1,
1,
UnitTestListUsersCmd(unit_test_context_only_lisa),
test_capture_stream2)
.is_resolved();
EXPECT_TRUE(resolution2);
std::string output_back_to_input(test_capture_stream2.str());
auto extractedTokens = TokenExtractor().extract_tokens(output_back_to_input);
EXPECT_EQ(extractedTokens.size(), (decltype(extractedTokens)::size_type)1);
EXPECT_EQ(extractedTokens[0], "lisa");
}
TEST(OmniSQLTest, ListUsersCommandTest_StartsWithLisa) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestListUsersCmd = ListUsersCmd<ListUsersCommandContextOpsPolicy,
ListUsersCommandMockupContextOperations,
RegexCmdDeterminant>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream3;
ListUsersCommandMockupContext unit_test_context_starts_with_lisa;
auto resolution3 =
CommandResolutionChain<>("\\u ^lisa.*",
"\\u",
1,
1,
UnitTestListUsersCmd(unit_test_context_starts_with_lisa),
test_capture_stream3)
.is_resolved();
EXPECT_TRUE(resolution3);
std::string output_back_to_input(test_capture_stream3.str());
auto extractedTokens = TokenExtractor().extract_tokens(output_back_to_input);
EXPECT_EQ(extractedTokens.size(), (decltype(extractedTokens)::size_type)2);
EXPECT_EQ(extractedTokens[0], "lisa");
EXPECT_EQ(extractedTokens[1], "lisaSimpson");
}
// \t command Unit Tests
//
// See Issue #143, Public Repo for manual test cases replicated in this unit test
//
// Test cases:
//
// \t
// \t _x$
// \t .+_x$
// \t inner
// \t .+inner.+
using ListTablesMockClient = RegexCommandsMockClient;
using ListTablesCommandMockupContext = RegexCommandsMockupContext;
using ListTablesCommandContextOpsPolicy = RegexCommandsContextOpsPolicy;
template <typename CONTEXT_OPS_POLICY_TYPE>
using ListTablesCommandMockupContextOperations =
RegexCommandsMockupContextOperations<CONTEXT_OPS_POLICY_TYPE>;
TEST(OmniSQLTest, ListTablesCommandTest_ListAll) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestListTablesCmd = ListTablesCmd<ListTablesCommandContextOpsPolicy,
ListTablesCommandMockupContextOperations,
RegexCmdDeterminant>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream;
ListTablesCommandMockupContext unit_test_context;
auto resolution = CommandResolutionChain<>("\\t",
"\\t",
1,
1,
UnitTestListTablesCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
std::string output_back_to_input(test_capture_stream.str());
auto extractedTokens = TokenExtractor().extract_tokens(output_back_to_input);
using TokenCount = decltype(extractedTokens)::size_type;
EXPECT_EQ(extractedTokens.size(), (TokenCount)6);
EXPECT_EQ(extractedTokens[0], "flights_2008_7M");
EXPECT_EQ(extractedTokens[1], "test");
EXPECT_EQ(extractedTokens[2], "test_x");
EXPECT_EQ(extractedTokens[3], "test_inner_x");
EXPECT_EQ(extractedTokens[4], "bar");
EXPECT_EQ(extractedTokens[5], "test_inner");
}
TEST(OmniSQLTest, ListTablesCommandTest_EndsWithNoMatch) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestListTablesCmd = ListTablesCmd<ListTablesCommandContextOpsPolicy,
ListTablesCommandMockupContextOperations,
RegexCmdDeterminant>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream;
ListTablesCommandMockupContext unit_test_context;
auto resolution = CommandResolutionChain<>("\\t _x$",
"\\t",
1,
1,
UnitTestListTablesCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
std::string output_back_to_input(test_capture_stream.str());
auto extractedTokens = TokenExtractor().extract_tokens(output_back_to_input);
using TokenCount = decltype(extractedTokens)::size_type;
EXPECT_TRUE(extractedTokens.empty());
}
TEST(OmniSQLTest, ListTablesCommandTest_EndsWithMatch) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestListTablesCmd = ListTablesCmd<ListTablesCommandContextOpsPolicy,
ListTablesCommandMockupContextOperations,
RegexCmdDeterminant>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream;
ListTablesCommandMockupContext unit_test_context;
auto resolution = CommandResolutionChain<>("\\t .+_x$",
"\\t",
1,
1,
UnitTestListTablesCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
std::string output_back_to_input(test_capture_stream.str());
auto extractedTokens = TokenExtractor().extract_tokens(output_back_to_input);
using TokenCount = decltype(extractedTokens)::size_type;
EXPECT_EQ(extractedTokens.size(), (TokenCount)2);
EXPECT_EQ(extractedTokens[0], "test_x");
EXPECT_EQ(extractedTokens[1], "test_inner_x");
}
TEST(OmniSQLTest, ListTablesCommandTest_InnerNoMatch) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestListTablesCmd = ListTablesCmd<ListTablesCommandContextOpsPolicy,
ListTablesCommandMockupContextOperations,
RegexCmdDeterminant>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream;
ListTablesCommandMockupContext unit_test_context;
auto resolution = CommandResolutionChain<>("\\t inner",
"\\t",
1,
1,
UnitTestListTablesCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
std::string output_back_to_input(test_capture_stream.str());
auto extractedTokens = TokenExtractor().extract_tokens(output_back_to_input);
using TokenCount = decltype(extractedTokens)::size_type;
EXPECT_EQ(extractedTokens.size(), (TokenCount)0);
}
TEST(OmniSQLTest, ListTablesCommandTest_InnerMatch) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestListTablesCmd = ListTablesCmd<ListTablesCommandContextOpsPolicy,
ListTablesCommandMockupContextOperations,
RegexCmdDeterminant>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream;
ListTablesCommandMockupContext unit_test_context;
auto resolution = CommandResolutionChain<>("\\t .+inner.+",
"\\t",
1,
1,
UnitTestListTablesCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
std::string output_back_to_input(test_capture_stream.str());
auto extractedTokens = TokenExtractor().extract_tokens(output_back_to_input);
using TokenCount = decltype(extractedTokens)::size_type;
EXPECT_EQ(extractedTokens.size(), (TokenCount)1);
EXPECT_EQ(extractedTokens[0], "test_inner_x");
}
// \v command Unit Tests
//
// See Issue #143, Public Repo for manual test cases replicated in this unit test
//
// Test cases:
//
// \v
// \v .+\d{4}$
using ListViewsMockClient = RegexCommandsMockClient;
using ListViewsCommandMockupContext = RegexCommandsMockupContext;
using ListViewsCommandContextOpsPolicy = RegexCommandsContextOpsPolicy;
template <typename CONTEXT_OPS_POLICY_TYPE>
using ListViewsCommandMockupContextOperations =
RegexCommandsMockupContextOperations<CONTEXT_OPS_POLICY_TYPE>;
TEST(OmniSQLTest, ViewListCommandTest_ListAll) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestListViewsCmd = ListViewsCmd<ListViewsCommandContextOpsPolicy,
ListViewsCommandMockupContextOperations,
RegexCmdDeterminant>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream;
ListViewsCommandMockupContext unit_test_context;
auto resolution = CommandResolutionChain<>("\\v",
"\\v",
1,
1,
UnitTestListViewsCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
std::string output_back_to_input(test_capture_stream.str());
auto extractedTokens = TokenExtractor().extract_tokens(output_back_to_input);
using TokenCount = decltype(extractedTokens)::size_type;
EXPECT_EQ(extractedTokens.size(), (TokenCount)5);
EXPECT_EQ(extractedTokens[0], "test_view");
EXPECT_EQ(extractedTokens[1], "cletus");
EXPECT_EQ(extractedTokens[2], "marge");
EXPECT_EQ(extractedTokens[3], "marge978");
EXPECT_EQ(extractedTokens[4], "marge1025");
}
TEST(OmniSQLTest, ViewListCommandTest_EndsWith4Digits) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestListViewsCmd = ListViewsCmd<ListViewsCommandContextOpsPolicy,
ListViewsCommandMockupContextOperations,
RegexCmdDeterminant>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream;
ListViewsCommandMockupContext unit_test_context;
auto resolution = CommandResolutionChain<>("\\v .+\\d{4}$",
"\\v",
1,
1,
UnitTestListViewsCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
std::string output_back_to_input(test_capture_stream.str());
auto extractedTokens = TokenExtractor().extract_tokens(output_back_to_input);
using TokenCount = decltype(extractedTokens)::size_type;
EXPECT_EQ(extractedTokens.size(), (TokenCount)1);
EXPECT_EQ(extractedTokens[0], "marge1025");
}
//
// \\import_dashboard Command Unit Test and Support Mockups
//
using ImportDashboardCommandMockupContext =
MetaClientContext<CoreMockClient, CoreMockTransport>;
struct ImportDashboardCommandContextOpsPolicy {
using ThriftServiceType = ThriftService;
using ContextType = ImportDashboardCommandMockupContext;
};
template <typename CONTEXT_OP_POLICY>
class ImportDashboardCommandMockupContextOperations {
public:
using ThriftService = typename CONTEXT_OP_POLICY::ThriftServiceType;
using ContextType = typename CONTEXT_OP_POLICY::ContextType;
};
TEST(OmniSQLTest, ImportDashboardCommandTest_SimpleDashSimpleFilename) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestImportDashboardCmd =
ImportDashboardCmd<ImportDashboardCommandContextOpsPolicy,
ImportDashboardCommandMockupContextOperations>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream;
ImportDashboardCommandMockupContext unit_test_context;
auto resolution =
CommandResolutionChain<>(
"\\import_dashboard simpledash unlikely_file_to_ever_be_opened_1234.txt",
"\\import_dashboard",
3,
3,
UnitTestImportDashboardCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
EXPECT_EQ(unit_test_context.view_name, "simpledash");
EXPECT_EQ(unit_test_context.view_state, "");
EXPECT_EQ(unit_test_context.view_metadata, "");
// We are relying on the behavior of not being able to open the file to capture the
// filename passed in
std::string output_back_to_input(test_capture_stream.str());
auto extracted_tokens = TokenExtractor().extract_tokens(output_back_to_input);
using TokenCount = decltype(extracted_tokens)::size_type;
EXPECT_EQ(extracted_tokens.size(), 5u);
EXPECT_EQ(extracted_tokens[4], "`unlikely_file_to_ever_be_opened_1234.txt`");
}
TEST(OmniSQLTest, ImportDashboardCommandTest_SimpleDashComplexFilename) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestImportDashboardCmd =
ImportDashboardCmd<ImportDashboardCommandContextOpsPolicy,
ImportDashboardCommandMockupContextOperations>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream;
ImportDashboardCommandMockupContext unit_test_context;
auto resolution =
CommandResolutionChain<>(
"\\import_dashboard simpledash \"C:\\\\Windows is Terrible\\\\lol.txt\"",
"\\import_dashboard",
3,
3,
UnitTestImportDashboardCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
EXPECT_EQ(unit_test_context.view_name, "simpledash");
EXPECT_EQ(unit_test_context.view_state, "");
EXPECT_EQ(unit_test_context.view_metadata, "");
// We are relying on the behavior of not being able to open the file to capture the
// filename passed in
std::string output_back_to_input(test_capture_stream.str());
auto extracted_tokens = TokenExtractor().extract_tokens(output_back_to_input);
using TokenCount = decltype(extracted_tokens)::size_type;
EXPECT_EQ(extracted_tokens.size(),
7u); // Inflated because of spaces in standard output
EXPECT_EQ(extracted_tokens[4], "`C:\\Windows");
EXPECT_EQ(extracted_tokens[5], "is");
EXPECT_EQ(extracted_tokens[6], "Terrible\\lol.txt`");
}
TEST(OmniSQLTest, ImportDashboardCommandTest_ComplexDashSimpleFilename) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestImportDashboardCmd =
ImportDashboardCmd<ImportDashboardCommandContextOpsPolicy,
ImportDashboardCommandMockupContextOperations>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream;
ImportDashboardCommandMockupContext unit_test_context;
auto resolution =
CommandResolutionChain<>(
"\\import_dashboard \"\\\"Who uses spaces anyway?\\\"\" simpledash.txt",
"\\import_dashboard",
3,
3,
UnitTestImportDashboardCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
EXPECT_EQ(unit_test_context.view_name, "\"Who uses spaces anyway?\"");
EXPECT_EQ(unit_test_context.view_state, "");
EXPECT_EQ(unit_test_context.view_metadata, "");
// We are relying on the behavior of not being able to open the file to capture the
// filename passed in
std::string output_back_to_input(test_capture_stream.str());
auto extracted_tokens = TokenExtractor().extract_tokens(output_back_to_input);
using TokenCount = decltype(extracted_tokens)::size_type;
EXPECT_EQ(extracted_tokens.size(),
5u); // Inflated because of spaces in standard output
EXPECT_EQ(extracted_tokens[4], "`simpledash.txt`");
}
TEST(OmniSQLTest, ImportDashboardCommandTest_ComplexDashComplexFilename) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestImportDashboardCmd =
ImportDashboardCmd<ImportDashboardCommandContextOpsPolicy,
ImportDashboardCommandMockupContextOperations>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
std::ostringstream test_capture_stream;
ImportDashboardCommandMockupContext unit_test_context;
auto resolution = CommandResolutionChain<>(
"\\import_dashboard \"\\\"Who uses spaces anyway?\\\"\" "
"\"C:\\\\Windows is Terrible\\\\lol.txt\"",
"\\import_dashboard",
3,
3,
UnitTestImportDashboardCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
EXPECT_EQ(unit_test_context.view_name, "\"Who uses spaces anyway?\"");
EXPECT_EQ(unit_test_context.view_state, "");
EXPECT_EQ(unit_test_context.view_metadata, "");
// We are relying on the behavior of not being able to open the file to capture the
// filename passed in
std::string output_back_to_input(test_capture_stream.str());
auto extracted_tokens = TokenExtractor().extract_tokens(output_back_to_input);
using TokenCount = decltype(extracted_tokens)::size_type;
EXPECT_EQ(extracted_tokens.size(),
7u); // Inflated because of spaces in standard output
EXPECT_EQ(extracted_tokens[4], "`C:\\Windows");
EXPECT_EQ(extracted_tokens[5], "is");
EXPECT_EQ(extracted_tokens[6], "Terrible\\lol.txt`");
}
//
// \\export_dashboard Command Unit Test and Support Mockups
//
using ExportDashboardCommandMockupContext =
MetaClientContext<CoreMockClient, CoreMockTransport>;
struct ExportDashboardCommandContextOpsPolicy {
using ThriftServiceType = ThriftService;
using ContextType = ExportDashboardCommandMockupContext;
};
template <typename CONTEXT_OP_POLICY>
class ExportDashboardCommandMockupContextOperations {
public:
using ThriftService = typename CONTEXT_OP_POLICY::ThriftServiceType;
using ContextType = typename CONTEXT_OP_POLICY::ContextType;
};
TEST(OmniSQLTest, ExportDashboardCommandTest_SimpleDashSimpleFilename) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestExportDashboardCmd =
ExportDashboardCmd<ExportDashboardCommandContextOpsPolicy,
ExportDashboardCommandMockupContextOperations>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
// Create a directory to force file open to fail
static const char* test_filename = "export_unlikely_file_to_ever_be_opened_1234.txt";
std::string fake_input = std::string("\\export_dashboard simpledash ") + test_filename;
#ifdef _WIN32
auto result = mkdir(test_filename);
#else
auto result = mkdir(test_filename, 0700);
#endif
EXPECT_EQ(result, 0);
std::ostringstream test_capture_stream;
ExportDashboardCommandMockupContext unit_test_context;
auto resolution =
CommandResolutionChain<>(fake_input.c_str(),
"\\export_dashboard",
3,
4,
UnitTestExportDashboardCmd(unit_test_context),
test_capture_stream)
.is_resolved();
result = ::rmdir(test_filename);
EXPECT_EQ(result, 0);
EXPECT_TRUE(resolution);
EXPECT_EQ(unit_test_context.view_name, "simpledash");
EXPECT_EQ(unit_test_context.view_state, "");
EXPECT_EQ(unit_test_context.view_metadata, "");
EXPECT_EQ(unit_test_context.dashboard_owner, "");
// The file name is not in the output
EXPECT_EQ(std::strstr(test_capture_stream.str().c_str(), test_filename), nullptr);
unlink(test_filename);
}
TEST(OmniSQLTest, ExportDashboardCommandTest_SimpleDashComplexFilename) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestExportDashboardCmd =
ExportDashboardCmd<ExportDashboardCommandContextOpsPolicy,
ExportDashboardCommandMockupContextOperations>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
static const char* test_filename = "Windows is Terrible.txt";
std::string fake_input =
std::string("\\export_dashboard simpledash \"") + test_filename + "\"";
std::ostringstream test_capture_stream;
ExportDashboardCommandMockupContext unit_test_context;
auto resolution =
CommandResolutionChain<>(fake_input.c_str(),
"\\export_dashboard",
3,
4,
UnitTestExportDashboardCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
EXPECT_EQ(unit_test_context.view_name, "simpledash");
EXPECT_EQ(unit_test_context.view_state, "");
EXPECT_EQ(unit_test_context.view_metadata, "");
EXPECT_EQ(unit_test_context.dashboard_owner, "");
EXPECT_EQ(std::strstr(test_capture_stream.str().c_str(), test_filename), nullptr);
unlink(test_filename);
}
TEST(OmniSQLTest, ExportDashboardCommandTest_ComplexDashSimpleFilename) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestExportDashboardCmd =
ExportDashboardCmd<ExportDashboardCommandContextOpsPolicy,
ExportDashboardCommandMockupContextOperations>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
static const char* test_filename = "export_simpledash.txt";
std::string fake_input =
std::string("\\export_dashboard \"\\\"Who uses spaces anyway?\\\"\" ") +
test_filename;
std::ostringstream test_capture_stream;
ExportDashboardCommandMockupContext unit_test_context;
auto resolution =
CommandResolutionChain<>(fake_input.c_str(),
"\\export_dashboard",
3,
4,
UnitTestExportDashboardCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
EXPECT_EQ(unit_test_context.view_name, "\"Who uses spaces anyway?\"");
EXPECT_EQ(unit_test_context.view_state, "");
EXPECT_EQ(unit_test_context.view_metadata, "");
EXPECT_EQ(unit_test_context.dashboard_owner, "");
EXPECT_EQ(std::strstr(test_capture_stream.str().c_str(), test_filename), nullptr);
unlink(test_filename);
}
TEST(OmniSQLTest, ExportDashboardCommandTest_ComplexDashComplexFilename) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestExportDashboardCmd =
ExportDashboardCmd<ExportDashboardCommandContextOpsPolicy,
ExportDashboardCommandMockupContextOperations>;
using TokenExtractor = UnitTestOutputTokenizer<std::vector<std::string>>;
static const char* test_filename = "Windows is Terrible.txt";
std::string fake_input =
std::string("\\export_dashboard \"\\\"Who uses spaces anyway?\\\"\" \"") +
test_filename + "\"";
std::ostringstream test_capture_stream;
ExportDashboardCommandMockupContext unit_test_context;
auto resolution =
CommandResolutionChain<>(fake_input.c_str(),
"\\export_dashboard",
3,
4,
UnitTestExportDashboardCmd(unit_test_context),
test_capture_stream)
.is_resolved();
EXPECT_TRUE(resolution);
EXPECT_EQ(unit_test_context.view_name, "\"Who uses spaces anyway?\"");
EXPECT_EQ(unit_test_context.view_state, "");
EXPECT_EQ(unit_test_context.view_metadata, "");
EXPECT_EQ(unit_test_context.dashboard_owner, "");
EXPECT_EQ(std::strstr(test_capture_stream.str().c_str(), test_filename), nullptr);
unlink(test_filename);
}
TEST(OmniSQLTest, ExportDashboardCommandTest_ComplexDashComplexFilenameWithOwnerName) {
using Params = CommandResolutionChain<>::CommandTokenList;
using UnitTestExportDashboardCmd =
ExportDashboardCmd<ExportDashboardCommandContextOpsPolicy,
ExportDashboardCommandMockupContextOperations>;