|
|
@ -1,6 +1,9 @@ |
|
|
|
|
|
import { CreateOrderDto } from '@ghostfolio/common/dtos'; |
|
|
|
|
|
import type { Account } from '@ghostfolio/prisma/browser'; |
|
|
|
|
|
|
|
|
import { HttpClient } from '@angular/common/http'; |
|
|
import { HttpClient } from '@angular/common/http'; |
|
|
import { TestBed } from '@angular/core/testing'; |
|
|
import { TestBed } from '@angular/core/testing'; |
|
|
import { of } from 'rxjs'; |
|
|
import { Observable, of } from 'rxjs'; |
|
|
|
|
|
|
|
|
import { ImportActivitiesService } from './import-activities.service'; |
|
|
import { ImportActivitiesService } from './import-activities.service'; |
|
|
|
|
|
|
|
|
@ -8,12 +11,16 @@ jest.mock('uuid', () => { |
|
|
return { v4: () => 'uuid' }; |
|
|
return { v4: () => 'uuid' }; |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
type PostArguments = [string, { activities: CreateOrderDto[] }]; |
|
|
|
|
|
|
|
|
describe('ImportActivitiesService', () => { |
|
|
describe('ImportActivitiesService', () => { |
|
|
let post: jest.Mock; |
|
|
let post: jest.Mock<Observable<unknown>, PostArguments>; |
|
|
let service: ImportActivitiesService; |
|
|
let service: ImportActivitiesService; |
|
|
|
|
|
|
|
|
beforeEach(() => { |
|
|
beforeEach(() => { |
|
|
post = jest.fn().mockReturnValue(of({ activities: [] })); |
|
|
post = jest |
|
|
|
|
|
.fn<Observable<unknown>, PostArguments>() |
|
|
|
|
|
.mockReturnValue(of({ activities: [] })); |
|
|
|
|
|
|
|
|
TestBed.configureTestingModule({ |
|
|
TestBed.configureTestingModule({ |
|
|
providers: [{ provide: HttpClient, useValue: { post } }] |
|
|
providers: [{ provide: HttpClient, useValue: { post } }] |
|
|
@ -29,7 +36,7 @@ describe('ImportActivitiesService', () => { |
|
|
'2021-09-01T00:00:00.000Z,AAPL,USD,150.5,2,buy,1,007,01' |
|
|
'2021-09-01T00:00:00.000Z,AAPL,USD,150.5,2,buy,1,007,01' |
|
|
].join('\n'), |
|
|
].join('\n'), |
|
|
isDryRun: true, |
|
|
isDryRun: true, |
|
|
userAccounts: [{ id: 'account-01', name: '01' } as any] |
|
|
userAccounts: [{ id: 'account-01', name: '01' } as Account] |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
const [activity] = post.mock.calls[0][1].activities; |
|
|
const [activity] = post.mock.calls[0][1].activities; |
|
|
@ -44,6 +51,40 @@ describe('ImportActivitiesService', () => { |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should keep symbols which look like a number or a boolean', async () => { |
|
|
|
|
|
await service.importCsv({ |
|
|
|
|
|
fileContent: [ |
|
|
|
|
|
'Date,Symbol,Currency,Price,Quantity,Type,Fee', |
|
|
|
|
|
'2021-09-01,0050,USD,150.5,2,buy,1', |
|
|
|
|
|
'2021-09-01,TRUE,USD,150.5,2,buy,1', |
|
|
|
|
|
'2021-09-01,7203,USD,150.5,2,buy,1' |
|
|
|
|
|
].join('\n'), |
|
|
|
|
|
isDryRun: true, |
|
|
|
|
|
userAccounts: [] |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const symbols = post.mock.calls[0][1].activities.map(({ symbol }) => { |
|
|
|
|
|
return symbol; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
expect(symbols).toEqual(['0050', 'TRUE', '7203']); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should parse a date with an offset', async () => { |
|
|
|
|
|
await service.importCsv({ |
|
|
|
|
|
fileContent: [ |
|
|
|
|
|
'Date,Symbol,Currency,Price,Quantity,Type,Fee', |
|
|
|
|
|
'2021-09-01T10:00:00+02:00,AAPL,USD,150.5,2,buy,1' |
|
|
|
|
|
].join('\n'), |
|
|
|
|
|
isDryRun: true, |
|
|
|
|
|
userAccounts: [] |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const [activity] = post.mock.calls[0][1].activities; |
|
|
|
|
|
|
|
|
|
|
|
expect(activity.date).toBe('2021-09-01T08:00:00.000Z'); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
it('should skip empty cells and use the next column', async () => { |
|
|
it('should skip empty cells and use the next column', async () => { |
|
|
await service.importCsv({ |
|
|
await service.importCsv({ |
|
|
fileContent: [ |
|
|
fileContent: [ |
|
|
@ -51,7 +92,7 @@ describe('ImportActivitiesService', () => { |
|
|
'2021-09-01,,AAPL,,USD,150.5,2,buy,1,,hello,,account-01' |
|
|
'2021-09-01,,AAPL,,USD,150.5,2,buy,1,,hello,,account-01' |
|
|
].join('\n'), |
|
|
].join('\n'), |
|
|
isDryRun: true, |
|
|
isDryRun: true, |
|
|
userAccounts: [{ id: 'account-01', name: '01' } as any] |
|
|
userAccounts: [{ id: 'account-01', name: '01' } as Account] |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
const [activity] = post.mock.calls[0][1].activities; |
|
|
const [activity] = post.mock.calls[0][1].activities; |
|
|
@ -64,6 +105,24 @@ describe('ImportActivitiesService', () => { |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('should trim cells and skip cells with only spaces', async () => { |
|
|
|
|
|
await service.importCsv({ |
|
|
|
|
|
fileContent: [ |
|
|
|
|
|
'Date,Code,Symbol,Currency,Price,Quantity,Type,Fee', |
|
|
|
|
|
' 20230403 , , 7203 ,USD,150.5,2,buy,1' |
|
|
|
|
|
].join('\n'), |
|
|
|
|
|
isDryRun: true, |
|
|
|
|
|
userAccounts: [] |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const [activity] = post.mock.calls[0][1].activities; |
|
|
|
|
|
|
|
|
|
|
|
expect(activity).toMatchObject({ |
|
|
|
|
|
date: new Date(2023, 3, 3).toISOString(), |
|
|
|
|
|
symbol: '7203' |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
it('should reject a fee without a symbol', async () => { |
|
|
it('should reject a fee without a symbol', async () => { |
|
|
await expect( |
|
|
await expect( |
|
|
service.importCsv({ |
|
|
service.importCsv({ |
|
|
|