0

테스트 데이터베이스에 일부 모델을 설치하고 파일 업로드가 포함 된 사용자 정의 양식에 게시하려고합니다.모델을 만들지 않는 장고 테스트 클라이언트 (--keepdb 옵션이 사용됨)

아무 것도 데이터베이스에 지속되는 것 같지 않습니다. POST 수행시 테스트가 200 응답을 보내는 이유에 대해 확실하지 않습니다. follow = False이면 302가 아니어야합니까?

또한 데이터베이스에서 모델을 찾으면 아무 것도 찾을 수 없습니다.

--keepdb 옵션을 사용할 때 데이터베이스를 보면 아무 것도 없습니까?

내가 뭘 잘못하고 있니?

class ImportTestCase(TestCase): 
    remote_data_url = "http://test_data.csv" 
    local_data_path = "test_data.csv" 
    c = Client() 
    password = "password" 

    def setUp(self): 
     utils.download_file_if_not_exists(self.remote_data_url, self.local_data_path) 
     self.my_admin = User.objects.create_superuser('jonny', '[email protected]', self.password) 
     self.c.login(username=self.my_admin.username, password=self.password) 

    def test_create_organisation(self): 
     self.o = Organization.objects.create(**{'name': 'TEST ORG'}) 

    def test_create_station(self): 
     self.s = Station.objects.create(**{'name': 'Player', 'organization_id': 1}) 

    def test_create_window(self): 
     self.w = Window.objects.create(**{'station_id': 1}) 

    def test_create_component(self): 
     self.c = Component.objects.create(**{ 
      'type': 'Player', 
      'window_id': 1, 
      'start': datetime.datetime.now(), 
      'end': datetime.datetime.now(), 
      'json': "", 
      'layer': 0} 
             ) 

    def test_csv_import(self): 
     """Testing that standard csv imports work""" 
     self.assertTrue(os.path.exists(self.local_data_path)) 
     with open(self.local_data_path) as fp: 
      response = self.c.post('/schedule/schedule-import/create/', { 
       'component': self.c, 
       'csv': fp, 
       'date_from': datetime.datetime.now(), 
       'date_to': datetime.datetime.now() 
      }, follow=False) 

     self.assertEqual(response.status_code, 200) 

    def test_record_exists(self): 
     new_component = Component.objects.all() 
     self.assertTrue(len(new_component) > 0) 

그리고 테스트가

Using existing test database for alias 'default'... 
.....[] 
F 
====================================================================== 
FAIL: test_record_exists (schedule.tests.ImportTestCase) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "tests.py", line 64, in test_record_exists 
    self.assertTrue(len(new_component) > 0) 
AssertionError: False is not true 

---------------------------------------------------------------------- 
Ran 6 tests in 1.250s 

FAILED (failures=1) 

답변

2

--keepdb 옵션 결과는 데이터베이스가 유지되는 것을 의미한다. 다시 말해서 테이블을 다시 만들 필요가 없기 때문에 테스트를 다시 실행하는 것이 더 빠릅니다.

그러나 TestCase 클래스의 각 테스트 메소드는 메소드가 완료된 후에 롤백되는 트랜잭션에서 실행됩니다. --keepdb을 사용해도 변경되지 않습니다.

즉,에서 생성 된 개체는 test_record_exists 테스트에서 볼 수 없습니다. test_record_exists 메서드 또는 setUp 메서드 또는 setUpTestData 클래스 메서드 내에서 개체를 만들 수 있습니다.

+0

test_csv_import 메소드에서 게시물 요청을 한 후에 왜 데이터베이스 레코드가 생성되지 않았습니까? 파일이 올바르게 업로드되었는지 테스트 한 다음 특정 방식으로 파일을 처리하고 싶습니다. 또한 200 응답이 맞습니까? –

+0

'test_csv_import method'에서 post 요청을 한 후에 ** 동일한 메소드 내에서 ** 객체 생성을 확인해야합니다. - 메소드가 실행되고 트랜잭션이 롤백되고 생성 된 객체가 사라지면 ** 확인하십시오. 200 응답은 개체가 만들어 졌음을 의미하지는 않습니다. 응답은 게시물 데이터가 유효하지 않다는 오류 일 수 있습니다. – Alasdair

관련 문제