<commit_before>#
# python_mldb_interface_test.py
# Mich, 2016-01-25
# This file is part of MLDB. Copyright 2016 Datacratic. All rights reserved.
#
import unittest

if False:
    mldb_wrapper = None

mldb = mldb_wrapper.wrap(mldb) # noqa


class PythonMldbInterfaceTest(unittest.TestCase):

    def test_log(self):
        mldb.log("Testing log")

    def test_get(self):
        res = mldb.get('/ping')
        mldb.log(res)
        mldb.log(res.text)
        mldb.log(type(res))
        mldb.log(dir(res))

        with self.assertRaises(mldb_wrapper.ResponseException):
            mldb.get("/unexisting")

    def test_put(self):
        mldb.put("/v1/datasets/test_put", {
            'type' : 'sparse.mutable'
        })

    def test_post(self):
        res = mldb.post("/v1/datasets", {
            'type' : 'sparse.mutable'
        })
        id_ = res.json()['id']
        url = '/v1/datasets/{}'.format(id_)

        mldb.post(url + '/rows', {
            'rowName' : 'row1',
            'columns' : [['colA', 1, 0]]
        })
        mldb.post(url + '/commit')

    def test_delete(self):
        # this test depends on put and post
        url = '/v1/datasets/ds'
        mldb.put(url, {
            'type' : 'sparse.mutable'
        })
        mldb.post(url + '/commit')
        mldb.delete(url)

    def test_advanced_get_parameters(self):
        url = '/v1/datasets/ds'
        mldb.put(url, {
            'type' : 'sparse.mutable'
        })
        mldb.post(url + '/rows', {
            'rowName' : 'row1',
            'columns' : [['colA', 1, 0]]
        })
        mldb.post(url + '/commit')

        res = mldb.get(url).json()
        self.assertEqual(res['status']['columnCount'], 1)
        self.assertEqual(res['status']['rowCount'], 1)

        res = mldb.get('/v1/query', q='SELECT * FROM ds').json()
        self.assertEqual(len(res), 1)
        self.assertEqual(res[0]['rowName'], 'row1')
        self.assertEqual(res[0]['columns'],
                         [['colA', 1, '1970-01-01T00:00:00Z']])

    def test_query(self):
        url = '/v1/datasets/ds'
        mldb.put(url, {
            'type' : 'sparse.mutable'
        })
        mldb.post(url + '/rows', {
            'rowName' : 'row1',
            'columns' : [['colA', 1, 0]]
        })
        mldb.post(url + '/commit')

        res = mldb.query('SELECT * FROM ds').json()
        self.assertEqual(res, [['_rowName', u'colA'], ['row1', 1]])

if __name__ == '__main__':
    mldb.run_tests()<commit_msg>MLDB-1295 added ResponseExeption test<commit_after>#
# python_mldb_interface_test.py
# Mich, 2016-01-25
# This file is part of MLDB. Copyright 2016 Datacratic. All rights reserved.
#
import unittest

if False:
    mldb_wrapper = None

mldb = mldb_wrapper.wrap(mldb) # noqa


class PythonMldbInterfaceTest(unittest.TestCase):

    def test_log(self):
        mldb.log("Testing log")

    def test_get(self):
        mldb.get('/ping')
        with self.assertRaises(mldb_wrapper.ResponseException):
            mldb.get("/unexisting")

    def test_put(self):
        mldb.put("/v1/datasets/test_put", {
            'type' : 'sparse.mutable'
        })

    def test_post(self):
        res = mldb.post("/v1/datasets", {
            'type' : 'sparse.mutable'
        })
        id_ = res.json()['id']
        url = '/v1/datasets/{}'.format(id_)

        mldb.post(url + '/rows', {
            'rowName' : 'row1',
            'columns' : [['colA', 1, 0]]
        })
        mldb.post(url + '/commit')

    def test_delete(self):
        # this test depends on put and post
        url = '/v1/datasets/ds'
        mldb.put(url, {
            'type' : 'sparse.mutable'
        })
        mldb.post(url + '/commit')
        mldb.delete(url)

    def test_advanced_get_parameters(self):
        url = '/v1/datasets/ds'
        mldb.put(url, {
            'type' : 'sparse.mutable'
        })
        mldb.post(url + '/rows', {
            'rowName' : 'row1',
            'columns' : [['colA', 1, 0]]
        })
        mldb.post(url + '/commit')

        res = mldb.get(url).json()
        self.assertEqual(res['status']['columnCount'], 1)
        self.assertEqual(res['status']['rowCount'], 1)

        res = mldb.get('/v1/query', q='SELECT * FROM ds').json()
        self.assertEqual(len(res), 1)
        self.assertEqual(res[0]['rowName'], 'row1')
        self.assertEqual(res[0]['columns'],
                         [['colA', 1, '1970-01-01T00:00:00Z']])

    def test_query(self):
        url = '/v1/datasets/ds'
        mldb.put(url, {
            'type' : 'sparse.mutable'
        })
        mldb.post(url + '/rows', {
            'rowName' : 'row1',
            'columns' : [['colA', 1, 0]]
        })
        mldb.post(url + '/commit')

        res = mldb.query('SELECT * FROM ds').json()
        self.assertEqual(res, [['_rowName', u'colA'], ['row1', 1]])

    def test_response_exception(self):
        url = "/unexisting"
        try:
            mldb.get(url)
        except mldb_wrapper.ResponseException as response_exception:
            pass

        res = response_exception.response
        self.assertEqual(res.status_code, 404)
        self.assertEqual(res.url, url)

if __name__ == '__main__':
    mldb.run_tests()
{'fraction_non_alphanumeric': 0.16733677962856597, 'fraction_numerical': 0.02086923224200651, 'mean_word_length': 1.9547511312217194, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1869710', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1827, 'n_words': 314}