jeudi 25 mai 2017

django - initial data in test

Here are two functions in my UserTestCase(APITestCase) class. The capital variables are constant. Is there any way to test api instead of creating too many constants and dumd data like "data" in test_create_user function?

def setUp(self):
    self.client = APIClient()
    # create staff
    self.admin = User.objects.create_user(email=ADMIN, password=PASSWORD)
    self.admin.is_staff = True
    self.admin.is_active = True
    self.admin.save()

    self.user = User.objects.create_user(email=ADMIN, password=PASSWORD)
    self.user.is_active = True
    self.user.save()

def test_create_user(self):

    login = self.client.login(email=ADMIN, password=PASSWORD)
    self.assertTrue(login)

    data = {
         'email': EMAIL,
         'password': PASSWORD,
         'first_name': FIRSTNAME,
         'last_name': LASTNAME,
         'nationality': NATIONALITY,
         'is_active': True,
    }

    # configure urls
    url = reverse('apiv2:user-list')
    # url1 = reverse('apiv2:user-detail', args=[self.user.id])

    # create new user
    user = self.client.post(url, data, format='json')
    self.assertEqual(user.status_code, 201)

    # login after create
    login = self.client.login(email=string_normalize(
        self.data.email),
        password=PASSWORD
    )
    self.assertTrue(login)

    self.client.logout()

Aucun commentaire:

Enregistrer un commentaire