Skip to content

Instantly share code, notes, and snippets.

@rudmanmrrod
Created January 16, 2019 14:45
Show Gist options
  • Save rudmanmrrod/390c603bc7267aceb37279ee18f07342 to your computer and use it in GitHub Desktop.
Save rudmanmrrod/390c603bc7267aceb37279ee18f07342 to your computer and use it in GitHub Desktop.
Prueba unitaria de modelo perfil en django
class ProfileRestTest(TestCase):
"""!
Clase para probar el perfil por servicios rest
"""
def setUp(self):
"""!
Método para configurar los valores iniciales de
la prueba unitaria
"""
self.client = APIClient()
self.user = User.objects.create_user(
username='testuser', email='[email protected]', password='prueba123')
self.profile = Profile.objects.create(
address = "dirección de prueba",
phone = "+58 123456",
gender = "M",
user = self.user
)
def test_list(self):
"""!
Método para probar el listado de perfiles
"""
response = self.client.get('/api/profile/',format="json")
self.assertEqual(response.status_code,200)
self.assertEqual(len(response.data),1)
def test_create(self):
"""!
Método para probar el creado de un perfil
"""
profile = Profile.objects.count()
response = self.client.post('/api/profile/',
{'address':'otra dirección','phone':'+1 234454',
'gender':'M','user_id':1},format="json")
self.assertEqual(response.status_code,201)
self.assertEqual(Profile.objects.count(),profile + 1)
def test_update(self):
"""!
Método para probar la actualización de un perfil
"""
response = self.client.put('/api/profile/1/',
{'address':'dirección actualizada','phone':'+1 234454',
'gender':'M','user_id':1},format="json")
self.assertEqual(response.status_code,200)
profile = Profile.objects.first()
self.assertEqual(profile.address,"dirección actualizada")
def test_delete(self):
"""!
Método para probar el eliminado de un perfil
"""
profile = Profile.objects.count()
response = self.client.delete('/api/profile/1/')
self.assertEqual(response.status_code,204)
self.assertEqual(Profile.objects.count(),profile - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment