def test_to_pb_w_empty_list(self):
        from google.protobuf.struct_pb2 import ListValue
        from google.protobuf.struct_pb2 import Value
        from google.cloud.spanner_v1.proto.keys_pb2 import KeyRange

        key = u'key'
        key_range = self._make_one(start_closed=[], end_closed=[key])
        key_range_pb = key_range.to_pb()
        expected = KeyRange(
            start_closed=ListValue(values=[]),
            end_closed=ListValue(values=[Value(string_value=key)]),
        )
        self.assertEqual(key_range_pb, expected)
    def test_to_pb_w_start_closed_and_end_open(self):
        from google.protobuf.struct_pb2 import ListValue
        from google.protobuf.struct_pb2 import Value
        from google.cloud.spanner_v1.proto.keys_pb2 import KeyRange

        key1 = u'key_1'
        key2 = u'key_2'
        key_range = self._make_one(start_closed=[key1], end_open=[key2])
        key_range_pb = key_range._to_pb()
        expected = KeyRange(
            start_closed=ListValue(values=[Value(string_value=key1)]),
            end_open=ListValue(values=[Value(string_value=key2)]),
        )
        self.assertEqual(key_range_pb, expected)
    def test_to_pb_w_only_ranges(self):
        from google.cloud.spanner_v1.proto.keys_pb2 import KeySet
        from google.cloud.spanner_v1.keyset import KeyRange

        KEY_1 = u'KEY_1'
        KEY_2 = u'KEY_2'
        KEY_3 = u'KEY_3'
        KEY_4 = u'KEY_4'
        RANGES = [
            KeyRange(start_open=KEY_1, end_closed=KEY_2),
            KeyRange(start_closed=KEY_3, end_open=KEY_4),
        ]
        keyset = self._make_one(ranges=RANGES)

        result = keyset._to_pb()

        self.assertIsInstance(result, KeySet)
        self.assertFalse(result.all)
        self.assertEqual(len(result.keys), 0)
        self.assertEqual(len(result.ranges), len(RANGES))

        for found, expected in zip(result.ranges, RANGES):
            self.assertEqual(found, expected._to_pb())
    def test_from_dict_w_ranges(self):
        from google.cloud.spanner_v1.keyset import KeyRange

        klass = self._get_target_class()
        key_1 = u'KEY_1'
        key_2 = u'KEY_2'
        key_3 = u'KEY_3'
        key_4 = u'KEY_4'
        mapping = {
            'ranges': [
                {'start_open': [key_1], 'end_closed': [key_2]},
                {'start_closed': [key_3], 'end_open': [key_4]},
            ],
        }

        keyset = klass._from_dict(mapping)

        range_1 = KeyRange(start_open=[key_1], end_closed=[key_2])
        range_2 = KeyRange(start_closed=[key_3], end_open=[key_4])

        self.assertFalse(keyset.all_)
        self.assertEqual(keyset.keys, [])
        self.assertEqual(keyset.ranges, [range_1, range_2])
    def test_from_dict_w_ranges(self):
        from google.cloud.spanner_v1.keyset import KeyRange

        klass = self._get_target_class()
        key_1 = "KEY_1"
        key_2 = "KEY_2"
        key_3 = "KEY_3"
        key_4 = "KEY_4"
        mapping = {
            "ranges": [
                {"start_open": [key_1], "end_closed": [key_2]},
                {"start_closed": [key_3], "end_open": [key_4]},
            ]
        }

        keyset = klass._from_dict(mapping)

        range_1 = KeyRange(start_open=[key_1], end_closed=[key_2])
        range_2 = KeyRange(start_closed=[key_3], end_open=[key_4])

        self.assertFalse(keyset.all_)
        self.assertEqual(keyset.keys, [])
        self.assertEqual(keyset.ranges, [range_1, range_2])