Skip to content

Commit 74dcc0f

Browse files
committed
Convert encoding tests to pure pytest
1 parent 2d80e67 commit 74dcc0f

File tree

1 file changed

+100
-109
lines changed

1 file changed

+100
-109
lines changed

test/unit/test_encoding.py

+100-109
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
# -*- encoding: utf-8 -*-
2-
"""Defines encoding related classes.
3-
4-
Classes:
5-
TestEncoding - A class that tests constructs located in the encoding.py module.
6-
"""
7-
8-
import unittest
2+
"""Test the generic data encoding and decoding module."""
93
import json
104

115
import pytest
@@ -15,105 +9,102 @@
159
import ipfshttpclient.exceptions
1610

1711

18-
class TestEncoding(unittest.TestCase):
19-
"""Unit tests the Encoding class
20-
21-
Public methods:
22-
setUp - create a Json encoder
23-
test_json_parse - Asserts parsed key/value json matches expected output
24-
test_json_parse_chained - Tests if concatenated string of JSON object is being parsed correctly
25-
test_json_parse_chained_newlines - Tests parsing of concatenated string of JSON object containing a new line
26-
test_json_encode - Tests serilization of json formatted string to an object
27-
test_get_encoder_by_name - Tests the process of obtaining an Encoder object given the named encoding
28-
test_get_invalid_encoder - Tests the exception handling given an invalid named encoding
29-
30-
"""
31-
def setUp(self):
32-
"""create a Json encoder"""
33-
self.encoder_json = ipfshttpclient.encoding.Json()
34-
35-
def test_json_parse(self):
36-
"""Asserts parsed key/value json matches expected output."""
37-
data = {'key': 'value'}
38-
raw = six.b(json.dumps(data))
39-
res = self.encoder_json.parse(raw)
40-
assert res['key'] == 'value'
41-
42-
def test_json_parse_partial(self):
43-
"""Tests if feeding parts of JSON strings in the right order to the JSON parser produces the right results."""
44-
data1 = {'key1': 'value1'}
45-
data2 = {'key2': 'value2'}
46-
47-
# Try single fragmented data set
48-
data1_binary = six.b(json.dumps(data1))
49-
assert list(self.encoder_json.parse_partial(data1_binary[:8])) == []
50-
assert list(self.encoder_json.parse_partial(data1_binary[8:])) == [data1]
51-
assert list(self.encoder_json.parse_finalize()) == []
52-
53-
# Try multiple data sets contained in whitespace
54-
data2_binary = six.b(json.dumps(data2))
55-
data2_final = b" " + data1_binary + b" \r\n " + data2_binary + b" "
56-
assert list(self.encoder_json.parse_partial(data2_final)) == [data1, data2]
57-
assert list(self.encoder_json.parse_finalize()) == []
58-
59-
# String containing broken UTF-8
60-
with pytest.raises(ipfshttpclient.exceptions.DecodingError):
61-
list(self.encoder_json.parse_partial(b'{"hello": "\xc3ber world!"}'))
62-
assert list(self.encoder_json.parse_finalize()) == []
63-
64-
def test_json_with_newlines(self):
65-
"""Tests if feeding partial JSON strings with line breaks behaves as expected."""
66-
data1 = '{"key1":\n"value1",\n'
67-
data2 = '"key2":\n\n\n"value2"\n}'
68-
69-
data_expected = json.loads(data1 + data2)
70-
71-
assert list(self.encoder_json.parse_partial(six.b(data1))) == []
72-
assert list(self.encoder_json.parse_partial(six.b(data2))) == [data_expected]
73-
assert list(self.encoder_json.parse_finalize()) == []
74-
75-
def test_json_parse_incomplete(self):
76-
"""Tests if feeding the JSON parse incomplete data correctly produces an error."""
77-
list(self.encoder_json.parse_partial(b'{"bla":'))
78-
with pytest.raises(ipfshttpclient.exceptions.DecodingError):
79-
self.encoder_json.parse_finalize()
80-
81-
list(self.encoder_json.parse_partial(b'{"\xc3')) # Incomplete UTF-8 sequence
82-
with pytest.raises(ipfshttpclient.exceptions.DecodingError):
83-
self.encoder_json.parse_finalize()
84-
85-
def test_json_parse_chained(self):
86-
"""Tests if concatenated string of JSON object is being parsed correctly."""
87-
data1 = {'key1': 'value1'}
88-
data2 = {'key2': 'value2'}
89-
res = self.encoder_json.parse(
90-
six.b(json.dumps(data1)) + six.b(json.dumps(data2)))
91-
assert len(res) == 2
92-
assert res[0]['key1'] == 'value1'
93-
assert res[1]['key2'] == 'value2'
94-
95-
def test_json_parse_chained_newlines(self):
96-
"""Tests parsing of concatenated string of JSON object containing a new line."""
97-
data1 = {'key1': 'value1'}
98-
data2 = {'key2': 'value2'}
99-
res = self.encoder_json.parse(
100-
six.b(json.dumps(data1)) + b'\n' + six.b(json.dumps(data2)))
101-
assert len(res) == 2
102-
assert res[0]['key1'] == 'value1'
103-
assert res[1]['key2'] == 'value2'
104-
105-
def test_json_encode(self):
106-
"""Tests serilization of an object into a json formatted UTF-8 string."""
107-
data = {'key': 'value with Ünicøde characters ☺'}
108-
assert self.encoder_json.encode(data) == \
109-
b'{"key":"value with \xc3\x9cnic\xc3\xb8de characters \xe2\x98\xba"}'
110-
111-
def test_get_encoder_by_name(self):
112-
"""Tests the process of obtaining an Encoder object given the named encoding."""
113-
encoder = ipfshttpclient.encoding.get_encoding('json')
114-
assert encoder.name == 'json'
115-
116-
def test_get_invalid_encoder(self):
117-
"""Tests the exception handling given an invalid named encoding."""
118-
with pytest.raises(ipfshttpclient.exceptions.EncoderMissingError):
119-
ipfshttpclient.encoding.get_encoding('fake')
12+
13+
@pytest.fixture
14+
def json_encoder():
15+
return ipfshttpclient.encoding.Json()
16+
17+
18+
def test_json_parse(json_encoder):
19+
"""Asserts parsed key/value json matches expected output."""
20+
data = {'key': 'value'}
21+
raw = six.b(json.dumps(data))
22+
res = json_encoder.parse(raw)
23+
assert res['key'] == 'value'
24+
25+
26+
def test_json_parse_partial(json_encoder):
27+
"""Tests if feeding parts of JSON strings in the right order to the JSON parser produces the right results."""
28+
data1 = {'key1': 'value1'}
29+
data2 = {'key2': 'value2'}
30+
31+
# Try single fragmented data set
32+
data1_binary = six.b(json.dumps(data1))
33+
assert list(json_encoder.parse_partial(data1_binary[:8])) == []
34+
assert list(json_encoder.parse_partial(data1_binary[8:])) == [data1]
35+
assert list(json_encoder.parse_finalize()) == []
36+
37+
# Try multiple data sets contained in whitespace
38+
data2_binary = six.b(json.dumps(data2))
39+
data2_final = b" " + data1_binary + b" \r\n " + data2_binary + b" "
40+
assert list(json_encoder.parse_partial(data2_final)) == [data1, data2]
41+
assert list(json_encoder.parse_finalize()) == []
42+
43+
# String containing broken UTF-8
44+
with pytest.raises(ipfshttpclient.exceptions.DecodingError):
45+
list(json_encoder.parse_partial(b'{"hello": "\xc3ber world!"}'))
46+
assert list(json_encoder.parse_finalize()) == []
47+
48+
49+
def test_json_with_newlines(json_encoder):
50+
"""Tests if feeding partial JSON strings with line breaks behaves as expected."""
51+
data1 = '{"key1":\n"value1",\n'
52+
data2 = '"key2":\n\n\n"value2"\n}'
53+
54+
data_expected = json.loads(data1 + data2)
55+
56+
assert list(json_encoder.parse_partial(six.b(data1))) == []
57+
assert list(json_encoder.parse_partial(six.b(data2))) == [data_expected]
58+
assert list(json_encoder.parse_finalize()) == []
59+
60+
61+
def test_json_parse_incomplete(json_encoder):
62+
"""Tests if feeding the JSON parse incomplete data correctly produces an error."""
63+
list(json_encoder.parse_partial(b'{"bla":'))
64+
with pytest.raises(ipfshttpclient.exceptions.DecodingError):
65+
json_encoder.parse_finalize()
66+
67+
list(json_encoder.parse_partial(b'{"\xc3')) # Incomplete UTF-8 sequence
68+
with pytest.raises(ipfshttpclient.exceptions.DecodingError):
69+
json_encoder.parse_finalize()
70+
71+
72+
def test_json_parse_chained(json_encoder):
73+
"""Tests if concatenated string of JSON object is being parsed correctly."""
74+
data1 = {'key1': 'value1'}
75+
data2 = {'key2': 'value2'}
76+
res = json_encoder.parse(
77+
six.b(json.dumps(data1)) + six.b(json.dumps(data2)))
78+
assert len(res) == 2
79+
assert res[0]['key1'] == 'value1'
80+
assert res[1]['key2'] == 'value2'
81+
82+
83+
def test_json_parse_chained_newlines(json_encoder):
84+
"""Tests parsing of concatenated string of JSON object containing a new line."""
85+
data1 = {'key1': 'value1'}
86+
data2 = {'key2': 'value2'}
87+
res = json_encoder.parse(
88+
six.b(json.dumps(data1)) + b'\n' + six.b(json.dumps(data2)))
89+
assert len(res) == 2
90+
assert res[0]['key1'] == 'value1'
91+
assert res[1]['key2'] == 'value2'
92+
93+
94+
def test_json_encode(json_encoder):
95+
"""Tests serilization of an object into a json formatted UTF-8 string."""
96+
data = {'key': 'value with Ünicøde characters ☺'}
97+
assert json_encoder.encode(data) == \
98+
b'{"key":"value with \xc3\x9cnic\xc3\xb8de characters \xe2\x98\xba"}'
99+
100+
101+
def test_get_encoder_by_name():
102+
"""Tests the process of obtaining an Encoder object given the named encoding."""
103+
encoder = ipfshttpclient.encoding.get_encoding('json')
104+
assert encoder.name == 'json'
105+
106+
107+
def test_get_invalid_encoder():
108+
"""Tests the exception handling given an invalid named encoding."""
109+
with pytest.raises(ipfshttpclient.exceptions.EncoderMissingError):
110+
ipfshttpclient.encoding.get_encoding('fake')

0 commit comments

Comments
 (0)