forked from coleifer/peewee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra_fields.py
57 lines (40 loc) · 1.45 KB
/
extra_fields.py
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
from peewee import *
from playhouse.fields import CompressedField
from playhouse.fields import PickleField
from .base import db
from .base import ModelTestCase
from .base import TestModel
class Comp(TestModel):
key = TextField()
data = CompressedField()
class Pickled(TestModel):
key = TextField()
data = PickleField()
class TestCompressedField(ModelTestCase):
requires = [Comp]
def test_compressed_field(self):
a = b'a' * 1024
b = b'b' * 1024
Comp.create(data=a, key='a')
Comp.create(data=b, key='b')
a_db = Comp.get(Comp.key == 'a')
self.assertEqual(a_db.data, a)
b_db = Comp.get(Comp.key == 'b')
self.assertEqual(b_db.data, b)
# Get at the underlying data.
CompTbl = Table('comp', ('id', 'data', 'key')).bind(self.database)
obj = CompTbl.select().where(CompTbl.key == 'a').get()
self.assertEqual(obj['key'], 'a')
# Ensure that the data actually was compressed.
self.assertTrue(len(obj['data']) < 1024)
class TestPickleField(ModelTestCase):
requires = [Pickled]
def test_pickle_field(self):
a = {'k1': 'v1', 'k2': [0, 1, 2], 'k3': None}
b = 'just a string'
Pickled.create(data=a, key='a')
Pickled.create(data=b, key='b')
a_db = Pickled.get(Pickled.key == 'a')
self.assertEqual(a_db.data, a)
b_db = Pickled.get(Pickled.key == 'b')
self.assertEqual(b_db.data, b)