'mock-alchemy: how to handle foreign-key/relationship
How can I mock the foreign-key/relationship correctly with the mock-alchemy package? The "content" object has no "content_type_config"
import unittest
from sqlalchemy import Column, String, Integer, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from alchemy_mock.mocking import UnifiedAlchemyMagicMock
Base = declarative_base()
class content_type_config(Base):
__tablename__ = 'content_type_config'
type_id = Column("type_id", Integer, primary_key=True)
name = Column("name", String(255))
def __init__(self, type_id, name):
self.type_id = type_id
self.name = name
class content(Base):
__tablename__ = 'content'
id = Column("id", Integer, primary_key=True)
name = Column("name", String(255))
f_type_id = Column("f_type_id", Integer, ForeignKey("content_type_config.type_id", name="content_f_type_id_fkey"))
content_type_config = relationship('content_type_config',
primaryjoin="content.f_type_id == content_type_config.type_id",
foreign_keys="[content.f_type_id]")
def __init__(self, id, name, f_type_id):
self.id = id
self.name = name
self.f_type_id = f_type_id
class TestSomething(unittest.TestCase):
def test_one(self):
session = UnifiedAlchemyMagicMock()
session.add(content_type_config(1, 'Name for type 1'))
session.add(content(1, 'TheName', 1))
res = session.query(content).all()
print(res[0].content_type_config)
def tearDown(self):
print('Tearing down unittest')
The printed result is None. Should be an content_type_config object
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
