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-17 16:32:53 +00:00
|
|
|
import isso
|
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()
|
2013-09-02 12:44:53 +00:00
|
|
|
self.db = SQLite(self.path, False)
|
2012-10-16 17:11:59 +00:00
|
|
|
|
2012-10-16 19:00:10 +00:00
|
|
|
def test_get(self):
|
|
|
|
|
2013-09-06 15:19:25 +00:00
|
|
|
rv = self.db.add('/', comment(text='Spam'), '')
|
2012-10-17 10:00:11 +00:00
|
|
|
c = self.db.get('/', rv.id)
|
2012-10-16 19:00:10 +00:00
|
|
|
|
|
|
|
assert c.id == 1
|
|
|
|
assert c.text == 'Spam'
|
|
|
|
|
2012-10-16 17:11:59 +00:00
|
|
|
def test_add(self):
|
|
|
|
|
2013-09-06 15:19:25 +00:00
|
|
|
self.db.add('/', comment(text='Foo'), '')
|
|
|
|
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('/'))
|
2012-10-19 19:12:56 +00:00
|
|
|
assert rv[0].id == 1
|
2012-10-24 18:38:07 +00:00
|
|
|
assert rv[0].path == '/'
|
2012-10-19 19:12:56 +00:00
|
|
|
assert rv[0].text == 'Foo'
|
2012-10-16 17:11:59 +00:00
|
|
|
|
2012-10-19 19:12:56 +00:00
|
|
|
assert rv[1].id == 2
|
|
|
|
assert rv[1].text == 'Bar'
|
2012-10-16 17:11:59 +00:00
|
|
|
|
|
|
|
rv = list(self.db.retrieve('/path/'))
|
|
|
|
assert rv[0].id == 1
|
2012-10-24 18:38:07 +00:00
|
|
|
assert rv[0].path == '/path/'
|
2012-10-16 17:11:59 +00:00
|
|
|
assert rv[0].text == 'Baz'
|
|
|
|
|
2012-10-23 18:36:43 +00:00
|
|
|
def test_add_return(self):
|
|
|
|
|
2013-09-06 15:19:25 +00:00
|
|
|
self.db.add('/', comment(text='1'), '')
|
|
|
|
self.db.add('/', comment(text='2'), '')
|
2012-10-23 18:36:43 +00:00
|
|
|
|
2013-09-06 15:19:25 +00:00
|
|
|
assert self.db.add('/path/', comment(text='1'), '').id == 1
|
2012-10-23 18:36:43 +00:00
|
|
|
|
2012-10-16 19:00:10 +00:00
|
|
|
def test_update(self):
|
|
|
|
|
2013-09-06 15:19:25 +00:00
|
|
|
rv = self.db.add('/', comment(text='Foo'), '')
|
2012-10-16 19:04:20 +00:00
|
|
|
time.sleep(0.1)
|
2012-10-17 10:00:11 +00:00
|
|
|
rv = self.db.update('/', rv.id, comment(text='Bla'))
|
|
|
|
c = self.db.get('/', rv.id)
|
2012-10-16 19:00:10 +00:00
|
|
|
|
|
|
|
assert c.id == 1
|
2012-10-17 08:56:43 +00:00
|
|
|
assert c.text == 'Bla'
|
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):
|
|
|
|
|
2012-10-17 10:00:11 +00:00
|
|
|
rv = self.db.add('/', comment(
|
2013-09-06 15:19:25 +00:00
|
|
|
text='F**CK', author='P*NIS', website='http://somebadhost.org/'), '')
|
2012-10-17 10:22:52 +00:00
|
|
|
assert self.db.delete('/', rv.id) == None
|
2012-10-16 19:00:10 +00:00
|
|
|
|
2012-10-24 18:09:22 +00:00
|
|
|
def test_recent(self):
|
|
|
|
|
2013-09-06 15:19:25 +00:00
|
|
|
self.db.add('/path/', comment(text='2'), '')
|
2012-10-24 18:09:22 +00:00
|
|
|
|
|
|
|
for x in range(5):
|
2013-09-06 15:19:25 +00:00
|
|
|
self.db.add('/', comment(text='%i' % (x+1)), '')
|
2012-10-24 18:09:22 +00:00
|
|
|
|
|
|
|
assert len(list(self.db.recent(mode=7))) == 6
|
|
|
|
assert len(list(self.db.recent(mode=7, limit=5))) == 5
|
|
|
|
|
2012-10-16 17:11:59 +00:00
|
|
|
def tearDown(self):
|
2012-10-16 17:32:55 +00:00
|
|
|
os.unlink(self.path)
|
2012-10-18 13:39:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestSQLitePending(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
|
|
|
fd, self.path = tempfile.mkstemp()
|
2013-09-02 12:44:53 +00:00
|
|
|
self.db = SQLite(self.path, True)
|
2012-10-18 13:39:04 +00:00
|
|
|
|
|
|
|
def test_retrieve(self):
|
|
|
|
|
2013-09-06 15:19:25 +00:00
|
|
|
self.db.add('/', comment(text='Foo'), '')
|
2012-10-18 13:39:04 +00:00
|
|
|
assert len(list(self.db.retrieve('/'))) == 0
|
|
|
|
|
|
|
|
def test_activate(self):
|
|
|
|
|
2013-09-06 15:19:25 +00:00
|
|
|
self.db.add('/', comment(text='Foo'), '')
|
|
|
|
self.db.add('/', comment(text='Bar'), '')
|
2012-10-18 13:39:04 +00:00
|
|
|
self.db.activate('/', 2)
|
|
|
|
|
|
|
|
assert len(list(self.db.retrieve('/'))) == 1
|
|
|
|
assert len(list(self.db.retrieve('/', mode=3))) == 2
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.unlink(self.path)
|