Show
Ignore:
Timestamp:
09/17/09 13:54:19 (4 years ago)
Author:
ged
Message:

- Changed the pattern used by default to generate column names for (all)

ManyToMany relationships so that the meaning of bidirectional
self-referential relationships does not depend on the order of declaration
of each side (closes #69). See upgrade notes for details.

- Made M2MCOL_NAMEFORMAT accept a callable, so that more complex naming

generation can be used if so desired.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/tests/test_m2m.py

    r451 r458  
    44 
    55from elixir import * 
     6import elixir 
    67 
    78#----------- 
     
    2425 
    2526        setup_all(True) 
    26  
     27        A.mapper.compile() 
     28 
     29        # check m2m table was generated correctly 
     30        m2m_table = A.bs_.property.secondary 
     31        assert m2m_table.name in metadata.tables 
     32 
     33        # check column names 
     34        m2m_cols = m2m_table.columns 
     35        assert 'bs__id' in m2m_cols 
     36        assert 'as__id' in m2m_cols 
     37 
     38        # check the relationships work as expected 
    2739        b1 = B(name='b1', as_=[A(name='a1')]) 
    2840 
     
    3648        assert b in a.bs_ 
    3749 
    38     def test_column_format(self): 
     50    def test_custom_global_column_nameformat(self): 
     51        # this needs to be done before declaring the classes 
     52        elixir.options.M2MCOL_NAMEFORMAT = elixir.options.OLD_M2MCOL_NAMEFORMAT 
     53 
     54        class A(Entity): 
     55            bs_ = ManyToMany('B') 
     56 
     57        class B(Entity): 
     58            as_ = ManyToMany('A') 
     59 
     60        setup_all(True) 
     61 
     62        # revert to original format 
     63        elixir.options.M2MCOL_NAMEFORMAT = elixir.options.NEW_M2MCOL_NAMEFORMAT 
     64 
     65        # check m2m table was generated correctly 
     66        A.mapper.compile() 
     67        m2m_table = A.bs_.property.secondary 
     68        assert m2m_table.name in metadata.tables 
     69 
     70        # check column names 
     71        m2m_cols = m2m_table.columns 
     72        assert '%s_id' % A.table.name in m2m_cols 
     73        assert '%s_id' % B.table.name in m2m_cols 
     74 
     75    def test_alternate_column_formatter(self): 
     76        # this needs to be done before declaring the classes 
     77        elixir.options.M2MCOL_NAMEFORMAT = \ 
     78            elixir.relationships.alternate_m2m_column_formatter 
     79 
     80        class A(Entity): 
     81            as_ = ManyToMany('A') 
     82            bs_ = ManyToMany('B') 
     83 
     84        class B(Entity): 
     85            as_ = ManyToMany('A') 
     86 
     87        setup_all(True) 
     88        A.mapper.compile() 
     89 
     90        # revert to original format 
     91        elixir.options.M2MCOL_NAMEFORMAT = elixir.options.NEW_M2MCOL_NAMEFORMAT 
     92 
     93        # check m2m table column names were generated correctly 
     94        m2m_cols = A.bs_.property.secondary.columns 
     95        assert '%s_id' % A.table.name in m2m_cols 
     96        assert '%s_id' % B.table.name in m2m_cols 
     97 
     98        # check selfref m2m table column names were generated correctly 
     99        m2m_cols = A.as_.property.secondary.columns 
     100        assert 'as__id' in m2m_cols 
     101        assert 'inverse_id' in m2m_cols 
     102 
     103    def test_manual_column_format(self): 
    39104        class A(Entity): 
    40105            using_options(tablename='aye') 
     
    49114        setup_all(True) 
    50115 
    51         b1 = B(name='b1', as_=[A(name='a1')]) 
    52  
    53         session.commit() 
    54         session.clear() 
    55  
    56         a = A.query.one() 
    57         b = B.query.one() 
    58  
    59         assert a in b.as_ 
    60         assert b in a.bs_ 
    61  
     116        # check column names were generated correctly 
     117        A.mapper.compile() 
    62118        m2m_cols = A.bs_.property.secondary.columns 
    63119        assert 'a_id' in m2m_cols 
    64120        assert 'b_id' in m2m_cols 
    65121 
     122        # check the relationships work as expected 
     123        b1 = B(name='b1', as_=[A(name='a1')]) 
     124 
     125        session.commit() 
     126        session.clear() 
     127 
     128        a = A.query.one() 
     129        b = B.query.one() 
     130 
     131        assert a in b.as_ 
     132        assert b in a.bs_ 
     133 
    66134    def test_multi_pk_in_target(self): 
    67135        class A(Entity): 
     
    137205        assert barney in homer.friends 
    138206 
     207        m2m_cols = Person.friends.property.secondary.columns 
     208        assert 'friends_id' in m2m_cols 
     209        assert 'inverse_id' in m2m_cols 
     210 
    139211    def test_bidirectional_selfref(self): 
    140212        class Person(Entity): 
     
    159231        assert homer in barney.friends 
    160232        assert barney in homer.friends 
     233 
     234        m2m_cols = Person.friends.property.secondary.columns 
     235        assert 'friends_id' in m2m_cols 
     236        assert 'is_friend_of_id' in m2m_cols 
    161237 
    162238    def test_has_and_belongs_to_many(self):