From 836b4106aeef9acd30a4fd4dbca00c5d90e9aef7 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Wed, 10 Feb 2021 09:32:37 +0100 Subject: Add the option to autoincrement integer values --- .../items/5084bc6e94f24dc6976629282ef30419.yaml | 1 + .../items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml | 1 + tests/data/complex/settings.yaml | 4 +++ tests/test_collection.py | 9 ++++--- tests/test_types.py | 29 ++++++++++++++++++++++ 5 files changed, 41 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml b/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml index bb97781..3132884 100644 --- a/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml +++ b/tests/data/complex/items/5084bc6e94f24dc6976629282ef30419.yaml @@ -16,4 +16,5 @@ order: delta created: updated: epoch: +version: 0 # and a comment at the end diff --git a/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml b/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml index 44d8b89..690b680 100644 --- a/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml +++ b/tests/data/complex/items/8e9fa1ed3c1b4a30a6be7a98eda0cfa7.yaml @@ -14,3 +14,4 @@ order: created: updated: epoch: +version: 2 diff --git a/tests/data/complex/settings.yaml b/tests/data/complex/settings.yaml index bf64934..7aaf47b 100644 --- a/tests/data/complex/settings.yaml +++ b/tests/data/complex/settings.yaml @@ -48,3 +48,7 @@ fields: - name: epoch type: datetime auto: false + - name: version + type: integer + auto: increment + increment: 2 diff --git a/tests/test_collection.py b/tests/test_collection.py index 527acf9..84c535d 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -297,7 +297,7 @@ class testComplexCollection(unittest.TestCase): self.collection.settings['name'], "Fully featured lesana collection", ) - self.assertEqual(len(self.collection.settings['fields']), 13) + self.assertEqual(len(self.collection.settings['fields']), 14) self.assertIsNotNone(self.collection.stemmer) self.assertEqual(len(self.collection.indexed_fields), 8) @@ -424,21 +424,24 @@ class testComplexCollection(unittest.TestCase): eid = '5084bc6e94f24dc6976629282ef30419' entry = self.collection.entry_from_eid(eid) # we keep the old data, and check that the updated field is - # empty + # empty and the version field is 0 old_data = entry.data.copy() self.assertEqual(entry.data['updated'], None) + self.assertEqual(entry.data['version'], 0) entry.update() # after the update, fields that were not supposed to be updated # are equal to what they were before, while updated has been # changed to a datetime in this year (we don't check too deeply - # to avoid breaking tests too often with race conditions). + # to avoid breaking tests too often with race conditions) and + # version has grown to 2. for field in ('created', 'epoch'): self.assertEqual(old_data[field], entry.data[field]) now = datetime.datetime.now(datetime.timezone.utc) self.assertIsInstance(entry.data['updated'], datetime.datetime) self.assertEqual(entry.data['updated'].year, now.year) + self.assertEqual(entry.data['version'], 2) class testCollectionWithErrors(unittest.TestCase): diff --git a/tests/test_types.py b/tests/test_types.py index 260167c..1efcde1 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -82,6 +82,35 @@ class testTypes(unittest.TestCase): v = checker.update(10) self.assertEqual(v, 10) + def test_datetime_auto_increment(self): + field_def = self._get_field_def('integer') + field_def['auto'] = 'increment' + checker = types.LesanaInt(field_def, {}) + + v = checker.empty() + self.assertEqual(v, 0) + + v = checker.update(0) + self.assertEqual(v, 1) + + field_def['increment'] = -1 + checker = types.LesanaInt(field_def, {}) + v = checker.update(0) + self.assertEqual(v, -1) + + field_def['increment'] = 0.5 + checker = types.LesanaInt(field_def, {}) + with self.assertLogs() as cm: + v = checker.update(0) + self.assertIn('WARNING', cm.output[0]) + self.assertIn('Invalid configuration value', cm.output[0]) + self.assertEqual(v, 0) + + field_def['auto'] = 'false' + checker = types.LesanaInt(field_def, {}) + v = checker.update(0) + self.assertEqual(v, 0) + def test_float(self): checker = types.LesanaFloat(self._get_field_def('float'), {}) -- cgit v1.2.3