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
| #!/usr/bin/env python # -*- coding:utf-8 -*- # Author: wxnacy([email protected]) # Description:
import unittest from wpy import JSON
class TestCase(unittest.TestCase): def setUp(self): pass def teardown(self): pass
def test_filter(self): book = dict(id = 2, name = 'size', price = 5) user = dict(id = 1, name = "wxnacy", book= book)
res = JSON.filter(source = dict(user), source_include=['id', 'book']) data = dict(id = 1, book= book) self.assertEqual(res, data)
res = JSON.filter(dict(user), 'id', 'book') data = dict(id = 1, book= book) self.assertEqual(res, data)
res = JSON.filter(dict(user), 'id', 'book.id') data = dict(id = 1, book= dict(id = 2)) self.assertEqual(res, data)
res = JSON.filter(dict(user), 'id', 'book.id', 'book.name') data = dict(id = 1, book= dict(id = 2, name = "size")) self.assertEqual(res, data)
res = JSON.filter(dict(user), 'id', 'book[id,name]') data = dict(id = 1, book= dict(id = 2, name = "size")) self.assertEqual(res, data)
res = JSON.filter(dict(user), source_exclude=['name', 'book']) data = dict(id = 1) self.assertEqual(res, data)
res = JSON.filter(dict(user), source_exclude=['name', 'book.price']) data = dict(id = 1, book= dict(id = 2, name = "size")) self.assertEqual(res, data)
print(user) res = JSON.filter(dict(user), source_exclude=['name', 'book.price', 'book.id']) data = dict(id = 1, book= dict(name = "size")) self.assertEqual(res, data)
res = JSON.filter(dict(user), source_exclude=['name', 'book[id,price]']) data = dict(id = 1, book= dict(name = "size")) self.assertEqual(res, data)
def test_getattr(self):
book = dict(id = 2, name = 'size', price = 5) user = dict(id = 1, name = "wxnacy", book= book) user = JSON.BaseDict(user) self.assertEqual(1, user.id) self.assertEqual(book, user.book)
def test_filter_list(self): book = dict(id = 2, name = 'size', price = 5) book1 = dict(id = 3, name = 'bo', price = 6) user = dict(id = 1, name = "wxnacy", books= [book, book1])
res = JSON.filter(user, 'id', 'books.id') data = dict(id = 1, books = [dict(id = 2), dict(id = 3)]) self.assertEqual(res, data)
res = JSON.filter(user, 'id', 'books.id', 'books.price') data = dict(id = 1, books = [dict(id = 2, price = 5), dict(id = 3, price=6)]) self.assertEqual(res, data)
res = JSON.filter(user, 'id', 'books[id,price]') data = dict(id = 1, books = [dict(id = 2, price = 5), dict(id = 3, price=6)]) self.assertEqual(res, data)
res = JSON.filter(user, source_exclude=['name', 'books.name']) data = dict(id = 1, books = [dict(id = 2, price = 5), dict(id = 3, price=6)]) self.assertEqual(res, data)
res = JSON.filter(user, source_exclude=['name', 'books.name', 'books.price']) data = dict(id = 1, books = [dict(id = 2), dict(id = 3)]) self.assertEqual(res, data)
res = JSON.filter(user, source_exclude = ['name', 'books[name,price]']) data = dict(id = 1, books = [dict(id = 2), dict(id = 3)]) self.assertEqual(res, data)
if __name__ == "__main__": unittest.main()
|