'Tests defining defer fields
@since 3.5
@jira_ticket PYTHON-560
@jira_ticket PYTHON-599
@expected_result deferred fields should not be returned
@test_category object_mapper'

def test_defining_defer_fields(self):

    q = TestModel.objects.defer(['attempt_id', 'description'])
    self.assertEqual(q._select_fields(), ['test_id', 'expected_result', 'test_result'])
    with self.assertRaises(query.QueryException):
        TestModel.objects.defer(['nonexistent_field'])
    q = TestModel.objects.defer(['attempt_id', 'description'])
    q = q.defer(['expected_result'])
    self.assertEqual(q._select_fields(), ['test_id', 'test_result'])
    q = TestModel.objects.defer(['description', 'attempt_id'])
    q = q.only(['description', 'test_id'])
    self.assertEqual(q._select_fields(), ['test_id'])
    q = TestModel.objects.defer(['description', 'attempt_id'])
    q = q.only(['description'])
    with self.assertRaises(query.QueryException):
        q._select_fields()
    q = TestModel.objects.filter(test_id=0)
    self.assertEqual(q._select_fields(), ['attempt_id', 'description', 'expected_result', 'test_result'])
    q = TestModel.objects.defer(['test_id', 'attempt_id', 'description', 'expected_result', 'test_result'])
    self.assertEqual(q._select_fields(), ['test_id'])
