def test_commit_fails_for_duplicate_name_for_same_country(self, addr_db): country = self._country(addr_db) addr_db.add(State(name='Lagos', code='LG', country=country)) addr_db.commit() with pytest.raises(Exception): addr_db.add(State(name='Lagos', code='??', country=country)) addr_db.commit() addr_db.rollback() # why? see hint at the top
def initdb(session): from elixr.sax.address import Country, State ca = Country(code='CA', name='Canada') ng = Country(code='NG', name='Nigeria') session.add_all([ ca, ng, State(code='BC', name='British Columbia', country=ca), State(code='BC', name='Bauchi', country=ng), # wrong code ;-D ]) session.commit()
def test_can_save_well_formed_state_object(self, addr_db): addr_db.add( State(name='Abuja', code='AB', country=self._country(addr_db))) addr_db.commit() state = addr_db.query(State).filter(State.name == 'Abuja').one() assert state and state.name == 'Abuja'
def test_state_delete_cascades_down_to_setr_null(self, db): country = Country(code='NG', name='Nigeria') state = State(code='KD', name='Kaduna', country=country) Address(raw='Street, Town', street='Street', town='Town', state=state) Address(raw='Street2, Town2', street='Street2', town='Town2', state=state) db.add(country) db.commit() assert db.query(Country).count() == 1 assert db.query(State).count() == 1 assert db.query(Address).count() == 2 assert db.query(Address).filter( Address.state_id.is_(None)).count() == 0 db.delete(state) db.commit() assert db.query(Country).count() == 1 assert db.query(State).count() == 0 assert db.query(Address).count() == 2 assert db.query(Address).filter( Address.state_id.is_(None)).count() == 2
def test_country_delete_cascades_down(self, db): country = Country(code='NG', name='Nigeria') country2 = Country(code='GH', name='Ghana') State(code='KD', name='Kaduna', country=country) State(code='KN', name='Kano', country=country) db.add_all([country, country2]) db.commit() assert db.query(Country).count() == 2 assert db.query(State).count() == 2 db.delete(country) db.commit() assert db.query(Country).count() == 1 assert db.query(State).count() == 0
def test_relationships_traversable_within_state_object(self, addr_db): country = self._country(addr_db) addr_db.add(State(name='Kano', code='KN', country=country)) addr_db.commit() state = addr_db.query(State).filter(State.name == 'Kano').one() assert state and state.country == country \ and state.country.uuid == state.country_id \ and state in state.country.states
def initdb(session): from elixr.sax.address import Country, State ca = Country(code='CA', name='Canada') ng = Country(code='NG', name='Nigeria') session.add_all([ ca, ng, State(code='KN', name='Kano', country=ng), # wrong code ;-D ]) from elixr.sax.party import OrganizationType ot1 = OrganizationType(name='hq', title='Headquarters', is_root=True) ot2 = OrganizationType(name='branch', title='Branch') session.add_all([ot1, ot2]) session.commit()
def initdb(session): cn = Country(code='CN', name='Country') org = Organisation(identifier="Org0", name="Org.Main", short_name="Org0") session.add_all([ ## voltages Voltage(value=415), Voltage(value=11000), Voltage(value=33000), ## states State(code='S1', name='State 1', country=cn), State(code='S2', name='State 2', country=cn), ## organisations Organisation(identifier="Org1", name="Child1", short_name="Org1", parent=org), Organisation(identifier="Org2", name="Child2", short_name="Org2", parent=org), ]) session.commit()
def test_commit_fails_for_omitted_country(self, addr_db): with pytest.raises(Exception): addr_db.add(State(name='Abuja', code='AB')) addr_db.commit() addr_db.rollback() # why? see hint at module top
def test_string_repr_without_country(self, addr_db): state = State(name='Abuja', code='AB') assert 'Abuja' == str(state)
def test_string_repr_with_country(self, addr_db): ng = addr_db.query(Country).filter(Country.name == 'Nigeria').one() state = State(name='Abuja', code='AB', country=ng) assert 'Abuja, Nigeria' == str(state)
def test_commit_fails_for_blank_code(self, addr_db): country = self._country(addr_db) with pytest.raises(Exception): addr_db.add(State(name='Rivers', country=country)) addr_db.commit() addr_db.rollback()
def test_commit_fails_for_blank_name(self, addr_db): country = self._country(addr_db) with pytest.raises(Exception): addr_db.add(State(code='??', country=country)) addr_db.commit() addr_db.rollback() # why? see hint at the top