2012-10-16 17:11:59 +00:00
|
|
|
|
2012-10-16 17:32:55 +00:00
|
|
|
import os
|
2012-10-16 18:20:29 +00:00
|
|
|
import json
|
2012-10-16 19:04:20 +00:00
|
|
|
import time
|
2012-10-16 17:11:59 +00:00
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
2012-10-16 19:07:29 +00:00
|
|
|
from isso.models import Comment
|
2012-10-16 17:11:59 +00:00
|
|
|
from isso.db import SQLite
|
|
|
|
|
|
|
|
|
2012-10-16 18:20:29 +00:00
|
|
|
def comment(**kw):
|
|
|
|
return Comment.fromjson(json.dumps(kw))
|
|
|
|
|
|
|
|
|
2012-10-16 17:11:59 +00:00
|
|
|
class TestSQLite(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
2012-10-16 17:32:55 +00:00
|
|
|
fd, self.path = tempfile.mkstemp()
|
2012-10-16 19:30:30 +00:00
|
|
|
self.db = SQLite({'SQLITE': self.path})
|
2012-10-16 17:11:59 +00:00
|
|
|
|
2012-10-16 19:00:10 +00:00
|
|
|
def test_get(self):
|
|
|
|
|
|
|
|
rv = self.db.add('/', comment(text='Spam'))
|
|
|
|
c = self.db.get(*rv)
|
|
|
|
|
|
|
|
assert c.id == 1
|
|
|
|
assert c.text == 'Spam'
|
|
|
|
|
2012-10-16 17:11:59 +00:00
|
|
|
def test_add(self):
|
|
|
|
|
2012-10-16 19:00:10 +00:00
|
|
|
x = self.db.add('/', comment(text='Foo'))
|
2012-10-16 18:20:29 +00:00
|
|
|
self.db.add('/', comment(text='Bar'))
|
|
|
|
self.db.add('/path/', comment(text='Baz'))
|
2012-10-16 17:11:59 +00:00
|
|
|
|
|
|
|
rv = list(self.db.retrieve('/'))
|
|
|
|
assert rv[0].id == 2
|
|
|
|
assert rv[0].text == 'Bar'
|
|
|
|
|
|
|
|
assert rv[1].id == 1
|
|
|
|
assert rv[1].text == 'Foo'
|
|
|
|
|
|
|
|
rv = list(self.db.retrieve('/path/'))
|
|
|
|
assert rv[0].id == 1
|
|
|
|
assert rv[0].text == 'Baz'
|
|
|
|
|
2012-10-16 19:00:10 +00:00
|
|
|
def test_update(self):
|
|
|
|
|
|
|
|
path, id = self.db.add('/', comment(text='Foo'))
|
2012-10-16 19:04:20 +00:00
|
|
|
time.sleep(0.1)
|
2012-10-16 19:00:10 +00:00
|
|
|
path, id = self.db.update(path, id, comment(text='Bla'))
|
|
|
|
c = self.db.get(path, id)
|
|
|
|
|
|
|
|
assert c.id == 1
|
|
|
|
assert c.text == 'Foo'
|
2012-10-16 19:04:20 +00:00
|
|
|
assert c.created < c.modified
|
2012-10-16 19:00:10 +00:00
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
|
|
|
|
path, id = self.db.add('/', comment(
|
|
|
|
text='F**CK', author='P*NIS', website='http://somebadhost.org/'))
|
|
|
|
|
|
|
|
self.db.delete(path, id)
|
|
|
|
c = self.db.get(path, id)
|
|
|
|
|
|
|
|
assert c.id == 1
|
|
|
|
assert c.text == ''
|
|
|
|
assert c.author is None
|
|
|
|
assert c.website is None
|
|
|
|
|
2012-10-16 17:11:59 +00:00
|
|
|
def tearDown(self):
|
2012-10-16 17:32:55 +00:00
|
|
|
os.unlink(self.path)
|