165af7d91SIrynaMironavaimport math
265af7d91SIrynaMironavafrom membase.api.rest_client import RestConnection
365af7d91SIrynaMironavafrom tuqquery.tuq import QueryTests
465af7d91SIrynaMironava
565af7d91SIrynaMironava
665af7d91SIrynaMironavaclass StatsTests(QueryTests):
765af7d91SIrynaMironava    def setUp(self):
865af7d91SIrynaMironava        super(StatsTests, self).setUp()
965af7d91SIrynaMironava        server = self.master
1065af7d91SIrynaMironava        if self.input.tuq_client and "client" in self.input.tuq_client:
1165af7d91SIrynaMironava            server = self.tuq_client
1265af7d91SIrynaMironava        self.rest = RestConnection(server)
1365af7d91SIrynaMironava
1465af7d91SIrynaMironava    def suite_setUp(self):
1565af7d91SIrynaMironava        super(StatsTests, self).suite_setUp()
1665af7d91SIrynaMironava
1765af7d91SIrynaMironava    def tearDown(self):
1865af7d91SIrynaMironava        super(StatsTests, self).tearDown()
1965af7d91SIrynaMironava
2065af7d91SIrynaMironava    def suite_tearDown(self):
2165af7d91SIrynaMironava        super(StatsTests, self).suite_tearDown()
2265af7d91SIrynaMironava
2365af7d91SIrynaMironava    def test_cmd_line(self):
2465af7d91SIrynaMironava        stats = self.rest.query_tool_stats()
2565af7d91SIrynaMironava        self.assertTrue(stats['cmdline'][0].find('cbq-engine') != -1, 'command line is incorrect')
2665af7d91SIrynaMironava        self.log.info('cmd line is checked')
2765af7d91SIrynaMironava
2865af7d91SIrynaMironava    def test_requests_select(self):
2965af7d91SIrynaMironava        stats = self.rest.query_tool_stats()
3065af7d91SIrynaMironava        for bucket in self.buckets:
3165af7d91SIrynaMironava            self.query = "SELECT name, CASE WHEN join_mo < 3 OR join_mo > 11 THEN" +\
3265af7d91SIrynaMironava            " 'winter' ELSE 'other' END AS period FROM %s WHERE CASE WHEN" % (bucket.name) +\
3365af7d91SIrynaMironava            " join_mo < 3 OR join_mo > 11 THEN 'winter' ELSE 'other' END LIKE 'win%'"
3465af7d91SIrynaMironava            self.run_cbq_query()
3565af7d91SIrynaMironava        new_stats = self.rest.query_tool_stats()
3665af7d91SIrynaMironava        self.assertTrue(new_stats['requests.Count'] == stats['requests.Count']+len(self.buckets), 'Request were not increased')
3765af7d91SIrynaMironava        self.assertTrue(new_stats['selects.Count'] == stats['selects.Count']+len(self.buckets), 'Selects count were not increased')
3865af7d91SIrynaMironava        self.log.info('select count is checked')
3965af7d91SIrynaMironava
4065af7d91SIrynaMironava    def test_errors(self):
4165af7d91SIrynaMironava        stats = self.rest.query_tool_stats()
4265af7d91SIrynaMironava        for bucket in self.buckets:
4365af7d91SIrynaMironava            self.query = "SELECT ALL FROM %s" % (bucket.name)
4465af7d91SIrynaMironava            try:
4565af7d91SIrynaMironava                self.run_cbq_query()
4665af7d91SIrynaMironava            except:
4765af7d91SIrynaMironava                pass
4865af7d91SIrynaMironava        new_stats = self.rest.query_tool_stats()
4965af7d91SIrynaMironava        self.assertTrue(new_stats['requests.Count'] == stats['requests.Count']+len(self.buckets), 'Request were not increased')
5065af7d91SIrynaMironava        self.assertTrue(new_stats['selects.Count'] == stats['selects.Count']+len(self.buckets), 'Selects count were not increased')
5165af7d91SIrynaMironava        self.assertTrue(new_stats['errors.Count'] == stats['errors.Count']+len(self.buckets), 'Selects count were not increased')
5265af7d91SIrynaMironava        self.log.info('errors count is checked')
5365af7d91SIrynaMironava
5465af7d91SIrynaMironava    def test_requests_insert(self):
5565af7d91SIrynaMironava        stats = self.rest.query_tool_stats()
5665af7d91SIrynaMironava        for bucket in self.buckets:
5765af7d91SIrynaMironava            self.query = 'INSERT into %s key "%s" VALUES %s' % (bucket.name, 'key', 'value')
5865af7d91SIrynaMironava            self.run_cbq_query()
5965af7d91SIrynaMironava        new_stats = self.rest.query_tool_stats()
6065af7d91SIrynaMironava        self.assertTrue(new_stats['requests.Count'] == stats['requests.Count']+len(self.buckets), 'Request were not increased')
6165af7d91SIrynaMironava        self.assertTrue(new_stats['inserts.Count'] == stats['inserts.Count']+len(self.buckets), 'Inserts count were not increased')
6265af7d91SIrynaMironava        self.log.info('insert count is checked')
6365af7d91SIrynaMironava
6465af7d91SIrynaMironava    def test_requests_update(self):
6565af7d91SIrynaMironava        stats = self.rest.query_tool_stats()
6665af7d91SIrynaMironava        for bucket in self.buckets:
6765af7d91SIrynaMironava            self.query = 'INSERT into %s key "%s" VALUES %s' % (bucket.name, 'key', 'value')
6865af7d91SIrynaMironava            self.query = "update %s use keys ['%s'] set name='new'" % (bucket.name, 'key')
6965af7d91SIrynaMironava            self.run_cbq_query()
7065af7d91SIrynaMironava        new_stats = self.rest.query_tool_stats()
7165af7d91SIrynaMironava        self.assertTrue(new_stats['requests.Count'] == stats['requests.Count']+len(self.buckets), 'Request were not increased')
7265af7d91SIrynaMironava        self.assertTrue(new_stats['updates.Count'] == stats['updates.Count']+len(self.buckets), 'Updates count were not increased')
7365af7d91SIrynaMironava        self.log.info('update count is checked')
7465af7d91SIrynaMironava
7565af7d91SIrynaMironava    def test_requests_delete(self):
7665af7d91SIrynaMironava        stats = self.rest.query_tool_stats()
7765af7d91SIrynaMironava        for bucket in self.buckets:
7865af7d91SIrynaMironava            self.query = 'INSERT into %s key "%s" VALUES %s' % (bucket.name, 'key', 'value')
7965af7d91SIrynaMironava            self.query = "delete from %s use keys ['%s']" % (bucket.name, 'key')
8065af7d91SIrynaMironava            self.run_cbq_query()
8165af7d91SIrynaMironava        new_stats = self.rest.query_tool_stats()
8265af7d91SIrynaMironava        self.assertTrue(new_stats['requests.Count'] == stats['requests.Count']+len(self.buckets), 'Request were not increased')
8365af7d91SIrynaMironava        self.assertTrue(new_stats['deletes.Count'] == stats['deletes.Count']+len(self.buckets), 'Deletes count were not increased')
8465af7d91SIrynaMironava        self.log.info('delete count is checked')
8565af7d91SIrynaMironava
8665af7d91SIrynaMironava