sqlite3.Cursor
sınıfını özellikle fetchall
yöntemiyle alay etmeyi denemeye çalışıyorum.sqlite3.Cursor'ı nasıl alayım
Ben fetchall
yöntemi alay etmek mümkün olmamalıdır hiç eğlenceli hata
E
======================================================================
ERROR: temp.test_foo
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/koddsson/.virtualenvs/temp/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/home/koddsson/.virtualenvs/temp/lib/python2.7/site-packages/mock.py", line 1214, in patched
patching.__exit__(*exc_info)
File "/home/koddsson/.virtualenvs/temp/lib/python2.7/site-packages/mock.py", line 1379, in __exit__
setattr(self.target, self.attribute, self.temp_original)
TypeError: can't set attributes of built-in/extension type 'sqlite3.Cursor'
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (errors=1)
yılında nosetests
sonuçları Running şu kod örneğini
import sqlite3
from mock import Mock, patch
from nose.tools import assert_false
class Foo:
def check_name(name):
conn = sqlite3.connect('temp.db')
c = conn.cursor()
c.execute('SELECT * FROM foo where name = ?', name)
if len(c.fetchall()) > 0:
return True
return False
@patch('sqlite3.Cursor.fetchall', Mock(return_value=['John', 'Bob']))
def test_foo():
foo = Foo()
assert_false(foo.check_name('Cane'))
düşünün ya da ben korkunç yanlış bir şey yapıyorum?
[Python'da, ac uzantı sınıfıyla nasıl başa çıkılır?] (Http://stackoverflow.com/q/17267587/222914) –
@JanneKarila Denedim ama şimdi bir AssertionError https: // gist alıyorum .github.com/anonymous/7931550: < –
Sanırım yanlış seviyede yama yapıyorsunuz. Şahsen ben sqlite3 kendini yama ve fetch_all yöntem gibi alay: https://gist.github.com/alexcouper/eec0d38454ce4bc43c6b –