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

def test_defining_only_fields(self):

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