Skip to content

Commit

Permalink
Merge pull request #8 from wesleykendall/develop
Browse files Browse the repository at this point in the history
fixed error related to handling deletions of multiple objects
  • Loading branch information
wesleykendall committed May 24, 2014
2 parents 6fd94cc + 76bdae1 commit 8d19390
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dynamic_initial_data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def handle_deletions(self):
RegisteredForDeletionReceipt(
model_obj_type=ContentType.objects.get_for_model(model_obj), model_obj_id=model_obj.id,
register_time=now)
for model_obj in self.model_objs_registered_for_deletion
for model_obj in set(self.model_objs_registered_for_deletion)
]

# Do a bulk upsert on all of the receipts, updating their registration time.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models


class Migration(SchemaMigration):

def forwards(self, orm):
# Adding unique constraint on 'RegisteredForDeletionReceipt', fields ['model_obj_type', 'model_obj_id']
db.create_unique(u'dynamic_initial_data_registeredfordeletionreceipt', ['model_obj_type_id', 'model_obj_id'])


def backwards(self, orm):
# Removing unique constraint on 'RegisteredForDeletionReceipt', fields ['model_obj_type', 'model_obj_id']
db.delete_unique(u'dynamic_initial_data_registeredfordeletionreceipt', ['model_obj_type_id', 'model_obj_id'])


models = {
u'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
u'dynamic_initial_data.registeredfordeletionreceipt': {
'Meta': {'unique_together': "(('model_obj_type', 'model_obj_id'),)", 'object_name': 'RegisteredForDeletionReceipt'},
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model_obj_id': ('django.db.models.fields.PositiveIntegerField', [], {}),
'model_obj_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
'register_time': ('django.db.models.fields.DateTimeField', [], {})
}
}

complete_apps = ['dynamic_initial_data']
3 changes: 3 additions & 0 deletions dynamic_initial_data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ class RegisteredForDeletionReceipt(models.Model):

# Use manager utils for bulk updating capabilities
objects = ManagerUtilsManager()

class Meta:
unique_together = ('model_obj_type', 'model_obj_id')
15 changes: 15 additions & 0 deletions dynamic_initial_data/tests/base_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ def test_create_one_obj(self):
self.assertEquals(receipt.model_obj_id, account.id)
self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

def test_create_dup_objs(self):
"""
Tests creating duplicate objects for deletion.
"""
account = G(Account)
self.initial_data_updater.model_objs_registered_for_deletion = [account, account]

self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 0)
with freeze_time('2013-04-12'):
self.initial_data_updater.handle_deletions()
receipt = RegisteredForDeletionReceipt.objects.get()
self.assertEquals(receipt.model_obj_type, ContentType.objects.get_for_model(Account))
self.assertEquals(receipt.model_obj_id, account.id)
self.assertEquals(receipt.register_time, datetime(2013, 4, 12))

def test_create_delete_one_obj(self):
"""
Tests creating one object to handle for deletion and then deleting it.
Expand Down
25 changes: 25 additions & 0 deletions dynamic_initial_data/tests/integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ def update_initial_data(self):
# Verify an account object was created
self.assertEquals(Account.objects.count(), 1)

@override_settings(INSTALLED_APPS=('one_installed_test_app',))
def test_multiple_same_objects(self):
"""
Tests initial data when registering the same object for deletion twice.
"""
class AccountInitialData1(BaseInitialData):
"""
Initial data code that registers the same object many times for deletion
"""
def update_initial_data(self):
# Return the object from update_initial_data, thus registering it for deletion
account = Account.objects.get_or_create()[0]
return [account, account, account]

# Verify no account objects exist
self.assertEquals(Account.objects.count(), 0)

with patch.object(InitialDataUpdater, 'load_app', return_value=AccountInitialData1):
InitialDataUpdater().update_all_apps()
InitialDataUpdater().update_all_apps()

# Verify an account object was created and is managed by a deletion receipt
self.assertEquals(Account.objects.count(), 1)
self.assertEquals(RegisteredForDeletionReceipt.objects.count(), 1)

@override_settings(INSTALLED_APPS=('one_installed_test_app',))
def test_handle_deletions_returned_from_update_initial_data(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion dynamic_initial_data/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.2.1'
__version__ = '0.3.0'

0 comments on commit 8d19390

Please sign in to comment.