@barchart/portfolio-api-common 1.2.45 → 1.2.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ const Currency = require('@barchart/common-js/lang/Currency'),
2
+ Day = require('@barchart/common-js/lang/Day'),
3
+ Decimal = require('@barchart/common-js/lang/Decimal');
4
+
5
+ const InstrumentType = require('./../../../lib/data/InstrumentType'),
6
+ TransactionType = require('./../../../lib/data/TransactionType');
7
+
8
+ const TransactionSchema = require('./../../../lib/serialization/TransactionSchema');
9
+
10
+ describe('When transactions are serialized', () => {
11
+ 'use strict';
12
+
13
+ function transactionsAreEqual(a, b) {
14
+ expect(a.portfolio === b.portfolio).toEqual(true);
15
+ expect(a.position === b.position).toEqual(true);
16
+ expect(a.sequence === b.sequence).toEqual(true);
17
+ expect(a.type === b.type).toEqual(true);
18
+ expect(a.date.getIsEqual(b.date)).toEqual(true);
19
+ expect(a.price.getIsEqual(b.price)).toEqual(true);
20
+ expect(a.quantity.getIsEqual(b.quantity)).toEqual(true);
21
+ expect(a.fee.getIsEqual(b.fee)).toEqual(true);
22
+ expect(a.reinvest === b.reinvest).toEqual(true);
23
+ expect(a.cash === b.cash).toEqual(true);
24
+ }
25
+
26
+ describe('for an edit operation (user error #1)', () => {
27
+ let transaction;
28
+ let serialized;
29
+
30
+ beforeEach(() => {
31
+ transaction = { };
32
+
33
+ transaction.portfolio = '063e00ea-8d1b-4faa-aedf-f43bdf23590e';
34
+ transaction.position = 'c2eefcde-f8d0-438d-9414-28f307d7b544';
35
+ transaction.sequence = 1;
36
+ transaction.type = TransactionType.BUY;
37
+ transaction.date = new Day(2018, 7, 9);
38
+ transaction.price = new Decimal(15.92);
39
+ transaction.quantity = new Decimal(100);
40
+ transaction.fee = new Decimal(9.95);
41
+ transaction.reinvest = 'default';
42
+ transaction.cash = 'default';
43
+
44
+ const formatted = TransactionSchema.BUY.schema.format(transaction);
45
+
46
+ serialized = JSON.stringify(formatted);
47
+ });
48
+
49
+ it('the serialized data should be correct', () => {
50
+ expect(typeof serialized === 'string').toEqual(true);
51
+ });
52
+
53
+ describe('and the serialized data is deserialized', function() {
54
+ let deserialized;
55
+
56
+ beforeEach(() => {
57
+ const reviver = TransactionSchema.BUY.schema.getReviver();
58
+
59
+ deserialized = JSON.parse(serialized, reviver);
60
+ });
61
+
62
+ it('the deserialized data should be correct', () => {
63
+ transactionsAreEqual(transaction, deserialized);
64
+ });
65
+ });
66
+ });
67
+ });