def test_datetime(self): user_id = 3 ym = '201911' cb1 = cashbook.build() cb2 = cashbook.build() cb1.attr["user_id"] = user_id cb1.attr["date"] = datetime.date(2019, 11, 18) cb1.attr["ym"] = cb1.attr["date"].year * 100 + cb1.attr["date"].month cb1.attr["summary"] = '交通費' cb1.attr["detail"] = None cb1.attr["income"] += 0 cb1.attr["expenses"] += 5000 cb1.attr["amount"] += -5000 cb1_id = cb1.save() cb2.attr["user_id"] = user_id cb2.attr["date"] = datetime.date(2019, 11, 2) cb2.attr["ym"] = cb2.attr["date"].year * 100 + cb2.attr["date"].month cb2.attr["summary"] = '光熱費' cb2.attr["detail"] = None cb2.attr["income"] += 0 cb2.attr["expenses"] += 7000 cb2.attr["amount"] += -7000 cb2_id = cb2.save() cb_list = cashbook.find_datetime(user_id, ym) self.assertEqual(len(cb_list), 2) self.assertTrue(type(cb_list[0]) is cashbook) self.assertTrue(cb_list[0].attr['ym'] == cb_list[1].attr['ym']) self.assertTrue(cb_list[0].attr['date'] < cb_list[1].attr['date'])
def test_summary(self): user_id = 2 summary = '光熱費' cb1 = cashbook.build() cb2 = cashbook.build() cb1.attr["user_id"] = user_id cb1.attr["date"] = '2019-11-30' cb1.attr["summary"] = summary cb1.attr["detail"] = None cb1.attr["income"] += 0 cb1.attr["expenses"] += 5000 cb1.attr["amount"] += -5000 cb1_id = cb1.save() cb2.attr["user_id"] = user_id cb2.attr["date"] = '2019-12-30' cb2.attr["summary"] = summary cb2.attr["detail"] = None cb2.attr["income"] += 0 cb2.attr["expenses"] += 7000 cb2.attr["amount"] += -7000 cb2_id = cb2.save() cb_list = cashbook.find_summary(user_id, summary) self.assertEqual(len(cb_list), 2) self.assertTrue(type(cb_list[0]) is cashbook) self.assertTrue(cb_list[0].attr['date'] < cb_list[1].attr['date'])
def test_ym(self): user_id = 3 ym = 199904 cb1 = cashbook.build() cb1.attr["user_id"] = user_id cb1.attr["ym"] = ym cb1.attr["date"] = '1999-04-30' cb1.attr["summary"] = '光熱費' cb1.attr["detail"] = None cb1.attr["income"] += 0 cb1.attr["expenses"] += 7000 cb1.attr["amount"] += -7000 cb1_id = cb1.save() cb2 = cashbook.build() cb2.attr["user_id"] = user_id cb2.attr["ym"] = ym cb2.attr["date"] = '1999-04-25' cb2.attr["summary"] = 'ブリーフ代' cb2.attr["detail"] = None cb2.attr["income"] += 0 cb2.attr["expenses"] += 5000 cb2.attr["amount"] += -5000 cb2_id = cb2.save() cb_list = cashbook.ym(user_id, ym) self.assertEqual(len(cb_list), 2) self.assertTrue(type(cb_list[0]) is cashbook) self.assertTrue(cb_list[0].attr['date'] < cb_list[1].attr['date'])
def post(self): if not self.current_user: self.redirect("/signin") return # サインインユーザーの取得 _id = tornado.escape.xhtml_escape(self.current_user) _signedInUser = user.find(int(_id)) # POSTされたパラメータを取得 p_date = self.get_argument("form-date", None) p_summary = self.get_argument("form-summary", None) p_detail = self.get_argument("form-detail", None) p_income = self.get_argument("form-income", None) p_expenses = self.get_argument("form-expenses", None) # 現金出納帳データの組み立て cb = cashbook.build() cb.attr["user_id"] = int(_id) # ユーザーIDはサインインユーザーより取得 # パラメータエラーチェック errors = [] if p_date is None: errors.append("日付は必須です。") else: # 文字列をdatetime.dateオブジェクトへをキャスト cb.attr["date"] = datetime.datetime.strptime(p_date, '%Y-%m-%d').date() if p_summary is None: errors.append("摘要は必須です。") #(交通費)を自動で追加 summary = "交通費" + "(" + p_summary + ")" cb.attr["summary"] = summary cb.attr["detail"] = p_detail if p_income is None and p_expenses is None: errors.append("収入/支出のどちらかは入力してください。") if p_income is None: p_income = 0 if p_expenses is None: p_expenses = 0 cb.attr["income"] = Decimal(p_income) cb.attr["expenses"] = Decimal(p_expenses) # 金額計算(収入 - 支出) cb.attr["amount"] = cb.attr["income"] - cb.attr["expenses"] if len(errors) > 0: # エラーは新規登録画面に渡す self.render("cashbook_formfixed.html", user=_signedInUser, mode="new", cashbook=cb, messages=[], errors=[]) return # 登録 # print(vars(cb)) cb_id = cb.save() if cb_id == False: self.render("cashbook_formfixed.html", user=_signedInUser, mode="new", cashbook=cb, messages=[], errors=["登録時に致命的なエラーが発生しました。"]) else: # 登録画面へリダイレクト(登録完了の旨を添えて) self.redirect("/cashbooks?message=%s" % tornado.escape.url_escape("新規登録完了しました。(ID:%s)" % cb_id))
def get(self): if not self.current_user: self.redirect("/signin") return # サインインユーザーの取得 _id = tornado.escape.xhtml_escape(self.current_user) _signedInUser = user.find(int(_id)) cb = cashbook.build() self.render("cashbookfixed_form.html", user=_signedInUser, mode="new", cashbook=cb, messages=[], errors=[])
def test_save(self): cb = cashbook.build() cb.attr["user_id"] = 2 cb.attr["summary"] = "テスト" cb.attr["detail"] = None cb.attr["income"] += 10000 cb.attr["expenses"] += 5000 cb.attr["amount"] += 5000 cb_id = cb.save() #import pdb; pdb.set_trace() self.assertTrue(type(cb_id) is int) self.assertTrue(cb.attr["id"] is not None) self.assertTrue(cb_id == cb.attr["id"]) self.assertTrue(cb_id == 2)
def test_build(self): cb = cashbook.build() self.assertTrue(type(cb) is cashbook)