Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from TribuneX/issue89-support-display-name-for…
…-relationship Closes issue #89: Add support for display_name property for relationship
- Loading branch information
Showing
2 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
****************************************** | ||
tests.test_relationship_sererialize | ||
****************************************** | ||
Tests display name when relationships are deserialized. | ||
""" | ||
|
||
from sqlathanor import declarative_base, Column, relationship | ||
|
||
from sqlalchemy import Integer, String, Sequence, ForeignKey | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class User(Base): | ||
__tablename__ = 'users' | ||
|
||
id = Column(Integer, Sequence('user_id_seq'), primary_key = True) | ||
|
||
addresses = relationship("Address", | ||
backref = "user", | ||
lazy = "joined", | ||
supports_json = True, | ||
display_name = "all-addresses" | ||
) | ||
|
||
|
||
class Address(Base): | ||
__tablename__ = 'addresses' | ||
|
||
id = Column(Integer,primary_key=True) | ||
|
||
email = Column(String, | ||
nullable=False, | ||
supports_json=True, | ||
) | ||
|
||
user_id = Column(Integer, ForeignKey('users.id')) | ||
|
||
|
||
def test_display_name_used_for_attribute(): | ||
|
||
scott = User() | ||
email_com = 'scott@example.com' | ||
scott.addresses = [Address(email=email_com)] | ||
|
||
json_str = scott.to_json(max_nesting=2) | ||
|
||
assert json_str == '{"all-addresses": [{"email": "%s"}]}' % (email_com) |