def test_update(self): init_job_data_dict = JobModel.get_by_pk(pk=4789).dict() to_update_data_dict = dict(title=u'后端吃饭工程师', work_year=2, city_id=1, company_id=11, department='飞天面条神教招聘', salary='20k-30k', education=2, description=u'日常工作:吃饭!') affect_rows = JobModel.update(filter_by={'id': 4789}, values=to_update_data_dict) self.assertEqual(affect_rows, 1) # 更新后预期的结果 init_job_data_dict.update(**to_update_data_dict) expected_job_data_dict = init_job_data_dict init_updated_at = init_job_data_dict.pop('updated_at') new_job_data_dict = JobModel.get_by_pk(pk=4789).dict() self.assertDictContainsSubset(expected_job_data_dict, new_job_data_dict) self.assertGreater(new_job_data_dict.updated_at, init_updated_at) # 其他记录不受影响 self.assertEqual(JobModel.get_by_pk(pk=6814).title, u'web前端') # 批量更改 affect_rows = JobModel.update(filter_by={'company_id': 11}, values={'title': '测试'}) self.assertEqual(affect_rows, 2) self.assertEqual(JobModel.get_by_pk(pk=6814).title, u'测试') self.assertEqual(JobModel.get_by_pk(pk=4789).title, u'测试')
def test_update(self): init_job_data_dict = JobModel.get_by_pk(pk=1).dict() to_update_data_dict = dict(title=u'后端吃饭工程师', work_year=1, city_id=1, company_id=1, department='飞天面条神教招聘', salary='20k-32k', education=2, description=u'日常工作:吃饭!') affect_rows = JobModel.update(filter_by={'id': 1}, values=to_update_data_dict) self.assertEqual(affect_rows, 1) # 更新后预期的结果 init_job_data_dict.update(**to_update_data_dict) predictive_job_data_dict = init_job_data_dict init_updated_at = init_job_data_dict.pop('updated_at') new_job_data_dict = JobModel.get_by_pk(pk=1).dict() self.assertDictContainsSubset(predictive_job_data_dict, new_job_data_dict) self.assertGreater(new_job_data_dict.updated_at, init_updated_at) # 其他记录不受影响 self.assertEqual(JobModel.get_by_pk(pk=2).title, u'前端开发工程师') # 批量更改 affect_rows = JobModel.update(filter_by={'city_id': 4}, values={'title': '测试'}) self.assertEqual(affect_rows, 2) jobs = JobModel.list(filter_by={'city_id': 4}) self.assertTrue(all([job.title == u'测试' for job in jobs]))
def test_execute_sql_string(self): job_rows = JobModel.execute_sql_string('select id, title from job where id = :id', {'id': 4789}) self.assertEqual(len(job_rows), 1) self.assertEqual(job_rows[0][0], 4789) self.assertEqual(job_rows[0][1], u'Android开发工程师') job_rows = JobModel.execute_sql_string('select id, title from job') self.assertEqual(len(job_rows), 2) self.assertEqual(job_rows[0][0], 4789) self.assertEqual(job_rows[0][1], u'Android开发工程师') affect_rows = JobModel.execute_sql_string("update job set title = '测试' where id = :id", {'id': 4789}) self.assertEqual(affect_rows, 1) job = JobModel.get_by_pk(pk=4789) self.assertEqual(job.title, u'测试')
def test_execute_sql_string(self): job_rows = JobModel.execute_sql_string( 'SELECT id, title FROM job WHERE id = :id', {'id': 1}) self.assertEqual(len(job_rows), 1) self.assertEqual(job_rows[0][0], 1) self.assertEqual(job_rows[0][1], u'高级前端开发工程师') job_rows = JobModel.execute_sql_string('SELECT id, title FROM job') self.assertEqual(len(job_rows), 3) self.assertEqual(job_rows[0][0], 1) self.assertEqual(job_rows[0][1], u'高级前端开发工程师') affect_rows = JobModel.execute_sql_string( "UPDATE job SET title = '测试' WHERE id = :id", {'id': 1}) self.assertEqual(affect_rows, 1) job = JobModel.get_by_pk(pk=1) self.assertEqual(job.title, u'测试')
def test_add(self): to_add_data_dict = dict(title=u'后端吃饭工程师', work_year=2, city_id=1, company_id=1, department='飞天面条神教招聘', salary='20k-30k', education=2, description=u'日常工作:吃饭!', advantage='饭管饱, 管够', nature=0) job_id = JobModel.add(**to_add_data_dict) self.assertTrue(job_id > 0) job = JobModel.get_by_pk(pk=job_id) self.assertDictContainsSubset(to_add_data_dict, job.dict()) self.assertGreater(job.created_at, 0) self.assertGreater(job.updated_at, 0)
def test_add(self): to_add_data_dict = dict( lg_job_id=10004, city_id=3, company_id=1, title='Python 开发工程师', work_year=5, department='吖吖项目组', salary='15k-35k', education=2, nature=1, description='职位介绍D', advantage='16薪,工作居住证,六十八险一金,双休', ) job_id = JobModel.add(**to_add_data_dict) self.assertTrue(job_id > 0) job = JobModel.get_by_pk(pk=job_id) self.assertDictContainsSubset(to_add_data_dict, job.dict())
def test_model_to_dict(self): job = JobModel.get_by_pk(pk=4789).dict() self.assertTrue(isinstance(job, dict)) self.assertDictEqual(job, test_job_data)
def test_get_by_pk(self): job = JobModel.get_by_pk(pk=4789) self.assertDictEqual(job.dict(), test_job_data)
def test_update_by_pk(self): affect_rows = JobModel.update_by_pk(pk=6814, values={'title': '你好啊啊'}) self.assertEqual(affect_rows, 1) self.assertEqual(JobModel.get_by_pk(pk=6814).title, u'你好啊啊')