1from membase.api.rest_client import RestConnection
2from remote.remote_util import RemoteMachineShellConnection
3from .tuq import QueryTests
4
5
6class QueriesIndexTestsExtended(QueryTests):
7
8    FIELDS_TO_INDEX = [('name', 'job_title'), ('name', 'join_yr'), ('VMs', 'name')]
9    COMPLEX_FIELDS_TO_INDEX = ['VMs', 'tasks_points', 'skills']
10
11    def setUp(self):
12        super(QueriesIndexTestsExtended, self).setUp()
13        self.log.info("==============  QueriesIndexTests setup has started ==============")
14        self.num_indexes = self.input.param('num_indexes', 1)
15        if self.num_indexes > len(self.FIELDS_TO_INDEX):
16            self.input.test_params["stop-on-failure"] = True
17            self.log.error("MAX NUMBER OF INDEXES IS 3. ALL TESTS WILL BE SKIPPED")
18            self.fail('MAX NUMBER OF INDEXES IS 3. ALL TESTS WILL BE SKIPPED')
19        self.rest = RestConnection(self.master)
20        self.shell = RemoteMachineShellConnection(self.master)
21        self.delete_sample = self.input.param("delete_sample", False)
22        self.log.info("==============  QueriesIndexTests setup has completed ==============")
23        self.log_config_info()
24        self.query_buckets = self.get_query_buckets(check_all_buckets=True)
25
26    def suite_setUp(self):
27        super(QueriesIndexTestsExtended, self).suite_setUp()
28        self.log.info("==============  QueriesIndexTests suite_setup has started ==============")
29        self.log.info("==============  QueriesIndexTests suite_setup has completed ==============")
30
31    def tearDown(self):
32        self.log.info("==============  QueriesIndexTests tearDown has started ==============")
33        self.log.info("==============  QueriesIndexTests tearDown has completed ==============")
34        super(QueriesIndexTestsExtended, self).tearDown()
35
36    def suite_tearDown(self):
37        self.log.info("==============  QueriesIndexTests suite_tearDown has started ==============")
38        self.log.info("==============  QueriesIndexTests suite_tearDown has completed ==============")
39        super(QueriesIndexTestsExtended, self).suite_tearDown()
40
41    def test_meta_indexcountscan(self):
42        test_dict = dict()
43        index_type = self.index_type.lower()
44        self.fail_if_no_buckets()
45        for bucket, query_bucket in zip(self.buckets, self.query_buckets):
46            query_bucket = self.get_collection_name(bucket.name)
47            bname = bucket.name
48
49            primary_idx = {'name': '#primary', 'bucket': bname, 'fields': [], 'state': 'online', 'using': index_type, 'is_primary': True}
50            idx_1 = {'name': "idx", 'bucket': bname, 'fields': [("name", 0)], 'state': "online", 'using': index_type}
51            idx_2 = {'name': "idx2", 'bucket': bname, 'fields': [("join_mo", 0)], 'state': "online", 'using': index_type}
52            idx_3 = {'name': "idx3", 'bucket': bname, 'fields': [("VMs[0].memory", 0)], 'state': "online", 'using': index_type}
53            idx_4 = {'name': "idx", 'bucket': bname, 'fields': [("name", 0), ("meta().id", 1)], 'state': "online", 'using': index_type}
54            idx_5 = {'name': "idx", 'bucket': bname, 'fields': [("meta().id", 0)], 'state': "online", 'using': index_type}
55            idx_6 = {'name': "idx", 'bucket': bname, 'fields': [("join_yr", 0)], 'state': "online", 'using': index_type, 'where': 'join_mo = 12'}
56            idx_7 = {'name': "idx", 'bucket': bname, 'fields': [("join_day", 0)], 'state': "online", 'using': index_type, 'where': 'join_yr < 2012'}
57            idx_8 = {'name': "idx", 'bucket': bname, 'fields': [("name", 0), ("join_day", 1), ("join_yr", 2)], 'state': "online", 'using': index_type}
58
59            query_1 = 'explain select count(1) from {0} where name = employee-23 and meta().id = "query-testemployee10317.900449741801-0"'.format(query_bucket)
60            explain_1 = lambda x: self.ExplainPlanHelper(x['q_res'][0])
61            assert_1 = lambda x: self.assertTrue("IndexCountScan2" not in x['post_q_res'][0])
62            test_dict["%s-01" % (bname)] = {"indexes": [primary_idx, idx_1, idx_2, idx_3], "queries": [query_1], "post_queries": [explain_1], "asserts": [assert_1]}
63
64            query_2 = 'select count(1) from {0} where name = "employee-23" and meta().id = "query-testemployee10317.900449741801-0"'.format(query_bucket)
65            assert_2 = lambda x: self.assertEqual(x['q_res'][0]['results'], ([{'$1': 1}]))
66            test_dict["%s-02" % (bname)] = {"indexes": [primary_idx, idx_1, idx_2, idx_3], "queries": [query_2], "asserts": [assert_2]}
67
68            query_3 = 'explain select name from {0} where name = "employee-23" and meta().id like "query-testemployee%" limit 3'.format(query_bucket)
69            assert_3 = lambda x: self.assertTrue("limit" not in x['post_q_res'][0]['~children'][0])
70            test_dict["%s-03" % (bname)] = {"indexes": [primary_idx, idx_1, idx_2, idx_3], "queries": [query_3], "post_queries": [explain_1],  "asserts": [assert_3]}
71
72            query_4 = 'select name from {0} where name = "employee-23" and meta().id like "query-testemployee%" limit 3'.format(query_bucket)
73            assert_4 = lambda x: self.assertEqual(x['q_res'][0]['results'], [{'name': 'employee-23'}, {'name': 'employee-23'}, {'name': 'employee-23'}])
74            test_dict["%s-04" % (bname)] = {"indexes": [primary_idx, idx_1, idx_2, idx_3], "queries": [query_4], "asserts": [assert_4]}
75
76            query_5 = 'explain select min(join_day) from {0} where VMs[0].memory=12 and meta().id = "query-testemployee10317.900449741801-0"'.format(query_bucket)
77            assert_5 = lambda x: self.assertTrue("limit" not in x['post_q_res'][0]['~children'][0])
78            test_dict["%s-05" % (bname)] = {"indexes": [primary_idx, idx_1, idx_2, idx_3], "queries": [query_5], "post_queries": [explain_1],"asserts": [assert_5]}
79
80            query_6 = 'select min(VMs[0].memory) from {0} where join_mo=12 and meta().id = "query-testemployee10317.900449741801-0"'.format(query_bucket)
81            assert_6 = lambda x: self.assertEqual(x['q_res'][0]['results'], [{'$1': 12}])
82            test_dict["%s-06" % (bname)] = {"indexes": [primary_idx, idx_1, idx_2, idx_3], "queries": [query_6], "asserts": [assert_6]}
83
84            query_7 = 'select min(join_mo) from {0} where  VMs[0].memory=12 and meta().id = "query-testemployee10317.900449741801-0"'.format(query_bucket)
85            assert_7 = lambda x: self.assertEqual(x['q_res'][0]['results'], [{'$1': 12}])
86            test_dict["%s-07" % (bname)] = {"indexes": [primary_idx, idx_1, idx_2, idx_3], "queries": [query_7], "asserts": [assert_7]}
87
88            query_8 = 'explain select count(1) from {0} where name = "employee-23"'.format(query_bucket)
89            assert_8 = lambda x: self.assertEqual(x['post_q_res'][0]['~children'][0]['#operator'], "IndexScan3")
90            test_dict["%s-08" % (bname)] = {"indexes": [primary_idx, idx_1, idx_2, idx_3], "queries": [query_8], "post_queries": [explain_1], "asserts": [assert_8]}
91
92            query_9 = 'select count(1) from {0} where name = "employee-23"'.format(query_bucket)
93            assert_9 = lambda x: self.assertEqual(x['q_res'][0]['results'], [{'$1': 432}])
94            test_dict["%s-09" % (bname)] = {"indexes": [primary_idx, idx_1, idx_2, idx_3], "queries": [query_9], "asserts": [assert_9]}
95
96            query_10 = 'explain select count(1) from {0} where name = "employee-23" and join_yr=2010'.format(query_bucket)
97            assert_10 = lambda x: self.assertEqual(x['post_q_res'][0]['~children'][0]['#operator'], "IndexScan3")
98            test_dict["%s-10" % (bname)] = {"indexes": [primary_idx, idx_1, idx_2, idx_3], "queries": [query_10], "post_queries": [explain_1], "asserts": [assert_10]}
99
100            query_11 = 'select count(1) from {0} where name = "employee-23" and join_mo=12'.format(query_bucket)
101            assert_11 = lambda x: self.assertEqual(x['q_res'][0]['results'], [{ "$1": 36}])
102            test_dict["%s-11" % (bname)] = {"indexes": [primary_idx, idx_1, idx_2, idx_3], "queries": [query_11], "asserts": [assert_11]}
103
104            query_12 = 'explain select count(1) from {0} where name = "employee-23" and meta().id = "query-testemployee10317.900449741801-0"'.format(query_bucket)
105            assert_12 = lambda x: self.assertEqual(x['post_q_res'][0]['~children'][0]['#operator'], "IndexScan3")
106            test_dict["%s-12" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_4], "queries": [query_12], "post_queries": [explain_1], "asserts": [assert_12]}
107
108            query_13 = 'select count(1) from {0} where name = "employee-23" and meta().id = "query-testemployee10317.900449741801-0"'.format(query_bucket)
109            assert_13 = lambda x: self.assertEqual(x['q_res'][0]['results'], ([{'$1': 1}]))
110            test_dict["%s-13" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_4], "queries": [query_13], "asserts": [assert_13]}
111
112            query_14 = 'explain select count(1) from {0} where meta().id = "query-testemployee10317.900449741801-0"'.format(query_bucket)
113            assert_14 = lambda x: self.assertEqual(x['post_q_res'][0]['~children'][0]['#operator'], "IndexScan3")
114            test_dict["%s-14" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_5], "queries": [query_14], "post_queries": [explain_1], "asserts": [assert_14]}
115
116            query_15 = 'select count(1) from {0} where meta().id = "query-testemployee10317.900449741801-0"'.format(query_bucket)
117            assert_15 = lambda x: self.assertEqual(x['q_res'][0]['results'], [{'$1': 1}])
118            test_dict["%s-15" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_5], "queries": [query_15], "asserts": [assert_15]}
119
120            query_16 = 'explain select count(1) from {0} where meta().id = "query-testemployee10317.900449741801-0"'.format(query_bucket)
121            assert_16 = lambda x: self.assertEqual(x['post_q_res'][0]['~children'][0]['#operator'], "IndexScan3")
122            test_dict["%s-16" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3], "queries": [query_16], "post_queries": [explain_1], "asserts": [assert_16]}
123
124            query_17 = 'select count(1) from {0} where meta().id = "query-testemployee10317.900449741801-0"'.format(query_bucket)
125            assert_17 = lambda x: self.assertEqual(x['q_res'][0]['results'], [{'$1': 1}])
126            test_dict["%s-17" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3], "queries": [query_17], "asserts": [assert_17]}
127
128            query_18 = 'explain select count(*) from {0} where join_yr>2010 and join_mo = 12'.format(query_bucket)
129            assert_18 = lambda x: self.assertEqual(x['post_q_res'][0]['~children'][0]['#operator'], "IndexScan3")
130            test_dict["%s-18" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_6], "queries": [query_18], "post_queries": [explain_1], "asserts": [assert_18]}
131
132            query_19 = 'select count(*) from {0} where join_yr>2010 and join_mo = 12'.format(query_bucket)
133            assert_19 = lambda x: self.assertEqual(x['q_res'][0]['results'], [{'$1': 504}])
134            test_dict["%s-19" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_6], "queries": [query_19], "asserts": [assert_19]}
135
136            query_20 = 'explain select count(*) from {0} where join_day = 23 and join_yr<2010'.format(query_bucket)
137            assert_20 = lambda x: self.assertEqual(x['post_q_res'][0]['~children'][0]['#operator'], "IndexScan3")
138            test_dict["%s-20" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_7], "queries": [query_20], "post_queries": [explain_1], "asserts": [assert_20]}
139
140            query_21 = 'select count(*) from {0} where join_day = 23 and join_yr<2011'.format(query_bucket)
141            assert_21 = lambda x: self.assertEqual(x['q_res'][0]['results'], [{'$1': 216}])
142            test_dict["%s-21" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_7], "queries": [query_21], "asserts": [assert_21]}
143
144            query_22 = 'explain select count(*) from {0} where join_day = 23 and join_yr<2012 limit 10'.format(query_bucket)
145            assert_22 = lambda x: self.assertTrue("limit" not in x['post_q_res'][0]['~children'][0])
146            test_dict["%s-22" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_7], "queries": [query_22], "post_queries": [explain_1], "asserts": [assert_22]}
147
148            query_23 = 'explain select count(1) from {0} where name = "employee-23" and join_day = 23'.format(query_bucket)
149            assert_23 = lambda x: self.assertTrue(x['post_q_res'][0]['~children'][0]['#operator'] != "IndexCountScan2")
150            test_dict["%s-23" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_8], "queries": [query_23], "post_queries": [explain_1], "asserts": [assert_23]}
151
152            query_24 = 'explain select count(1) from {0} where name = "employee-23" and join_yr = 2010'.format(query_bucket)
153            assert_24 = lambda x: self.assertTrue(x['post_q_res'][0]['~children'][0]['#operator'] != "IndexCountScan2")
154            test_dict["%s-24" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_8], "queries": [query_24], "post_queries": [explain_1], "asserts": [assert_24]}
155
156            query_25 = 'explain select tasks from {0} where name = "employee-23" and join_day=23 limit 5'.format(query_bucket)
157            assert_25 = lambda x: self.assertTrue("limit" in x['post_q_res'][0]['~children'][0]['~children'][0])
158            test_dict["%s-25" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_8], "queries": [query_25], "post_queries": [explain_1], "asserts": [assert_25]}
159
160            query_26 = 'explain select join_day from {0} where name = "employee-23" and join_yr = 2010 limit 5'.format(query_bucket)
161            assert_26 = lambda x: self.assertTrue("limit" not in x['post_q_res'][0]['~children'][0])
162            test_dict["%s-26" % (bname)] = {"indexes": [primary_idx, idx_2, idx_3, idx_8], "queries": [query_26], "post_queries": [explain_1], "asserts": [assert_26]}
163        self.query_runner(test_dict)
164