Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/__mocks__/api/v3/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { default as company } from '~/__mocks__/fixtures/v3/company.js'
import { default as daily } from '~/__mocks__/fixtures/v3/daily.js'
import { default as quarter } from '~/__mocks__/fixtures/v3/quarter.js'
import { Daily } from '~/entities/v3/daily'

export class BuffettCodeApiClientV3 {
public mockCompany = jest.fn()
Expand All @@ -13,24 +14,21 @@ export class BuffettCodeApiClientV3 {
this.mockQuarter.mockReturnValue(quarter)
}

company(): object | null {
const company = this.mockCompany()['data']
return company ? company : null
company(): object {
return this.mockCompany()['data']
}

quarter(): object | null {
const quarter = this.mockQuarter()['data']
return quarter ? quarter : null
}

daily(): object | null {
const daily = this.mockDaily()['data']
return daily ? daily : null
daily(): Daily {
return Daily.fromResponse(this.mockDaily())
}

ondemandDaily(): object | null {
const daily = this.mockDaily()['data']
return daily ? daily : null
ondemandDaily(): Daily {
return Daily.fromResponse(this.mockDaily())
}

ondemandQuarter(): object | null {
Expand Down
29 changes: 27 additions & 2 deletions src/__mocks__/services/daily-cache.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { Daily } from '~/entities/v3/daily'
import { DateParam } from '~/fiscal-periods/date-param'

export class DailyCache {
static readonly cache = {}

static get(ticker: string, date: Date | DateParam): object | null {
static get(ticker: string, date: Date | DateParam): Daily | null {
const cachedData = this.getData(ticker, date)
const cachedColumnDescription = this.getColumnDescription()
if (cachedData == undefined || cachedColumnDescription == undefined) {
return null
}

return new Daily(cachedData, cachedColumnDescription)
}

static getData(ticker: string, date: Date | DateParam): object | null {
if (date instanceof Date) {
date = new DateParam(date)
}
Expand All @@ -12,10 +23,24 @@ export class DailyCache {
return cached === undefined ? null : cached
}

static put(ticker: string, daily: object): void {
static getColumnDescription(): object | null {
const cached = this.cache['column-description']
return cached === undefined ? null : cached
}

static put(ticker: string, daily: Daily): void {
this.putData(ticker, daily.data)
this.putColumnDescription(daily.columnDescription)
}

static putData(ticker: string, daily: object): void {
DailyCache.cache[`${ticker}-${daily['day']}`] = daily
}

static putColumnDescription(columnDescription: object): void {
this.cache['column-description'] = columnDescription
}

// for testing
static clearAll(): void {
Object.keys(this.cache).forEach(key => delete this.cache[key])
Expand Down