-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_doceasy.py
46 lines (33 loc) · 1.4 KB
/
test_doceasy.py
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
"""Tests for doceasy."""
import schema
import pytest
from doceasy import Mapping, File
def test_mapping_str():
"""It should create a mapping of strings when provided no arguments."""
assert Mapping().validate("a=b") == {"a": "b"}
assert Mapping().validate("a=b,c=d") == {"a": "b", "c": "d"}
def test_mapping_with_single_type():
"""If provided a single type, it should convert the values to that
type.
"""
assert Mapping(int).validate("a=1") == {"a": 1}
assert Mapping(int).validate("a=1,c=2") == {"a": 1, "c": 2}
with pytest.raises(schema.SchemaError):
Mapping(int).validate("a=b,c=d")
def test_mapping_with_two_types():
"""It should convert both the key and value."""
assert Mapping(int, int).validate("1=2") == {1: 2}
assert Mapping(int, int).validate("1=2,3=4") == {1: 2, 3: 4}
def test_mapping_to_string():
"""It should create a string of the dict."""
assert Mapping.to_string({"a": 1, "b": 2}) == "a=1,b=2"
assert Mapping(int).validate(Mapping.to_string({"a": 1, "b": 2})) == {
"a": 1, "b": 2}
def test_file_open_stdout_binary_mode(capsys):
"""It should open stdout in binary mode."""
handle = File(mode="w").validate("-")
with pytest.raises(TypeError):
handle.write(b"Hello World")
handle = File(mode="wb").validate("-")
handle.write(b"Hello World Bytes")
assert capsys.readouterr().out == "Hello World Bytes"