<commit_before># Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import unittest


class TestStreamedResultSet(unittest.TestCase):

    def _getTargetClass(self):
        from google.cloud.spanner.streamed import StreamedResultSet

        return StreamedResultSet

    def _make_one(self, *args, **kwargs):
        return self._getTargetClass()(*args, **kwargs)

    def test_ctor_defaults(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        self.assertIs(streamed._response_iterator, iterator)
        self.assertEqual(streamed.rows, [])
        self.assertIsNone(streamed.metadata)
        self.assertIsNone(streamed.stats)
        self.assertIsNone(streamed.resume_token)

    def test_fields_unset(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        with self.assertRaises(AttributeError):
            _ = streamed.fields

    @staticmethod
    def _makeScalarField(name, type_):
        from google.cloud.proto.spanner.v1.type_pb2 import StructType
        from google.cloud.proto.spanner.v1.type_pb2 import Type

        return StructType.Field(name=name, type=Type(code=type_))

    @staticmethod
    def _makeArrayField(name, element_type_code=None, element_type=None):
        from google.cloud.proto.spanner.v1.type_pb2 import StructType
        from google.cloud.proto.spanner.v1.type_pb2 import Type

        if element_type is None:
            element_type = Type(code=element_type_code)
        array_type = Type(
            code='ARRAY', array_element_type=element_type)
        return StructType.Field(name=name, type=array_type)

    @staticmethod
    def _makeStructType(struct_type_fields):
        from google.cloud.proto.spanner.v1.type_pb2 import StructType
        from google.cloud.proto.spanner.v1.type_pb2 import Type

        fields = [
            StructType.Field(name=key, type=Type(code=value))
            for key, value in struct_type_fields
        ]
        struct_type = StructType(fields=fields)
        return Type(code='STRUCT', struct_type=struct_type)

    @staticmethod
    def _makeValue(value):
        from google.cloud.spanner._helpers import _make_value_pb

        return _make_value_pb(value)

    @staticmethod
    def _makeListValue(values=(), value_pbs=None):
        from google.protobuf.struct_pb2 import ListValue
        from google.protobuf.struct_pb2 import Value
        from google.cloud.spanner._helpers import _make_list_value_pb

        if value_pbs is not None:
            return Value(list_value=ListValue(values=value_pbs))
        return Value(list_value=_make_list_value_pb(values))

    def test_properties_set(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('full_name', 'STRING'),
            self._makeScalarField('age', 'INT64'),
        ]
        metadata = streamed._metadata = _ResultSetMetadataPB(FIELDS)
        stats = streamed._stats = _ResultSetStatsPB()
        self.assertEqual(list(streamed.fields), FIELDS)
        self.assertIs(streamed.metadata, metadata)
        self.assertIs(streamed.stats, stats)

    def test__merge_chunk_bool(self):
        from google.cloud.spanner.streamed import Unmergeable

        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('registered_voter', 'BOOL'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(True)
        chunk = self._makeValue(False)

        with self.assertRaises(Unmergeable):
            streamed._merge_chunk(chunk)

    def test__merge_chunk_int64(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('age', 'INT64'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(42)
        chunk = self._makeValue(13)

        merged = streamed._merge_chunk(chunk)
        self.assertEqual(merged.string_value, '4213')
        self.assertIsNone(streamed._pending_chunk)

    def test__merge_chunk_float64_nan_string(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('weight', 'FLOAT64'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(u'Na')
        chunk = self._makeValue(u'N')

        merged = streamed._merge_chunk(chunk)
        self.assertEqual(merged.string_value, u'NaN')

    def test__merge_chunk_float64_w_empty(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('weight', 'FLOAT64'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(3.14159)
        chunk = self._makeValue('')

        merged = streamed._merge_chunk(chunk)
        self.assertEqual(merged.number_value, 3.14159)

    def test__merge_chunk_float64_w_float64(self):
        from google.cloud.spanner.streamed import Unmergeable

        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('weight', 'FLOAT64'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(3.14159)
        chunk = self._makeValue(2.71828)

        with self.assertRaises(Unmergeable):
            streamed._merge_chunk(chunk)

    def test__merge_chunk_string(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('name', 'STRING'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(u'phred')
        chunk = self._makeValue(u'wylma')

        merged = streamed._merge_chunk(chunk)

        self.assertEqual(merged.string_value, u'phredwylma')
        self.assertIsNone(streamed._pending_chunk)

    def test__merge_chunk_array_of_bool(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeArrayField('name', element_type_code='BOOL'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeListValue([True, True])
        chunk = self._makeListValue([False, False, False])

        merged = streamed._merge_chunk(chunk)

        expected = self._makeListValue([True, True, False, False, False])
        self.assertEqual(merged, expected)
        self.assertIsNone(streamed._pending_chunk)

    def test__merge_chunk_array_of_int(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [<commit_msg>test case for BYTES merger<commit_after># Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import unittest


class TestStreamedResultSet(unittest.TestCase):

    def _getTargetClass(self):
        from google.cloud.spanner.streamed import StreamedResultSet

        return StreamedResultSet

    def _make_one(self, *args, **kwargs):
        return self._getTargetClass()(*args, **kwargs)

    def test_ctor_defaults(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        self.assertIs(streamed._response_iterator, iterator)
        self.assertEqual(streamed.rows, [])
        self.assertIsNone(streamed.metadata)
        self.assertIsNone(streamed.stats)
        self.assertIsNone(streamed.resume_token)

    def test_fields_unset(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        with self.assertRaises(AttributeError):
            _ = streamed.fields

    @staticmethod
    def _makeScalarField(name, type_):
        from google.cloud.proto.spanner.v1.type_pb2 import StructType
        from google.cloud.proto.spanner.v1.type_pb2 import Type

        return StructType.Field(name=name, type=Type(code=type_))

    @staticmethod
    def _makeArrayField(name, element_type_code=None, element_type=None):
        from google.cloud.proto.spanner.v1.type_pb2 import StructType
        from google.cloud.proto.spanner.v1.type_pb2 import Type

        if element_type is None:
            element_type = Type(code=element_type_code)
        array_type = Type(
            code='ARRAY', array_element_type=element_type)
        return StructType.Field(name=name, type=array_type)

    @staticmethod
    def _makeStructType(struct_type_fields):
        from google.cloud.proto.spanner.v1.type_pb2 import StructType
        from google.cloud.proto.spanner.v1.type_pb2 import Type

        fields = [
            StructType.Field(name=key, type=Type(code=value))
            for key, value in struct_type_fields
        ]
        struct_type = StructType(fields=fields)
        return Type(code='STRUCT', struct_type=struct_type)

    @staticmethod
    def _makeValue(value):
        from google.cloud.spanner._helpers import _make_value_pb

        return _make_value_pb(value)

    @staticmethod
    def _makeListValue(values=(), value_pbs=None):
        from google.protobuf.struct_pb2 import ListValue
        from google.protobuf.struct_pb2 import Value
        from google.cloud.spanner._helpers import _make_list_value_pb

        if value_pbs is not None:
            return Value(list_value=ListValue(values=value_pbs))
        return Value(list_value=_make_list_value_pb(values))

    def test_properties_set(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('full_name', 'STRING'),
            self._makeScalarField('age', 'INT64'),
        ]
        metadata = streamed._metadata = _ResultSetMetadataPB(FIELDS)
        stats = streamed._stats = _ResultSetStatsPB()
        self.assertEqual(list(streamed.fields), FIELDS)
        self.assertIs(streamed.metadata, metadata)
        self.assertIs(streamed.stats, stats)

    def test__merge_chunk_bool(self):
        from google.cloud.spanner.streamed import Unmergeable

        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('registered_voter', 'BOOL'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(True)
        chunk = self._makeValue(False)

        with self.assertRaises(Unmergeable):
            streamed._merge_chunk(chunk)

    def test__merge_chunk_int64(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('age', 'INT64'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(42)
        chunk = self._makeValue(13)

        merged = streamed._merge_chunk(chunk)
        self.assertEqual(merged.string_value, '4213')
        self.assertIsNone(streamed._pending_chunk)

    def test__merge_chunk_float64_nan_string(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('weight', 'FLOAT64'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(u'Na')
        chunk = self._makeValue(u'N')

        merged = streamed._merge_chunk(chunk)
        self.assertEqual(merged.string_value, u'NaN')

    def test__merge_chunk_float64_w_empty(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('weight', 'FLOAT64'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(3.14159)
        chunk = self._makeValue('')

        merged = streamed._merge_chunk(chunk)
        self.assertEqual(merged.number_value, 3.14159)

    def test__merge_chunk_float64_w_float64(self):
        from google.cloud.spanner.streamed import Unmergeable

        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('weight', 'FLOAT64'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(3.14159)
        chunk = self._makeValue(2.71828)

        with self.assertRaises(Unmergeable):
            streamed._merge_chunk(chunk)

    def test__merge_chunk_string(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('name', 'STRING'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(u'phred')
        chunk = self._makeValue(u'wylma')

        merged = streamed._merge_chunk(chunk)

        self.assertEqual(merged.string_value, u'phredwylma')
        self.assertIsNone(streamed._pending_chunk)

    def test__merge_chunk_string_w_bytes(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeScalarField('image', 'BYTES'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeValue(u'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA\n')
        chunk = self._makeValue(u'B3RJTUUH4QQGFwsBTL3HMwAAABJpVFh0Q29tbWVudAAAAAAAU0FNUExFMG3E+AAAAApJREFUCNdj\nYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\n')

        merged = streamed._merge_chunk(chunk)

        self.assertEqual(merged.string_value, u'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA\nB3RJTUUH4QQGFwsBTL3HMwAAABJpVFh0Q29tbWVudAAAAAAAU0FNUExFMG3E+AAAAApJREFUCNdj\nYAAAAAIAAeI<KEY>ASUV<KEY>n')
        self.assertIsNone(streamed._pending_chunk)     

    def test__merge_chunk_array_of_bool(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
            self._makeArrayField('name', element_type_code='BOOL'),
        ]
        streamed._metadata = _ResultSetMetadataPB(FIELDS)
        streamed._pending_chunk = self._makeListValue([True, True])
        chunk = self._makeListValue([False, False, False])

        merged = streamed._merge_chunk(chunk)

        expected = self._makeListValue([True, True, False, False, False])
        self.assertEqual(merged, expected)
        self.assertIsNone(streamed._pending_chunk)

    def test__merge_chunk_array_of_int(self):
        iterator = _MockCancellableIterator()
        streamed = self._make_one(iterator)
        FIELDS = [
{'fraction_non_alphanumeric': 0.08883107229139565, 'fraction_numerical': 0.010468933903239795, 'mean_word_length': 3.056281407035176, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5753065', 'n_tokens_mistral': 4932, 'n_tokens_neox': 4756, 'n_words': 836}