Skip to content

Commit

Permalink
Merge pull request #1551 from MattFisher/syncdata-natural-keys
Browse files Browse the repository at this point in the history
Failing test and fixture for syncdata with natural keys
  • Loading branch information
camilonova committed Jun 11, 2022
2 parents 02aebf0 + 6444385 commit 4c59aec
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"pk" : 2,
"fields" : {
"global_id" : "must be unique"
},
"model" : "testapp.UniqueTestAppModel"
}
]
30 changes: 30 additions & 0 deletions tests/management/commands/fixtures/fixture_with_natural_key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"model": "auth.group",
"pk": 1,
"fields": {
"name": "teachers",
"permissions": []
}
},
{
"model": "auth.user",
"pk": 1,
"fields": {
"password": "",
"last_login": null,
"is_superuser": false,
"username": "staff",
"first_name": "",
"last_name": "",
"email": "",
"is_staff": true,
"is_active": true,
"date_joined": "2017-05-01T03:49:00.823Z",
"groups": [
["teachers"]
],
"user_permissions": []
}
}
]
16 changes: 15 additions & 1 deletion tests/management/commands/test_syncdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
import os

import pytest
from django.contrib.auth.models import User
from django.contrib.auth.models import User, Group
from django.core.management import call_command, CommandError
from django.test import TestCase
from django.test.utils import override_settings
from io import StringIO

from unittest.mock import patch

from tests.testapp.models import UniqueTestAppModel


TEST_FIXTURE_DIR = os.path.join(os.path.dirname(__file__), 'fixtures')

Expand Down Expand Up @@ -72,3 +74,15 @@ def test_should_delete_old_objects_and_load_data_from_json_fixture(self, m_stdou
self.assertTrue(User.objects.filter(username='jdoe').exists())
self.assertEqual(User.objects.count(), 1)
self.assertIn('Installed 1 object from 1 fixture', m_stdout.getvalue())

def test_should_handle_natural_keys_in_fixtures(self):
call_command('syncdata', 'fixture_with_natural_key.json')

self.assertTrue(Group.objects.filter(name='teachers').exists())
self.assertTrue(User.objects.filter(username='staff').exists())

def test_should_resolve_transient_constraint_conflicts_in_fixtures(self):
UniqueTestAppModel.objects.create(pk=1, global_id="must be unique")
# This fixture creates a second instance with a different pk but the same global_id
call_command('syncdata', 'fixture_with_constraint.json')
self.assertEqual(UniqueTestAppModel.objects.count(), 1)

0 comments on commit 4c59aec

Please sign in to comment.