def test_statement_cursor_buffer_size(self):
        valid_inputs = [1, 10, 999999]

        for valid in valid_inputs:
            statement = SqlStatement("something")
            statement.cursor_buffer_size = valid
            self.assertEqual(valid, statement.cursor_buffer_size)

        invalid_inputs = [0, -10, -99999, "hey", None, 1.0]

        for invalid in invalid_inputs:
            statement = SqlStatement("something")
            with self.assertRaises((ValueError, AssertionError)):
                statement.cursor_buffer_size = invalid
    def test_blocking_iterator_with_multi_paged_result(self):
        self._populate_map()
        statement = SqlStatement("SELECT __key FROM %s" % self.map_name)
        statement.cursor_buffer_size = 1  # Each page will contain just 1 result
        result = self.client.sql.execute_statement(statement)

        six.assertCountEqual(self, [i for i in range(10)],
                             [row.get_object_with_index(0) for row in result])
    def test_with_statement_when_iteration_throws(self):
        self._populate_map()
        statement = SqlStatement("SELECT this FROM %s" % self.map_name)
        statement.cursor_buffer_size = 1  # so that it doesn't close immediately

        with self.assertRaises(RuntimeError):
            with self.client.sql.execute_statement(statement) as result:
                for _ in result:
                    raise RuntimeError("expected")

        self.assertIsInstance(result.close(), ImmediateFuture)
    def test_close_when_query_is_active(self):
        self._populate_map()
        statement = SqlStatement("SELECT * FROM %s " % self.map_name)
        statement.cursor_buffer_size = 1  # Each page will contain 1 row
        result = self.client.sql.execute_statement(statement)

        # Fetch couple of pages
        iterator = iter(result)
        next(iterator)

        self.assertIsNone(result.close().result())

        with self.assertRaises(HazelcastSqlError):
            # Next fetch requests should fail
            next(iterator)
    def test_execute_statement_with_cursor_buffer_size(self):
        entry_count = 50
        self._populate_map(entry_count, lambda v: Student(v, v))
        statement = SqlStatement("SELECT age FROM %s" % self.map_name)
        statement.cursor_buffer_size = 3
        result = self.client.sql.execute_statement(statement)

        with patch.object(result,
                          "_fetch_next_page",
                          wraps=result._fetch_next_page) as patched:
            six.assertCountEqual(self, [i for i in range(entry_count)],
                                 [row.get_object("age") for row in result])
            # -1 comes from the fact that, we don't fetch the first page
            self.assertEqual(
                math.ceil(float(entry_count) / statement.cursor_buffer_size) -
                1, patched.call_count)
    def test_iterator_with_multi_paged_result(self):
        self._populate_map()
        statement = SqlStatement("SELECT __key FROM %s" % self.map_name)
        statement.cursor_buffer_size = 1  # Each page will contain just 1 result
        result = self.client.sql.execute_statement(statement)

        iterator = result.iterator().result()

        rows = []
        for row_future in iterator:
            try:
                row = row_future.result()
                rows.append(row.get_object_with_index(0))
            except StopIteration:
                break

        six.assertCountEqual(self, [i for i in range(10)], rows)