File: test_kinesis.py

package info (click to toggle)
python-botocore 1.20.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 60,608 kB
  • sloc: python: 50,632; xml: 15,052; makefile: 131
file content (89 lines) | stat: -rw-r--r-- 3,712 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright 2012-2014 Amazon.com, Inc. or its affiliates. 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. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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 json
import time
from base64 import b64decode
from uuid import uuid4
from tests import unittest, BaseSessionTest, ClientHTTPStubber


class TestKinesisListStreams(BaseSessionTest):
    def setUp(self):
        super(TestKinesisListStreams, self).setUp()
        self.stream_name = "kinesis-test-stream"
        self.region = "us-east-1"
        self.client = self.session.create_client("kinesis", self.region)
        self.http_stubber = ClientHTTPStubber(self.client)
        self.http_stubber.add_response()

    def assert_base64encoded_str_equals(self, encoded_str, expected_value):
        """Validate a value can be base64 decoded and equals expected value"""
        try:
            decoded_str = b64decode(encoded_str).decode("utf-8")
        except UnicodeDecodeError:
            self.fail("Base64 encoded record is not a valid utf-8 string")
        self.assertEqual(decoded_str, expected_value)

    def test_can_put_stream_blob(self):
        unique_data = str(uuid4())
        with self.http_stubber as stub:
            self.client.put_record(
                StreamName=self.stream_name, PartitionKey="foo", Data=unique_data
            )
            self.assertEqual(len(stub.requests), 1)
            request = json.loads(stub.requests[0].body.decode("utf-8"))
            self.assertEqual(request["StreamName"], self.stream_name)
            self.assertEqual(request["PartitionKey"], "foo")
            self.assert_base64encoded_str_equals(
                request["Data"], unique_data
            )

    def test_can_put_records_single_blob(self):
        unique_data = str(uuid4())
        with self.http_stubber as stub:
            self.client.put_records(
                StreamName=self.stream_name,
                Records=[{"Data": unique_data, "PartitionKey": "foo"}],
            )
            self.assertEqual(len(stub.requests), 1)
            request = json.loads(stub.requests[0].body.decode("utf-8"))
            self.assertEqual(len(request["Records"]), 1)
            self.assertEqual(request["StreamName"], self.stream_name)

            record = request["Records"][0]
            self.assertEqual(record["PartitionKey"], "foo")
            self.assert_base64encoded_str_equals(
                record["Data"], unique_data
            )

    def test_can_put_records_multiple_blob(self):
        with self.http_stubber as stub:
            self.client.put_records(
                StreamName=self.stream_name,
                Records=[
                    {"Data": "foobar", "PartitionKey": "foo"},
                    {"Data": "barfoo", "PartitionKey": "foo"},
                ],
            )
            self.assertEqual(len(stub.requests), 1)
            request = json.loads(stub.requests[0].body.decode("utf-8"))
            self.assertEqual(len(request["Records"]), 2)

            record_foobar = request["Records"][0]
            record_barfoo = request["Records"][1]
            self.assert_base64encoded_str_equals(
                record_foobar["Data"], "foobar"
            )
            self.assert_base64encoded_str_equals(
                record_barfoo["Data"], "barfoo"
            )