<commit_before>import datetime

import pydbc

from query_fixture import query_fixture
from cursor_test_case import CursorTestCase


class SelectTests(object):
    """
    Parent class for database-specific SELECT tests. Children are expected to provide
    the following attributes:

    self.supports_row_count
    """
    def test_too_many_parameters_raise(self):
        with self.assertRaises(pydbc.Error):
            self.cursor.execute("SELECT 42", [42])

        with self.assertRaises(pydbc.Error):
            self.cursor.execute_many("SELECT 42", [[42]])

    def _test_single_row_result_set(self, query, expected_row):
        self.cursor.execute(query)

        if self.supports_row_count:
            self.assertEqual(self.cursor.rowcount, 1)
        else:
            self.assertEqual(self.cursor.rowcount, -1)

        row = self.cursor.fetchone()
        self.assertItemsEqual(row, expected_row)

        row = self.cursor.fetchone()
        self.assertIsNone(row)

    def test_single_row_NULL_result(self):
        self._test_single_row_result_set("SELECT NULL", [None])

    def test_single_row_integer_result(self):
        self._test_single_row_result_set("SELECT 42", [42])

    def test_single_row_bool_result(self):
        self._test_single_row_result_set("SELECT True", [True])
        self._test_single_row_result_set("SELECT False", [False])

    def test_single_row_string_result(self):
        self._test_single_row_result_set("SELECT 'value'", ["value"])

    def test_single_row_unicode_result(self):
        self._test_single_row_result_set(u"SELECT 'value \u2665'", [u"value \u2665"])

    def test_single_row_double_result(self):
        with query_fixture(self.cursor, self.fixtures, 'SELECT DOUBLE') as query:
            self.cursor.execute(query)
            row = self.cursor.fetchone()
            self.assertItemsEqual(row, [3.14])

    def test_single_row_date_result(self):
        self._test_single_row_result_set("SELECT CAST('2015-12-31' AS DATE) AS a",
                                         [datetime.date(2015, 12, 31)])

    def test_single_row_timestamp_result(self):
        with query_fixture(self.cursor, self.fixtures, 'SELECT TIMESTAMP') as query:
            self.cursor.execute(query)
            row = self.cursor.fetchone()
            self.assertItemsEqual(row, [datetime.datetime(2015, 12, 31, 1, 2, 3)])

    def test_single_row_large_numeric_result_as_string(self):
        self._test_single_row_result_set("SELECT -1234567890123.123456789", ['-1234567890123.123456789'])

    def test_single_row_multiple_columns(self):
        self._test_single_row_result_set("SELECT 40, 41, 42, 43", [40, 41, 42, 43])

    def test_multiple_rows(self):
        with query_fixture(self.cursor, self.fixtures, 'SELECT MULTIPLE INTEGERS') as query:
            self.cursor.execute(query)
            row = self.cursor.fetchone()
            self.assertItemsEqual(row, [42])
            row = self.cursor.fetchone()
            self.assertItemsEqual(row, [43])
            row = self.cursor.fetchone()
            self.assertItemsEqual(row, [44])

            row = self.cursor.fetchone()
            self.assertIsNone(row)


# Actual test cases
<commit_msg>Add test for large result sets<commit_after>import datetime

import pydbc

from query_fixture import query_fixture
from cursor_test_case import CursorTestCase


class SelectTests(object):
    """
    Parent class for database-specific SELECT tests. Children are expected to provide
    the following attributes:

    self.supports_row_count
    """
    def test_too_many_parameters_raise(self):
        with self.assertRaises(pydbc.Error):
            self.cursor.execute("SELECT 42", [42])

        with self.assertRaises(pydbc.Error):
            self.cursor.execute_many("SELECT 42", [[42]])

    def _test_single_row_result_set(self, query, expected_row):
        self.cursor.execute(query)

        if self.supports_row_count:
            self.assertEqual(self.cursor.rowcount, 1)
        else:
            self.assertEqual(self.cursor.rowcount, -1)

        row = self.cursor.fetchone()
        self.assertItemsEqual(row, expected_row)

        row = self.cursor.fetchone()
        self.assertIsNone(row)

    def test_single_row_NULL_result(self):
        self._test_single_row_result_set("SELECT NULL", [None])

    def test_single_row_integer_result(self):
        self._test_single_row_result_set("SELECT 42", [42])

    def test_single_row_bool_result(self):
        self._test_single_row_result_set("SELECT True", [True])
        self._test_single_row_result_set("SELECT False", [False])

    def test_single_row_string_result(self):
        self._test_single_row_result_set("SELECT 'value'", ["value"])

    def test_single_row_unicode_result(self):
        self._test_single_row_result_set(u"SELECT 'value \u2665'", [u"value \u2665"])

    def test_single_row_double_result(self):
        with query_fixture(self.cursor, self.fixtures, 'SELECT DOUBLE') as query:
            self.cursor.execute(query)
            row = self.cursor.fetchone()
            self.assertItemsEqual(row, [3.14])

    def test_single_row_date_result(self):
        self._test_single_row_result_set("SELECT CAST('2015-12-31' AS DATE) AS a",
                                         [datetime.date(2015, 12, 31)])

    def test_single_row_timestamp_result(self):
        with query_fixture(self.cursor, self.fixtures, 'SELECT TIMESTAMP') as query:
            self.cursor.execute(query)
            row = self.cursor.fetchone()
            self.assertItemsEqual(row, [datetime.datetime(2015, 12, 31, 1, 2, 3)])

    def test_single_row_large_numeric_result_as_string(self):
        self._test_single_row_result_set("SELECT -1234567890123.123456789", ['-1234567890123.123456789'])

    def test_single_row_multiple_columns(self):
        self._test_single_row_result_set("SELECT 40, 41, 42, 43", [40, 41, 42, 43])

    def test_multiple_rows(self):
        with query_fixture(self.cursor, self.fixtures, 'SELECT MULTIPLE INTEGERS') as query:
            self.cursor.execute(query)
            row = self.cursor.fetchone()
            self.assertItemsEqual(row, [42])
            row = self.cursor.fetchone()
            self.assertItemsEqual(row, [43])
            row = self.cursor.fetchone()
            self.assertItemsEqual(row, [44])

            row = self.cursor.fetchone()
            self.assertIsNone(row)

    def test_number_of_rows_exceeds_buffer_size(self):
        with query_fixture(self.cursor, self.fixtures, 'INSERT INTEGER') as table_name:
            numbers = 123
            for i in xrange(numbers):
                self.cursor.execute("INSERT INTO {} VALUES ({})".format(table_name, i))

            self.cursor.execute("SELECT a FROM {}".format(table_name))
            retrieved = self.cursor.fetchall()
            actual_sum = sum([row[0] for row in retrieved])
            expected_sum = sum(xrange(numbers))
            self.assertEqual(expected_sum, actual_sum)


# Actual test cases

{'fraction_non_alphanumeric': 0.10637387708541281, 'fraction_numerical': 0.034222158847853984, 'mean_word_length': 2.9582392776523703, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3716319', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2107, 'n_words': 399}