def test_transfer_record_updated_assets_balance_updated(self): asset_from = AssetFactory.create(name='AST1', owner=self.owner) asset_to = AssetFactory.create(name='AST2', owner=self.owner) asset_from_other = AssetFactory.create(name='AST1-O', owner=self.owner) asset_to_other = AssetFactory.create(name='AST2-O', owner=self.owner) old_asset_from_balance = asset_from.balance old_asset_to_balance = asset_to.balance old_asset_from_other_balance = asset_from_other.balance old_asset_to_other_balance = asset_to_other.balance record = RecordFactory.create(asset_from=asset_from, asset_to=asset_to, owner=self.owner) record.asset_to = asset_to_other record.asset_from = asset_from_other serializer = CreateRecordSerializer(record) self.jpatch(reverse('records-detail', args=[record.pk]), serializer.data) asset_to.refresh_from_db() asset_from.refresh_from_db() asset_to_other.refresh_from_db() asset_from_other.refresh_from_db() self.assertEqual(asset_from.balance, old_asset_from_balance + record.delta) self.assertEqual(asset_from_other.balance, old_asset_from_other_balance - record.delta) self.assertEqual(asset_to.balance, old_asset_to_balance - record.delta) self.assertEqual(asset_to_other.balance, old_asset_to_other_balance + record.delta)
def test_transfer_record_updated_assets_updated(self): asset_from = AssetFactory.create(name='AST1', owner=self.owner) asset_to = AssetFactory.create(name='AST2', owner=self.owner) old_asset_from_balance = asset_from.balance old_asset_to_balance = asset_to.balance record = RecordFactory.create(asset_from=asset_from, asset_to=asset_to, owner=self.owner) old_record_delta = record.delta new_record_delta = random.randint(0, 1000) record.delta = new_record_delta serializer = CreateRecordSerializer(record) self.jpatch(reverse('records-detail', args=[record.pk]), serializer.data) asset_to.refresh_from_db() asset_from.refresh_from_db() new_asset_from_balance = asset_from.balance new_asset_to_balance = asset_to.balance new_record_delta = record.delta self.assertEqual(new_asset_from_balance, old_asset_from_balance + old_record_delta - new_record_delta) self.assertEqual(new_asset_to_balance, old_asset_to_balance - old_record_delta + new_record_delta)
def test_create_asset_partial(self): asset_to = AssetFactory.create(owner=self.owner) record = RecordFactory.build(asset_to=asset_to, asset_from=None, owner=self.owner) serializer = CreateRecordSerializer(record) data_in = serializer.data res = self.jpost(reverse('records-list'), data_in) self.assert_created(res) self._compare_records_data(data_in, res.data)
def test_create_invalid_mode(self): asset_to = AssetFactory.create(owner=self.owner) asset_from = AssetFactory.create(owner=self.owner) record = RecordFactory.build(asset_to=asset_to, mode=Record.TRANSFER, owner=self.owner) res = self.jpost(reverse('records-list'), CreateRecordSerializer(record).data) self.assert_bad(res) record = RecordFactory.build(asset_to=asset_to, mode=Record.EXPENSE, owner=self.owner) res = self.jpost(reverse('records-list'), CreateRecordSerializer(record).data) self.assert_bad(res) record = RecordFactory.build(asset_from=asset_from, mode=Record.TRANSFER, owner=self.owner) res = self.jpost(reverse('records-list'), CreateRecordSerializer(record).data) self.assert_bad(res) record = RecordFactory.build(asset_from=asset_from, mode=Record.INCOME, owner=self.owner) res = self.jpost(reverse('records-list'), CreateRecordSerializer(record).data) self.assert_bad(res)
def test_income_record_created_asset_updated(self): asset_to = AssetFactory.create(owner=self.owner) old_asset_balance = asset_to.balance record = RecordFactory.build(asset_to=asset_to, asset_from=None, owner=self.owner) serializer = CreateRecordSerializer(record) self.jpost(reverse('records-list'), serializer.data) asset_to.refresh_from_db() new_asset_balance = asset_to.balance self.assertEqual(new_asset_balance, old_asset_balance + record.delta)
def test_bulk_expense_single_record_created_asset_updated(self): asset_from = AssetFactory.create(owner=self.owner) old_asset_balance = asset_from.balance record = RecordFactory.build(asset_from=asset_from, asset_to=None, owner=self.owner) serializer = CreateRecordSerializer(record) self.jpost(reverse('records-list'), [serializer.data]) asset_from.refresh_from_db() new_asset_balance = asset_from.balance self.assertEqual(new_asset_balance, old_asset_balance - record.delta)
def test_create_full(self): asset_to = AssetFactory.create(owner=self.owner) asset_from = AssetFactory.create(owner=self.owner) record = RecordFactory.build(asset_to=asset_to, asset_from=asset_from, owner=self.owner) serializer = CreateRecordSerializer(record) # add a list of tags tags = TagFactory.build_batch(3) data_in = serializer.data data_in['tags_names'] = list(set(tag.name for tag in tags)) res = self.jpost(reverse('records-list'), data_in) self.assert_created(res) self._compare_records_data(data_in, res.data)
def test_bulk_expense_records_created_asset_updated(self): asset_from = AssetFactory.create(owner=self.owner) old_asset_balance = asset_from.balance record = RecordFactory.build(asset_from=asset_from, asset_to=None, owner=self.owner) serializer = CreateRecordSerializer(record) data_in_1 = serializer.data data_in_2 = copy.deepcopy(data_in_1) self.jpost(reverse('records-list'), [data_in_1, data_in_2]) asset_from.refresh_from_db() new_asset_balance = asset_from.balance self.assertEqual(new_asset_balance, old_asset_balance - 2 * record.delta)
def test_income_record_assets_updated_asset_balance_updated(self): asset_to = AssetFactory.create(owner=self.owner) asset_to_other = AssetFactory.create(owner=self.owner) old_asset_balance = asset_to.balance old_asset_other_balance = asset_to_other.balance record = RecordFactory.create(asset_to=asset_to, asset_from=None, owner=self.owner) record.asset_to = asset_to_other serializer = CreateRecordSerializer(record) self.jpatch(reverse('records-detail', args=[record.pk]), serializer.data) asset_to.refresh_from_db() asset_to_other.refresh_from_db() self.assertEqual(asset_to.balance, old_asset_balance - record.delta) self.assertEqual(asset_to_other.balance, old_asset_other_balance + record.delta)
def test_mass_create_one_invalid(self): asset_to = AssetFactory.create(owner=self.owner) asset_from = AssetFactory.create(owner=self.owner) record = RecordFactory.build(asset_to=asset_to, asset_from=asset_from, owner=self.owner) serializer = CreateRecordSerializer(record) # add a list of tags tags = TagFactory.build_batch(3) data_in_1 = serializer.data data_in_1['tags_names'] = [tag.name for tag in tags] data_in_2 = copy.deepcopy(data_in_1) del data_in_2['delta'] res = self.jpost(reverse('records-list'), [data_in_1, data_in_2]) self.assert_bad(res) self.assertEqual({}, res.data[0]) self.assertEqual(['delta'], list(res.data[1].keys()))
def test_mass_create(self): asset_to = AssetFactory.create(owner=self.owner) asset_from = AssetFactory.create(owner=self.owner) record = RecordFactory.build(asset_to=asset_to, asset_from=asset_from, owner=self.owner) serializer = CreateRecordSerializer(record) # add a list of tags tags = TagFactory.build_batch(3) data_in_1 = serializer.data data_in_1['tags_names'] = [tag.name for tag in tags] data_in_2 = copy.deepcopy(data_in_1) data_in_2['delta'] = '200.000' res = self.jpost(reverse('records-list'), [data_in_1, data_in_2]) self.assert_created(res) self._compare_records_data(data_in_1, res.data[0]) self._compare_records_data(data_in_2, res.data[1])
def test_income_record_updated_asset_updated(self): asset_to = AssetFactory.create(owner=self.owner) old_asset_balance = asset_to.balance record = RecordFactory.create(asset_to=asset_to, asset_from=None, owner=self.owner) old_record_delta = record.delta new_record_delta = random.randint(0, 1000) record.delta = new_record_delta serializer = CreateRecordSerializer(record) self.jpatch(reverse('records-detail', args=[record.pk]), serializer.data) asset_to.refresh_from_db() new_asset_balance = asset_to.balance new_record_delta = record.delta self.assertEqual(new_asset_balance, old_asset_balance - old_record_delta + new_record_delta)
def test_transfer_record_created_assets_updated(self): asset_from = AssetFactory.create(name='AST1', owner=self.owner) asset_to = AssetFactory.create(name='AST2', owner=self.owner) old_asset_from_balance = asset_from.balance old_asset_to_balance = asset_to.balance record = RecordFactory.build(asset_from=asset_from, asset_to=asset_to, owner=self.owner) serializer = CreateRecordSerializer(record) self.jpost(reverse('records-list'), serializer.data) asset_to.refresh_from_db() asset_from.refresh_from_db() new_asset_from_balance = asset_from.balance new_asset_to_balance = asset_to.balance self.assertEqual(new_asset_from_balance, old_asset_from_balance - record.delta) self.assertEqual(new_asset_to_balance, old_asset_to_balance + record.delta)
def test_create_empty_data_with_context(self): req = RequestFactory() req.user = self.owner record_data = {} serializer = CreateRecordSerializer(data=record_data, context={'request': req}) self.assertFalse(serializer.is_valid())
def test_update(self): record = RecordFactory.create(owner=self.owner) record.delta = random.randint(0, 1000) serializer = CreateRecordSerializer(record) res = self.jpatch(reverse('records-detail', args=[record.pk]), serializer.data) self.assert_updated(res)