I'm attempting to create a fixture from my development database data and storing it my test database. This data happens to be only one model. I ran the dumpdata
command as such:
python manage.py dumpdata minerals.mineral --all --indent=4
--output=minerals/test_minerals/fixtures/test_mineral_data.json
When attempting to run the python manage.py test
, the following error is raised:
django.db.utils.IntegrityError: Problem installing fixture
'../minerals/test_minerals/fixtures/test_mineral_data.json':
Could not load minerals.Mineral(pk=872): UNIQUE constraint failed: minerals_mineral.name
https://docs.djangoproject.com/en/2.2/topics/testing/tools/#django.test.TransactionTestCase.fixtures
By default, fixtures are only loaded into the default database.
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
I was under the initial impression that fixtures were stored in an empty database when attempting to unit test but apparently that is incorrect? What can be done to emulate the data so I can test?
tests.py
class SearchFormResults(TestCase):
fixtures = ['minerals/test_minerals/fixtures/test_mineral_data.json']
def test_user_search_query_fail(self):
response = self.client.get(
reverse("minerals:search_list"), data={'query': 'Kryptonite'},
HTTP_REFERRER=reverse(
"minerals:letter_list", kwargs={'query': 'N'}
), follow=True
)
models.py
class Mineral(models.Model):
name = models.CharField(unique=True, max_length=255)
image_filename = models.ImageField(max_length=255, blank=True)
image_caption = models.CharField(max_length=255, blank=True)
category = models.CharField(max_length=255, blank=True)
formula = models.CharField(max_length=255, blank=True)
strunz_classification = models.CharField(max_length=255, blank=True)
crystal_system = models.CharField(max_length=255, blank=True)
unit_cell = models.CharField(max_length=255, blank=True)
color = models.CharField(max_length=255, blank=True)
crystal_symmetry = models.CharField(max_length=255, blank=True)
cleavage = models.CharField(max_length=255, blank=True)
mohs_scale_hardness = models.CharField(max_length=255, blank=True)
luster = models.CharField(max_length=255, blank=True)
streak = models.CharField(max_length=255, blank=True)
diaphaneity = models.CharField(max_length=255, blank=True)
optical_properties = models.CharField(max_length=255, blank=True)
refractive_index = models.CharField(max_length=255, blank=True)
crystal_habit = models.CharField(max_length=255, blank=True)
specific_gravity = models.CharField(max_length=255, blank=True)
group = models.CharField(max_length=255, blank=True)
def __str__(self):
return self.name