jeudi 16 juillet 2020

How to test Django admin?

I'm trying to test usage of the admin site. Simple tests, if viewing, adding, changing and deleting stuff works. This is my code so far:

    class BaseAdminTestCase(TestCase):
    """
    Base admin site test case, create admin user and login.
    """

    def setUp(self):
        self.client = Client()
        self.login()

    def create_user(self):
        username, password = 'admin', 'test'
        user = User.objects.get_or_create(
            username=username,
            email='admin@test.com',
            is_superuser=True
        )[0]
        user.set_password(password)
        user.save()
        self.user = user
        return username, password

    def login(self):
        username, password = self.create_user()
        self.client.login(username=username, password=password)


class ClientAdminTestCsae(BaseAdminTestCase):

    def test_get_client_list(self):
        page = '/logistirio/client/'
        resp = self.client.get(page, follow=True)
        assert resp.status_code == 200

    def test_create_client(self):
        resp = self.client.post(
            reverse('admin:logistirio_client_add'),
            {
                'title': "mr",
                'name': "Toni",
                'surname': "Montana",
                'phone1': "6969696969",
                'email1': "ant@gmail.com",
            },
        )
        print(resp)
        firstClient = client.objects.filter(pk=1).exists()
        self.assertTrue(firstClient)

The first test works, the second one doesn't, and I'm not sure why. The client object has inlines, if this is a problem. Has anyone any experience with this? Thank you.

Aucun commentaire:

Enregistrer un commentaire