AssertionError: 400 != 201 when running APITestCase after TestCase
AssertionError: 400 != 201 when running APITestCase after TestCase
问题来了:
我有 2 个单独的测试,一个基于 Django 的 TestCase,另一个继承自 APITestCase(Django Rest Framework)。当 运行ning 分别没有错误发生,但是当我 运行 APITestCase after TestCase self.assertEqual(response.status_code, status.HTTP_201_CREATED) 不再工作了。
这是部分代码:
class CalendarTests(APITestCase):
def setUp(self):
self.user = User.objects.create_user(
email='test@test.com', 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='test@test.com', 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()
感谢任何帮助。
好的,伙计们,谢谢,我已经解决了。
通过打印响应对象,我发现 calendar_id 是错误的。因为我将 MySQL 与 InnoDB 一起使用(可能必须切换到 PostreSQL),IntegrityError "Cannot add or update a child row: a foreign key constraint fails" 在第二次测试中导致了奇怪的行为。帮助我的解决方案是通过这种方式将 settings.py 从 InnoDB 引擎切换到 MyISAM:
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},
}
}
会考虑切换到 PostreSQL。
感谢那些试图提供帮助的人。
问题来了: 我有 2 个单独的测试,一个基于 Django 的 TestCase,另一个继承自 APITestCase(Django Rest Framework)。当 运行ning 分别没有错误发生,但是当我 运行 APITestCase after TestCase self.assertEqual(response.status_code, status.HTTP_201_CREATED) 不再工作了。
这是部分代码:
class CalendarTests(APITestCase):
def setUp(self):
self.user = User.objects.create_user(
email='test@test.com', 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='test@test.com', 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()
感谢任何帮助。
好的,伙计们,谢谢,我已经解决了。 通过打印响应对象,我发现 calendar_id 是错误的。因为我将 MySQL 与 InnoDB 一起使用(可能必须切换到 PostreSQL),IntegrityError "Cannot add or update a child row: a foreign key constraint fails" 在第二次测试中导致了奇怪的行为。帮助我的解决方案是通过这种方式将 settings.py 从 InnoDB 引擎切换到 MyISAM:
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},
}
}
会考虑切换到 PostreSQL。 感谢那些试图提供帮助的人。