Reputation: 660
Here is the problem: I have 2 seperate tests, one is based on Django's TestCase and another one inherits from APITestCase (Django Rest Framework). When running separately no errors occur, but when I run APITestCase after TestCase self.assertEqual(response.status_code, status.HTTP_201_CREATED) doesn't work anymore.
Here is a part of code:
class CalendarTests(APITestCase):
def setUp(self):
self.user = User.objects.create_user(
email='[email protected]', password='secret', username='tester')
self.calendar = Calendar(owner=self.user, caltype='p', name='Test Calendar')
self.calendar.save();
def test_api_create_bla(self):
self.client.login(username='[email protected]', password='secret')
url = reverse('calendar-api')
data_initial = {'text': 'testing hurray', 'cal_date':'2015-03-15', 'calendar':1}
data_expected = {'cal_date': '2015-03-15',
'text': u'testing hurray',
'otype': 'd',
'owner': u'tester',
'calendar': 1,
'id': 1}
response = self.client.post(url, data_initial, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data, data_expected)
self.client.logout()
Any help appreciated.
Upvotes: 0
Views: 2277
Reputation: 660
Ok, guys, thanks, it's solved for me. By printing response object I figured out that calendar_id was wrong. Because I use MySQL with InnoDB (maybe have to switch to PostreSQL), the IntegrityError "Cannot add or update a child row: a foreign key constraint fails" caused strange behaviour in the second test. The solution that helped me is to switch in settings.py from InnoDB engine to MyISAM this way:
db_options = "INNODB"
import sys
if 'test' in sys.argv:
db_options = "MYISAM"
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'callist',
'USER': 'test',
'PASSWORD': 'test',
'HOST': 'localhost',
'PORT': '80',
'OPTIONS': {'init_command': 'SET storage_engine=' + db_options},
}
}
Will think of switching to PostreSQL. Thanks for those who tried to help.
Upvotes: 0