While testing routes of my django prodject, i got Type error: expected sting or bites like object. How i can fix this error?

huangapple go评论59阅读模式
英文:

While testing routes of my django prodject, i got Type error: expected sting or bites like object. How i can fix this error?

问题

我的博客上发布的新闻页面对任何用户都可用。我使用pytest检查页面是否对匿名用户可访问。
URL由新闻的ID组成,我将其传递给地址参数(作为元组)。

在结果中,我得到了这个类型错误。test_pages_availability[news:detail-news] - TypeError: 期望字符串或类似字节的对象

我尝试过将args格式化为字符串,为元组格式的参数添加逗号,但没有帮助。

测试代码

@pytest.mark.django_db
@pytest.mark.parametrize(
    'name, args',
    (
        ('news:detail', pytest.lazy_fixture('news')),
        ('news:home', None),
        ('users:login', None),
        ('users:logout', None),
        ('users:signup', None),
    )
)
def test_pages_availability(client, name, args):
    if args is not None:
        url = reverse(name, args=(news.id,))
    else:
        url = reverse(name)
    response = client.get(url)
    assert response.status_code == HTTPStatus.OK

夹具

@pytest.fixture
def news():
    news = News.objects.create(
        title='新闻',
        text='令人惊奇的事件',
        date=datetime.today,
    )
    return news

类NewsDetail(generic.DetailView)的代码

class NewsDetail(generic.DetailView):
    model = News
    template_name = 'news/detail.html'

    def get_object(self, queryset=None):
        obj = get_object_or_404(
            self.model.objects.prefetch_related('comment_set__author'),
            pk=self.kwargs['pk']
        )
        return obj

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        if self.request.user.is_authenticated:
            context['form'] = CommentForm()
        return context

回溯:

request = <FixtureRequest for <Function test_pages_availability[news:detail-news]>>

def fill(request):
    item = request._pyfuncitem
    fixturenames = getattr(item, "fixturenames", None)
    if fixturenames is None:
        fixturenames = request.fixturenames

    if hasattr(item, 'callspec'):
        for param, val in sorted_by_dependency(item.callspec.params, fixturenames):
            if val is not None and is_lazy_fixture(val):

> item.callspec.params[param] = request.getfixturevalue(val.name)
>
value = <built-in method today of type object at 0x00007FFE9CA83650>

def parse_date(value):
    &quot;&quot;&quot;Parse a string and return a datetime.date.

    Raise ValueError if the input is well formatted but not a valid date.
    Return None if the input isn't well formatted.
    &quot;&quot;&quot;

> match = date_re.match(value)
E TypeError: expected string or bytes-like object

..\venv\lib\site-packages\django\utils\dateparse.py:75: TypeError

英文:

The page of the published news on my blog, is available to any user. I use pytest to check if the page is accessible to an anonymous user.
Url is formed by using the id of the news, which I pass in the address parameters (as a tuple).

In the result I got this Type error. test_pages_availability[news:detail-news] - TypeError: expected string or bytes-like object

I had tried to format args to str, puted a coma after arg for tuple format, it didn't helped

code of test

@pytest.mark.django_db
@pytest.mark.parametrize(
    &#39;name, args&#39;,
    (
        (&#39;news:detail&#39;, pytest.lazy_fixture(&#39;news&#39;)),
        (&#39;news:home&#39;, None),
        (&#39;users:login&#39;, None),
        (&#39;users:logout&#39;, None),
        (&#39;users:signup&#39;, None),
    )
)
def test_pages_availability(client, name, args):
    if args is not None:
        url = reverse(name, args=(news.id,))
    else:
        url = reverse(name)
    response = client.get(url)
    assert response.status_code == HTTPStatus.OK

`
fixture

@pytest.fixture
def news():
    news = News.objects.create(
        title=&#39;Новость&#39;,
        text=&#39;Невероятное событие&#39;,
        date=datetime.today,
    )
    return news
class NewsDetail(generic.DetailView):
model = News
template_name = &#39;news/detail.html&#39;

def get_object(self, queryset=None):
    obj = get_object_or_404(
        self.model.objects.prefetch_related(&#39;comment_set__author&#39;),
        pk=self.kwargs[&#39;pk&#39;]
    )
    return obj

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    if self.request.user.is_authenticated:
        context[&#39;form&#39;] = CommentForm()
    return context

Traceback:

request = &lt;FixtureRequest for &lt;Function test_pages_availability[news:detail-news]&gt;&gt;

def fill(request):
    item = request._pyfuncitem
    fixturenames = getattr(item, &quot;fixturenames&quot;, None)
    if fixturenames is None:
        fixturenames = request.fixturenames

    if hasattr(item, &#39;callspec&#39;):
        for param, val in sorted_by_dependency(item.callspec.params, fixturenames):
            if val is not None and is_lazy_fixture(val):

> item.callspec.params[param] = request.getfixturevalue(val.name)
>
value = <built-in method today of type object at 0x00007FFE9CA83650>

def parse_date(value):
    &quot;&quot;&quot;Parse a string and return a datetime.date.

    Raise ValueError if the input is well formatted but not a valid date.
    Return None if the input isn&#39;t well formatted.
    &quot;&quot;&quot;

> match = date_re.match(value)
E TypeError: expected string or bytes-like object

..\venv\lib\site-packages\django\utils\dateparse.py:75: TypeError

答案1

得分: 0

您的data=参数不是一个日期,而是一个将其转换为date的函数。您需要调用该方法,因此:

@pytest.fixture
def news():
    return News.objects.create(
        title='Твой заголовок',
        text='Текст новости о чем-то интересном',
        date=datetime<b>.today()</b>,
    )

对于default=&hellip; [Django-doc],您确实可以传递一个Django将调用可调用对象,但不是作为值,而是该函数调用的结果,而不是函数本身。

英文:

Your data= parameter is not a date, it is a function to convert it into a date. You need to call the method, so:

<pre><code>@pytest.fixture
def news():
return News.objects.create(
title='&#1053;&#1086;&#1074;&#1086;&#1089;&#1090;&#1100;',
text='&#1053;&#1077;&#1074;&#1077;&#1088;&#1086;&#1103;&#1090;&#1085;&#1086;&#1077; &#1089;&#1086;&#1073;&#1099;&#1090;&#1080;&#1077;',
date=datetime<b>.today()</b>,
)</code></pre>

For a <code>default=&hellip;</code>&nbsp;<sup>[Django-doc]</sup> you can indeed pass a callable that Django will then call, but not as a value, the value is then the result of the function call, not the function itself.

huangapple
  • 本文由 发表于 2023年6月22日 04:42:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定