vendredi 20 mars 2015

Django REST API fails tests when saving models, but runs fine

I'm pretty lost on this. I have a ModelViewSet from Django's REST Framework that performs some custom functionality on POST requests by overriding the perform_create method. It looks like this:



class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserFlat

def perform_create(self, serializer):
serializer.save()

try:
profile_country = self.request.data['country']
profile_phone = self.request.data['phone']

profile = Profile.objects.get(user_id=serializer.data['id'])
country = Country.objects.get(country_code=profile_country)

# both of these return the object just fine
# print profile
# print country

# Take the profile object and set these attributes. It has a ForeignKey relation with the country
profile.phone = profile_phone
profile.origin = country

# This is where Django's TestCase fails
profile.save()

except:
print "Attributes not provided"


So I tested the above in my terminal with httpie and it POSTS just fine. When I go to check the DB, every all the information is there. The User object was created and a new Profile object was created with the ForeignKey to the Country and phone number (I create the Profile object with a post_save signal)


When I run the tests though, it fails at the profile.save() section. I've confirmed that the Country object was created in the DB prior to the test and if I print it out while running the test I can see that it clearly picks up the all the relevant info.


http POST :8000/api/v1/users/ username=test password=test last_name=test first_name=test email=test@test.com country=US phone=555


The line above runs in my terminal posts successfully to my database


Here's the failing TestCase though



class APITestCase(TransactionTestCase):
def test_create_user_profile_through_api(self):
url = '/api/v1/users/'

# country = Country.objects.get(country_code='US')

data = {
'first_name':'Tom',
'last_name':'Sawyer',
'username':'tomsawyer',
'password':'tomsawyer',
'email':'tomsawyer@tomsawyer.com',
'country': 'US',
'phone':'5555555555'
}

# The test fails to save the additional profile information profile.save() within
# the view's perform_create method. Why is that?
post = self.client.post(url, data, format='json')

# request = self.client.get('/api/v1/users/3')
# Profile.objects.get(id=3)
# both lines above are me checking to see if the profile object showed
# up with the relevant country and phone number fields. Fields are
# are empty although the profile object is there

# I didn't write the assertion yet as the line above keeps coming up empty


When I run my test, the except block of my view comes up "Attributes not provided"


EDIT: On another note, I've checked things like authentication and even turned it off/forced it through the REST Framework and that's all fine. I'm able to successfully post and retrieve from the API and without the API for all other scenarios


Aucun commentaire:

Enregistrer un commentaire