Projet

Général

Profil

0001-snapshots-lower-size-where-a-full-snapshot-is-prefer.patch

Frédéric Péters, 20 mai 2022 13:48

Télécharger (3,49 ko)

Voir les différences:

Subject: [PATCH] snapshots: lower size where a full snapshot is preferred over
 a diff (#65497)

 tests/test_snapshots.py | 13 +++++++------
 wcs/snapshots.py        |  5 +++--
 2 files changed, 10 insertions(+), 8 deletions(-)
tests/test_snapshots.py
10 10
from wcs.carddef import CardDef
11 11
from wcs.categories import Category
12 12
from wcs.data_sources import NamedDataSource
13
from wcs.fields import ItemField, StringField
13
from wcs.fields import CommentField, ItemField, StringField
14 14
from wcs.formdef import FormDef
15 15
from wcs.mail_templates import MailTemplate
16 16
from wcs.qommon.form import UploadedFile
......
62 62
def test_snapshot_basics(pub):
63 63
    formdef = FormDef()
64 64
    formdef.name = 'testform'
65
    formdef.fields = []
65
    # start with a big content
66
    formdef.fields = [CommentField(id='0', label='Test ' * 500, type='comment')]
66 67
    formdef.store()
67 68

  
68 69
    # first occurence, complete snapshot stored
......
98 99

  
99 100
    # patch is longer as serialization, store serialization
100 101
    formdef.name = 'testform3'
101
    formdef.fields = [StringField(id=str(i), label='Test %s' % i, type='string') for i in range(0, 10)]
102
    formdef.fields += [StringField(id=str(i + 1), label='Test %s' % i, type='string') for i in range(0, 10)]
102 103
    formdef.store()
103 104
    assert pub.snapshot_class.count() == 4
104 105
    snapshot4 = pub.snapshot_class.get_latest('formdef', formdef.id)
......
119 120
    snapshot6 = None
120 121
    for i in range(10):
121 122
        formdef.name = 'testform%s' % (i + 6)
122
        formdef.fields.append(StringField(id=str(i + 10), label='Test %s' % (i + 10), type='string'))
123
        formdef.fields.append(StringField(id=str(i + 11), label='Test %s' % (i + 10), type='string'))
123 124
        formdef.store()
124 125
        snapshot = pub.snapshot_class.get_latest('formdef', formdef.id)
125
        assert snapshot.patch is not None
126
        assert snapshot.patch is None or len(snapshot.patch) < len(snapshot.get_serialization()) / 10
126 127
        snapshot6 = snapshot6 or snapshot
127 128
    assert pub.snapshot_class.count() == 15
128 129

  
......
148 149
    )
149 150
    assert latest_complete.id == snapshot4.id
150 151
    assert snapshot6.instance  # possible to restore
151
    assert [int(f.id) for f in snapshot6.instance.fields] == list(range(0, 11))
152
    assert [int(f.id) for f in snapshot6.instance.fields] == list(range(0, 12))
152 153

  
153 154

  
154 155
def test_snapshot_diff(pub):
wcs/snapshots.py
175 175
                store_snapshot = False
176 176

  
177 177
        if store_snapshot:
178
            if len(obj.serialization) > len(patch):
179
                # serialization is bigger than patch, store patch
178
            if len(patch) < min(len(obj.serialization) / 10, 1_000_000):
179
                # patch is small (compared to full serialization and an absolute value)
180
                # store patch instead of full serialization
180 181
                obj.serialization = None
181 182
                obj.patch = patch
182 183
            # else: keep serialization and ignore patch
183
-