def test_001(self): ''' 不会统计其他用户的账单 确认只有支出时的统计结果 ''' # 其他用户新建账单 self.goto_bill_page('*****@*****.**') self.create_bill_normally('1000990.1', 'other users bills') self.quit_browser() # 当前用户访问浏览器 self.init_browser() self.goto_bill_page('*****@*****.**') self.create_bill_normally('-2000999.1', 'my bills') # 只能看到当前用户的统计账单 date = date_now() self.select_billym(date.year, date.month) aggs = self.wait_for(lambda: self.browser. find_elements_by_css_selector('#id_aggs_row td')) self.assertEqual(aggs[0].text, '0') self.assertEqual(aggs[1].text, '-2000999.1') self.assertEqual(aggs[2].text, '-2000999.1') self.assertNotIn('text-danger', aggs[0].get_attribute('class')) self.assertIn('text-danger', aggs[1].get_attribute('class')) self.assertIn('text-danger', aggs[2].get_attribute('class'))
def save(self, owner): date = date_now() self.instance.billym, _ = Billym.objects.get_or_create(\ owner=owner, year=date.year, month=date.month ) # 账单发生日期为当天 self.instance.date = date return super().save()
def test_001(self): ''' 新建账单 ''' # 进入新建账单页面,新建一条账单 self.goto_bill_page('*****@*****.**') self.create_bill_normally('1000000.1', 'incomes 1') bill_fields = self.get_bill_record_fields(1) self.assertEqual(bill_fields[0].text, date_now_str()) self.assertEqual(bill_fields[1].text, '1000000.1') self.assertEqual(bill_fields[2].text, 'incomes 1') # 正数不会显示成红色 self.assertEqual( bill_fields[1].find_elements_by_css_selector('.text-danger'), []) # 同时,会新建一条月账单 self.wait_for(lambda: self.assertEqual(len(self.get_billyms()), 1)) # 月账单没有被选中 self.wait_for(lambda: self.assertEqual( self.browser.find_elements_by_css_selector( '#id_billyms_table td.app-selected'), [])) # 月账单会显示某年某月 date = date_now() billym_element = self.get_billym_element(date.year, date.month) self.assertRegex(billym_element.get_attribute('href'), r'/bills/(\d+)/$') # 再新建一条账单,并显示出来 self.create_bill_normally('-9999999.9', 'expends 1') bill_fields = self.get_bill_record_fields(1) self.assertEqual(bill_fields[0].text, date_now_str()) self.assertEqual(bill_fields[1].text, '-9999999.9') self.assertEqual(bill_fields[2].text, 'expends 1') # 负数显示成红色 self.assertEqual( bill_fields[1].find_element_by_css_selector('.text-danger').text, bill_fields[1].text) # 还是会显示刚刚的月账单 self.wait_for(lambda: self.assertEqual(len(self.get_billyms()), 1)) # 月账单没有被选中 self.wait_for(lambda: self.assertEqual( self.browser.find_elements_by_css_selector( '#id_billyms_table td.app-selected'), [])) # 月账单会显示某年某月 billym_element = self.get_billym_element(date.year, date.month) self.assertRegex(billym_element.get_attribute('href'), r'/bills/(\d+)/$')
def make_other_data(self): ''' 其他用户的数据 ''' date = date_now() other_user = User.objects.create(email='*****@*****.**') other_billym = Billym.objects.create(owner=other_user, year=date.year, month=date.month) Bill.objects.create(billym=other_billym, money=10, comment='other bill', date=date)
def test_013(self): ''' 账单明细页面,不会显示其他未被选择的月账单的账单明细 ''' # 创建当前用户的账单明细 if (self.staging_tests): make_bills_on_server('*****@*****.**') else: make_bills('*****@*****.**') # 当前用户访问浏览器 self.goto_bill_page('*****@*****.**') # 可以看到所有的月账单 billym_elements = self.wait_for(lambda: self.get_billyms()) self.assertEqual(len(billym_elements), 3) self.assertEqual(billym_elements[0].text, '2019年11月') self.assertEqual(billym_elements[1].text, '2019年10月') self.assertEqual(billym_elements[2].text, '2018年12月') # 新建一条账单 self.create_bill_normally('-2000999', 'my bills') # 可以看到刚刚新建的月账单 date = date_now() billym_elements = self.wait_for(lambda: self.get_billyms()) self.assertEqual(len(billym_elements), 4) self.assertEqual(billym_elements[0].text, '{}年{}月'.format(date.year, date.month)) self.assertEqual(billym_elements[1].text, '2019年11月') self.assertEqual(billym_elements[2].text, '2019年10月') self.assertEqual(billym_elements[3].text, '2018年12月') # 选择刚刚新建的月账单 self.select_billym(date.year, date.month) self.wait_for(lambda: self.assertEqual(len(self.get_bills()), 1)) bill_fields = self.get_bill_record_fields(1) self.assertEqual(bill_fields[0].text, date_now_str()) self.assertEqual(bill_fields[1].text, '-2000999.0') self.assertEqual(bill_fields[2].text, 'my bills') # 也可以选择其他月账单 self.select_billym(2019, 11) self.wait_for(lambda: self.assertEqual(len(self.get_bills()), 2)) bill_fields = self.get_bill_record_fields(1) self.assertEqual(bill_fields[0].text, '2019-11-11') self.assertEqual(bill_fields[1].text, '599.1') self.assertEqual(bill_fields[2].text, 'billym_3: old bill 2') bill_fields = self.get_bill_record_fields(2) self.assertEqual(bill_fields[0].text, '2019-11-11') self.assertEqual(bill_fields[1].text, '-499.0') self.assertEqual(bill_fields[2].text, 'billym_3: old bill 1')
def test_012(self): ''' 被指定的月账单有账单明细 ''' # 其他用户的数据 self.make_other_data() # 当前用户的数据 date = date_now() owner = User.objects.create(email='*****@*****.**') billym = Billym.objects.create(owner=owner, year=date.year, month=date.month) # 以前的数据 date_before = timezone.now() - timedelta(days=1) bill_before = Bill.objects.create(billym=billym, money=+32.1, date=date_before.date(), comment='date before') bill_before.create_ts = date_before bill_before.save() # 当天的数据 Bill.objects.create(billym=billym, money=1000.1, comment='todays bill 1', date=date) Bill.objects.create(billym=billym, money=-200.9, comment='todays bill 2', date=date) # 其他月份的月账单(不会被抽出) other_billym = Billym.objects.create(owner=owner, year=2019, month=1) Bill.objects.create(billym=other_billym, money='-99999.9', comment='other billym', date='2019-1-1') context = view_bills(owner, billym.id) bills = context['bills'] self.assertEqual(len(bills), 3) self.assertEqual(bills[0]['money'], Decimal('-200.9')) self.assertEqual(bills[0]['comment'], 'todays bill 2') self.assertEqual(bills[0]['date'], date) self.assertEqual(bills[1]['money'], Decimal('1000.1')) self.assertEqual(bills[1]['comment'], 'todays bill 1') self.assertEqual(bills[1]['date'], date) self.assertEqual(bills[2]['money'], Decimal('32.1')) self.assertEqual(bills[2]['comment'], 'date before') self.assertEqual(bills[2]['date'], date_before.date())
def view_bills(owner, billym_id=None): if (billym_id): # 选择月账单的账单明细 return { 'bills': Bill.objects.select_related('billym')\ .values('date', 'money', 'comment')\ .filter(billym__owner=owner)\ .filter(billym__id=billym_id) } else: # 当天新建账单的账单明细 return { 'bills': Bill.objects.select_related('billym')\ .values('date', 'money', 'comment')\ .filter(billym__owner=owner)\ .filter(create_ts__date=date_now()) }
def test_003(self): ''' 统计多条账单明细 ''' # 当前用户访问浏览器 self.goto_bill_page('*****@*****.**') date = date_now() # 余额正好为零 self.create_bill_normally('+1', 'my bills 1') self.create_bill_normally('-1', 'my bills 2') self.select_billym(date.year, date.month) aggs = self.wait_for(lambda: self.browser. find_elements_by_css_selector('#id_aggs_row td')) self.assertEqual(aggs[0].text, '1.0') self.assertEqual(aggs[1].text, '-1.0') self.assertEqual(aggs[2].text, '0.0') self.assertNotIn('text-danger', aggs[0].get_attribute('class')) self.assertIn('text-danger', aggs[1].get_attribute('class')) self.assertNotIn('text-danger', aggs[2].get_attribute('class')) # 返回新建账单页面 self.browser.find_element_by_link_text('新建账单').click() # 新建账单 self.create_bill_normally('+8900.3', 'my bills 3') self.create_bill_normally('-5600.7', 'my bills 4') self.create_bill_normally('-10.0', 'my bills 5') self.create_bill_normally('-625.2', 'my bills 6') self.create_bill_normally('+300', 'my bills 7') self.select_billym(date.year, date.month) aggs = self.wait_for(lambda: self.browser. find_elements_by_css_selector('#id_aggs_row td')) incomes = round(1 + 8900.3 + 300, 1) expends = round(-1 - 5600.7 - 10.0 - 625.2, 1) balance = round(incomes + expends, 1) self.assertEqual(aggs[0].text, str(incomes)) self.assertEqual(aggs[1].text, str(expends)) self.assertEqual(aggs[2].text, str(balance))
def test_002(self): ''' 只能取得当天的账单明细 ''' # 其他用户的数据 self.make_other_data() # 当前用户的数据 date = date_now() owner = User.objects.create(email='*****@*****.**') billym = Billym.objects.create(owner=owner, year=date.year, month=date.month) # 以前的数据 date_before = timezone.now() - timedelta(days=1) bill_before = Bill.objects.create(billym=billym, money=+32.1, date=date_before.date(), comment='date before') bill_before.create_ts = date_before bill_before.save() # 当天的数据 Bill.objects.create(billym=billym, money=1000.1, comment='todays bill 1', date=date) Bill.objects.create(billym=billym, money=-200.9, comment='todays bill 2', date=date) context = view_bills(owner) bills = context['bills'] self.assertEqual(len(bills), 2) self.assertEqual(bills[0]['money'], Decimal('-200.9')) self.assertEqual(bills[0]['comment'], 'todays bill 2') self.assertEqual(bills[0]['date'], date) self.assertEqual(bills[1]['money'], Decimal('1000.1')) self.assertEqual(bills[1]['comment'], 'todays bill 1') self.assertEqual(bills[1]['date'], date)
def test_011(self): ''' 账单明细页面,不会显示其他用户的数据 ''' # 其他用户新建账单 self.goto_bill_page('*****@*****.**') self.create_bill_normally('100.1', 'other users bills 1') self.create_bill_normally('-90.9', 'other users bills 2') self.quit_browser() # 当前用户访问浏览器 self.init_browser() self.goto_bill_page('*****@*****.**') self.create_bill_normally('-2000999', 'my bills') # 只能看到当前用户的账单明细 date = date_now() self.select_billym(date.year, date.month) self.wait_for(lambda: self.assertEqual(len(self.get_bills()), 1)) bill_fields = self.get_bill_record_fields(1) self.assertEqual(bill_fields[0].text, date_now_str()) self.assertEqual(bill_fields[1].text, '-2000999.0') self.assertEqual(bill_fields[2].text, 'my bills')
def test_011(self): ''' 被指定的月账单,没有账单明细 ''' # 其他用户的数据 self.make_other_data() # 当前用户的数据 date = date_now() owner = User.objects.create(email='*****@*****.**') billym = Billym.objects.create(owner=owner, year=date.year, month=date.month) # 其他月份的月账单(不会被抽出) other_billym = Billym.objects.create(owner=owner, year=2019, month=1) Bill.objects.create(billym=other_billym, money='-99999.9', comment='other billym', date='2019-1-1') # 没有账单明细 self.assertEqual(len(billym.bill_set.all()), 0) context = view_bills(owner, billym.id) self.assertEqual(len(context['bills']), 0)