File: test_enum.py

package info (click to toggle)
json-tricks 3.17.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 372 kB
  • sloc: python: 2,319; makefile: 159
file content (109 lines) | stat: -rw-r--r-- 2,869 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from datetime import datetime
from functools import partial
from enum import Enum, IntEnum
from json_tricks import dumps, loads, encode_intenums_inplace
from json_tricks.encoders import enum_instance_encode


PY2 = sys.version_info[0] == 2


class MyEnum(Enum):
	member1 = 'VALUE1'
	member2 = 'VALUE2'


class MyIntEnum(IntEnum):
	int_member = 1


def test_enum():
	member = MyEnum.member1
	txt = dumps(member)
	back = loads(txt)

	assert isinstance(back, MyEnum)
	assert back == member


def test_enum_instance_global():
	json = '{"__enum__": {"__enum_instance_type__": [null, "MyEnum"], "name": "member1"}}'
	back = loads(json, cls_lookup_map=globals())
	assert isinstance(back, MyEnum)
	assert back == MyEnum.member1


def test_enum_primitives():
	member = MyEnum.member1
	txt = dumps(member, primitives=True)
	assert txt == '{"member1": "VALUE1"}'


def test_encode_int_enum():
	member = MyIntEnum.int_member
	txt = dumps(member)
	# IntEnum are serialized as strings in enum34 for python < 3.4. This comes from how the JSON serializer work. We can't do anything about this besides documenting.
	# See https://bitbucket.org/stoneleaf/enum34/issues/17/difference-between-enum34-and-enum-json
	if PY2:
		assert txt == u"MyIntEnum.int_member"
	else:
		assert txt == "1"


def test_encode_int_enum_inplace():
	obj = {
		'int_member': MyIntEnum.int_member,
		'list': [MyIntEnum.int_member],
		'nested': {
			'member': MyIntEnum.int_member,
		}
	}

	txt = dumps(encode_intenums_inplace(obj))
	data = loads(txt)

	assert isinstance(data['int_member'], MyIntEnum)
	assert data['int_member'] == MyIntEnum.int_member
	assert isinstance(data['list'][0], MyIntEnum)
	assert isinstance(data['nested']['member'], MyIntEnum)


class EnumValueTest(object):
	alpha = 37
	def __init__(self, beta):
		self.beta = beta


class CombineComplexTypesEnum(Enum):
	class_inst = EnumValueTest(beta=42)
	timepoint = datetime(year=1988, month=3, day=15, hour=8, minute=3, second=59, microsecond=7)
	img = 1j


def test_complex_types_enum():
	obj = [
		CombineComplexTypesEnum.timepoint,
		CombineComplexTypesEnum.img,
		CombineComplexTypesEnum.class_inst,
	]
	txt = dumps(encode_intenums_inplace(obj))
	back = loads(txt)
	assert obj == back


def test_with_value():
	obj = [CombineComplexTypesEnum.class_inst, CombineComplexTypesEnum.timepoint]
	encoder = partial(enum_instance_encode, with_enum_value=True)
	txt = dumps(obj, extra_obj_encoders=(encoder,))
	assert '"value":' in txt
	back = loads(txt, obj_pairs_hooks=())
	class_inst_encoding = loads(dumps(CombineComplexTypesEnum.class_inst.value), obj_pairs_hooks=())
	timepoint_encoding = loads(dumps(CombineComplexTypesEnum.timepoint.value), obj_pairs_hooks=())
	assert back[0]['__enum__']['value'] == class_inst_encoding
	assert back[1]['__enum__']['value'] == timepoint_encoding