2013-07-14 22:51:35 +02:00
|
|
|
import os
|
2013-04-01 19:38:02 +02:00
|
|
|
import unittest
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
2013-09-15 20:12:30 +02:00
|
|
|
from dataset import connect
|
2013-04-05 00:47:40 +02:00
|
|
|
from dataset.util import DatasetException
|
2013-12-18 11:33:45 +01:00
|
|
|
from sample_data import TEST_DATA, TEST_CITY_1
|
2013-09-08 17:35:43 +02:00
|
|
|
from sqlalchemy.exc import IntegrityError
|
2013-04-01 19:38:02 +02:00
|
|
|
|
2013-04-05 11:46:45 +02:00
|
|
|
|
2013-04-01 19:38:02 +02:00
|
|
|
class DatabaseTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
2013-07-14 22:51:35 +02:00
|
|
|
os.environ['DATABASE_URL'] = 'sqlite:///:memory:'
|
2013-09-08 17:35:43 +02:00
|
|
|
self.db = connect('sqlite:///:memory:')
|
2013-04-01 19:46:17 +02:00
|
|
|
self.tbl = self.db['weather']
|
|
|
|
|
for row in TEST_DATA:
|
|
|
|
|
self.tbl.insert(row)
|
2013-04-01 19:38:02 +02:00
|
|
|
|
2013-07-14 22:51:35 +02:00
|
|
|
def tearDown(self):
|
|
|
|
|
# ensure env variable was unset
|
|
|
|
|
del os.environ['DATABASE_URL']
|
|
|
|
|
|
|
|
|
|
def test_valid_database_url(self):
|
|
|
|
|
assert self.db.url, os.environ['DATABASE_URL']
|
|
|
|
|
|
2013-11-15 22:23:03 +01:00
|
|
|
def test_database_url_query_string(self):
|
|
|
|
|
db = connect('sqlite:///:memory:/?cached_statements=1')
|
|
|
|
|
assert 'cached_statements' in db.url, db.url
|
|
|
|
|
|
2013-04-01 22:09:16 +02:00
|
|
|
def test_tables(self):
|
2013-04-05 11:46:45 +02:00
|
|
|
assert self.db.tables == ['weather'], self.db.tables
|
2013-04-01 22:09:16 +02:00
|
|
|
|
2013-04-01 19:38:02 +02:00
|
|
|
def test_create_table(self):
|
|
|
|
|
table = self.db['foo']
|
|
|
|
|
assert table.table.exists()
|
|
|
|
|
assert len(table.table.columns) == 1, table.table.columns
|
2013-04-01 19:46:17 +02:00
|
|
|
assert 'id' in table.table.c, table.table.c
|
|
|
|
|
|
2013-09-08 17:35:43 +02:00
|
|
|
def test_create_table_custom_id1(self):
|
|
|
|
|
pid = "string_id"
|
2013-12-07 06:22:29 +01:00
|
|
|
table = self.db.create_table("foo2", pid, 'String')
|
2013-09-08 17:35:43 +02:00
|
|
|
assert table.table.exists()
|
|
|
|
|
assert len(table.table.columns) == 1, table.table.columns
|
|
|
|
|
assert pid in table.table.c, table.table.c
|
|
|
|
|
|
|
|
|
|
table.insert({
|
|
|
|
|
'string_id': 'foobar'})
|
2013-11-15 22:23:03 +01:00
|
|
|
assert table.find_one(string_id = 'foobar')['string_id'] == 'foobar'
|
2013-09-08 17:35:43 +02:00
|
|
|
|
|
|
|
|
def test_create_table_custom_id2(self):
|
2013-12-07 06:22:29 +01:00
|
|
|
pid = "string_id"
|
|
|
|
|
table = self.db.create_table("foo3", pid, 'String(50)')
|
|
|
|
|
assert table.table.exists()
|
|
|
|
|
assert len(table.table.columns) == 1, table.table.columns
|
|
|
|
|
assert pid in table.table.c, table.table.c
|
|
|
|
|
|
|
|
|
|
table.insert({
|
|
|
|
|
'string_id': 'foobar'})
|
|
|
|
|
assert table.find_one(string_id = 'foobar')['string_id'] == 'foobar'
|
|
|
|
|
|
|
|
|
|
def test_create_table_custom_id3(self):
|
2013-09-08 17:35:43 +02:00
|
|
|
pid = "int_id"
|
2013-12-07 06:22:29 +01:00
|
|
|
table = self.db.create_table("foo4", primary_id = pid)
|
2013-09-08 17:35:43 +02:00
|
|
|
assert table.table.exists()
|
|
|
|
|
assert len(table.table.columns) == 1, table.table.columns
|
|
|
|
|
assert pid in table.table.c, table.table.c
|
|
|
|
|
|
|
|
|
|
table.insert({'int_id': 123})
|
|
|
|
|
table.insert({'int_id': 124})
|
2013-11-15 22:23:03 +01:00
|
|
|
assert table.find_one(int_id = 123)['int_id'] == 123
|
|
|
|
|
assert table.find_one(int_id = 124)['int_id'] == 124
|
2013-12-18 13:02:18 +01:00
|
|
|
self.assertRaises(IntegrityError, lambda: table.insert({'int_id': 123}))
|
2013-09-08 17:35:43 +02:00
|
|
|
|
2013-09-15 20:12:30 +02:00
|
|
|
def test_create_table_shorthand1(self):
|
|
|
|
|
pid = "int_id"
|
2013-12-07 06:22:29 +01:00
|
|
|
table = self.db['foo5', pid]
|
2013-09-15 20:12:30 +02:00
|
|
|
assert table.table.exists
|
|
|
|
|
assert len(table.table.columns) == 1, table.table.columns
|
|
|
|
|
assert pid in table.table.c, table.table.c
|
|
|
|
|
|
|
|
|
|
table.insert({'int_id': 123})
|
|
|
|
|
table.insert({'int_id': 124})
|
2013-11-15 22:23:03 +01:00
|
|
|
assert table.find_one(int_id = 123)['int_id'] == 123
|
|
|
|
|
assert table.find_one(int_id = 124)['int_id'] == 124
|
2013-12-18 13:02:18 +01:00
|
|
|
self.assertRaises(IntegrityError, lambda: table.insert({'int_id': 123}))
|
2013-09-15 20:12:30 +02:00
|
|
|
|
|
|
|
|
def test_create_table_shorthand2(self):
|
|
|
|
|
pid = "string_id"
|
2013-12-07 06:22:29 +01:00
|
|
|
table = self.db['foo6', pid, 'String']
|
|
|
|
|
assert table.table.exists
|
|
|
|
|
assert len(table.table.columns) == 1, table.table.columns
|
|
|
|
|
assert pid in table.table.c, table.table.c
|
|
|
|
|
|
|
|
|
|
table.insert({
|
|
|
|
|
'string_id': 'foobar'})
|
|
|
|
|
assert table.find_one(string_id = 'foobar')['string_id'] == 'foobar'
|
|
|
|
|
|
|
|
|
|
def test_create_table_shorthand3(self):
|
|
|
|
|
pid = "string_id"
|
|
|
|
|
table = self.db['foo7', pid, 'String(20)']
|
2013-09-15 20:12:30 +02:00
|
|
|
assert table.table.exists
|
|
|
|
|
assert len(table.table.columns) == 1, table.table.columns
|
|
|
|
|
assert pid in table.table.c, table.table.c
|
|
|
|
|
|
|
|
|
|
table.insert({
|
|
|
|
|
'string_id': 'foobar'})
|
2013-11-15 22:23:03 +01:00
|
|
|
assert table.find_one(string_id = 'foobar')['string_id'] == 'foobar'
|
2013-09-15 20:12:30 +02:00
|
|
|
|
2013-04-01 19:46:17 +02:00
|
|
|
def test_load_table(self):
|
|
|
|
|
tbl = self.db.load_table('weather')
|
2013-04-05 11:46:45 +02:00
|
|
|
assert tbl.table == self.tbl.table
|
2013-04-01 19:46:17 +02:00
|
|
|
|
|
|
|
|
def test_query(self):
|
|
|
|
|
r = self.db.query('SELECT COUNT(*) AS num FROM weather').next()
|
2013-04-05 11:46:45 +02:00
|
|
|
assert r['num'] == len(TEST_DATA), r
|
2013-04-01 19:46:17 +02:00
|
|
|
|
2013-04-01 19:38:02 +02:00
|
|
|
|
2013-04-01 19:56:14 +02:00
|
|
|
class TableTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
2013-07-14 23:07:42 +02:00
|
|
|
self.db = connect('sqlite:///:memory:')
|
2013-04-01 19:56:14 +02:00
|
|
|
self.tbl = self.db['weather']
|
|
|
|
|
for row in TEST_DATA:
|
|
|
|
|
self.tbl.insert(row)
|
|
|
|
|
|
|
|
|
|
def test_insert(self):
|
2013-04-05 11:46:45 +02:00
|
|
|
assert len(self.tbl) == len(TEST_DATA), len(self.tbl)
|
2013-04-06 03:06:51 +02:00
|
|
|
last_id = self.tbl.insert({
|
2013-12-18 03:22:00 +01:00
|
|
|
'date': datetime(2011, 1, 2),
|
2013-04-01 19:56:14 +02:00
|
|
|
'temperature': -10,
|
|
|
|
|
'place': 'Berlin'}
|
2013-04-05 11:46:45 +02:00
|
|
|
)
|
|
|
|
|
assert len(self.tbl) == len(TEST_DATA)+1, len(self.tbl)
|
2013-04-06 03:06:51 +02:00
|
|
|
assert self.tbl.find_one(id=last_id)['place'] == 'Berlin'
|
2013-04-01 19:56:14 +02:00
|
|
|
|
2013-04-01 22:03:01 +02:00
|
|
|
def test_upsert(self):
|
|
|
|
|
self.tbl.upsert({
|
2013-12-18 03:22:00 +01:00
|
|
|
'date': datetime(2011, 1, 2),
|
2013-04-01 22:03:01 +02:00
|
|
|
'temperature': -10,
|
|
|
|
|
'place': 'Berlin'},
|
|
|
|
|
['place']
|
2013-04-05 11:46:45 +02:00
|
|
|
)
|
|
|
|
|
assert len(self.tbl) == len(TEST_DATA)+1, len(self.tbl)
|
2013-04-01 22:03:01 +02:00
|
|
|
self.tbl.upsert({
|
2013-12-18 03:22:00 +01:00
|
|
|
'date': datetime(2011, 1, 2),
|
2013-04-01 22:03:01 +02:00
|
|
|
'temperature': -10,
|
|
|
|
|
'place': 'Berlin'},
|
|
|
|
|
['place']
|
2013-04-05 11:46:45 +02:00
|
|
|
)
|
|
|
|
|
assert len(self.tbl) == len(TEST_DATA)+1, len(self.tbl)
|
2013-04-01 22:03:01 +02:00
|
|
|
|
2013-06-27 14:13:28 +02:00
|
|
|
def test_upsert_all_key(self):
|
|
|
|
|
for i in range(0,2):
|
|
|
|
|
self.tbl.upsert({
|
2013-12-18 03:22:00 +01:00
|
|
|
'date': datetime(2011, 1, 2),
|
2013-06-27 14:13:28 +02:00
|
|
|
'temperature': -10,
|
|
|
|
|
'place': 'Berlin'},
|
|
|
|
|
['date', 'temperature', 'place']
|
|
|
|
|
)
|
|
|
|
|
|
2013-04-01 22:03:01 +02:00
|
|
|
def test_delete(self):
|
|
|
|
|
self.tbl.insert({
|
2013-12-18 03:22:00 +01:00
|
|
|
'date': datetime(2011, 1, 2),
|
2013-04-01 22:03:01 +02:00
|
|
|
'temperature': -10,
|
|
|
|
|
'place': 'Berlin'}
|
2013-04-05 11:46:45 +02:00
|
|
|
)
|
|
|
|
|
assert len(self.tbl) == len(TEST_DATA)+1, len(self.tbl)
|
2013-04-01 22:03:01 +02:00
|
|
|
self.tbl.delete(place='Berlin')
|
2013-04-05 11:46:45 +02:00
|
|
|
assert len(self.tbl) == len(TEST_DATA), len(self.tbl)
|
2013-04-05 11:53:50 +02:00
|
|
|
self.tbl.delete()
|
|
|
|
|
assert len(self.tbl) == 0, len(self.tbl)
|
2013-04-01 22:03:01 +02:00
|
|
|
|
|
|
|
|
def test_find_one(self):
|
|
|
|
|
self.tbl.insert({
|
2013-12-18 03:22:00 +01:00
|
|
|
'date': datetime(2011, 1, 2),
|
2013-04-01 22:03:01 +02:00
|
|
|
'temperature': -10,
|
|
|
|
|
'place': 'Berlin'}
|
2013-04-05 11:46:45 +02:00
|
|
|
)
|
2013-04-01 22:03:01 +02:00
|
|
|
d = self.tbl.find_one(place='Berlin')
|
2013-04-05 11:46:45 +02:00
|
|
|
assert d['temperature'] == -10, d
|
2013-04-01 22:03:01 +02:00
|
|
|
d = self.tbl.find_one(place='Atlantis')
|
|
|
|
|
assert d is None, d
|
|
|
|
|
|
|
|
|
|
def test_find(self):
|
2013-12-18 11:33:45 +01:00
|
|
|
ds = list(self.tbl.find(place=TEST_CITY_1))
|
2013-04-05 11:46:45 +02:00
|
|
|
assert len(ds) == 3, ds
|
2013-12-18 11:33:45 +01:00
|
|
|
ds = list(self.tbl.find(place=TEST_CITY_1, _limit=2))
|
2013-04-05 11:46:45 +02:00
|
|
|
assert len(ds) == 2, ds
|
2013-04-01 22:03:01 +02:00
|
|
|
|
2013-04-01 19:56:14 +02:00
|
|
|
def test_distinct(self):
|
|
|
|
|
x = list(self.tbl.distinct('place'))
|
2013-04-05 11:46:45 +02:00
|
|
|
assert len(x) == 2, x
|
2013-04-01 19:56:14 +02:00
|
|
|
x = list(self.tbl.distinct('place', 'date'))
|
2013-04-05 11:46:45 +02:00
|
|
|
assert len(x) == 6, x
|
2013-04-01 19:56:14 +02:00
|
|
|
|
2013-04-04 15:43:05 +02:00
|
|
|
def test_insert_many(self):
|
|
|
|
|
data = TEST_DATA * 5000
|
|
|
|
|
self.tbl.insert_many(data)
|
|
|
|
|
assert len(self.tbl) == len(data) + 6
|
|
|
|
|
|
2013-04-05 00:47:40 +02:00
|
|
|
def test_drop_warning(self):
|
|
|
|
|
assert self.tbl._is_dropped is False, 'table shouldn\'t be dropped yet'
|
|
|
|
|
self.tbl.drop()
|
|
|
|
|
assert self.tbl._is_dropped is True, 'table should be dropped now'
|
|
|
|
|
try:
|
|
|
|
|
list(self.tbl.all())
|
|
|
|
|
except DatasetException:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
assert False, 'we should not reach else block, no exception raised!'
|
2013-04-01 19:56:14 +02:00
|
|
|
|
2013-04-05 11:46:45 +02:00
|
|
|
def test_columns(self):
|
|
|
|
|
cols = self.tbl.columns
|
|
|
|
|
assert isinstance(cols, set), 'columns should be a set'
|
|
|
|
|
assert len(cols) == 4, 'column count mismatch'
|
|
|
|
|
assert 'date' in cols and 'temperature' in cols and 'place' in cols
|
|
|
|
|
|
|
|
|
|
def test_iter(self):
|
|
|
|
|
c = 0
|
|
|
|
|
for row in self.tbl:
|
|
|
|
|
c += 1
|
|
|
|
|
assert c == len(self.tbl)
|
|
|
|
|
|
|
|
|
|
def test_update(self):
|
2013-12-18 03:22:00 +01:00
|
|
|
date = datetime(2011, 1, 2)
|
2013-04-05 11:50:50 +02:00
|
|
|
res = self.tbl.update({
|
|
|
|
|
'date': date,
|
2013-04-05 11:46:45 +02:00
|
|
|
'temperature': -10,
|
2013-12-18 11:33:45 +01:00
|
|
|
'place': TEST_CITY_1},
|
2013-04-05 11:50:50 +02:00
|
|
|
['place', 'date']
|
2013-04-05 11:46:45 +02:00
|
|
|
)
|
2013-04-05 11:50:50 +02:00
|
|
|
assert res, 'update should return True'
|
2013-12-18 11:33:45 +01:00
|
|
|
m = self.tbl.find_one(place=TEST_CITY_1, date=date)
|
2013-04-05 11:59:47 +02:00
|
|
|
assert m['temperature'] == -10, 'new temp. should be -10 but is %d' % m['temperature']
|
2013-04-01 19:38:02 +02:00
|
|
|
|
2013-04-05 11:58:16 +02:00
|
|
|
def test_create_column(self):
|
|
|
|
|
from sqlalchemy import FLOAT
|
|
|
|
|
tbl = self.tbl
|
|
|
|
|
tbl.create_column('foo', FLOAT)
|
|
|
|
|
assert 'foo' in tbl.table.c, tbl.table.c
|
|
|
|
|
assert FLOAT == type(tbl.table.c['foo'].type), tbl.table.c['foo'].type
|
|
|
|
|
assert 'foo' in tbl.columns, tbl.columns
|
|
|
|
|
|
2013-12-19 23:46:10 +01:00
|
|
|
def test_key_order(self):
|
|
|
|
|
res = self.db.query('SELECT temperature, place FROM weather LIMIT 1')
|
|
|
|
|
keys = res.next().keys()
|
|
|
|
|
assert keys[0] == 'temperature'
|
|
|
|
|
assert keys[1] == 'place'
|
|
|
|
|
|
2013-04-01 19:38:02 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|