add options to create custom primary id and type with shorthand format

This commit is contained in:
Yi Xie
2013-09-15 14:12:30 -04:00
parent 95cc5dd411
commit 257d767b6d
3 changed files with 54 additions and 17 deletions
+28 -3
View File
@@ -2,7 +2,7 @@ import os
import unittest
from datetime import datetime
from dataset import connect, Integer, String
from dataset import connect
from dataset.util import DatasetException
from sample_data import TEST_DATA
from sqlalchemy.exc import IntegrityError
@@ -35,7 +35,7 @@ class DatabaseTestCase(unittest.TestCase):
def test_create_table_custom_id1(self):
pid = "string_id"
table = self.db.create_table("foo2", primary_id = pid, primary_type=String)
table = self.db.create_table("foo2", pid, 'Text')
assert table.table.exists()
assert len(table.table.columns) == 1, table.table.columns
assert pid in table.table.c, table.table.c
@@ -46,7 +46,7 @@ class DatabaseTestCase(unittest.TestCase):
def test_create_table_custom_id2(self):
pid = "int_id"
table = self.db.create_table("foo3", primary_id = pid, primary_type=Integer)
table = self.db.create_table("foo3", 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
@@ -58,6 +58,31 @@ class DatabaseTestCase(unittest.TestCase):
with self.assertRaises(IntegrityError):
table.insert({'int_id': 123})
def test_create_table_shorthand1(self):
pid = "int_id"
table = self.db['foo4', 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({'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_create_table_shorthand2(self):
pid = "string_id"
table = self.db['foo5', pid, 'Text']
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_load_table(self):
tbl = self.db.load_table('weather')
assert tbl.table == self.tbl.table