I am doing a test to check the contents of a csv file using assertContains():
response = client.get('/abc/1/a_b_csv')
print(response.content)
self.assertContains(response.content, 'aakash')
I tried different options such as self.assertContains(response,'aakash')
but didn't get any result.
My csv file looks like,
Name Age
Aakash 22
Sometimes there is the error:
bName\r\nVipul\r\n'
E
======================================================================
ERROR: test_csv (timepay.new_test.ReportTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/aakash/Projects/test.py", line 28, in test_csv
self.assertNotContains(response.content, 'Vipul')
File "/home/aakash/Projects/project_env/lib/python3.6/site-packages/django/test/testcases.py", line 402, in assertNotContains
response, text, status_code, msg_prefix, html)
File "/home/vipul/Projects/project_env/lib/python3.6/site-packages/django/test/testcases.py", line 355, in _assert_contains
response.status_code, status_code,
AttributeError: 'bytes' object has no attribute 'status_code'
Looking at the stacktrace you added to the question, the stacktrace corresponds to the call to self.assertNotContains(response.content, 'Vipul').
Looking at the docs for SimpleTestCase.assertNotContains(), I notice that the method expects the whole response, not just the content as parameter.
So, changing this line:
self.assertNotContains(response.content, 'Vipul')
to this
self.assertNotContains(response, 'Vipul')
should clear up the error for which you added the stacktrace.
Now, I notice that you also have this line in your question:
I tried different options such as self.assertContains(response,'aakash'), but didn't get any result.
You will have to be more specific as to what you mean by "didn't get any result" if you want us to be able to help you.
Related
I am very new at Django. I am following the tutorial by Vitor Freitas, "A Complete Beginner's Guide to Django". In the test module, I get the error shown below. Can some kind sole guide me as to my error.
ERROR: test_redirection (boards.tests.test_view_reply_topic.SuccessfulReplyTopicTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/hp/Desktop/PythonProjects/myproject/myproject/boards/tests/test_view_reply_topic.py", line 34, in test_redirection
self.assertRedirects(self.response, topic_posts_url)
AttributeError: 'SuccessfulReplyTopicTests' object has no attribute 'response'
The file it is referring to is this file
class SuccessfulReplyTopicTests(ReplyTopicTestCase):
def test_redirection(self):
url = reverse('topic_posts', kwargs={'pk': self.board.pk, 'topic_pk': self.topic.pk})
topic_posts_url = '{url}?page=1#2'.format(url=url)
self.assertRedirects(self.response, topic_posts_url)
=====================================================
FAIL: test_redirection (accounts.tests.test_view_signup.SuccessfulSignUpTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/hp/Desktop/PythonProjects/myproject/myproject/accounts/tests/test_view_signup.py", line 62, in test_redirection
self.assertRedirects(self.response, self.home_url)
File "/usr/local/lib/python3.7/dist-packages/django/test/testcases.py", line 348, in assertRedirects
% (response.status_code, status_code)
AssertionError: 200 != 302 : Response didn't redirect as expected: Response code was 200 (expected 302)
The file it is referring to is this file
class SuccessfulSignUpTests(TestCase):
def test_redirection(self):
self.assertRedirects(self.response,self.home_url)
Based on the code here : https://simpleisbetterthancomplex.com/series/2017/10/09/a-complete-beginners-guide-to-django-part-6.html
1 - Do you have this part written correct ?
class LoginRequiredPostUpdateViewTests(PostUpdateViewTestCase):
def test_redirection(self):
'''
Test if only logged in users can edit the posts
'''
login_url = reverse('login')
response = self.client.get(self.url)
self.assertRedirects(response, '{login_url}?next={url}'.format(login_url=login_url, url=self.url))
2- Obviously the code has many errors, and there is comments about it on the post, have you checked them all ?
3- Have you checked the code in github ?
Best of luck!
First, my apologies as a Python newbie that I'm asking this question. It probably has nothing at all to do with convertapi and more to do with my basic lack of understanding as to how to interact with APIs.
I'm reading a Google sheet to find embedded hyperlinks containing references to files (PDF, html, whatever) and then using convertapi to get a txt version so that I can do content analysis based on existence, count and proximity of various terms.
My question has to do with the convertapi.convert failing because (in this case) it turns out convertapi thinks the PDF is invalid (because I have tested the file # convertapi.com and it returned a 5002 error). I don't dispute the file may be bad - all I want to do is detect that convertapi.convert can't convert the file so that I can ignore it and move on.
My python code has a small function:
def convert_PDF_to_text(inputfilename):
result = convertapi.convert('txt', { 'File': inputfilename }, from_format = 'pdf')
result.save_files('converted_pdf_files')
...and while it works fine for some inputs there is a particular URL PDF that results in this output (including my own messages from program):
about to call convertapi.convert with filename (https://www.epa.gov/sites/production/files/2016-06/documents/2016_policy_order_revision_6-10-16.pdf)
yes this is the specific file causing the problem: https://www.epa.gov/sites/production/files/2016-06/documents/2016_policy_order_revision_6-10-16.pdf
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/convertapi/client.py", line 46, in handle_response
r.raise_for_status()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/models.py", line 940, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: https://v2.convertapi.com/convert/pdf/to/txt?Secret=PIuLcqNVL8w4rc9Y
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./p1.py", line 244, in <module>
convert_PDF_to_text(source_URL)
File "./p1.py", line 63, in convert_PDF_to_text
result = convertapi.convert('txt', { 'File': inputfilename }, from_format = 'pdf')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/convertapi/api.py", line 7, in convert
return task.run()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/convertapi/task.py", line 26, in run
response = convertapi.client.post(path, params, timeout = timeout)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/convertapi/client.py", line 16, in post
return self.handle_response(r)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/convertapi/client.py", line 49, in handle_response
raise ApiError(r.json())
convertapi.exceptions.ApiError: <exception str() failed>
I know it should be obvious just from the errors what I should check...but I'm too much of a newbie to Python and APIs to know how to decipher.
How do I test for errors so that my Python code doesn't abort?
Thanks in advance and again sorry for the basic question - yes I did search for answers and don't find anyone addressing my question, it's likely too simple...
All - disregard. I used try: & except: to manage this.
I'm trying to learn mechanize to create a chat logging bot later, so I tested out some basic code
import mechanize as mek
import re
br = mek.Browser()
br.open("google.com")
However, whenever I run it, I get this error.
Traceback (most recent call last):
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/mechanize/_mechanize.py", line 262, in _mech_open
url.get_full_url
AttributeError: 'str' object has no attribute 'get_full_url'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 5, in <module>
br.open("google.com")
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/mechanize/_mechanize.py", line 253, in open
return self._mech_open(url_or_request, data, timeout=timeout)
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/mechanize/_mechanize.py", line 269, in _mech_open
raise BrowserStateError("can't fetch relative reference: "
mechanize._mechanize.BrowserStateError: can't fetch relative reference: not viewing any document
I double checked with the documentation on the mechanize page and it seems consistent. What am I doing wrong?
You have to use a schema, otherwise mechanize thinks you are trying to open a local/relative path (as the error suggests).
br.open("google.com") should be br.open("http://google.com").
Then you will see an error mechanize._response.httperror_seek_wrapper: HTTP Error 403: b'request disallowed by robots.txt', because google.com does not allow crawlers. This can be remedied with br.set_handle_robots(False) before open.
When i am doing browser.submit() ,its showing this error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize/_mechanize.py", line 541, in submit
return self.open(self.click(*args, **kwds))
File "/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize/_mechanize.py", line 528, in click
if not self.viewing_html():
File "/usr/local/lib/python2.7/dist-packages/mechanize-0.2.5-py2.7.egg/mechanize/_mechanize.py", line 443, in viewing_html
raise BrowserStateError("not viewing any document")
mechanize._mechanize.BrowserStateError: not viewing any document
one of the reason i can think of is when i am printing browser.form
then its showing
-form-urlencoded
<TextControl(username)>
<PasswordControl(password)>
<HiddenControl(AUTH_STATE=08539313-ae8d-4133-ba21-247a90668ccb) (readonly)>
<SubmitButtonControl(<None>=) (readonly)>>
may be its because its showing none in SubmitButtonControl. Can anyone suggest how can i solve this issue.
In html submit button's code is
login
how to resolve this ?
The issue is you aren't getting ANY response to the browser.submit() because the SubmitButtonControl is set to None i believe. Here is what I would do first:
select the form via br.select_form(nr=WHATEVER)
then use
for control in br.form.controls:
print control.name
to see if it will print out some identifier.
Your goal is to manually assign the control a value by selecting the control like so...
browser.form['controlname']='login'
browser.urlopen(form.click())
If you can't select the control then just try the browser.urlopen(form.click()) option instead of browser.submit()
Let me know how it works out.
I am getting the following error:
InvalidURLError: ApplicationError: 1
Checked my code, and logged some various things and the url's causing this error to appear look pretty normal. They are being quoted through urllib.quote and visiting them through a browser results in a normal result.
The error is happening with many URL's, not one. The URL points to an API service and is constructed within the app.
Btw,here's a link to the google.appengine.api.urlfetch source code: http://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/api/urlfetch.py?r=56.
The docstrings say that the error should happen when: "InvalidURLError if the url was invalid." and "If the URL is an empty string or obviously invalid, we throw an urlfetch.InvalidURLError"
Just to make it simple for those who would like to test this:
url = 'http://api.embed.ly/1/oembed?key=REMOVEDKEY&maxwidth=400&urls=http%3A//V.interesting.As,http%3A//abcn.ws/z26G9a,http%3A//apne.ws/z37VyP,http%3A//bambuser.com/channel/baba-omer/broadcast/2348417,http%3A//bambuser.com/channel/baba-omer/broadcast/2348417,http%3A//bambuser.com/channel/baba-omer/broadcast/2348417,http%3A//bbc.in/xFx3rc,http%3A//bbc.in/zkkLJq,http%3A//billingsgazette.com/news/local/former-president-bush-to-speak-at-billings-fundraiser-in-may/article_f7ef425a-349c-56a9-a399-606b48033f35.html,http%3A//billingsgazette.com/news/local/former-president-bush-to-speak-at-billings-fundraiser-in-may/article_f7ef425a-349c-56a9-a399-606b48033f35.html,http%3A//billingsgazette.com/news/local/friday-forecast-calls-for-cloudy-windy-day-nighttime-snow-possible/article_d3eb3159-68b0-5559-8255-03fce56eaedd.html,http%3A//billingsgazette.com/news/local/gallery-toy-run/collection_f5042a31-bfd4-5f63-a901-2a8c3e8fb26a.html%230,http%3A//billingsgazette.com/news/local/gas-prices-continue-to-drop-in-billings/article_4e8fd07e-0e1e-5c0e-b551-4162b60c4b60.html,http%3A//billingsgazette.com/news/local/gas-prices-continue-to-drop-in-billings/article_713a0c32-32c9-59f1-9aeb-67b8462bbe88.html,http%3A//billingsgazette.com/news/local/gas-prices-continue-to-fall-in-billings-area/article_2bdebf4b-242c-569e-b414-f388a48f4a14.html,http%3A//billingsgazette.com/news/local/gas-prices-dip-below-a-gallon-at-some-billings-stations/article_c7f4d373-dc2b-55c0-b457-10346c0274a6.html,http%3A//billingsgazette.com/news/local/gas-prices-keep-dropping-in-billings-area/article_3666cf9c-4552-5108-9d5c-de2bba12fa3f.html,http%3A//billingsgazette.com/news/local/government-and-politics/city-picks-st-vincent-as-care-provider-for-health-insurance/article_a899f885-15e1-5b98-b899-75acc01e8feb.html,http%3A//billingsgazette.com/news/local/government-and-politics/linder-settles-in-after-first-year-as-sheriff/article_55a9836e-2196-546d-80f0-48bdef717fa3.html,http%3A//billingsgazette.com/news/local/government-and-politics/new-council-members-city-judge-sworn-in/article_bb7ac948-1d45-579c-a057-1323fb2e643d.html'
from google.appengine.api import urlfetch
result = urlfetch.fetch(url=url)
Here's the traceback:
Traceback (most recent call last):
File "", line 1, in
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/urlfetch.py", line 263, in fetch return rpc.get_result()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 592, in get_result
return self.__get_result_hook(self)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/urlfetch.py", line 359, in _get_fetch_result
raise InvalidURLError(str(err))
InvalidURLError: ApplicationError: 1
I wonder if it's something very simple that I'm missing from all of this. Would appreciate your comments and ideas. Thanks!
Your URL is too long, there is a limit on the length of URLs.