class TestCycleService(TestCase): currency = None account = None cycle = None def setUp(self): TestCase.setUp(self) DataInitializer() \ .init_categories(USER_ID) self.currency = Currency('XXX', USER_ID).add() self.account = Account(USER_ID, self.currency.id, 'test').add() self.cycle = Cycle(self.account.id, 'test cycle', CURRENT_TIME, 'days', 10, 200, CATEGORY_ID, TYPE) def test_execute_active_cycle(self): # given self.cycle.add().flush() # when ret = save_cycle_execution(self.cycle.id, 110, CURRENT_TIME, self.account.id) # then self.assertEqual(self.account.balance, 110) self.assertEqual(ret.amount, 110) self.assertEqual(ret.category_id, CATEGORY_ID) self.assertEqual(ret.transaction_type, TYPE)
def setUp(self): TestCase.setUp(self) DataInitializer() \ .init_categories(USER_ID) self.CURRENCY_ID = Currency('XXX', USER_ID).add().id self.item = Cycle(ACCOUNT_ID, 'test name', CURRENT_TIME, 'years', 10, 200, CATEGORY_ID, TYPE)
def setUp(self): TestCase.setUp(self) DataInitializer() \ .init_categories(USER_ID) self.currency = Currency('XXX', USER_ID).add() self.account = Account(USER_ID, self.currency.id, 'test').add() self.cycle = Cycle(self.account.id, 'test cycle', CURRENT_TIME, 'days', 10, 200, CATEGORY_ID, TYPE)
def read_cycle_from_request(): account_id = request.json['item']['account']['id'] category_id = request.json['item']['category']['id'] date_start = to_date(request.json['item']['date_start']) name = request.json['item']['name'] repeat_type = request.json['item']['repeatType']['enum'] repeat_every = request.json['item']['repeat_every'] transaction_type = request.json['item']['type']['enum'] amount = to_decimal(request.json['item']['amount']) max_count = request.json['item'].get('max_count', None) date_end = to_date(request.json['item'].get('date_end', None)) item = Cycle(account_id, name, date_start, repeat_type, repeat_every, amount, category_id, transaction_type, max_count, date_end) return item
def save_cycle_execution(cycle_id, amount, execution_date, account_id=None): cycle = Cycle.get(cycle_id) if account_id is None: account = Account.get(cycle.account_id) else: account = Account.get(account_id) cycle.date_next = cycle.calculate_next() cycle.save_execute(execution_date) if cycle.transaction_type == 'INCOME': return account.add_income(amount, cycle.category_id, str(cycle.count + 1), execution_date) else: return account.add_outcome(amount, cycle.category_id, str(cycle.count + 1), execution_date)
class TestCycle(TestCase): item = None cat = Category CURRENCY_ID = None def setUp(self): TestCase.setUp(self) DataInitializer() \ .init_categories(USER_ID) self.CURRENCY_ID = Currency('XXX', USER_ID).add().id self.item = Cycle(ACCOUNT_ID, 'test name', CURRENT_TIME, 'years', 10, 200, CATEGORY_ID, TYPE) def test_added_is_active(self): # when self.item.add().flush() # then self.assertTrue(self.item.active) def test_added_is_have_no_executions(self): # when self.item.add().flush() # then self.assertIsNone(self.item.date_last) self.assertEqual(0, self.item.count) def test_added_have_truncated_dates(self): # when self.item.add().flush() # then self.assertEquals(self.item.date_start, CURRENT_TIME_TRUNC) self.assertEquals(self.item.date_next, CURRENT_TIME_TRUNC) def test_calculate_proper_delta(self): # given start_date = datetime.datetime(2015, 10, 25, 11, 51, 22) self.item.add().flush() # when ret = self.item.calculate_delta() + start_date # then self.assertEqual(ret, datetime.datetime(2025, 10, 25, 11, 51, 22)) def test_calculate_next_date_when_first_time(self): # given self.item.add().flush() self.item.date_start = START_DATE self.item.repeat_type = 'weeks' # when ret = self.item.calculate_next() # then self.assertEqual(ret, START_DATE) def test_calculate_next_date_when_second_time(self): # given self.item.add().flush() self.item.date_start = datetime.datetime(2010, 1, 1) self.item.repeat_type = 'weeks' self.item.repeat_every = 2 self.item.date_last = datetime.datetime(2015, 12, 1) # when ret = self.item.calculate_next() # then self.assertEqual(ret, datetime.datetime(2010, 1, 15)) def test_is_completed_when_last_date_equal_end(self): # given self.item.add().flush() end_date = datetime.datetime(2015, 12, 1) self.item.date_end = end_date self.item.date_last = end_date # when ret = self.item.is_completed() # then self.assertTrue(ret) def test_it_is_not_completed_before_start(self): # given self.item.add().flush() end_date = datetime.datetime(2015, 12, 1) self.item.date_end = end_date # when ret = self.item.is_completed() # then self.assertFalse(ret) def test_it_is_not_completed_before_end(self): # given self.item.add().flush() end_date = datetime.datetime(2015, 12, 1) self.item.date_end = end_date self.item.date_last = datetime.datetime(2015, 11, 1) # when ret = self.item.is_completed() # then self.assertFalse(ret) def test_it_is_not_completed_when_endless(self): # given self.item.add().flush() self.item.date_last = datetime.datetime(2015, 11, 1) # when ret = self.item.is_completed() # then self.assertFalse(ret) def test_is_completed_when_max_occurs(self): # given self.item.add().flush() self.item.date_last = datetime.datetime(2015, 11, 1) self.item.max_count = 10 self.item.count = 10 # when ret = self.item.is_completed() # then self.assertTrue(ret) def test_when_executed_last_date_updated(self): # given self.item.add().flush() next_date = self.item.calculate_next() # when self.item.save_execute(next_date) # then self.assertEquals(self.item.date_last, CURRENT_TIME_TRUNC) self.assertEquals(self.item.count, 1) def test_when_cycle_completed_is_going_inactive(self): # given self.item.max_count = 1 self.item.add().flush() next_date = self.item.calculate_next() # when self.item.save_execute(next_date) # then self.assertFalse(self.item.active)
def delete_cycle(cycle_id): Cycle.get(cycle_id).delete() return jsonify(response=True)
def get_cycles(): user_id = current_user.id return jsonify(response=Cycle.serialize_list(Cycle.all(user_id)))