It's a test db, there's only one record being added to the table. And
I ensure that with a assertIn() test and it passes.
The original code was:
class Application(mydb.Model):
__tablename__ = 'APPLICATION'
app_id = mydb.Column(mydb.Integer, primary_key = True)#app_id =
mydb.Column(mydb.String(30), primary_key = True)
created_datetime = mydb.Column(mydb.DateTime(), primary_key = True)
case_owner = mydb.Column(mydb.String(20),
mydb.ForeignKey('STAFF.staff_id'))
applicant = mydb.Column(mydb.String(20),
mydb.ForeignKey('STUDENT.study_no'))
app_description = mydb.Column(mydb.Text())
case_priority = mydb.Column(mydb.String(20))
case_status = mydb.Column(mydb.String(10))
app_decision = mydb.Column(mydb.String(10))
deadline_datetime = mydb.Column(mydb.DateTime())
close_datetime = mydb.Column(mydb.DateTime())
std_comment = mydb.Column(mydb.Text())
std_document = mydb.Column(mydb.LargeBinary())
staff_comment = mydb.Column(mydb.Text())
staff_document = mydb.Column(mydb.LargeBinary())
app_category = mydb.Column(mydb.String(30))
applicant_obj = mydb.relationship('Student', backref='application_list')
case_owner_obj = mydb.relationship('Staff', backref='application_list')
__mapper_args__ = {
'polymorphic_identity':'application',
'polymorphic_on':app_category
}
class ProjectApp(Application):
__tablename__ = 'PROJECT_APP'
__table_args__ = (
mydb.ForeignKeyConstraint(
['app_id','created_datetime'],
['APPLICATION.app_id', 'APPLICATION.created_datetime']
),
)
app_id = mydb.Column(mydb.Integer, primary_key = True)
created_datetime = mydb.Column(mydb.DateTime(), primary_key = True)
c_supervisor = mydb.Column(mydb.String(62),
mydb.ForeignKey('C_SUPERVISOR.c_sup_email'))
u_supervisor = mydb.Column(mydb.String(20),
mydb.ForeignKey('STAFF.staff_id'))
proj_module = mydb.Column(mydb.String(40),
mydb.ForeignKey('MODULE.proj_module'))
proj_title = mydb.Column(mydb.Text())
proj_description = mydb.Column(mydb.Text())
proj_salary = mydb.Column(mydb.Text())
proj_contract = mydb.Column(mydb.LargeBinary())
proj_insurance = mydb.Column(mydb.LargeBinary())
proj_s_date = mydb.Column(mydb.Date())
proj_e_date = mydb.Column(mydb.Date())
proj_h_date = mydb.Column(mydb.Date())
csupervisor_obj = mydb.relationship('CSupervisor',
backref='projectapp_list')
usupervisor_obj = mydb.relationship('Staff', backref='projectapp_list')
module_obj = mydb.relationship('Module', backref='projectapp_list')
__mapper_args__ = {
'polymorphic_identity':'projApp',
}
#test
def create_new_internship_app(self, student):
internship_app = ProjectApp(
app_id=None,
created_datetime=datetime.date.today(),
#deadline_datetime=datetime.date.today() +
datetime.timedelta(days=PROCESS_TIMEFRAME[CASE_PRIORITY['Project app']]),
c_supervisor=None,
u_supervisor=None,
proj_module=None,
proj_title='Internship',
proj_description='It\'s long story',
proj_salary='No salary',
proj_contract=None,
proj_insurance=None,
proj_s_date=datetime.date(2014,2,1),
proj_e_date=datetime.date(2014,5,30),
proj_h_date=datetime.date(2014,6,1),
#std_comment=None,
#std_document=None
)
mydb.session.add(internship_app)
mydb.session.commit()
internship_app_rst = mydb.session.query(ProjectApp).one()
self.assertIs(internship_app_rst, internship_app)
self.assertEquals(internship_app_rst.proj_s_date ,
internship_app.proj_s_date)
return internship_app
On Sunday, June 21, 2015 at 7:20:04 PM UTC+2, Michael Bayer wrote:
did you try adding an ORDER BY to that query? calling
query.first() will return only the first result in an unordered
list. It is essentially random.
On 6/21/15 12:45 PM, Kevin Qiu wrote:
I create a unit test that add new ProjApp object to the database,
but the returned object shows it is not the same object inserted,
I don't follow why it's like this because I have others tests on
inserting a row to a different table, and returned shows the same
row object.
Test result:
self.assertIs(internship_app_rst, internship_app)
AssertionError: <project.models.ProjectApp object at
0x0000000003EE7E10> is not <project.models.ProjectApp object at 0x0
000000003EE7710>
Test:
def create_new_internship_app(self, student):
internship_app = ProjectApp(
app_id=None,
created_datetime=datetime.date.today(),
proj_title='Long story'
)
mydb.session.add(internship_app)
mydb.session.commit()
internship_app_rst = mydb.session.query(ProjectApp).first()
self.assertIs(internship_app_rst, internship_app)
Model:
class Application(mydb.Model):
__tablename__ = 'APPLICATION'
app_id = mydb.Column(mydb.Integer, primary_key = True)
created_datetime = mydb.Column(mydb.DateTime(), primary_key = True)
app_description = mydb.Column(mydb.Text())
app_category = mydb.Column(mydb.String(30))
case_owner_obj = mydb.relationship('Staff',
backref='application_list')
__mapper_args__ = {
'polymorphic_identity':'application',
'polymorphic_on':app_category
}
class ProjectApp(Application):
__tablename__ = 'PROJECT_APP'
__table_args__ = (
mydb.ForeignKeyConstraint(
['app_id','created_datetime'],
['APPLICATION.app_id', 'APPLICATION.created_datetime']
),
)
app_id = mydb.Column(mydb.Integer, primary_key = True)
created_datetime = mydb.Column(mydb.DateTime(), primary_key = True)
proj_title = mydb.Column(mydb.Text())
__mapper_args__ = {
'polymorphic_identity':'projApp',
}
--
You received this message because you are subscribed to the
Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it,
send an email to sqlalchemy+...@googlegroups.com <javascript:>.
To post to this group, send email to sqlal...@googlegroups.com
<javascript:>.
Visit this group at http://groups.google.com/group/sqlalchemy
<http://groups.google.com/group/sqlalchemy>.
For more options, visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>.
--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.