File: test_custom_encoders.py

package info (click to toggle)
python-sdjson 0.5.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 560 kB
  • sloc: python: 1,566; makefile: 6; sh: 6
file content (235 lines) | stat: -rw-r--r-- 6,711 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""
Create several custom encoders and test that they work
"""

# stdlib
import collections
from datetime import date, datetime, time, timedelta, timezone
from decimal import Decimal
from fractions import Fraction

# 3rd party
import pytest

# this package
import sdjson


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_decimal_float() -> None:
	# Create and register a custom encoder for Decimal that turns it into a float
	@sdjson.encoders.register(Decimal)
	def encode_decimal_float(obj):
		return float(obj)

	assert sdjson.dumps(Decimal(str(12.3456))) == "12.3456"

	# Cleanup
	sdjson.encoders.unregister(Decimal)


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_decimal_str() -> None:
	# Create and register a custom encoder for Decimal that turns it into a str
	@sdjson.encoders.register(Decimal)
	def encode_decimal_str(obj):
		return str(obj)

	assert sdjson.dumps(Decimal(str(12.3456))) == '"12.3456"'

	# Cleanup
	sdjson.encoders.unregister(Decimal)


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_fraction_float() -> None:
	# Create and register a custom encoder for Fraction that turns it into a float
	@sdjson.encoders.register(Fraction)
	def encode_fraction_float(obj):
		return float(obj)

	assert sdjson.dumps(Fraction(13, 10)) == "1.3"
	assert sdjson.dumps(Fraction(3, 4)) == "0.75"
	assert sdjson.dumps(Fraction(9, 11)) == "0.8181818181818182"
	assert sdjson.dumps(Fraction(140, 144)) == "0.9722222222222222"
	assert sdjson.dumps(Fraction(2, 7)) == "0.2857142857142857"

	# Cleanup
	sdjson.encoders.unregister(Fraction)


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_fraction_str() -> None:
	# Create and register a custom encoder for Fraction that turns it into a str
	@sdjson.encoders.register(Fraction)
	def encode_fraction_str(obj):
		return str(obj)

	assert sdjson.dumps(Fraction(13, 10)) == '"13/10"'
	assert sdjson.dumps(Fraction(3, 4)) == '"3/4"'
	assert sdjson.dumps(Fraction(9, 11)) == '"9/11"'
	assert sdjson.dumps(Fraction(140, 144)) == '"35/36"'
	assert sdjson.dumps(Fraction(2, 7)) == '"2/7"'

	# Cleanup
	sdjson.encoders.unregister(Fraction)


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_datetime_float() -> None:
	# Create and register a custom encoder for datetime that turns it into a float
	@sdjson.encoders.register(datetime)
	def encode_datetime_float(obj):
		return obj.timestamp()

	assert sdjson.dumps(datetime(1945, 5, 8, 19, 20, tzinfo=timezone.utc)) == "-777876000.0"

	# Cleanup
	sdjson.encoders.unregister(datetime)


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_datetime_str() -> None:
	# Create and register a custom encoder for datetime that turns it into a str
	@sdjson.encoders.register(datetime)
	def encode_datetime_str(obj):
		return f"{obj:%Y/%m/%d %H:%M}"

	assert sdjson.dumps(datetime(1945, 5, 8, 19, 20)) == '"1945/05/08 19:20"'

	# Cleanup
	sdjson.encoders.unregister(datetime)


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_datetime_tuple() -> None:
	# Create and register a custom encoder for datetime that turns it into a timetuple
	@sdjson.encoders.register(datetime)
	def encode_datetime_tuple(obj):
		return obj.timetuple()

	assert sdjson.dumps(datetime(1945, 5, 8, 19, 20)) == "[1945, 5, 8, 19, 20, 0, 1, 128, -1]"

	# Cleanup
	sdjson.encoders.unregister(datetime)


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_timedelta_float() -> None:
	# Create and register a custom encoder for timedelta that turns it into a float
	@sdjson.encoders.register(timedelta)
	def encode_timedelta_float(obj):
		return obj.total_seconds()

	start_date = datetime(1945, 5, 8, 19, 20).replace(tzinfo=timezone.utc)
	end_date = datetime(2020, 5, 8, 9, 0).replace(tzinfo=timezone.utc)
	delta = end_date - start_date
	assert sdjson.dumps(delta) == "2366804400.0"

	# Cleanup
	sdjson.encoders.unregister(timedelta)


# def test_date_float() -> None:
# 	# Create and register a custom encoder for date that turns it into a float
# 	@sdjson.encoders.register(date)
# 	def encode_date_float(obj):
# 		return obj.timestamp()
#
# 	assert sdjson.dumps(date(1945, 5, 8)) == "-777952800.0"
#
# 	# Cleanup
# 	sdjson.encoders.unregister(date)


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_date_str() -> None:
	# Create and register a custom encoder for date that turns it into a str
	@sdjson.encoders.register(date)
	def encode_date_str(obj):
		return f"{obj:%Y/%m/%d}"

	assert sdjson.dumps(date(1945, 5, 8)) == '"1945/05/08"'

	# Cleanup
	sdjson.encoders.unregister(date)


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_date_tuple() -> None:
	# Create and register a custom encoder for date that turns it into a timetuple
	@sdjson.encoders.register(date)
	def encode_date_tuple(obj):
		return obj.timetuple()

	assert sdjson.dumps(date(1945, 5, 8)) == "[1945, 5, 8, 0, 0, 0, 1, 128, -1]"

	# Cleanup
	sdjson.encoders.unregister(date)


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_time_float() -> None:
	# Create and register a custom encoder for time that turns it into a float
	@sdjson.encoders.register(time)
	def encode_date_float(obj):
		return int(timedelta(hours=obj.hour, minutes=obj.minute, seconds=obj.second).total_seconds())

	assert sdjson.dumps(time(9, 10, 11)) == "33011"

	# Cleanup
	sdjson.encoders.unregister(time)


# Create and register a custom encoder for date that turns it into a float


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_time_str() -> None:
	# Create and register a custom encoder for time that turns it into a str
	@sdjson.encoders.register(time)
	def encode_time_str(obj):
		return f"{obj:%H:%M:%S}"

	assert sdjson.dumps(time(9, 10, 11)) == '"09:10:11"'

	# Cleanup
	sdjson.encoders.unregister(time)


@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_time_tuple() -> None:
	# Create and register a custom encoder for time that turns it into a timetuple
	@sdjson.encoders.register(time)
	def encode_time_tuple(obj):
		return obj.hour, obj.minute, obj.second

	assert sdjson.dumps(time(9, 10, 11)) == "[9, 10, 11]"

	# Cleanup
	sdjson.encoders.unregister(time)


@pytest.mark.xfail(reason="Not implemented in CPython yet.")
@pytest.mark.usefixtures("monkeypatch_sdjson")
def test_named_tuple() -> None:
	# Ref: https://bugs.python.org/issue30343
	#      https://github.com/python/cpython/pull/1558

	Student = collections.namedtuple("Student", "name, age, teacher")

	try:

		@sdjson.encoders.register(Student)
		def encode_student(obj):
			return {
					"name": obj.name,
					"age": obj.age,
					"teacher": obj.teacher,
					}

		assert sdjson.dumps(Student("Alice", 12, "Sue")) == '{"name": "Alice", "age": 12, "teacher": "Sue"}'

	finally:
		# Cleanup
		sdjson.encoders.unregister(Student, True)