@dhyasama/totem-models 1.0.1 → 1.6.0

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.
package/test/Round.js ADDED
@@ -0,0 +1,684 @@
1
+ "use strict";
2
+
3
+ var
4
+
5
+ should = require("should"),
6
+ _ = require('underscore'),
7
+ config = require('./config')['test'],
8
+ mongoose = require('mongoose'),
9
+ clearDB = require('mocha-mongoose')(config.db.uri, {noClear: true}),
10
+ Fund = mongoose.model('Fund'),
11
+ Investment = mongoose.model('Investment'),
12
+ Organization = mongoose.model('Organization'),
13
+ Person = mongoose.model('Person'),
14
+ Round = mongoose.model('Round');
15
+
16
+ var orgid = new mongoose.Types.ObjectId();
17
+ var personid = new mongoose.Types.ObjectId();
18
+ var fundid = new mongoose.Types.ObjectId();
19
+ var investmentid = new mongoose.Types.ObjectId();
20
+ var organization, organization2, fund, person, investment, investment2;
21
+
22
+ describe('Rounds', function() {
23
+
24
+ before(function(done) {
25
+ if (mongoose.connection.db) return done();
26
+ mongoose.connect(config.db.uri, done);
27
+ });
28
+
29
+ before(function(done) {
30
+ clearDB(done);
31
+ });
32
+
33
+ before(function (done) {
34
+ Round.collection.dropIndexes(function (err, result) {
35
+ should.not.exist(err);
36
+ should.exist(result);
37
+ result.should.equal(true);
38
+ return done();
39
+ });
40
+ });
41
+
42
+ before(function(done) {
43
+
44
+ fund = new Fund({
45
+ name: 'Test Fund',
46
+ slug: 'test-fund',
47
+ shortName: 'test'
48
+ });
49
+
50
+ Fund.upsert(fund, 'test', function(err, result) {
51
+ should.not.exist(err);
52
+ should.exist(result);
53
+ fund = result;
54
+ done();
55
+ });
56
+
57
+ });
58
+
59
+ before(function(done) {
60
+
61
+ person = new Person({
62
+ name: {
63
+ first: 'Jim',
64
+ last: 'James'
65
+ },
66
+ slug: 'jim-james'
67
+ });
68
+
69
+ Person.upsert(person, 'test', function(err, result) {
70
+ should.not.exist(err);
71
+ should.exist(result);
72
+ person = result;
73
+ done();
74
+ });
75
+
76
+ });
77
+
78
+ before(function(done) {
79
+
80
+ organization = new Organization({
81
+ name: 'Test Org',
82
+ slug: 'test-org',
83
+ website: 'testorg.com'
84
+ });
85
+
86
+ organization2 = new Organization({
87
+ name: 'Dope Org',
88
+ slug: 'dope-org',
89
+ website: 'dopeorg.com',
90
+ description: 'mad dope',
91
+ logoUrl: 'https://logo.dopeorg.com',
92
+ status: 'Active'
93
+ });
94
+
95
+ Organization.upsert(organization, 'test', function(err, result) {
96
+ should.not.exist(err);
97
+ should.exist(result);
98
+ organization = result;
99
+ Organization.upsert(organization2, 'test', function(err, result) {
100
+ should.not.exist(err);
101
+ should.exist(result);
102
+ organization2 = result;
103
+ done();
104
+ });
105
+ });
106
+
107
+ });
108
+
109
+ before(function(done) {
110
+
111
+ var inv = new Investment({
112
+ customer: config.CUSTOMER_ID,
113
+ cost: 200000,
114
+ costPlusInterest: 200000,
115
+ investmentDate: Date.now(),
116
+ pricePerShare: 0.002,
117
+ preMoneyValuation: 1000000,
118
+ dollarsRaised: 200000,
119
+ closingDate: Date.now()
120
+ });
121
+
122
+ Investment.upsert(inv, function(err, result) {
123
+ should.not.exist(err);
124
+ should.exist(result);
125
+ investment = result;
126
+ done();
127
+ });
128
+
129
+ });
130
+
131
+ before(function(done) {
132
+
133
+ var inv = new Investment({
134
+ customer: config.CUSTOMER_ID,
135
+ cost: 200000,
136
+ costPlusInterest: 200000,
137
+ investmentDate: Date.now(),
138
+ pricePerShare: 0.002,
139
+ preMoneyValuation: 1000000,
140
+ dollarsRaised: 200000,
141
+ closingDate: Date.now()
142
+ });
143
+
144
+ Investment.upsert(inv, function(err, result) {
145
+ should.not.exist(err);
146
+ should.exist(result);
147
+ investment2 = result;
148
+ done();
149
+ });
150
+
151
+ });
152
+
153
+ it('creates a round', function(done) {
154
+
155
+ var r = new Round({
156
+ organization: organization,
157
+ roundName: 'Series Test'
158
+ });
159
+
160
+ Round.upsert(r, function(err, result) {
161
+ should.not.exist(err);
162
+ should.exist(result);
163
+ done();
164
+ });
165
+
166
+ });
167
+
168
+ it('gets all rounds for an org', function(done) {
169
+
170
+ var r1 = new Round({
171
+ organization: organization2,
172
+ roundName: 'Series Seed'
173
+ });
174
+
175
+ var r2 = new Round({
176
+ organization: organization,
177
+ roundName: 'Series F'
178
+ });
179
+
180
+ Round.upsert(r1, function(err, result) {
181
+ should.not.exist(err);
182
+ should.exist(result);
183
+ Round.upsert(r2, function(err, result) {
184
+
185
+ should.not.exist(err);
186
+ should.exist(result);
187
+
188
+ Round.getByOrg(organization, function(err, rounds) {
189
+ should.not.exist(err);
190
+ should.exist(rounds);
191
+ rounds.length.should.equal(2);
192
+ done();
193
+ });
194
+
195
+ });
196
+ });
197
+
198
+ });
199
+
200
+ it('updates a round', function(done) {
201
+
202
+ // get a round to update
203
+ Round.find({}, function(err, rounds) {
204
+
205
+ should.not.exist(err);
206
+ should.exist(rounds);
207
+ rounds.length.should.be.greaterThan(0);
208
+
209
+ var round = rounds[0];
210
+ round.preMoneyValuation = 100;
211
+ round.amountRaised = 10;
212
+
213
+ Round.upsert(round, function(err, result) {
214
+ should.not.exist(err);
215
+ should.exist(result);
216
+ result.preMoneyValuation.should.equal(100);
217
+ result.amountRaised.should.equal(10);
218
+ done();
219
+ });
220
+
221
+ });
222
+
223
+ });
224
+
225
+ it('adds a person', function(done) {
226
+
227
+ // get a round to update
228
+ Round.find({}, function(err, rounds) {
229
+
230
+ should.not.exist(err);
231
+ should.exist(rounds);
232
+ rounds.length.should.be.greaterThan(0);
233
+
234
+ var round = rounds[0];
235
+ var numberOfPeople = round.people.length;
236
+
237
+ round.addPerson(person);
238
+ round.addPerson(person); // make sure it doesn't get added twice
239
+
240
+ Round.upsert(round, function(err, result) {
241
+ should.not.exist(err);
242
+ should.exist(result);
243
+ result.people.length.should.equal(numberOfPeople + 1);
244
+ done();
245
+ });
246
+
247
+ });
248
+
249
+ });
250
+
251
+ it('removes a person', function(done) {
252
+
253
+ // get a round to update
254
+ Round.find({$where: 'this.people && this.people.length >= 1' }, function(err, rounds) {
255
+
256
+ should.not.exist(err);
257
+ should.exist(rounds);
258
+ rounds.length.should.be.greaterThan(0);
259
+
260
+ var round = rounds[0];
261
+ var numberOfPeople = round.people.length;
262
+
263
+ Round.removePerson(round._id, person._id, function(err, result) {
264
+
265
+ should.not.exist(err);
266
+ should.exist(result);
267
+
268
+ Round.findById(round._id, function(err, result) {
269
+ should.not.exist(err);
270
+ should.exist(result);
271
+ result.people.length.should.equal(numberOfPeople - 1);
272
+ done();
273
+ });
274
+
275
+ });
276
+
277
+ });
278
+
279
+ });
280
+
281
+ it('adds a vehicle', function(done) {
282
+
283
+ // get a round to update
284
+ Round.find({}, function(err, rounds) {
285
+
286
+ should.not.exist(err);
287
+ should.exist(rounds);
288
+ rounds.length.should.be.greaterThan(0);
289
+
290
+ var round = rounds[0];
291
+ var numberOfVehicles = round.vehicles.length;
292
+
293
+ round.addVehicle(fundid);
294
+ round.addVehicle(fundid); // make sure it doesn't get added twice
295
+
296
+ Round.upsert(round, function(err, result) {
297
+ should.not.exist(err);
298
+ should.exist(result);
299
+ result.vehicles.length.should.equal(numberOfVehicles + 1);
300
+ result.vehicles[0].fund.should.equal(fundid);
301
+ done();
302
+ });
303
+
304
+ });
305
+
306
+ });
307
+
308
+ it('removes a vehicle', function(done) {
309
+
310
+ // get a round to update
311
+ Round.find({$where: 'this.vehicles && this.vehicles.length >= 1' }, function(err, rounds) {
312
+
313
+ should.not.exist(err);
314
+ should.exist(rounds);
315
+ rounds.length.should.be.greaterThan(0);
316
+
317
+ var round = rounds[0];
318
+ var numberOfVehicles = round.vehicles.length;
319
+
320
+ Round.removeVehicle(round._id, fundid, function(err, result) {
321
+
322
+ should.not.exist(err);
323
+ should.exist(result);
324
+
325
+ Round.findById(round._id, function(err, result) {
326
+ should.not.exist(err);
327
+ should.exist(result);
328
+ result.vehicles.length.should.equal(numberOfVehicles - 1);
329
+ done();
330
+ });
331
+
332
+ });
333
+
334
+ });
335
+
336
+ });
337
+
338
+ it('adds an investment to a non-existing vehicle', function(done) {
339
+
340
+ // get a round to update
341
+ Round.find({$where:'this.vehicles && this.vehicles.length == 0'}, function(err, rounds) {
342
+
343
+ should.not.exist(err);
344
+ should.exist(rounds);
345
+ rounds.length.should.be.greaterThan(0);
346
+
347
+ var round = rounds[0];
348
+ round.addInvestment(investment, fundid);
349
+
350
+ Round.upsert(round, function(err, result) {
351
+ should.not.exist(err);
352
+ should.exist(result);
353
+ result.vehicles.length.should.equal(1);
354
+ result.vehicles[0].fund.should.equal(fundid);
355
+ result.vehicles[0].investments.length.should.equal(1);
356
+ done();
357
+ });
358
+
359
+ });
360
+
361
+ });
362
+
363
+ it('adds an investment to an existing vehicle', function(done) {
364
+
365
+ // get a round to update
366
+ Round.find({$where:'this.vehicles && this.vehicles.length == 1'}, function(err, rounds) {
367
+
368
+ should.not.exist(err);
369
+ should.exist(rounds);
370
+ rounds.length.should.be.greaterThan(0);
371
+
372
+ var round = rounds[0];
373
+ round.addInvestment(investment2, fundid);
374
+
375
+ Round.upsert(round, function(err, result) {
376
+ should.not.exist(err);
377
+ should.exist(result);
378
+ result.vehicles.length.should.equal(1);
379
+ result.vehicles[0].investments.length.should.equal(2);
380
+ done();
381
+ });
382
+
383
+ });
384
+
385
+ });
386
+
387
+ it('gets the portfolio of a person with bad data', function(done) {
388
+
389
+ Round.find({}, function(err, rounds) {
390
+
391
+ should.not.exist(err);
392
+ should.exist(rounds);
393
+ rounds.length.should.be.greaterThan(0);
394
+
395
+ var round = rounds[0];
396
+ round.addPerson(personid);
397
+ round.organization = orgid;
398
+
399
+ Round.upsert(round, function(err, result) {
400
+
401
+ should.not.exist(err);
402
+ should.exist(result);
403
+
404
+ Round.getPortfolioByPerson(personid, function(err, result) {
405
+ should.exist(err);
406
+ err.message.should.equal('Round.getPortfolioByPerson has one or more orgs that is not populated which means there is bad data');
407
+ done();
408
+
409
+ });
410
+
411
+ });
412
+
413
+ });
414
+
415
+ });
416
+
417
+ it('gets the portfolio of a person', function(done) {
418
+
419
+ Round.find({}, function(err, rounds) {
420
+
421
+ should.not.exist(err);
422
+ should.exist(rounds);
423
+ rounds.length.should.be.greaterThan(0);
424
+
425
+ var round = rounds[0];
426
+ round.organization = organization;
427
+ round.addPerson(personid);
428
+
429
+ Round.upsert(round, function(err, result) {
430
+
431
+ should.not.exist(err);
432
+ should.exist(result);
433
+
434
+ Round.getPortfolioByPerson(personid, function(err, result) {
435
+ should.not.exist(err);
436
+ should.exist(result);
437
+ done();
438
+ });
439
+
440
+ });
441
+
442
+ });
443
+
444
+ });
445
+
446
+ it('gets the portfolio of a fund', function(done) {
447
+
448
+ var r1 = new Round({
449
+ organization: organization,
450
+ roundName: 'Series X'
451
+ });
452
+ r1.addVehicle(fund);
453
+
454
+ var r2 = new Round({
455
+ organization: organization2,
456
+ roundName: 'Series Y',
457
+ preMoneyValuation: 54321
458
+ });
459
+ r2.addVehicle(fund);
460
+
461
+ Round.upsert(r1, function(err, result) {
462
+ should.not.exist(err);
463
+ should.exist(result);
464
+ Round.upsert(r2, function(err, result) {
465
+
466
+ should.not.exist(err);
467
+ should.exist(result);
468
+
469
+ Round.getPortfolioByFunds([fund._id], function(err, result) {
470
+
471
+ should.not.exist(err);
472
+ should.exist(result);
473
+ result.length.should.equal(2);
474
+
475
+ var sample = result[0];
476
+ should.exist(sample._id);
477
+ should.exist(sample.name);
478
+ should.exist(sample.logoUrl);
479
+ should.exist(sample.description);
480
+ should.exist(sample.status);
481
+ should.exist(sample.preMoneyValuation);
482
+ should.exist(sample.cost);
483
+ should.exist(sample.unrealized);
484
+ should.exist(sample.realized);
485
+ should.exist(sample.multiple);
486
+
487
+ done();
488
+
489
+ });
490
+
491
+ });
492
+ });
493
+
494
+ });
495
+
496
+ it('gets the portfolio of two funds', function(done) {
497
+
498
+ Round.getPortfolioByFunds([fund._id, new mongoose.Types.ObjectId()], function(err, result) {
499
+ should.not.exist(err);
500
+ should.exist(result);
501
+ result.length.should.equal(2);
502
+ done();
503
+ });
504
+
505
+ });
506
+
507
+ it('merges private data into public data', function(done) {
508
+
509
+ var o1 = new Organization({
510
+ name: 'Test',
511
+ slug: 'test',
512
+ website: 'test.io'
513
+ });
514
+
515
+ Organization.upsert(o1, 'test', function(err, result) {
516
+
517
+ should.not.exist(err);
518
+ should.exist(result);
519
+ o1 = result;
520
+
521
+ var inv1 = new Investment({
522
+ customer: config.CUSTOMER_ID,
523
+ investmentDate: Date.now(),
524
+ preMoneyValuation: 2,
525
+ dollarsRaised: 4,
526
+ closingDate: Date.now()
527
+ });
528
+
529
+ Investment.upsert(inv1, function(err, result) {
530
+
531
+ should.not.exist(err);
532
+ should.exist(result);
533
+ inv1 = result;
534
+
535
+ var r1 = new Round({
536
+ organization: o1,
537
+ roundName: 'Series Z',
538
+ preMoneyValuation: 1,
539
+ dollarsRaised: 3
540
+ });
541
+ r1.addInvestment(inv1, fund);
542
+
543
+ Round.upsert(r1, function(err, result) {
544
+
545
+ should.not.exist(err);
546
+ should.exist(result);
547
+ r1 = result;
548
+
549
+ Round.getByOrg(r1.organization, function(err, result) {
550
+
551
+ should.not.exist(err);
552
+ should.exist(result);
553
+ result.length.should.equal(1);
554
+ result[0].preMoneyValuation.should.equal(2);
555
+ result[0].dollarsRaised.should.equal(4);
556
+ done();
557
+
558
+ });
559
+
560
+ });
561
+
562
+ });
563
+
564
+ });
565
+
566
+ });
567
+
568
+ it('does not merge private data into public data', function(done) {
569
+
570
+ var o1 = new Organization({
571
+ name: 'Test 2',
572
+ slug: 'test-2',
573
+ website: 'test2.io'
574
+ });
575
+
576
+ Organization.upsert(o1, 'test', function(err, result) {
577
+
578
+ should.not.exist(err);
579
+ should.exist(result);
580
+ o1 = result;
581
+
582
+ var inv1 = new Investment({
583
+ customer: new mongoose.Types.ObjectId(),
584
+ investmentDate: Date.now(),
585
+ preMoneyValuation: 2,
586
+ dollarsRaised: 4,
587
+ closingDate: Date.now()
588
+ });
589
+
590
+ Investment.upsert(inv1, function(err, result) {
591
+
592
+ should.not.exist(err);
593
+ should.exist(result);
594
+ inv1 = result;
595
+
596
+ var r1 = new Round({
597
+ organization: o1,
598
+ roundName: 'Series Z',
599
+ preMoneyValuation: 1,
600
+ dollarsRaised: 3
601
+ });
602
+ r1.addInvestment(inv1, fund);
603
+
604
+ Round.upsert(r1, function(err, result) {
605
+
606
+ should.not.exist(err);
607
+ should.exist(result);
608
+ r1 = result;
609
+
610
+ Round.getByOrg(r1.organization, function(err, result) {
611
+
612
+ should.not.exist(err);
613
+ should.exist(result);
614
+ result.length.should.equal(1);
615
+ result[0].preMoneyValuation.should.equal(1);
616
+ result[0].dollarsRaised.should.equal(3);
617
+ done();
618
+
619
+ });
620
+
621
+ });
622
+
623
+ });
624
+
625
+ });
626
+
627
+ });
628
+
629
+ it('gets the performance of a fund', function(done) {
630
+
631
+ Round.getFundPerformance(fund._id, function(err, result) {
632
+
633
+ should.not.exist(err);
634
+ should.exist(result);
635
+
636
+ should.exist(result.cost);
637
+ should.exist(result.unrealized);
638
+ should.exist(result.realized);
639
+ should.exist(result.value);
640
+ should.exist(result.multiple);
641
+
642
+ done();
643
+
644
+ });
645
+
646
+ });
647
+
648
+ it('removes an investment', function(done) {
649
+
650
+ Round.find({}, function(err, rounds) {
651
+
652
+ should.not.exist(err);
653
+ should.exist(rounds);
654
+ rounds.length.should.be.greaterThan(0);
655
+
656
+ var round = _.find(rounds, function(r) {
657
+ return r.vehicles.length >= 1 && r.vehicles[0].investments.length >= 1;
658
+ });
659
+
660
+ should.exist(round);
661
+
662
+ Round.removeInvestment(round.vehicles[0].investments[0], function(err, result) {
663
+
664
+ should.not.exist(err);
665
+ should.exist(result);
666
+
667
+ Round.findById(round._id, function(err, r) {
668
+
669
+ should.not.exist(err);
670
+ should.exist(r);
671
+
672
+ r.vehicles[0].investments.length.should.equal(round.vehicles[0].investments.length - 1);
673
+
674
+ done();
675
+
676
+ });
677
+
678
+ });
679
+
680
+ });
681
+
682
+ });
683
+
684
+ });