Как проверить на Django Create и Update View - PullRequest
0 голосов
/ 24 апреля 2019

Для упрощения я не могу получить тест для создания или обновления имеющейся у меня модели.Я написал UpdateView и CreateView, но не могу заставить тест работать.Вот мнения:

class InventoryCreateView(CreateView):

    model = BaseInventory
    context_object_name = 'item'
    fields = [
        'product_id',
        'name',
        'color_primary',
        'color_secondary',
        'category',
        'description',
        'gender',
    ]

    template_name = 'inventory_list/product_detail.html'

class InventoryUpdateView(UpdateView):
    model = BaseInventory
    context_object_name = 'item'
    pk_url_kwarg = 'product_id'

    fields = [
        'product_id',
        'name',
        'color_primary',
        'color_secondary',
        'category',
        'description',
        'gender',
        ]

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['colors'] = Color.objects.all()
        return context

    template_name = 'inventory_list/product_detail.html'

Вот тест, который я пытался создать:

def test_create_inventory_view(self):
    """Test to see if inventory can be created"""
    form = {
        'product_id': 'vp12312',
        'name': 'Not a panda',
        'color_primary': self.color,
        'color_secondary': self.color,
        'category': BaseInventory.SHOES,
        'description': "Yellow panda",
        'gender': BaseInventory.MALE,
    }

    request = self.factory.post(InventoryCreateView, data=form)
    response = InventoryCreateView.as_view()(request)
    print(request)

    assert BaseInventory.objects.count() == 1
    assert 'inventory_list/product_detail.html' in response.template_name
    assert response.status_code == 302

def test_inventory_update_view(self):
    """Test for updating object"""
    item = BaseInventoryFactory(description='And everything you do')

    data = {
        'product_id': item.product_id,
        'name': item.name,
        'color_primary': item.color_primary,
        'color_secondary': item.color_secondary,
        'category': item.category,
        'description': 'Yeah they were all yellow',
        'gender': item.gender
    }

    request = self.factory.post('inventory_list:product-update',data,
                                kwargs={'product_id': item.product_id},
                                )
    response = InventoryUpdateView.as_view()(request, product_id=item.product_id)

    assert response.status_code == 302

    item.refresh_from_db()
    assert item.description == data['description']

оба теста дают код состояния 200, и данные не создаютсяни обновленный.Есть что-то, что я сделал не так?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...