| 1 | from tg.jsonify import jsonify, encode |
|---|
| 2 | from simplejson import loads |
|---|
| 3 | from datetime import date |
|---|
| 4 | |
|---|
| 5 | class Person(object): |
|---|
| 6 | def __init__(self, first_name, last_name): |
|---|
| 7 | self.first_name = first_name |
|---|
| 8 | self.last_name = last_name |
|---|
| 9 | |
|---|
| 10 | @property |
|---|
| 11 | def name(self): |
|---|
| 12 | return '%s %s' % (self.first_name, self.last_name) |
|---|
| 13 | |
|---|
| 14 | def __json__(self): |
|---|
| 15 | return dict(first_name=self.first_name, last_name=self.last_name) |
|---|
| 16 | |
|---|
| 17 | def test_simple_rule(): |
|---|
| 18 | # skip this test if simplegeneric is not installed |
|---|
| 19 | try: |
|---|
| 20 | import simplegeneric |
|---|
| 21 | except ImportError: |
|---|
| 22 | return |
|---|
| 23 | |
|---|
| 24 | # create a Person instance |
|---|
| 25 | p = Person('Jonathan', 'LaCour') |
|---|
| 26 | |
|---|
| 27 | # encode the object using the existing "default" rules |
|---|
| 28 | result = loads(encode(p)) |
|---|
| 29 | assert result['first_name'] == 'Jonathan' |
|---|
| 30 | assert result['last_name'] == 'LaCour' |
|---|
| 31 | assert len(result) == 2 |
|---|
| 32 | |
|---|
| 33 | # register a generic JSON rule |
|---|
| 34 | @jsonify.when_type(Person) |
|---|
| 35 | def jsonify_person(obj): |
|---|
| 36 | return dict( |
|---|
| 37 | name=obj.name |
|---|
| 38 | ) |
|---|
| 39 | |
|---|
| 40 | # encode the object using our new rule |
|---|
| 41 | result = loads(encode(p)) |
|---|
| 42 | assert result['name'] == 'Jonathan LaCour' |
|---|
| 43 | assert len(result) == 1 |
|---|
| 44 | |
|---|
| 45 | def test_builtin_override(): |
|---|
| 46 | # skip this test if simplegeneric is not installed |
|---|
| 47 | try: |
|---|
| 48 | import simplegeneric |
|---|
| 49 | except ImportError: |
|---|
| 50 | return |
|---|
| 51 | |
|---|
| 52 | # create a few date objects |
|---|
| 53 | d1 = date(1979, 10, 12) |
|---|
| 54 | d2 = date(2000, 1, 1) |
|---|
| 55 | d3 = date(2012, 1, 1) |
|---|
| 56 | |
|---|
| 57 | # jsonify using the built in rules |
|---|
| 58 | result1 = encode(d1) |
|---|
| 59 | assert result1 == '"1979-10-12"' |
|---|
| 60 | result2 = encode(d2) |
|---|
| 61 | assert result2 == '"2000-01-01"' |
|---|
| 62 | result3 = encode(d3) |
|---|
| 63 | assert result3 == '"2012-01-01"' |
|---|
| 64 | |
|---|
| 65 | # create a custom rule |
|---|
| 66 | @jsonify.when_type(date) |
|---|
| 67 | def jsonify_date(obj): |
|---|
| 68 | if obj.year == 1979 and obj.month == 10 and obj.day == 12: |
|---|
| 69 | return "Jon's Birthday!" |
|---|
| 70 | elif obj.year == 2000 and obj.month == 1 and obj.day == 1: |
|---|
| 71 | return "Its Y2K! Panic!" |
|---|
| 72 | return '%d/%d/%d' % (obj.month, obj.day, obj.year) |
|---|
| 73 | |
|---|
| 74 | # jsonify using the built in rules |
|---|
| 75 | result1 = encode(d1) |
|---|
| 76 | assert result1 == '"Jon\'s Birthday!"' |
|---|
| 77 | result2 = encode(d2) |
|---|
| 78 | assert result2 == '"Its Y2K! Panic!"' |
|---|
| 79 | result3 = encode(d3) |
|---|
| 80 | assert result3 == '"1/1/2012"' |
|---|