1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from __future__ import absolute_import, division, print_function |
3
|
|
|
import json |
4
|
|
|
import logging |
5
|
|
|
from enum import Enum |
6
|
|
|
import testtools |
7
|
|
|
|
8
|
|
|
from auxlib.entity import Entity, StringField, ComposableField, EnumField, ListField |
9
|
|
|
|
10
|
|
|
log = logging.getLogger(__name__) |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
json_string_1 = """ |
15
|
|
|
{ |
16
|
|
|
"actor": "User", |
17
|
|
|
"repository": "Repository", |
18
|
|
|
"push": { |
19
|
|
|
"changes": [ |
20
|
|
|
{ |
21
|
|
|
"new": { |
22
|
|
|
"type": "branch", |
23
|
|
|
"name": "name-of-branch", |
24
|
|
|
"target": { |
25
|
|
|
"type": "commit", |
26
|
|
|
"hash": "709d658dc5b6d6afcd46049c2f332ee3f515a67d", |
27
|
|
|
"author": User, |
28
|
|
|
"message": "new commit message", |
29
|
|
|
"date": "2015-06-09T03:34:49+00:00", |
30
|
|
|
"parents": [ |
31
|
|
|
{ |
32
|
|
|
"hash": "1e65c05c1d5171631d92438a13901ca7dae9618c", |
33
|
|
|
"type": "commit" |
34
|
|
|
} |
35
|
|
|
] |
36
|
|
|
} |
37
|
|
|
}, |
38
|
|
|
"old": { |
39
|
|
|
"type": "branch", |
40
|
|
|
"name": "name-of-branch", |
41
|
|
|
"target": { |
42
|
|
|
"type": "commit", |
43
|
|
|
"hash": "1e65c05c1d5171631d92438a13901ca7dae9618c", |
44
|
|
|
"author": User, |
45
|
|
|
"message": "old commit message", |
46
|
|
|
"date": "2015-06-08T21:34:56+00:00", |
47
|
|
|
"parents": [ |
48
|
|
|
{ |
49
|
|
|
"hash": "e0d0c2041e09746be5ce4b55067d5a8e3098c843", |
50
|
|
|
"type": "commit" |
51
|
|
|
} |
52
|
|
|
] |
53
|
|
|
} |
54
|
|
|
}, |
55
|
|
|
"created": false, |
56
|
|
|
"forced": false, |
57
|
|
|
"closed": false |
58
|
|
|
} |
59
|
|
|
] |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
""" |
63
|
|
|
|
64
|
|
|
json_string_simple = """ |
65
|
|
|
{ |
66
|
|
|
"actor": "User", |
67
|
|
|
"repository": "Repository", |
68
|
|
|
"parent": { |
69
|
|
|
"hash": "e0d0c2041e09746be5ce4b55067d5a8e3098c843", |
70
|
|
|
"type": "commit" |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
""" |
74
|
|
|
|
75
|
|
|
json_string_simple_list = """ |
76
|
|
|
{ |
77
|
|
|
"parents": [ |
78
|
|
|
{ |
79
|
|
|
"hash": "e0d0c2041e09746be5ce4b55067d5a8e3098c843", |
80
|
|
|
"type": "commit" |
81
|
|
|
}, |
82
|
|
|
{ |
83
|
|
|
"hash": "e0d0c2041e09746be5ce4b55061029384756beef", |
84
|
|
|
"type": "tag" |
85
|
|
|
} |
86
|
|
|
] |
87
|
|
|
} |
88
|
|
|
""" |
89
|
|
|
class CommitType(Enum): |
90
|
|
|
COMMIT = "commit" |
91
|
|
|
TAG = "tag" |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
class Commit(Entity): |
95
|
|
|
|
96
|
|
|
hash = StringField() |
97
|
|
|
type = EnumField(CommitType) |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
class Simple(Entity): |
101
|
|
|
actor = StringField() |
102
|
|
|
repository = StringField() |
103
|
|
|
parent = ComposableField(Commit) |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
class SimpleList(Entity): |
107
|
|
|
parents = ListField(Commit) |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
class ClassFieldTests(testtools.TestCase): |
111
|
|
|
|
112
|
|
|
def test_most_simplest(self): |
113
|
|
|
obj_dict = json.loads(json_string_simple) |
114
|
|
|
simple = Simple(**obj_dict) |
115
|
|
|
assert simple.actor == "User" |
116
|
|
|
assert isinstance(simple.parent, Commit) |
117
|
|
|
assert simple.parent.hash == "e0d0c2041e09746be5ce4b55067d5a8e3098c843" |
118
|
|
|
assert simple.parent.type == CommitType.COMMIT |
119
|
|
|
|
120
|
|
|
print(json.dumps(simple.dump())) |
121
|
|
|
|
122
|
|
|
assert simple == Simple(**obj_dict) |
123
|
|
|
assert Simple(**simple.dump()) == Simple(**obj_dict) |
124
|
|
|
|
125
|
|
|
def test_simple_list(self): |
126
|
|
|
obj_dict = json.loads(json_string_simple_list) |
127
|
|
|
simplelist = SimpleList(**obj_dict) |
128
|
|
|
|
129
|
|
|
assert len(simplelist.parents) == 2 |
130
|
|
|
|
131
|
|
|
assert simplelist == SimpleList(**obj_dict) |
132
|
|
|
assert SimpleList(**simplelist.dump()) == SimpleList(**obj_dict) |
133
|
|
|
|