Python- Assertion error when performing nosetests - python-2.7

I am new to web.py . I was following ex52 LPTHW , and I had to perform nosetests on all the tests in tests folder. But I am getting 1 failed test due to assertion error. I tried reading different assertion errors and why they occur , but I cant figure out this one. I tried expanding the errors which the server could possibly show up like 500 Internal , 400 but its still failing the test. I have created the other code just like mentioned in the book: http://learnpythonthehardway.org/book/ex52.html
Here is my traceback:
C:\lpthw\gothonweb>cd tests
C:\lpthw\gothonweb\tests>nosetests
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\web\application.py", line 239, in process
return self.handle()
File "C:\Python27\lib\site-packages\web\application.py", line 230, in handle
return self._delegate(fn, self.fvars, args)
File "C:\Python27\lib\site-packages\web\application.py", line 420, in _delegate
return handle_class(cls)
File "C:\Python27\lib\site-packages\web\application.py", line 396, in handle_class
return tocall(*args)
File "C:\lpthw\gothonweb\bin\app.py", line 29, in GET
return render.hello_form()
File "C:\Python27\lib\site-packages\web\template.py", line 1017, in __getattr__
t = self._template(name)
File "C:\Python27\lib\site-packages\web\template.py", line 1014, in _template
return self._load_template(name)
File "C:\Python27\lib\site-packages\web\template.py", line 1001, in _load_template
raise AttributeError, "No template named " + name
AttributeError: No template named hello_form
F...
======================================================================
FAIL: tests.app_tests.test_index
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "C:\lpthw\gothonweb\tests\app_tests.py", line 12, in test_index
assert_response(resp)
File "C:\lpthw\gothonweb\tests\tools.py", line 5, in assert_response
assert status in resp.status, "Expected response %r not in %r" % (status , resp.status)
AssertionError: Expected response '200' not in '500 Internal Server Error'
----------------------------------------------------------------------
Ran 4 tests in 0.562s
FAILED (failures=1)
My tests code: app_tests.py
from nose.tools import *
from bin.app import app
from tests.tools import assert_response
def test_index():
#check that we get a 404 on the / URL
resp = app.request("/")
assert_response(resp, status= "404")
#test our first GET request to /hello
resp = app.request("/hello")
assert_response(resp)
#make sure default values work for the form
resp = app.request("/hello" , method="POST")
assert_response(resp , contains="Nobody")
#test that we get expected values
data = {'name':'Tejas','greet':'Ola!'}
resp = app.request("/hello " , method= "POST", data=data)
assert_response(resp , contains="Zed")
tools.py :
from nose.tools import *
import re
def assert_response(resp, contains=None, matches=None, headers=None, status= "200"):
assert status in resp.status, "Expected response %r not in %r" % (status , resp.status)
if status == "200":
assert resp.data , "Response data is empty."
if contains:
assert contains in resp.data, "Response does not contain %r" % contains
if matches:
reg = re.compile(matches)
assert reg.matches(resp.data), "Response does not match %r" % matches
if headers:
assert_equal(resp.headers , headers)
app.py code:
import web
from gothonweb import map
urls = (
'/game' , 'GameEngine' ,
'/' , 'Index',
)
app = web.application(urls, globals())
#little hack so that debug mode works with sessions
if web.config.get('_session') is None:
store= web.session.DiskStore('sessions')
session= web.session.Session(app, store, initializer={'room':None})
web.config._session = session
else:
session= web.config._session
render = web.template.render('templates/', base="layout")
class Index(object):
def GET(self):
#this is used to "setup" the session with starting values
session.room= map.START
web.seeother("/game")
class GameEngine(object):
def GET(self):
if session.room:
return render.show_room(room= session.room)
else:
#why is there here? do you need it?
return render.you_died()
def POST(self):
form= web.input(action=None)
if session.room and form.action:
session.room= session.room.go(form.action)
else:
session.room= None
if __name__ == "__main__" :
app.run()
After going ahead with the exercise now it gives me 2 import errors:
PS C:\lpthw\gothonweb\tests> nosetests
EE
======================================================================
ERROR: Failure: SyntaxError (invalid syntax (app.py, line 2))
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose\loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "C:\Python27\lib\site-packages\nose\importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "C:\Python27\lib\site-packages\nose\importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "C:\lpthw\gothonweb\tests\app_tests.py", line 2, in <module>
from bin.app import app
File "C:\lpthw\gothonweb\bin\app.py", line 2
from gothonweb import map.py
^
SyntaxError: invalid syntax
======================================================================
ERROR: Failure: ImportError (cannot import name map)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose\loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "C:\Python27\lib\site-packages\nose\importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "C:\Python27\lib\site-packages\nose\importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "C:\lpthw\gothonweb\tests\map_tests.py", line 2, in <module>
from gothonweb import map
ImportError: cannot import name map
----------------------------------------------------------------------
Ran 2 tests in 0.002s
FAILED (errors=2)
directory structure:
C:/lpthw/
gothonweb/
bin build dist doc sessions Gothonweb templates tests map.py app.py
tests/
map_tests.py
app_tests.py
__init__.py
tools.py
What should I do to fix the error? Thanks for the suggestions.

For this error,
"AttributeError: No template named hello_form"
use an absolute path and see if that works, instead of
"render = web.template.render('templates/', base="layout")
try:
"render = web.template.render('C:/lpthw/gothonweb/bin/templates', base="layout")

Amazingly, your test has nothing to do with app.py.
from nose.tools import *
from bin.app import app
from tests.tools import assert_response
def test_index():
#check that we get a 404 on the / URL
resp = app.request("/")
assert_response(resp, status= "404")
#test our first GET request to /hello
resp = app.request("/hello")
assert_response(resp)
#make sure default values work for the form
resp = app.request("/hello" , method="POST")
assert_response(resp , contains="Nobody")
#test that we get expected values
data = {'name':'Tejas','greet':'Ola!'}
resp = app.request("/hello " , method= "POST", data=data)
assert_response(resp , contains="Zed")
App.py
import web
from gothonweb import map
urls = (
'/game' , 'GameEngine' ,
'/' , 'Index',
)
app = web.application(urls, globals())
#little hack so that debug mode works with sessions
if web.config.get('_session') is None:
store= web.session.DiskStore('sessions')
session= web.session.Session(app, store, initializer={'room':None})
web.config._session = session
else:
session= web.config._session
render = web.template.render('templates/', base="layout")
class Index(object):
def GET(self):
#this is used to "setup" the session with starting values
session.room= map.START
web.seeother("/game")
class GameEngine(object):
def GET(self):
if session.room:
return render.show_room(room= session.room)
else:
#why is there here? do you need it?
return render.you_died()
def POST(self):
form= web.input(action=None)
if session.room and form.action:
session.room= session.room.go(form.action)
else:
session.room= None
if __name__ == "__main__" :
app.run()
Can you see, how they are not related
Your first test says you should get a 404 on the '/' url but that would only happen if '/' didn't exist. Your app.py code clearly shows that '/' calls on GET in the Index class.
Second test, resp = app.request("/hello") now that would give you a 404 error because that url doesn't exist on app.py
The same goes for the third test as '/hello' doesn't exist in your tuple of urls in app.py
And the fifth test as well
Although your tests are invalid, the primary problem you have though is trying to perform the automated test from inside the tests directory; that's wrong.
C:\lpthw\gothonweb\tests> nosetests
This is not the right thing to do, you need to be a directory below for all your imports to work e.g In app_tests you are trying to import bin/app.py but that's not in the 'tests' directory
Do this instead
C:\lpthw\gothonweb> nosetests
This way all the files that you need would be imported
Back to app_tests.py. I'll write out a very simple test that's actually related to app.py
from bin.app import app
from test.tools import assert_response
def test_index():
#check that we get a 303 on the / url
#this is because web.seeother() will always send the browser this http code
resp = app.request("/")
assert_response(resp, status= "303")
def test_game():
#check that we get a 200 on /game
resp = app.request("/game")
assert_response(resp, status = '200')
#check that we have response data on a form sumbit
resp = app.request("/game", method = 'POST')
aseert_response(resp.data)

The assertion_error you getting because your ran nosetests in this directory
/gothroweb/test
you should run it in
/gothroweb
bin docs gothonweb templates tests
./bin:
app.py app.pyc __init__.py __init__.pyc
./docs:
./gothonweb:
__init__.py __init__.pyc
./templates:
hello_form.html index.html layout.html
./tests:
app_tests.py __init__.py test.html tools.pyc
app_tests.pyc __init__.pyc tools.py
cheers

Related

How to authenticate with django-grahql-jwt in a graphene-django Using GraphQLTestCase?

I use Django and Graphene in my project. I wrote tests with GraphQLTestCase. When i try to authenticate users using JWT, i usually get errors.
Here is my code:
from django.test import TestCase
import json
from graphene_django.utils.testing import GraphQLTestCase
from resume.graph.schema import schema
from .models import Post
from django.contrib.auth import get_user_model
from graphql_jwt.shortcuts import get_token
User = get_user_model()
class PostTestCase(GraphQLTestCase):
GRAPHQL_SCHEMA = schema
def test_post_list(self):
token = get_token(User.objects.get(pk=1))
headers = {"HTTP_AUTHORIZATION": f"JWT {token}"}
response = self.query(
'''
query {
post{
user
text
}
}
''',
op_name = 'post',
headers=headers,
)
content = json.loads(response.content)
self.assertResponseNoErrors(response)
Here are the errors I get after running python manage.py test.
Traceback (most recent call last):
File "C:\Users\Udemezue\Desktop\resume\post\tests.py", line 25, in test_post_list
token = get_token(User.objects.get(pk=9))
File "C:\Users\Udemezue\Desktop\resume\env\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Udemezue\Desktop\resume\env\lib\site-packages\django\db\models\query.py", line 415, in get
raise self.model.DoesNotExist(
accounts.models.User.DoesNotExist: User matching query does not exist.
Here is the errors i get.
======================================================================
FAIL: test_post_list (post.tests.PostTestCase)
Traceback (most recent call last):
File "C:\Users\Udemezue\Desktop\resume\post\tests.py", line 62, in test_post_list
self.assertResponseNoErrors(response)
File "C:\Users\Udemezue\Desktop\resume\env\lib\site-packages\graphene_django\utils\testing.py", line 75, in assertResponseNoErrors
self.assertEqual(resp.status_code, 200)
AssertionError: 400 != 200
Ran 1 test in 0.137s
FAILED (failures=1)
Destroying test database for alias 'default'...
This works for me.
from django.contrib.auth import get_user_model
from graphql_jwt.shortcuts import get_token
User = get_user_model()
import json
class PostTestCase(GraphQLTestCase):
def test_post_list(self):
user = get_user_model().objects.create(username='myuser')
token = get_token(user)
headers = {"HTTP_AUTHORIZATION": f"JWT {token}"}
response = self.query(
'''
query GetUser($username: String!) {
user(username: $username) {
id
}
}
''',
headers=headers,
)
content = json.loads(response.content)
self.assertResponseNoErrors(response)

scrapy TypeError: object() takes no parameters

I am new to Scrapy and trying to crawl a couple of links as a test using Scrapy. Whenever I run scrapy crawl tier1, I get "TypeError: object() takes no parameters" as the following:
Traceback (most recent call last):
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/Users/btaek/TaeksProgramming/adv/crawler/adv_crawler/adv_crawler/spiders/tier1_crawler.py", line 93, in parse
mk_loader.add_xpath('title', 'h1[#class="top_title"]') # Title of the article
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 167, in add_xpath
self.add_value(field_name, values, *processors, **kw)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 77, in add_value
self._add_value(field_name, value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 91, in _add_value
processed_value = self._process_input_value(field_name, value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 150, in _process_input_value
return proc(value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/processors.py", line 28, in __call__
next_values += arg_to_iter(func(v))
TypeError: object() takes no parameters
2017-08-23 17:25:02 [tier1-parse-logger] INFO: Entered the parse function to parse and index: http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166
2017-08-23 17:25:02 [tier1-parse-logger] ERROR: Error (object() takes no parameters) when trying to parse <<date>> from a mk article: http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166
2017-08-23 17:25:02 [tier1-parse-logger] ERROR: Error (object() takes no parameters) when trying to parse <<author>> from a mk article: http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166
2017-08-23 17:25:02 [scrapy.core.scraper] ERROR: Spider error processing <GET http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166> (referer: None)
Traceback (most recent call last):
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/Users/btaek/TaeksProgramming/adv/crawler/adv_crawler/adv_crawler/spiders/tier1_crawler.py", line 93, in parse
mk_loader.add_xpath('title', 'h1[#class="top_title"]') # Title of the article
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 167, in add_xpath
self.add_value(field_name, values, *processors, **kw)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 77, in add_value
self._add_value(field_name, value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 91, in _add_value
processed_value = self._process_input_value(field_name, value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 150, in _process_input_value
return proc(value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/processors.py", line 28, in __call__
next_values += arg_to_iter(func(v))
TypeError: object() takes no parameters
And, my spider file (tier1_crawler.py):
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import os
sys.path.append(os.path.abspath('..'))
import logging
import scrapy
from scrapy.loader import ItemLoader
from adv_crawler.items import AdvCrawlerItem
from datetime import datetime, date, time
t1_parse_logger = logging.getLogger("tier1-parse-logger")
t1_parse_logger.LOG_FILE = "Tier1-log.txt"
content_type_dic = {
'news': 'news',
}
class Tier1Crawler(scrapy.Spider):
name = "tier1"
def start_requests(self):
urls = ['http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535982',
'http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
t1_parse_logger.info("Entered the parse function to parse and index: %s" % response.url) # Log at the beginning of the parse function
item_loader = ItemLoader(item=AdvCrawlerItem(), response=response)
if 'mk.co.kr' in response.url:
mk_loader = item_loader.nested_xpath('//div[#id="top_header"]/div[#class="news_title"]/div[#class="news_title_text"]')
try:
mk_loader.add_xpath('date', 'div[#class="news_title_author"]/ul/li[#class="lasttime"]')
except AttributeError: # if the date is not in "lasttime" li tag
mk_loader.add_xpath('date', 'div[#class="news_title_author"]/ul/li[#class="lasttime1"]')
except Exception as e: # in case the error is not AttributeError
t1_parse_logger.error("Error "+"("+str(e)+")"+" when trying to parse <<date>> from a mk article: %s" % response.url)
try:
mk_loader.add_xpath('author', 'div[#class="news_title_author"]/ul/li[#class="author"]')
except AttributeError: # in case there is no author (some mk articles have no author)
item_loader.add_value('author', "None") # ir error, replace with the line below
# item['author'] = "None" # if the above gives any error, replace the above with this line
except Exception as e: # in case the error is not AttributeError
t1_parse_logger.error("Error "+"("+str(e)+")"+" when trying to parse <<author>> from a mk article: %s" % response.url)
item_loader.add_xpath('content', '//div[#id="Content"]/div[#class="left_content"]/div[#id="article_body"]/div[#class="art_txt"]') # Content of the article (entire contents)
mk_loader.add_xpath('title', 'h1[#class="top_title"]') # Title of the article
item_loader.add_value('content_type', content_type_dic['news'])
item_loader.add_value('timestamp', str(datetime.now())) # timestamp of when the document is being indexed
item_loader.add_value('url', response.url) # url of the article
t1_parse_logger.info("Parsed and indexed: %s" % response.url)
return item_loader.load_item()
And, my items.py file:
# -*- coding: utf-8 -*-
import scrapy
from scrapy.loader.processors import Join, MapCompose, TakeFirst
from w3lib.html import remove_tags
def filter_date(value):
if isinstance(value, unicode):
(year, month, day) = str(value.split(" ")[-2]).split(".")
return year+"-"+month+"-"+day
def filter_utf(value):
if isinstance(value, unicode):
return value.encode('utf-8')
class AdvCrawlerItem(scrapy.Item):
author = scrapy.Field(input_processor=MapCompose(remove_tags, TakeFirst, filter_utf),) # Name of the publisher/author
content = scrapy.Field(input_processor=MapCompose(remove_tags, Join, filter_utf),) # Content of the article (entire contents)
content_type = scrapy.Field()
date = scrapy.Field(input_processor=MapCompose(remove_tags, TakeFirst, filter_date),)
timestamp = scrapy.Field() # timestamp of when the document is being indexed
title = scrapy.Field(input_processor=MapCompose(remove_tags, TakeFirst, filter_utf),) # title of the article
url = scrapy.Field() # url of the article
And, pipelines.py file:
import json
from scrapy import signals
from scrapy.exporters import JsonLinesItemExporter
class AdvCrawlerJsonExportPipeline(object):
def open_spider(self, spider):
self.file = open('crawled-articles1.txt', 'w')
def close_spider(self, spider):
self.file.close()
def process_item(self, item, spider):
line = json.dummps(dict(item)) + "\n"
self.file.write(line)
return item
I am aware that "TypeError: object() takes no parameters" error is usually thrown when __init__ method of a class is not defined at all or not defined to take in parameter(s).
However, in the case above, how can i fix the error? Am I doing something wrong using the item loader or nested item loader??
When using scrapy processors you should use the classes to create objects that do the processing:
# wrong
field = Field(output_processor=MapCompose(TakeFirst))
# right
field = Field(output_processor=MapCompose(TakeFirst()))
^^

Log warning from Selenium on Django [duplicate]

Whenever I try to construct a string based on self.live_server_url, I get python TypeError messages. For example, I've tried the following string constructions (form 1 & 2 below), but I experience the same TypeError. My desired string is the Live Server URL with "/lists" appended. NOTE: the actual test does succeed to create a server and I can manually access the server, and more specifically, I can manually access the exact URL that I'm trying to build programmatically (e.g. 'http://localhost:8081/lists').
TypeErrors occur with these string constructions.
# FORM 1
lists_live_server_url = '%s%s' % (self.live_server_url, '/lists')
# FORM 2
lists_live_server_url = '{0}{1}'.format(self.live_server_url, '/lists')
self.browser.get(lists_live_server_url)
There is no python error with this form (nothing appended to string), albeit my test fails (as I would expect since it isn't accessing /lists).
self.browser.get(self.live_server_url)
Here is the python error that I'm getting.
/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py test functional_tests.lists_tests.LiveNewVisitorTest.test_can_start_a_list_and_retrieve_it_later /Users/myusername/PycharmProjects/mysite_proj
Testing started at 11:55 AM ...
Creating test database for alias 'default'...
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python3.4/site-packages/django/test/testcases.py", line 1104, in __call__
return super(FSFilesHandler, self).__call__(environ, start_response)
File "/usr/local/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.4/site-packages/django/test/testcases.py", line 1087, in get_response
return self.serve(request)
File "/usr/local/lib/python3.4/site-packages/django/test/testcases.py", line 1099, in serve
return serve(request, final_rel_path, document_root=self.get_base_dir())
File "/usr/local/lib/python3.4/site-packages/django/views/static.py", line 54, in serve
fullpath = os.path.join(document_root, newpath)
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/posixpath.py", line 82, in join
path += b
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'
Am I unknowingly attempting to modify the live_server_url, which is leading to these TypeErrors? How could I programmatically build a string of live_server_url + "/lists"?
Here is the test that I am attempting...
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.test import LiveServerTestCase
class LiveNewVisitorTest(LiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.close()
def test_can_start_a_list_and_retrieve_it_later(self):
#self.browser.get('http://localhost:8000/lists')
#self.browser.get('http://www.google.com')
#lists_live_server_url = '%s%s' % (self.live_server_url, '/lists')
#lists_live_server_url = '{0}{1}'.format(self.live_server_url, '/lists')
lists_live_server_url = self.live_server_url
self.browser.get(lists_live_server_url)
self.assertIn('To-Do', self.browser.title)
header_text = self.browser.find_element_by_tag_name('h1').text
self.assertIn('To-Do', header_text)
See this discussion on Reddit featuring the same error Traceback.
Basically, this is not a problem with anything within the Selenium tests but rather with your project's static file configuration.
From your question, I believe the key line within the Traceback is:
File "/usr/local/lib/python3.4/site-packages/django/views/static.py", line 54, in serve
fullpath = os.path.join(document_root, newpath)
This line indicates that an unsuccessful os.path.join is being attempted within django.views.static.
Set STATIC_ROOT in your project's settings.pyfile and you should be good.
Use StaticLiveServerTestCase instead may help

Python SnakeSQL Table not found error

I am learning Python and SnakeSQL for my database operations. I am following the examples of both web.py and SnakeSQL's. I combined both codes from their web sites and tried to make the below code worked but I am getting this error:
Traceback (most recent call last):
File ".\app.py", line 9, in
cursor.execute("INSERT INTO Test (dateColumn, numberColumn) VALUES ('2004->11-8', 4)")
File "D:\Python\virtualenvs\new4\textweb\bin\SnakeSQL\driver\base.py", line >1552, in execute
self.info = self.connection._insert(parsedSQL['table'], >parsedSQL['columns'], parsedSQL['sqlValues'], parameters)
File "D:\Python\virtualenvs\new4\textweb\bin\SnakeSQL\driver\base.py", line >1040, in _insert
raise SQLError("Table '%s' not found."%(table))
error.SQLError: Table 'Test' not found.
And here my app.py code:
import web
import SnakeSQL
render = web.template.render('templates/')
connection = SnakeSQL.connect(database='test', autoCreate=True)
connection = SnakeSQL.connect(database='test')
cursor = connection.cursor()
cursor.execute("INSERT INTO Test (dateColumn, numberColumn) VALUES ('2004-11-8', 4)")
urls = (
'/', 'index'
)
class index:
def GET(self, name):
cursor.execute("SELECT * FROM Test")
results = cursor.fetchall()
return render.index(results)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
What could be wrong in here?

testing django web app that uses cookies/session

In views.py:
get_dict = Site.objects.getDictionary(request.COOKIES['siteid'])
{gets a dictionary with site information based on id from cookie}
In tests.py:
from django.test import TestCase
class WebAppTest(TestCase):
def test_status(self):
response = self.client.get('/main/',{})
response.status_code # --->passed with code 200
response = self.client.get('/webpage/',{'blog':1})
response.status_code # ----> this is failing
In order to present blog page it goes to a view where it gets a dictionary using existing cookie, process it, renders templates, which works fine when running the app. But the tests are failing.Having never tested Django webapps I'm not sure how to test it right. Here is the traceback.
Traceback (most recent call last):
File "<console>", line 2, in <module>
File "/usr/lib/pymodules/python2.6/django/test/client.py", line 313, in post
response = self.request(**r)
File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py", line 92, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/var/lib/django/data/../webpage/views.py", line 237, in getCostInfo
get_dict = Site.objects.getDictionary(request.COOKIES['siteid'])
KeyError: 'siteid'
Went through some online samples but couldn't find something that deals in depth with cookies/sessions. Any ideas or directs to useful links are highly appreciated.
Take a look at the Persistent State section of the Django Testing docs.
In your case, I would expect your test to be something more like:
from django.test import TestCase
from django.test.client import Client
class WebAppTest(TestCase):
def setUp(self):
self.client = Client()
session = self.client.session
session['siteid'] = 69 ## Or any valid siteid.
session.save()
def test_status(self):
response = self.client.get('/main/',{})
self.assertEqual(response.status_code, 200)
response = self.client.get('/webpage/',{'blog':1})
self.assertEqual(response.status_code, 200)