create table with custom primary_id
This commit is contained in:
@@ -5,13 +5,14 @@ from datetime import datetime
|
||||
from dataset import connect
|
||||
from dataset.util import DatasetException
|
||||
from sample_data import TEST_DATA
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
|
||||
class DatabaseTestCase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
os.environ['DATABASE_URL'] = 'sqlite:///:memory:'
|
||||
self.db = connect()
|
||||
self.db = connect('sqlite:///:memory:')
|
||||
self.tbl = self.db['weather']
|
||||
for row in TEST_DATA:
|
||||
self.tbl.insert(row)
|
||||
@@ -32,6 +33,31 @@ class DatabaseTestCase(unittest.TestCase):
|
||||
assert len(table.table.columns) == 1, table.table.columns
|
||||
assert 'id' in table.table.c, table.table.c
|
||||
|
||||
def test_create_table_custom_id1(self):
|
||||
pid = "string_id"
|
||||
table = self.db.create_table("foo2", primary_id = pid)
|
||||
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')[0] == 'foobar'
|
||||
|
||||
def test_create_table_custom_id2(self):
|
||||
pid = "int_id"
|
||||
table = self.db.create_table("foo3", primary_id = pid, is_integer=True)
|
||||
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})
|
||||
assert table.find_one(int_id = 123)[0] == 123
|
||||
assert table.find_one(int_id = 124)[0] == 124
|
||||
with self.assertRaises(IntegrityError):
|
||||
table.insert({'int_id': 123})
|
||||
|
||||
def test_load_table(self):
|
||||
tbl = self.db.load_table('weather')
|
||||
assert tbl.table == self.tbl.table
|
||||
|
||||
Reference in New Issue
Block a user