Here are the examples of the python api sqlalchemy.types.String taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
73 Examples
3
Example 1
def __init__(self, type_in=None, key=None, engine=None, **kwargs):
"""Initialization."""
if not cryptography:
raise ImproperlyConfigured(
"'cryptography' is required to use EncryptedType"
)
super(EncryptedType, self).__init__(**kwargs)
# set the underlying type
if type_in is None:
type_in = String()
elif isinstance(type_in, type):
type_in = type_in()
self.underlying_type = type_in
self._key = key
if not engine:
engine = AesEngine
self.engine = engine()
3
Example 2
Project: ironic Source File: test_migrations.py
def _check_dd34e1f1303b(self, engine, data):
nodes = db_utils.get_table(engine, 'nodes')
col_names = [column.name for column in nodes.c]
self.assertIn('resource_class', col_names)
self.assertIsInstance(nodes.c.resource_class.type,
sqlalchemy.types.String)
3
Example 3
Project: ironic Source File: test_migrations.py
def _check_242cc6a923b3(self, engine, data):
nodes = db_utils.get_table(engine, 'nodes')
col_names = [column.name for column in nodes.c]
self.assertIn('maintenance_reason', col_names)
self.assertIsInstance(nodes.c.maintenance_reason.type,
sqlalchemy.types.String)
3
Example 4
def test_deferred(self):
class A( self.Entity ):
name = Field(String(20))
stuff = Field( Text, deferred=True )
self.create_all()
with self.session.begin():
A(name='foo')
3
Example 5
Project: Camelot Source File: test_class_methods.py
def test_get( self ):
class A( self.Entity ):
name = Field(String(32))
self.create_all()
with self.session.begin():
a1 = A(name="a1")
self.session.expire_all()
self.assertEqual( A.get(1).name, "a1" )
3
Example 6
Project: Camelot Source File: test_fields.py
def test_attr_syntax(self):
class Person(self.Entity):
firstname = Field(String(30))
surname = Field(String(30))
self.create_all()
self.session.begin()
Person(firstname="Homer", surname="Simpson")
Person(firstname="Bart", surname="Simpson")
self.session.commit()
self.session.expunge_all()
p = Person.get_by(firstname="Homer")
assert p.surname == 'Simpson'
3
Example 7
Project: SAUCE Source File: selectors.py
def __init__(self, *args, **kwargs):
self.default_widgets.update({
sqlat.String: MediumTextField,
sqlat.Integer: SmallTextField,
sqlat.Numeric: SmallTextField,
sqlat.DateTime: CalendarDateTimePicker,
sqlat.Date: twb.CalendarDatePicker,
sqlat.Time: twb.CalendarTimePicker,
sqlat.Binary: twb.FileField,
sqlat.BLOB: twb.FileField,
sqlat.PickleType: MediumTextField,
sqlat.Enum: twjc.ChosenSingleSelectField,
})
super(MyWidgetSelector, self).__init__(*args, **kwargs)
3
Example 8
Project: Camelot Source File: test_fields.py
def test_has_field(self):
class Person(self.Entity):
has_field('firstname', String(30))
has_field('surname', String(30))
self.create_all()
self.session.begin()
Person(firstname="Homer", surname="Simpson")
Person(firstname="Bart", surname="Simpson")
self.session.commit()
self.session.expunge_all()
p = Person.get_by(firstname="Homer")
assert p.surname == 'Simpson'
3
Example 9
Project: sqlalchemy-migrate Source File: schema.py
def are_column_types_eq(self, old_type, new_type):
"""Compares two types to be equal"""
ret = old_type.__class__ == new_type.__class__
# String length is a special case
if ret and isinstance(new_type, sqlalchemy.types.String):
ret = (getattr(old_type, 'length', None) == \
getattr(new_type, 'length', None))
return ret
3
Example 10
Project: ironic Source File: test_migrations.py
def _check_e294876e8028(self, engine, data):
nodes = db_utils.get_table(engine, 'nodes')
col_names = [column.name for column in nodes.c]
self.assertIn('network_interface', col_names)
self.assertIsInstance(nodes.c.network_interface.type,
sqlalchemy.types.String)
3
Example 11
Project: Camelot Source File: test_m2o.py
def test_belongs_to_syntax(self):
class Person( self.Entity ):
has_field('name', String(30))
class Animal( self.Entity ):
has_field('name', String(30))
belongs_to('owner', of_kind='Person')
self.create_all()
with self.session.begin():
santa = Person(name="Santa Claus")
rudolph = Animal(name="Rudolph", owner=santa)
self.session.expunge_all()
assert "Claus" in Animal.get_by(name="Rudolph").owner.name
3
Example 12
def test_string_length(self):
from sqlalchemy import Column, types
column = Column('_column', types.String(10))
elements = self._get_elements((('column', column),))
self.assertEqual(len(elements), 1)
self.assertEqual(elements[0].attrib, {
'minOccurs': '0',
'name': 'column',
'nillable': 'true'})
restrictions = elements[0].findall(
self._make_xpath('. simpleType restriction'))
self.assertEqual(len(restrictions), 1)
self.assertEqual(restrictions[0].attrib, {'base': 'xsd:string'})
maxLengths = restrictions[0].findall(self._make_xpath('. maxLength'))
self.assertEqual(len(maxLengths), 1)
self.assertEqual(maxLengths[0].attrib, {'value': '10'})
3
Example 13
def test_lifecycle(self):
class A( self.Entity ):
name = Field( String( 20 ) )
assert isinstance( A.name, Field )
self.create_all()
assert not isinstance( A.name, Field )
3
Example 14
def filters(self, column):
"""
Return filters for given column.
:param column: SQLAlchemy Column object
"""
should_trim = column.info.get('trim', None)
filters = column.info.get('filters', [])
if (
(
isinstance(column.type, sa.types.String) and
self.meta.strip_string_fields and
should_trim is None
) or
should_trim is True
):
filters.append(strip_string)
return filters
3
Example 15
Project: Camelot Source File: test_dict.py
def test_set_on_aliased_column(self):
class A( self.Entity ):
name = Field(String(60), colname='strName')
self.create_all()
with self.session.begin():
a = A()
a.set(name='Aye')
self.session.expire_all()
assert a.name == 'Aye'
3
Example 16
Project: django-url-filter Source File: test_sqlalchemy.py
def test_get_form_field_for_field(self):
fs = SQLAlchemyModelFilterSet()
assert isinstance(
fs._get_form_field_for_field(ColumnProperty(Column('name', String(50)))),
forms.CharField
)
assert isinstance(
fs._get_form_field_for_field(ColumnProperty(Column('name', Integer))),
forms.IntegerField
)
with pytest.raises(SkipFilter):
fs._get_form_field_for_field(ColumnProperty(Column('name', TypeEngine)))
3
Example 17
Project: wtforms-alchemy Source File: generator.py
def length_validator(self, column):
"""
Returns length validator for given column
:param column: SQLAlchemy Column object
"""
if (
isinstance(column.type, sa.types.String) and
hasattr(column.type, 'length') and
column.type.length
):
return self.get_validator('length', max=column.type.length)
3
Example 18
Project: ironic Source File: test_migrations.py
def _check_48d6c242bb9b(self, engine, data):
node_tags = db_utils.get_table(engine, 'node_tags')
col_names = [column.name for column in node_tags.c]
self.assertIn('tag', col_names)
self.assertIsInstance(node_tags.c.tag.type,
sqlalchemy.types.String)
nodes = db_utils.get_table(engine, 'nodes')
data = {'id': '123', 'name': 'node1'}
nodes.insert().execute(data)
data = {'node_id': '123', 'tag': 'tag1'}
node_tags.insert().execute(data)
tag = node_tags.select(node_tags.c.node_id == '123').execute().first()
self.assertEqual('tag1', tag['tag'])
3
Example 19
Project: sqlalchemy Source File: test_firebird.py
def test_update_returning(self):
table1 = table('mytable', column('myid', Integer), column('name'
, String(128)), column('description',
String(128)))
u = update(table1, values=dict(name='foo'
)).returning(table1.c.myid, table1.c.name)
self.assert_compile(u,
'UPDATE mytable SET name=:name RETURNING '
'mytable.myid, mytable.name')
u = update(table1, values=dict(name='foo')).returning(table1)
self.assert_compile(u,
'UPDATE mytable SET name=:name RETURNING '
'mytable.myid, mytable.name, '
'mytable.description')
u = update(table1, values=dict(name='foo'
)).returning(func.length(table1.c.name))
self.assert_compile(u,
'UPDATE mytable SET name=:name RETURNING '
'char_length(mytable.name) AS length_1')
3
Example 20
@testing.provide_metadata
def test_strlen(self):
metadata = self.metadata
# On FB the length() function is implemented by an external UDF,
# strlen(). Various SA tests fail because they pass a parameter
# to it, and that does not work (it always results the maximum
# string length the UDF was declared to accept). This test
# checks that at least it works ok in other cases.
t = Table('t1', metadata, Column('id', Integer,
Sequence('t1idseq'), primary_key=True), Column('name'
, String(10)))
metadata.create_all()
t.insert(values=dict(name='dante')).execute()
t.insert(values=dict(name='alighieri')).execute()
select([func.count(t.c.id)], func.length(t.c.name)
== 5).execute().first()[0] == 1
3
Example 21
Project: ironic Source File: test_migrations.py
def _check_789acc877671(self, engine, data):
nodes = db_utils.get_table(engine, 'nodes')
col_names = [column.name for column in nodes.c]
self.assertIn('raid_config', col_names)
self.assertIn('target_raid_config', col_names)
self.assertIsInstance(nodes.c.raid_config.type,
sqlalchemy.types.String)
self.assertIsInstance(nodes.c.target_raid_config.type,
sqlalchemy.types.String)
3
Example 22
def test_string(self):
from sqlalchemy import Column, types
column = Column('_column', types.String)
elements = self._get_elements((('column', column),))
self.assertEqual(len(elements), 1)
self.assertEqual(elements[0].attrib, {
'minOccurs': '0',
'name': 'column',
'nillable': 'true',
'type': 'xsd:string'})
3
Example 23
Project: ironic Source File: test_migrations.py
def _check_4f399b21ae71(self, engine, data):
nodes = db_utils.get_table(engine, 'nodes')
col_names = [column.name for column in nodes.c]
self.assertIn('clean_step', col_names)
self.assertIsInstance(nodes.c.clean_step.type,
sqlalchemy.types.String)
3
Example 24
def test_simple(self):
class A( self.Entity ):
name = Field(String(60))
class B( self.Entity ):
name = Field(String(60))
a = ManyToOne('A')
self.create_all()
with self.session.begin():
b1 = B( name='b1', a = A( name = 'a1' ) )
self.session.expunge_all()
b = B.query.one()
assert b.a.name == 'a1'
3
Example 25
Project: ironic Source File: test_migrations.py
def _check_3ae36a5f5131(self, engine, data):
nodes = db_utils.get_table(engine, 'nodes')
column_names = [column.name for column in nodes.c]
self.assertIn('name', column_names)
self.assertIsInstance(nodes.c.name.type,
sqlalchemy.types.String)
data = {'driver': 'fake',
'uuid': uuidutils.generate_uuid(),
'name': 'node'
}
nodes.insert().values(data).execute()
data['uuid'] = uuidutils.generate_uuid()
self.assertRaises(db_exc.DBDuplicateEntry,
nodes.insert().execute, data)
3
Example 26
def is_string(self, col_name):
try:
return (isinstance(self.list_columns[col_name].type, sa.types.String) or
isinstance(self.list_columns[col_name].type, LowerCaseString))
except:
return False
3
Example 27
Project: Camelot Source File: test_m2o.py
def test_one_pk(self):
class A( self.Entity ):
name = Field(String(40), primary_key=True)
class B( self.Entity ):
a = ManyToOne('A', primary_key=True)
class C( self.Entity ):
b = ManyToOne('B', primary_key=True)
self.create_all()
assert 'name' in colnames( A.table.primary_key.columns )
assert 'a_name' in colnames( B.table.primary_key.columns )
assert 'b_a_name' in colnames( C.table.primary_key.columns )
2
Example 28
Project: ironic-inspector Source File: test_migrations.py
def _check_578f84f38d(self, engine, data):
nodes = db_utils.get_table(engine, 'nodes')
col_names = [column.name for column in nodes.c]
self.assertIn('uuid', col_names)
self.assertIsInstance(nodes.c.uuid.type, sqlalchemy.types.String)
self.assertIn('started_at', col_names)
self.assertIsInstance(nodes.c.started_at.type, sqlalchemy.types.Float)
self.assertIn('finished_at', col_names)
self.assertIsInstance(nodes.c.started_at.type, sqlalchemy.types.Float)
self.assertIn('error', col_names)
self.assertIsInstance(nodes.c.error.type, sqlalchemy.types.Text)
attributes = db_utils.get_table(engine, 'attributes')
col_names = [column.name for column in attributes.c]
self.assertIn('uuid', col_names)
self.assertIsInstance(attributes.c.uuid.type, sqlalchemy.types.String)
self.assertIn('name', col_names)
self.assertIsInstance(attributes.c.name.type, sqlalchemy.types.String)
self.assertIn('value', col_names)
self.assertIsInstance(attributes.c.value.type, sqlalchemy.types.String)
options = db_utils.get_table(engine, 'options')
col_names = [column.name for column in options.c]
self.assertIn('uuid', col_names)
self.assertIsInstance(options.c.uuid.type, sqlalchemy.types.String)
self.assertIn('name', col_names)
self.assertIsInstance(options.c.name.type, sqlalchemy.types.String)
self.assertIn('value', col_names)
self.assertIsInstance(options.c.value.type, sqlalchemy.types.Text)
2
Example 29
Project: ironic-inspector Source File: test_migrations.py
def _check_d588418040d(self, engine, data):
rules = db_utils.get_table(engine, 'rules')
col_names = [column.name for column in rules.c]
self.assertIn('uuid', col_names)
self.assertIsInstance(rules.c.uuid.type, sqlalchemy.types.String)
self.assertIn('created_at', col_names)
self.assertIsInstance(rules.c.created_at.type,
sqlalchemy.types.DateTime)
self.assertIn('description', col_names)
self.assertIsInstance(rules.c.description.type, sqlalchemy.types.Text)
self.assertIn('disabled', col_names)
# in some backends bool type is integer
self.assertIsInstance(rules.c.disabled.type,
(sqlalchemy.types.Boolean,
sqlalchemy.types.Integer))
conditions = db_utils.get_table(engine, 'rule_conditions')
col_names = [column.name for column in conditions.c]
self.assertIn('id', col_names)
self.assertIsInstance(conditions.c.id.type, sqlalchemy.types.Integer)
self.assertIn('rule', col_names)
self.assertIsInstance(conditions.c.rule.type, sqlalchemy.types.String)
self.assertIn('op', col_names)
self.assertIsInstance(conditions.c.op.type, sqlalchemy.types.String)
self.assertIn('multiple', col_names)
self.assertIsInstance(conditions.c.multiple.type,
sqlalchemy.types.String)
self.assertIn('field', col_names)
self.assertIsInstance(conditions.c.field.type, sqlalchemy.types.Text)
self.assertIn('params', col_names)
self.assertIsInstance(conditions.c.params.type, sqlalchemy.types.Text)
actions = db_utils.get_table(engine, 'rule_actions')
col_names = [column.name for column in actions.c]
self.assertIn('id', col_names)
self.assertIsInstance(actions.c.id.type, sqlalchemy.types.Integer)
self.assertIn('rule', col_names)
self.assertIsInstance(actions.c.rule.type, sqlalchemy.types.String)
self.assertIn('action', col_names)
self.assertIsInstance(actions.c.action.type, sqlalchemy.types.String)
self.assertIn('params', col_names)
self.assertIsInstance(actions.c.params.type, sqlalchemy.types.Text)
0
Example 30
def is_string(self, col_name):
try:
return isinstance(self.list_columns[col_name].type, sa.types.String)
except:
return False
0
Example 31
def test_multi_pk_in_target(self):
class A( self.Entity ):
key1 = Field(Integer, primary_key=True)
key2 = Field(String(40), primary_key=True)
class B( self.Entity ):
num = Field(Integer, primary_key=True)
a = ManyToOne('A', primary_key=True)
class C( self.Entity ):
num = Field(Integer, primary_key=True)
b = ManyToOne('B', primary_key=True)
self.create_all()
assert 'key1' in colnames( A.table.primary_key.columns )
assert 'key2' in colnames( A.table.primary_key.columns )
assert 'num' in colnames( B.table.primary_key.columns )
assert 'a_key1' in colnames( B.table.primary_key.columns )
assert 'a_key2' in colnames( B.table.primary_key.columns )
assert 'num' in colnames( C.table.primary_key.columns )
assert 'b_num' in colnames( C.table.primary_key.columns )
assert 'b_a_key1' in colnames( C.table.primary_key.columns )
assert 'b_a_key2' in colnames( C.table.primary_key.columns )
0
Example 32
Project: sqlalchemy Source File: test_firebird.py
def test_insert_returning(self):
table1 = table('mytable', column('myid', Integer), column('name'
, String(128)), column('description',
String(128)))
i = insert(table1, values=dict(name='foo'
)).returning(table1.c.myid, table1.c.name)
self.assert_compile(i,
'INSERT INTO mytable (name) VALUES (:name) '
'RETURNING mytable.myid, mytable.name')
i = insert(table1, values=dict(name='foo')).returning(table1)
self.assert_compile(i,
'INSERT INTO mytable (name) VALUES (:name) '
'RETURNING mytable.myid, mytable.name, '
'mytable.description')
i = insert(table1, values=dict(name='foo'
)).returning(func.length(table1.c.name))
self.assert_compile(i,
'INSERT INTO mytable (name) VALUES (:name) '
'RETURNING char_length(mytable.name) AS '
'length_1')
0
Example 33
Project: changes Source File: 8550c7394f4_change_commands_env_to_text.py
def downgrade():
op.alter_column('command', 'env', type_ = sa.types.String(length=2048))
0
Example 34
Project: Camelot Source File: test_m2m.py
def test_simple( self ):
class A( self.Entity ):
using_options( shortnames = True )
name = Field(String(60))
as_ = ManyToMany('A')
bs_ = ManyToMany('B')
class B( self.Entity ):
using_options( shortnames = True )
name = Field(String(60))
as_ = ManyToMany('A')
self.create_all()
# check m2m table was generated correctly
m2m_table = A.bs_.property.secondary
assert m2m_table.name in self.metadata.tables
# check column names
m2m_cols = m2m_table.columns
assert 'a_id' in m2m_cols
assert 'b_id' in m2m_cols
# check selfref m2m table column names were generated correctly
m2m_cols = A.as_.property.secondary.columns
assert 'as__id' in m2m_cols
assert 'inverse_id' in m2m_cols
# check the relationships work as expected
with self.session.begin():
b1 = B(name='b1', as_=[A(name='a1')])
self.session.expire_all()
a = A.query.one()
b = B.query.one()
assert a in b.as_
assert b in a.bs_
0
Example 35
Project: Camelot Source File: test_m2m.py
def test_multi_pk_in_target(self):
class A(self.Entity):
key1 = Field(Integer, primary_key=True, autoincrement=False)
key2 = Field(String(40), primary_key=True)
bs_ = ManyToMany('B')
class B(self.Entity):
name = Field(String(60))
as_ = ManyToMany('A')
self.create_all()
with self.session.begin():
b1 = B(name='b1', as_=[A(key1=10, key2='a1')])
self.session.expire_all()
a = A.query.one()
b = B.query.one()
assert a in b.as_
assert b in a.bs_
0
Example 36
@staticmethod
def impl():
return types.String(64)
0
Example 37
Project: Camelot Source File: test_m2m.py
def test_multi(self):
class A(self.Entity):
name = Field(String(100))
rel1 = ManyToMany('B')
rel2 = ManyToMany('B')
class B(self.Entity):
name = Field(String(20), primary_key=True)
self.create_all()
with self.session.begin():
b1 = B(name='b1')
a1 = A(name='a1', rel1=[B(name='b2'), b1],
rel2=[B(name='b3'), B(name='b4'), b1])
self.session.expire_all()
a1 = A.query.one()
b1 = B.get_by(name='b1')
b2 = B.get_by(name='b2')
assert b1 in a1.rel1
assert b1 in a1.rel2
assert b2 in a1.rel1
0
Example 38
Project: sqlalchemy Source File: test_firebird.py
def test_varchar_raise(self):
for type_ in (
String,
VARCHAR,
String(),
VARCHAR(),
Unicode,
Unicode(),
):
type_ = sqltypes.to_instance(type_)
assert_raises_message(
exc.CompileError,
"VARCHAR requires a length on dialect firebird",
type_.compile,
dialect=firebird.dialect())
t1 = Table('sometable', MetaData(),
Column('somecolumn', type_)
)
assert_raises_message(
exc.CompileError,
r"\(in table 'sometable', column 'somecolumn'\)\: "
r"(?:N)?VARCHAR requires a length on dialect firebird",
schema.CreateTable(t1).compile,
dialect=firebird.dialect()
)
0
Example 39
Project: Camelot Source File: test_m2m.py
def test_selfref(self):
class Person(self.Entity):
using_options(shortnames=True)
name = Field(String(30))
friends = ManyToMany('Person')
self.create_all()
with self.session.begin():
barney = Person(name="Barney")
homer = Person(name="Homer", friends=[barney])
barney.friends.append(homer)
self.session.expire_all()
homer = Person.get_by(name="Homer")
barney = Person.get_by(name="Barney")
assert homer in barney.friends
assert barney in homer.friends
m2m_cols = Person.friends.property.secondary.columns
assert 'friends_id' in m2m_cols
assert 'inverse_id' in m2m_cols
0
Example 40
Project: Camelot Source File: test_m2m.py
def test_bidirectional_selfref(self):
class Person(self.Entity):
using_options(shortnames=True)
name = Field(String(30))
friends = ManyToMany('Person')
is_friend_of = ManyToMany('Person')
self.create_all()
with self.session.begin():
barney = Person(name="Barney")
homer = Person(name="Homer", friends=[barney])
barney.friends.append(homer)
self.session.expire_all()
homer = Person.get_by(name="Homer")
barney = Person.get_by(name="Barney")
assert homer in barney.friends
assert barney in homer.friends
m2m_cols = Person.friends.property.secondary.columns
assert 'friends_id' in m2m_cols
assert 'is_friend_of_id' in m2m_cols
0
Example 41
Project: Camelot Source File: test_m2m.py
def test_has_and_belongs_to_many(self):
class A(self.Entity):
has_field('name', String(100))
has_and_belongs_to_many('bs', of_kind='B')
class B(self.Entity):
has_field('name', String(100), primary_key=True)
self.create_all()
with self.session.begin():
b1 = B(name='b1')
a1 = A(name='a1', bs=[B(name='b2'), b1])
a2 = A(name='a2', bs=[B(name='b3'), b1])
a3 = A(name='a3')
self.session.expire_all()
a1 = A.get_by(name='a1')
a2 = A.get_by(name='a2')
a3 = A.get_by(name='a3')
b1 = B.get_by(name='b1')
b2 = B.get_by(name='b2')
assert b1 in a1.bs
assert b2 in a1.bs
assert b1 in a2.bs
assert not a3.bs
0
Example 42
Project: Camelot Source File: test_m2m.py
def test_local_and_remote_colnames(self):
class A(self.Entity):
using_options(shortnames=True)
key1 = Field(Integer, primary_key=True, autoincrement=False)
key2 = Field(String(40), primary_key=True)
bs_ = ManyToMany('B', local_colname=['foo', 'bar'],
remote_colname="baz")
class B(self.Entity):
using_options(shortnames=True)
name = Field(String(60))
as_ = ManyToMany('A', remote_colname=['foo', 'bar'],
local_colname="baz")
self.create_all()
with self.session.begin():
b1 = B(name='b1', as_=[A(key1=10, key2='a1')])
self.session.expire_all()
a = A.query.one()
b = B.query.one()
assert a in b.as_
assert b in a.bs_
0
Example 43
Project: sqlalchemy Source File: test_firebird.py
@testing.provide_metadata
def test_rowcount_flag(self):
metadata = self.metadata
engine = engines.testing_engine(
options={'enable_rowcount': True})
assert engine.dialect.supports_sane_rowcount
metadata.bind = engine
t = Table('t1', metadata, Column('data', String(10)))
metadata.create_all()
r = t.insert().execute({'data': 'd1'}, {'data': 'd2'}, {'data'
: 'd3'})
r = t.update().where(t.c.data == 'd2').values(data='d3'
).execute()
eq_(r.rowcount, 1)
r = t.delete().where(t.c.data == 'd3').execute()
eq_(r.rowcount, 2)
r = \
t.delete().execution_options(enable_rowcount=False).execute()
eq_(r.rowcount, -1)
engine.dispose()
engine = engines.testing_engine(options={'enable_rowcount'
: False})
assert not engine.dialect.supports_sane_rowcount
metadata.bind = engine
r = t.insert().execute({'data': 'd1'}, {'data': 'd2'}, {'data'
: 'd3'})
r = t.update().where(t.c.data == 'd2').values(data='d3'
).execute()
eq_(r.rowcount, -1)
r = t.delete().where(t.c.data == 'd3').execute()
eq_(r.rowcount, -1)
r = t.delete().execution_options(enable_rowcount=True).execute()
eq_(r.rowcount, 1)
r.close()
engine.dispose()
0
Example 44
Project: Camelot Source File: test_m2m.py
def test_manual_table_auto_joins(self):
from sqlalchemy import Table, Column, ForeignKey, ForeignKeyConstraint
a_b = schema.Table('a_b', self.metadata,
schema.Column('a_key1', Integer()),
schema.Column('a_key2', String(40) ),
schema.Column('b_id', Integer(), schema.ForeignKey('b.id')),
schema.ForeignKeyConstraint(['a_key1', 'a_key2'],
['a.key1', 'a.key2']))
class A(self.Entity):
using_options(shortnames=True)
key1 = Field(Integer, primary_key=True, autoincrement=False)
key2 = Field(String(40), primary_key=True)
bs_ = ManyToMany('B', table=a_b)
class B(self.Entity):
using_options(shortnames=True)
name = Field(String(60))
as_ = ManyToMany('A', table=a_b)
self.create_all()
with self.session.begin():
b1 = B(name='b1', as_=[A(key1=10, key2='a1')])
self.session.expire_all()
a = A.query.one()
b = B.query.one()
assert a in b.as_
assert b in a.bs_
0
Example 45
Project: Camelot Source File: test_m2m.py
def test_manual_table_manual_joins(self):
from sqlalchemy import Table, Column, and_
a_b = schema.Table('a_b', self.metadata,
schema.Column('a_key1', Integer),
schema.Column('a_key2', String(40)),
schema.Column('b_id', String(60)))
class A(self.Entity):
using_options(shortnames=True)
key1 = Field(Integer, primary_key=True, autoincrement=False)
key2 = Field(String(40), primary_key=True)
bs_ = ManyToMany('B', table=a_b,
primaryjoin=lambda: and_(A.key1 == a_b.c.a_key1,
A.key2 == a_b.c.a_key2),
secondaryjoin=lambda: B.id == a_b.c.b_id,
foreign_keys=[a_b.c.a_key1, a_b.c.a_key2,
a_b.c.b_id])
class B(self.Entity):
using_options(shortnames=True)
name = Field(String(60))
self.create_all()
with self.session.begin():
a1 = A(key1=10, key2='a1', bs_=[B(name='b1')])
self.session.expire_all()
a = A.query.one()
b = B.query.one()
assert b in a.bs_
0
Example 46
Project: sqlalchemy Source File: test_reflection.py
@testing.provide_metadata
def test_max_ident_in_varchar_not_present(self):
"""test [ticket:3504].
Here we are testing not just that the "max" token comes back
as None, but also that these types accept "max" as the value
of "length" on construction, which isn't a directly docuemented
pattern however is likely in common use.
"""
metadata = self.metadata
Table(
't', metadata,
Column('t1', types.String),
Column('t2', types.Text('max')),
Column('t3', types.Text('max')),
Column('t4', types.LargeBinary('max')),
Column('t5', types.VARBINARY('max')),
)
metadata.create_all()
for col in inspect(testing.db).get_columns('t'):
is_(col['type'].length, None)
in_('max', str(col['type'].compile(dialect=testing.db.dialect)))
0
Example 47
Project: Camelot Source File: test_class_methods.py
def test_query( self ):
#
# The query attribute of a class should return a query bound to
# the session belonging to the current thread
#
from PyQt4 import QtCore
class A( self.Entity ):
name = Field(String(32))
self.create_all()
with self.session.begin():
a1 = A(name="a1")
self.assertEqual( A.query.count(), 1 )
session_1 = A.query.session
self.assertEqual( session_1, Session() )
class QueryThread( QtCore.QThread ):
def run( self ):
self.query_session_2 = A.query.session
self.orm_session_2 = Session()
thread = QueryThread()
thread.start()
thread.wait()
self.assertNotEqual( session_1, thread.orm_session_2 )
self.assertNotEqual( session_1, thread.query_session_2 )
0
Example 48
Project: Camelot Source File: test_m2o.py
def test_wh_key_in_m2o_col_kwargs(self):
class A( self.Entity ):
name = Field(String(128), default="foo")
class B( self.Entity ):
# specify a different key for the column so that
# it doesn't override the property when the column
# gets created.
a = ManyToOne('A', colname='a',
column_kwargs=dict(key='a_id'))
self.create_all()
assert 'id' in colnames( A.table.primary_key.columns )
assert 'a_id' not in colnames( B.table.columns )
with self.session.begin():
a = A()
self.session.flush()
b = B(a=a)
self.session.expunge_all()
assert B.query.first().a == A.query.first()
0
Example 49
Project: Camelot Source File: test_dict.py
def setUp( self ):
super( TestDeepSet, self ).setUp()
class Table1( self.Entity ):
t1id = Field(Integer, primary_key=True)
name = Field(String(30))
tbl2s = OneToMany('Table2')
tbl3 = OneToOne('Table3')
class Table2( self.Entity ):
t2id = Field(Integer, primary_key=True)
name = Field(String(30))
tbl1 = ManyToOne(Table1)
class Table3( self.Entity ):
t3id = Field(Integer, primary_key=True)
name = Field(String(30))
tbl1 = ManyToOne(Table1)
self.create_all()
self.Table1 = Table1
self.Table2 = Table2
self.Table3 = Table3
0
Example 50
Project: Camelot Source File: test_m2o.py
def test_specified_field(self):
class Person( self.Entity ):
name = Field(String(30))
class Animal( self.Entity ):
name = Field(String(30))
owner_id = Field( Integer, colname = 'owner' )
owner = ManyToOne('Person', field=owner_id)
self.create_all()
assert 'owner' in colnames( Animal.table.c )
assert 'owner_id' not in colnames( Animal.table.c )
mapper = orm.class_mapper( Animal )
properties = list( mapper.iterate_properties )
prop_keys = [p.key for p in properties]
assert 'owner' in prop_keys
assert 'owner_id' in prop_keys
with self.session.begin():
homer = Person(name="Homer")
slh = Animal( name="Santa's Little Helper", owner = homer )
self.session.expunge_all()
homer = Person.get_by(name="Homer")
animals = Animal.query.all()
assert animals[0].owner is homer