Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 为什么在从SQLite集成到PostgreSQL后测试结果会发生变化而不通过?_Django_Docker_Django Rest Framework - Fatal编程技术网

Django 为什么在从SQLite集成到PostgreSQL后测试结果会发生变化而不通过?

Django 为什么在从SQLite集成到PostgreSQL后测试结果会发生变化而不通过?,django,docker,django-rest-framework,Django,Docker,Django Rest Framework,我一直在开发一个没有Docker的项目,我已经集成并确保一切正常,我已经运行了测试。这让我震惊,我几乎一半的测试都没有正确运行。他们中的大多数人都在测试详细的api视图。我将在下面发布代码。如果您能找到丢失或隐藏的内容,请告知met。) [这是示例项目] models.py class Book(models.Model): name = models.CharField(max_length=255) author = models.CharField(max_length=2

我一直在开发一个没有Docker的项目,我已经集成并确保一切正常,我已经运行了测试。这让我震惊,我几乎一半的测试都没有正确运行。他们中的大多数人都在测试详细的api视图。我将在下面发布代码。如果您能找到丢失或隐藏的内容,请告知met。)

[这是示例项目]

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)
url.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'),
]
测试结果

..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的情况下工作。
请让met知道我缺少的东西,以使测试通过。

您已硬编码
DETAIL\u BOOK\u API\u 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)

您已经硬编码了
DETAIL\u BOOK\u API\u 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)

DETAIL\u BOOK\u API\u URL
的值是多少?对不起,我错过了,我现在就添加它。
DETAIL\u BOOK\u API\u URL
的值是多少?对不起,我错过了,我现在就添加它