def setup_monthly_compost_density(self): # get the monthly density record and approve if compost_density = CompostDensityRegister.get( CompostDensityRegister.date == date(2014, 5, 1)) compost_density.status = Submission.APPROVED with transaction.manager: DBSession.add(compost_density)
def test_get_or_create_report_returns_existing_report_if_one_exist(self): submission = Submission.newest() submission_id = submission.id report = Report(submission=submission, report_json={'key': 'value'}) with transaction.manager: DBSession.add(report) submission = Submission.get(Submission.id == submission_id) report = submission.get_or_create_report() self.assertTrue(instance_state(report).persistent)
def test_delete_report(self): submission = Submission.newest() submission_id = submission.id report = Report(submission=submission, report_json={'key': 'value'}) with transaction.manager: DBSession.add(report) num_reports = Report.count(Report.submission_id == submission_id) self.assertEqual(num_reports, 1) submission = Submission.get(Submission.id == submission_id) submission.delete_report() self.assertEqual(Report.count(Report.submission_id == submission_id), num_reports - 1)
def __call__(self, json_payload, **kwargs): submission, is_updated = self.get_or_create_submission( json_payload, **kwargs) if not is_updated: # determine the municipality id(object) municipality = self.get_municipality_from_payload(json_payload) if municipality is not None: municipality_submission = MunicipalitySubmission( submission=submission, municipality=municipality) DBSession.add(municipality_submission) return # add the submission because we can save even when we can't determine # its municipality DBSession.add(submission)
def test_weight_calculation(self): self.setup_test_data() municipality = Municipality.get(Municipality.name == "Mukono") compost_density = CompostDensityRegister.get( CompostDensityRegister.date >= datetime.date(2014, 5, 1), CompostDensityRegister.date <= datetime.date(2014, 5, 31)) compost_density.status = Submission.APPROVED with transaction.manager: DBSession.add(compost_density) compost_sale = CompostSalesRegister( date=datetime.datetime(2014, 05, 01), json_data={ 'bagged_compost': 'no', 'compost_length': '3.0', 'compost_width': '4.0', 'compost_height': '5.0' }, municipality_submission=MunicipalitySubmission( municipality=municipality)) weight = compost_sale.weight() self.assertAlmostEqual(weight, 864.0 / 1000) # div to convert to tonnes