def test_overlap(self): """Test unit overlap""" start1 = datetime.strptime('2018-09-01 17:00:00', "%Y-%m-%d %H:%M:%S") end1 = datetime.strptime('2019-09-15 17:00:00', '%Y-%m-%d %H:%M:%S') start2 = datetime.strptime('2019-09-01 17:00:00', '%Y-%m-%d %H:%M:%S') end2 = datetime.strptime('2019-09-15 17:00:00', '%Y-%m-%d %H:%M:%S') v1 = Voucher(code='SNSD', value=5000, start=start1, end=end1) v2 = Voucher(code='SNSD', value=10000, start=start2, end=end2) db.session.add(v1) db.session.add(v2) db.session.commit() start3 = datetime.strptime('2019-09-14 17:00:00', "%Y-%m-%d %H:%M:%S") end3 = datetime.strptime('2019-09-15 17:00:00', "%Y-%m-%d %H:%M:%S") v3 = Voucher(code='SNSD', value=10000, start=start3, end=end3) self.assertEqual(v3.is_overlap(), True) """Test unit overlap Fails""" v3.start = datetime.strptime('2020-09-14 17:00:00', "%Y-%m-%d %H:%M:%S") v3.end = datetime.strptime('2021-09-14 17:00:00', "%Y-%m-%d %H:%M:%S") self.assertEqual(v3.is_overlap(), False)
def create_voucher(): """register new voucher""" data = request.get_json(silent=True) if not data or not 'code' or not 'value' or not 'start' or not 'end' in data: abort(400) start = datetime.strptime(data['start'], '%Y-%m-%d %H:%M:%S') end = datetime.strptime(data['end'], '%Y-%m-%d %H:%M:%S') if end <= start: abort(400) voucher = Voucher(code=data['code'], value=data['value'], start=start, end=end) if voucher.is_overlap(): abort(400) voucher.save() return make_response(jsonify(voucher=voucher.serialize()), 201)