gpt4 book ai didi

django - 从SQLite集成到PostgreSQL后,为什么测试结果会有所变化而不通过?

转载 作者:行者123 更新时间:2023-12-02 20:59:44 25 4
gpt4 key购买 nike

我一直在开发一个没有Docker的项目,并且已经集成并确保一切正常,我已经运行了测试。令我震惊的是,几乎有一半的测试无法正常运行。他们中的大多数人正在测试详细的api View 。我将在下面发布代码。如果您能找到丢失或隐藏的东西,请告诉我们))

[这是示例项目]

models.py

class Book(models.Model):
name = models.CharField(max_length=255)
author = models.CharField(max_length=255)
created_at = models.DateField(auto_now_add=True)

def __str__(self):
return self.name

serialisers.py
class BookSerializers(ModelSerializer):
class Meta:
model = Book
fields = ('id', 'name', 'author')

views.py
class BookListApiView(ListAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializers
permission_classes = [AllowAny, ]


class BookCreateApiView(CreateAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializers
permission_classes = [AllowAny, ]


class BookRetrieveUpdateDeleteView(RetrieveUpdateDestroyAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializers
permission_classes = [AllowAny, ]

tests.py
LIST_BOOK_API_URL = reverse('book:list')
CREATE_BOOK_API_URL = reverse('book:create')
DETAIL_BOOK_API_URL = reverse('book:detail', kwargs={'pk': 1})

class TestBookApiView(TestCase):
def setUp(self):
self.client = APIClient()

def test_create_book_through_api(self):
payload = {
'name': 'this is book',
'author': 'test author'
}
res = self.client.post(CREATE_BOOK_API_URL, payload)
self.assertEqual(res.status_code, status.HTTP_201_CREATED)

def test_listing_book_through_api(self):
Book.objects.create(
name='test',
author='testing',
)
res = self.client.get(LIST_BOOK_API_URL)
self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertContains(res, 'test')

def test_retreiving_book_through_api(self):
Book.objects.create(
name='test',
author='testing',
)
res = self.client.get(DETAIL_BOOK_API_URL)
print(DETAIL_BOOK_API_URL)
self.assertEqual(res.status_code, status.HTTP_200_OK)

urls.py [用于图书应用]
app_name = 'book'

urlpatterns = [
path('list/', BookListApiView.as_view(), name='list'),
path('create/', BookCreateApiView.as_view(), name='create'),
path('<int:pk>/', BookRetrieveUpdateDeleteView.as_view(), name='detail'),
]

urls.py [main]
urlpatterns = [
path('admin/', admin.site.urls),
path('book/', include('book.urls')),
]

测试结果
..F
======================================================================
FAIL: test_retreiving_book_through_api (book.tests.TestBookApiView)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/user/web/book/tests.py", line 39, in test_retreiving_book_through_api
self.assertEqual(res.status_code, status.HTTP_200_OK)
AssertionError: 404 != 200

----------------------------------------------------------------------
Ran 3 tests in 0.040s

FAILED (failures=1)

我的Dockerfile
# the base image for the python that we are using for the project
FROM python:3.8.1-alpine

ENV PYHTONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

# creating a folder.
RUN mkdir -p /home/user

ENV HOME=/home/user
ENV APP_HOME=/home/user/web

WORKDIR ${APP_HOME}

RUN mkdir ${APP_HOME}/staticfiles
RUN mkdir ${APP_HOME}/media

RUN apk update \
&& apk add postgresql-dev gcc python3-dev musl-dev

RUN apk add zlib zlib-dev jpeg-dev

RUN pip install --upgrade pip

COPY ./requirements.txt ${APP_HOME}/requirements.txt

RUN pip install -r requirements.txt
COPY entrypoint.sh ${APP_HOME}/entrypoint.sh

COPY . ${APP_HOME}

RUN adduser -D user
USER user

ENTRYPOINT [ "/home/user/web/entrypoint.sh" ]

我几乎100%确信已创建对象并且此功能正常运行。由于我已经在浏览器上对其进行了测试,因此效果很好。如果我检查它是否在没有Docker的情况下正常工作。
请让遇到的人知道我无法通过测试的内容。

最佳答案

您已将DETAIL_BOOK_API_URL硬编码为使用ID 1,但可能是一本书没有此ID的情况。您应该使用刚创建的书的ID

def test_retreiving_book_through_api(self):
book = Book.objects.create(
name='test',
author='testing',
)
res = self.client.get(reverse('book:detail', kwargs={'pk': book.pk}))
self.assertEqual(res.status_code, status.HTTP_200_OK)

关于django - 从SQLite集成到PostgreSQL后,为什么测试结果会有所变化而不通过?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61037742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com