File: test_multipart_monitor.py

package info (click to toggle)
python-requests-toolbelt 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 876 kB
  • sloc: python: 3,653; makefile: 166; sh: 7
file content (65 lines) | stat: -rw-r--r-- 2,087 bytes parent folder | download | duplicates (5)
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
# -*- coding: utf-8 -*-
import math
import unittest
from requests_toolbelt.multipart.encoder import (
    IDENTITY, MultipartEncoder, MultipartEncoderMonitor
    )


class TestMultipartEncoderMonitor(unittest.TestCase):
    def setUp(self):
        self.fields = {'a': 'b'}
        self.boundary = 'thisisaboundary'
        self.encoder = MultipartEncoder(self.fields, self.boundary)
        self.monitor = MultipartEncoderMonitor(self.encoder)

    def test_content_type(self):
        assert self.monitor.content_type == self.encoder.content_type

    def test_length(self):
        assert self.encoder.len == self.monitor.len

    def test_read(self):
        new_encoder = MultipartEncoder(self.fields, self.boundary)
        assert new_encoder.read() == self.monitor.read()

    def test_callback_called_when_reading_everything(self):
        callback = Callback(self.monitor)
        self.monitor.callback = callback
        self.monitor.read()
        assert callback.called == 1

    def test_callback(self):
        callback = Callback(self.monitor)
        self.monitor.callback = callback
        chunk_size = int(math.ceil(self.encoder.len / 4.0))
        while self.monitor.read(chunk_size):
            pass
        assert callback.called == 5

    def test_bytes_read(self):
        bytes_to_read = self.encoder.len
        self.monitor.read()
        assert self.monitor.bytes_read == bytes_to_read

    def test_default_callable_is_the_identity(self):
        assert self.monitor.callback == IDENTITY
        assert IDENTITY(1) == 1

    def test_from_fields(self):
        monitor = MultipartEncoderMonitor.from_fields(
            self.fields, self.boundary
            )
        assert isinstance(monitor, MultipartEncoderMonitor)
        assert isinstance(monitor.encoder, MultipartEncoder)
        assert monitor.encoder.boundary_value == self.boundary


class Callback(object):
    def __init__(self, monitor):
        self.called = 0
        self.monitor = monitor

    def __call__(self, monitor):
        self.called += 1
        assert monitor == self.monitor