Skip to content

Commit 75cd2e2

Browse files
committed
use nested atomic transaction for reversion
1 parent 82db131 commit 75cd2e2

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ env:
1414
- DJANGO="django==1.8.18" WITH_WQDB="pip install wq.db==1.0.0rc1"
1515
- DJANGO="django==1.11.1"
1616
- DJANGO="django==1.11.1" WITH_WQDB="pip install wq.db==1.0.0rc1"
17+
- DJANGO="django==1.11.1" WITH_WQDB="pip install wq.db==1.0.0rc1" WITH_REVERSION="pip install django-reversion"
1718
- DJANGO="django==1.11.1" LINT="pip install flake8"
1819
before_script:
1920
- createdb -Upostgres data_wizard_test
@@ -24,6 +25,7 @@ install:
2425
- pip install psycopg2
2526
- pip install .
2627
- $WITH_WQDB
28+
- $WITH_REVERSION
2729
- $LINT
2830
script:
2931
- ./runtests.sh

data_wizard/tasks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .signals import import_complete, new_metadata
66
from functools import wraps
77

8+
from django.db import transaction
89
from django.contrib.contenttypes.models import ContentType
910
from django.contrib.auth import get_user_model
1011

@@ -780,7 +781,8 @@ def import_row(run, row, instance_globals, matched):
780781
try:
781782
serializer = Serializer(data=parse_json_form(record))
782783
if serializer.is_valid():
783-
obj = serializer.save()
784+
with transaction.atomic():
785+
obj = serializer.save()
784786
error = None
785787
else:
786788
obj = None

tests/data_app/wizard.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ class Meta:
99
fields = "__all__"
1010

1111

12+
class IncompleteSerializer(serializers.ModelSerializer):
13+
# Serializer will report is_valid(), but save will fail
14+
15+
date = serializers.CharField() # Should be date field
16+
color = serializers.CharField() # Should limit length (and choices)
17+
18+
class Meta:
19+
model = SimpleModel
20+
fields = "__all__"
21+
22+
1223
class FKSerializer(serializers.ModelSerializer):
1324
class Meta:
1425
model = FKModel
@@ -48,6 +59,7 @@ class Meta:
4859

4960

5061
registry.register('Simple Model', SimpleSerializer)
62+
registry.register('Simple Model - Incomplete', IncompleteSerializer)
5163
registry.register('FK Model', FKSerializer)
5264
registry.register('FK Model By Name', SlugSerializer)
5365
registry.register('New Type + FK Model', NestedSerializer)

tests/test_simple_import.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,44 @@ def check_data(self, run):
134134
' Use one of these formats instead: YYYY[-MM[-DD]]."]}'
135135
])
136136
self.assert_urls(run, 'simplemodels/%s')
137+
138+
139+
class IncompleteTestCase(BaseImportTestCase):
140+
serializer_name = 'tests.data_app.wizard.IncompleteSerializer'
141+
142+
def test_incomplete_serializer(self):
143+
# Initialize identifier before auto import
144+
self.create_identifier('field notes', 'notes')
145+
146+
# Should succeed since field notes is already mapped
147+
run = self.upload_file('simplemodel.csv')
148+
self.auto_import(run, expect_input_required=False)
149+
150+
# Verify results
151+
self.check_data(run)
152+
self.assert_log(run, [
153+
'created',
154+
'auto_import',
155+
'parse_columns',
156+
'parse_row_identifiers',
157+
'do_import',
158+
'import_complete',
159+
])
160+
161+
def check_data(self, run):
162+
self.assert_status(run, 3)
163+
self.assert_ranges(run, [
164+
"Data Column 'date -> date' at Rows 1-5, Column 0",
165+
"Data Column 'color -> color' at Rows 1-5, Column 1",
166+
"Data Column 'field notes -> notes' at Rows 1-5, Column 2",
167+
])
168+
self.assert_records(run, [
169+
"imported '2017-06-01: red (Test Note 1)' at row 1",
170+
"imported '2017-06-02: green (Test Note 2)' at row 2",
171+
"imported '2017-06-03: blue (Test Note 3)' at row 3",
172+
"failed at row 4: DataError('value too long for type character"
173+
" varying(5)\\n',)",
174+
"failed at row 5: ValidationError([\"'2017-06-50' value has the"
175+
" correct format (YYYY-MM-DD) but it is an invalid date.\"])"
176+
])
177+
self.assert_urls(run, 'simplemodels/%s')

0 commit comments

Comments
 (0)