<commit_before>#! /usr/bin/env python
"""
Copyright (C) 2014 CompleteDB LLC.

This program is free software: you can redistribute it and/or modify
it under the terms of the Apache License Version 2.0 http://www.apache.org/licenses.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

"""

import unittest
import time
from pubsubsql import Client

class TestClient(unittest.TestCase):
    """MAKE SURE TO RUN PUBSUBSQL SERVER!"""

    def __ADDRESS(self):
        return "localhost:7777"

    def __generateTableName(self):
        return "T" + str(int(round(time.time() * 1000)))

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testConnectDisconnect(self):
        client = Client()
        #
        client.connect(self.__ADDRESS())
        self.assertTrue(client.isConnected())
        client.disconnect()
        #
        with self.assertRaises(Exception):
            client.connect("addresswithnoport")
        self.assertFalse(client.isConnected())
        client.disconnect()
        #
        with self.assertRaises(Exception):
            client.connect("addresswithnoport:")
        self.assertFalse(client.isConnected())
        client.disconnect()
        #
        with self.assertRaises(Exception):
            client.connect("localhost:7778")
        self.assertFalse(client.isConnected())
        client.disconnect()

    def testExecuteStatus(self):
        client = Client()
        client.connect(self.__ADDRESS())
        #
        client.execute("status")
        self.assertEqual("status", client.getAction())
        client.disconnect()

    def testExecuteInvalidCommand(self):
        client = Client()
        client.connect(self.__ADDRESS())
        #
        with self.assertRaises(Exception):
            client.execute("blablabla")

    def testInsertOneRow(self):
        client = Client()
        client.connect(self.__ADDRESS())
        tableName = self.__generateTableName()
        command = "insert into {} (col1, col2, col3) values (1:col1, 1:col2, 1:col3) returning *".format(tableName)
        client.execute(command)
        self.assertEqual("insert", client.getAction())
        self.assertEqual(1, client.getRowCount())
        self.assertTrue(client.nextRow())
        self.assertNotEqual("", client.getValue("id"))
        self.assertEqual("1:col1", client.getValue("col1"))
        self.assertEqual("1:col2", client.getValue("col2"))
        self.assertEqual("1:col3", client.getValue("col3"))
        self.assertEqual(4, client.getColumnCount())
        self.assertFalse(client.nextRow())
        client.disconnect()

if __name__ == "__main__":
    unittest.main()<commit_msg>add testInsertManyRow<commit_after>#! /usr/bin/env python
"""
Copyright (C) 2014 CompleteDB LLC.

This program is free software: you can redistribute it and/or modify
it under the terms of the Apache License Version 2.0 http://www.apache.org/licenses.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

"""

import unittest
import time
from pubsubsql import Client

class TestClient(unittest.TestCase):
    """MAKE SURE TO RUN PUBSUBSQL SERVER!"""

    def __ADDRESS(self):
        return "localhost:7777"

    def __ROWS(self):
        return 3

    def __generateTableName(self):
        return "T" + str(int(round(time.time() * 1000)))

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testConnectDisconnect(self):
        client = Client()
        #
        client.connect(self.__ADDRESS())
        self.assertTrue(client.isConnected())
        client.disconnect()
        #
        with self.assertRaises(Exception):
            client.connect("addresswithnoport")
        self.assertFalse(client.isConnected())
        client.disconnect()
        #
        with self.assertRaises(Exception):
            client.connect("addresswithnoport:")
        self.assertFalse(client.isConnected())
        client.disconnect()
        #
        with self.assertRaises(Exception):
            client.connect("localhost:7778")
        self.assertFalse(client.isConnected())
        client.disconnect()

    def testExecuteStatus(self):
        client = Client()
        client.connect(self.__ADDRESS())
        #
        client.execute("status")
        self.assertEqual("status", client.getAction())
        client.disconnect()

    def testExecuteInvalidCommand(self):
        client = Client()
        client.connect(self.__ADDRESS())
        #
        with self.assertRaises(Exception):
            client.execute("blablabla")

    def testInsertOneRow(self):
        client = Client()
        client.connect(self.__ADDRESS())
        tableName = self.__generateTableName()
        command = "insert into {} (col1, col2, col3) values (1:col1, 1:col2, 1:col3) returning *".format(tableName)
        #
        client.execute(command)
        self.assertEqual("insert", client.getAction())
        self.assertEqual(1, client.getRowCount())
        self.assertTrue(client.nextRow())
        #
        self.assertNotEqual("", client.getValue("id"))
        self.assertEqual("1:col1", client.getValue("col1"))
        self.assertEqual("1:col2", client.getValue("col2"))
        self.assertEqual("1:col3", client.getValue("col3"))
        self.assertTrue(client.hasColumn("col1"))
        self.assertTrue(client.hasColumn("col2"))
        self.assertTrue(client.hasColumn("col3"))
        self.assertEqual(4, client.getColumnCount()) # including id
        #
        self.assertFalse(client.nextRow())
        client.disconnect()

    def testInsertManyRow(self):
        client = Client()
        client.connect(self.__ADDRESS())
        tableName = self.__generateTableName()
        command = "insert into {} (col1, col2, col3) values (1:col1, 1:col2, 1:col3) returning *".format(tableName)
        for _ in range(self.__ROWS()):
            client.execute(command)
            self.assertEqual("insert", client.getAction())
            self.assertEqual(1, client.getRowCount())
            self.assertTrue(client.nextRow())
            #
            self.assertNotEqual("", client.getValue("id"))
            self.assertEqual("1:col1", client.getValue("col1"))
            self.assertEqual("1:col2", client.getValue("col2"))
            self.assertEqual("1:col3", client.getValue("col3"))
            self.assertTrue(client.hasColumn("col1"))
            self.assertTrue(client.hasColumn("col2"))
            self.assertTrue(client.hasColumn("col3"))
            self.assertEqual(4, client.getColumnCount()) # including id
            #
            self.assertFalse(client.nextRow())
        client.disconnect()

if __name__ == "__main__":
    unittest.main()
{'fraction_non_alphanumeric': 0.12400405620744603, 'fraction_numerical': 0.014921048819353903, 'mean_word_length': 2.721832884097035, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4597433', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1886, 'n_words': 396}