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
67 changes: 59 additions & 8 deletions src/api/company-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { CompanyService } from '~/api/company-service'
import { CachingBuffettCodeApiClientV2 } from '~/api/v2/caching-client'
import { CachingBuffettCodeApiClientV3 } from '~/api/v3/caching-client'
import { DateParam } from '~/fiscal-periods/date-param'
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'
import { YearQuarter } from '~/fiscal-periods/year-quarter'
import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param'

const LY = new LyWithOffset()
const LQ = new LqWithOffset()

jest.mock('~/api/v2/client', () =>
jest.requireActual('~/__mocks__/api/v2/client')
)
Expand All @@ -25,18 +30,49 @@ test('convertYearQuarterParamToYearQuarter', () => {
const client = new CachingBuffettCodeApiClientV2('token')
const service = new CompanyService('2371', client)
expect(
service.convertYearQuarterParamToYearQuarter(new YearQuarterParam('LY', 3))
service.convertYearQuarterParamToYearQuarter(new YearQuarterParam(LY, 3))
).toEqual(new YearQuarter(2021, 3))
expect(
service.convertYearQuarterParamToYearQuarter(new YearQuarterParam(2016, LQ))
).toEqual(new YearQuarter(2016, 2))
expect(
service.convertYearQuarterParamToYearQuarter(new YearQuarterParam(LY, LQ))
).toEqual(new YearQuarter(2021, 2))
expect(
service.convertYearQuarterParamToYearQuarter(
new YearQuarterParam(2016, 'LQ')
new YearQuarterParam(new LyWithOffset(-5), LQ)
)
).toEqual(new YearQuarter(2016, 2))
expect(
service.convertYearQuarterParamToYearQuarter(
new YearQuarterParam('LY', 'LQ')
new YearQuarterParam(LY, new LqWithOffset(-1))
)
).toEqual(new YearQuarter(2021, 2))
).toEqual(new YearQuarter(2021, 1))
expect(
service.convertYearQuarterParamToYearQuarter(
new YearQuarterParam(LY, new LqWithOffset(-2))
)
).toEqual(new YearQuarter(2020, 4))
expect(
service.convertYearQuarterParamToYearQuarter(
new YearQuarterParam(LY, new LqWithOffset(-3))
)
).toEqual(new YearQuarter(2020, 3))
expect(
service.convertYearQuarterParamToYearQuarter(
new YearQuarterParam(LY, new LqWithOffset(-4))
)
).toEqual(new YearQuarter(2020, 2))
expect(
service.convertYearQuarterParamToYearQuarter(
new YearQuarterParam(LY, new LqWithOffset(-5))
)
).toEqual(new YearQuarter(2020, 1))
expect(
service.convertYearQuarterParamToYearQuarter(
new YearQuarterParam(new LyWithOffset(-5), new LqWithOffset(-5))
)
).toEqual(new YearQuarter(2015, 1))
})

test('isOndemandQuarterApiPeriod', () => {
Expand All @@ -52,15 +88,30 @@ test('isOndemandQuarterApiPeriod', () => {
expect(service.isOndemandQuarterApiPeriod(new YearQuarter(2016, 3))).toBe(
false
)
expect(service.isOndemandQuarterApiPeriod(new YearQuarterParam(LY, 3))).toBe(
false
)
expect(
service.isOndemandQuarterApiPeriod(new YearQuarterParam(2016, LQ))
).toBe(true)
expect(service.isOndemandQuarterApiPeriod(new YearQuarterParam(LY, LQ))).toBe(
false
)
expect(
service.isOndemandQuarterApiPeriod(new YearQuarterParam('LY', 3))
service.isOndemandQuarterApiPeriod(
new YearQuarterParam(new LyWithOffset(-4), new LqWithOffset(-3))
)
).toBe(false)
expect(
service.isOndemandQuarterApiPeriod(new YearQuarterParam(2016, 'LQ'))
service.isOndemandQuarterApiPeriod(
new YearQuarterParam(new LyWithOffset(-4), new LqWithOffset(-4))
)
).toBe(true)
expect(
service.isOndemandQuarterApiPeriod(new YearQuarterParam('LY', 'LQ'))
).toBe(false)
service.isOndemandQuarterApiPeriod(
new YearQuarterParam(new LyWithOffset(-5), LQ)
)
).toBe(true)
})

test('isOndemandDailyApiPeriod', () => {
Expand Down
18 changes: 14 additions & 4 deletions src/api/company-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { BuffettCodeApiClientV2 } from '~/api/v2/client'
import { BuffettCodeApiClientV3 } from '~/api/v3/client'
import { DateParam } from '~/fiscal-periods/date-param'
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'
import { YearQuarter } from '~/fiscal-periods/year-quarter'
import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param'

Expand All @@ -22,12 +24,20 @@ export class CompanyService {
public convertYearQuarterParamToYearQuarter(
period: YearQuarterParam
): YearQuarter {
if (period.isLatestYear()) {
period.year = this.company['latest_fiscal_year']
if (period.year instanceof LyWithOffset) {
period.year = this.company['latest_fiscal_year'] + period.year.offset
}

if (period.isLatestQuarter()) {
period.quarter = this.company['latest_fiscal_quarter']
if (period.quarter instanceof LqWithOffset) {
period.year =
(period.year as number) + Math.ceil(period.quarter.offset / 4)
period.quarter =
this.company['latest_fiscal_quarter'] + (period.quarter.offset % 4)

if (period.quarter <= 0) {
period.year -= 1
period.quarter = 4 + (period.quarter as number)
}
}
Comment on lines 25 to 41
Copy link
Copy Markdown
Contributor Author

@akiomik akiomik Dec 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

相対値を含むLY・LQを、 /api/v*/company のレスポンスを元に実際のFY・FQに変換するロジックです。


return period.toYearQuarter()
Expand Down
9 changes: 7 additions & 2 deletions src/api/v2/caching-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CompanyCache } from '~/__mocks__/services/company-cache'
import { QuarterCache } from '~/__mocks__/services/quarter-cache'
import { CachingBuffettCodeApiClientV2 } from '~/api/v2/caching-client'
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'
import { YearQuarter } from '~/fiscal-periods/year-quarter'
import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param'
import { IndicatorCache } from '~/services/indicator-cache'
Expand All @@ -18,6 +20,9 @@ jest.mock('~/services/quarter-cache', () =>
jest.requireActual('~/__mocks__/services/quarter-cache')
)

const LY = new LyWithOffset()
const LQ = new LqWithOffset()

describe('company', () => {
const ticker = '2371'

Expand Down Expand Up @@ -111,7 +116,7 @@ describe('quarter', () => {
QuarterCache.clearAll()
})

const period = new YearQuarterParam('LY', 'LQ')
const period = new YearQuarterParam(LY, LQ)

test('(uncached)', () => {
expect(QuarterCache.getData(ticker, new YearQuarter(2018, 1))).toBeNull()
Expand Down Expand Up @@ -168,7 +173,7 @@ describe('ondemandQuarter', () => {
QuarterCache.clearAll()
})

const period = new YearQuarterParam('LY', 'LQ')
const period = new YearQuarterParam(LY, LQ)

test('(uncached)', () => {
expect(QuarterCache.getData(ticker, new YearQuarter(2018, 1))).toBeNull()
Expand Down
9 changes: 7 additions & 2 deletions src/api/v3/caching-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { DailyCache } from '~/__mocks__/services/daily-cache'
import { QuarterCache } from '~/__mocks__/services/quarter-cache'
import { CachingBuffettCodeApiClientV3 } from '~/api/v3/caching-client'
import { DateParam } from '~/fiscal-periods/date-param'
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'
import { YearQuarter } from '~/fiscal-periods/year-quarter'
import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param'

Expand All @@ -19,6 +21,9 @@ jest.mock('~/services/quarter-cache', () =>
jest.requireActual('~/__mocks__/services/quarter-cache')
)

const LY = new LyWithOffset()
const LQ = new LqWithOffset()

describe('company', () => {
const ticker = '2371'

Expand Down Expand Up @@ -112,7 +117,7 @@ describe('quarter', () => {
QuarterCache.clearAll()
})

const period = new YearQuarterParam('LY', 'LQ')
const period = new YearQuarterParam(LY, LQ)

test('(uncached)', () => {
expect(QuarterCache.get(ticker, new YearQuarter(2018, 1))).toBeNull()
Expand Down Expand Up @@ -165,7 +170,7 @@ describe('ondemandQuarter', () => {
QuarterCache.clearAll()
})

const period = new YearQuarterParam('LY', 'LQ')
const period = new YearQuarterParam(LY, LQ)

test('(uncached)', () => {
expect(QuarterCache.get(ticker, new YearQuarter(2018, 1))).toBeNull()
Expand Down
26 changes: 25 additions & 1 deletion src/api/v3/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Daily } from '~/entities/v3/daily'
import { Quarter } from '~/entities/v3/quarter'
import { DateParam } from '~/fiscal-periods/date-param'
import { DateRange } from '~/fiscal-periods/date-range'
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'
import { YearQuarter } from '~/fiscal-periods/year-quarter'
import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param'
import { YearQuarterRange } from '~/fiscal-periods/year-quarter-range'
Expand Down Expand Up @@ -97,7 +99,7 @@ describe('BuffettCodeApiClientV3', () => {
})
})

test('quarter', () => {
test('quarter (FY/FQ)', () => {
const mockFetch = useMockedUrlFetchApp(200, JSON.stringify(quarter))

const client = new BuffettCodeApiClientV3('foo')
Expand All @@ -117,6 +119,28 @@ describe('BuffettCodeApiClientV3', () => {
})
})

test('quarter (LY/LQ)', () => {
const mockFetch = useMockedUrlFetchApp(200, JSON.stringify(quarter))

const LY = new LyWithOffset()
const LQ = new LqWithOffset()
const client = new BuffettCodeApiClientV3('foo')
const ticker = '2371'
const period = new YearQuarterParam(LY, LQ)
expect(client.quarter(ticker, period)).toEqual(
Quarter.fromResponse(quarter)
)
expect(mockFetch.mock.calls.length).toBe(1)
expect(mockFetch.mock.calls[0].length).toBe(2)
expect(mockFetch.mock.calls[0][0]).toBe(
'https://api.buffett-code.com/api/v3/quarter?ticker=2371&fy=LY&fq=LQ'
)
expect(mockFetch.mock.calls[0][1]).toEqual({
headers: { 'x-api-key': 'foo' },
muteHttpExceptions: true
})
})

test('bulkQuarter', () => {
const bulkQuarter = {
data: {
Expand Down
3 changes: 3 additions & 0 deletions src/custom-functions/bcode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ test('isV3Call', () => {
expect(isV3Call('ly', '4')).toBeFalsy()
expect(isV3Call('2020', 'LQ')).toBeFalsy()
expect(isV3Call('2020', 'lq')).toBeFalsy()
expect(isV3Call('LY', 'LQ')).toBeFalsy()
expect(isV3Call('LY-1', 'LQ-1')).toBeFalsy()
expect(isV3Call('', '')).toBeFalsy()
expect(isV3Call('2020Q1', 'net_sales')).toBeTruthy()
expect(isV3Call('LYLQ', 'net_sales')).toBeTruthy()
})
7 changes: 6 additions & 1 deletion src/custom-functions/bcode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { bcode as bcodeV2 } from '~/custom-functions/v2/bcode'
import { bcode as bcodeV3 } from '~/custom-functions/v3/bcode'
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'

export function castStringAsBoolean(bool: string | boolean): boolean {
return typeof bool === 'string' ? bool.toLowerCase() === 'true' : bool
Expand All @@ -15,7 +17,10 @@ export function isV3Call(
return false
} else if (param1 === '' || param2 === '') {
return false
} else if (param1.toUpperCase() === 'LY' || param2.toUpperCase() === 'LQ') {
} else if (
LyWithOffset.isValidFormat(param1) ||
LqWithOffset.isValidFormat(param2)
) {
return false
} else if (param1.match(/^\d+$/) || param1.match(/^\d+$/)) {
return false
Expand Down
6 changes: 5 additions & 1 deletion src/custom-functions/v2/bcode-quarter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { QuarterCache } from '~/__mocks__/services/quarter-cache'
import { CachingBuffettCodeApiClientV2 } from '~/api/v2/caching-client'
import { bcodeQuarter } from '~/custom-functions/v2/bcode-quarter'
import { BcodeResult } from '~/custom-functions/v2/bcode-result'
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'
import { YearQuarter } from '~/fiscal-periods/year-quarter'
import { QuarterPropertyCache } from '~/services/quarter-property-cache'

Expand Down Expand Up @@ -45,13 +47,15 @@ describe('bcodeQuarter', () => {
})

test('(quarter, LY, LQ)', () => {
const LY = new LyWithOffset()
const LQ = new LqWithOffset()
const ticker = '2371'
const period = new YearQuarter(2018, 1)
expect(QuarterCache.getData(ticker, period)).toBeNull()
expect(QuarterPropertyCache.get()).toBeNull()

const client = new CachingBuffettCodeApiClientV2('token')
const result = bcodeQuarter(client, ticker, 'LY', 'LQ', 'net_sales', false)
const result = bcodeQuarter(client, ticker, LY, LQ, 'net_sales', false)

expect(result).toEqual(new BcodeResult(12513000000.0, '百万円'))
expect(QuarterCache.getData(ticker, period)['net_sales']).toBe(
Expand Down
6 changes: 4 additions & 2 deletions src/custom-functions/v2/bcode-quarter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import {
UnsupportedTickerError
} from '~/custom-functions/error'
import { BcodeResult } from '~/custom-functions/v2/bcode-result'
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'
import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param'

export function bcodeQuarter(
client: CachingBuffettCodeApiClientV2,
ticker: string,
fiscalYear: number | 'LY',
fiscalQuarter: number | 'LQ',
fiscalYear: number | LyWithOffset,
fiscalQuarter: number | LqWithOffset,
propertyName: string,
ondemandApiEnabled: boolean
): BcodeResult {
Expand Down
19 changes: 14 additions & 5 deletions src/custom-functions/v2/bcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
InvalidYearError,
InvalidQuarterError
} from '~/fiscal-periods/error'
import { LqWithOffset } from '~/fiscal-periods/lq-with-offset'
import { LyWithOffset } from '~/fiscal-periods/ly-with-offset'
import { Setting } from '~/setting'

function validate(
Expand Down Expand Up @@ -42,7 +44,6 @@ function validate(
}

// TODO: エラーハンドリングの改善
// TODO: fiscalYearとfiscalQuarterの型をstringではなく'LY'と'LQ'に変更する
export function bcode(
ticker: string,
fiscalYear: string | number,
Expand All @@ -68,13 +69,21 @@ export function bcode(
try {
let result: BcodeResult
if (fiscalYear && fiscalQuarter) {
const fy =
typeof fiscalYear === 'string' && fiscalYear.substring(0, 2) === 'LY'
? LyWithOffset.parse(fiscalYear)
: parseInt(fiscalYear.toString(), 10)
const fq =
typeof fiscalQuarter === 'string' &&
fiscalQuarter.substring(0, 2) === 'LQ'
? LqWithOffset.parse(fiscalQuarter)
: parseInt(fiscalQuarter.toString(), 10)

result = bcodeQuarter(
client,
ticker,
fiscalYear === 'LY' ? fiscalYear : parseInt(fiscalYear.toString(), 10),
fiscalQuarter === 'LQ'
? fiscalQuarter
: parseInt(fiscalQuarter.toString(), 10),
fy,
fq,
propertyName,
setting.ondemandApiEnabled
)
Expand Down
2 changes: 1 addition & 1 deletion src/custom-functions/v3/bcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function handleError(e): void {
)
}
} else if (e instanceof InvalidLYLQError) {
throw new Error('<<LYとLQは同時に指定する必要があります>>')
throw new Error('<<無効なLY・LQが指定されています>>')
} else if (e instanceof InvalidYearError) {
throw new Error(`<<無効な決算年度が指定されています>>`)
} else if (e instanceof InvalidQuarterError) {
Expand Down
Loading