feat: 增加 基础类型使用错误常量

This commit is contained in:
zhangxiangliang 2020-08-22 17:02:29 +08:00
parent 10119a70ca
commit 5bd99ecd3a
9 changed files with 121 additions and 121 deletions

View File

@ -1,3 +1,6 @@
// Utils
import { ERROR_UNDEFINED_GET_STOCK, ERROR_UNDEFINED_GET_STOCKS, ERROR_UNDEFINED_SEARCH_STOCK } from "@utils/constant";
// Types
import Stock from "types/utils/stock";
import StockApi from "types/stocks/index";
@ -11,7 +14,7 @@ const Base: StockApi = {
* @param code
*/
async getStock(code: string): Promise<Stock> {
throw new Error("未实现获取股票数据");
throw new Error(ERROR_UNDEFINED_GET_STOCK);
},
/**
@ -19,7 +22,7 @@ const Base: StockApi = {
* @param codes
*/
async getStocks(codes: string[]): Promise<Stock[]> {
throw new Error("未实现获取股票数据组");
throw new Error(ERROR_UNDEFINED_GET_STOCKS);
},
/**
@ -27,7 +30,7 @@ const Base: StockApi = {
* @param key
*/
async searchStocks(key: string): Promise<Stock[]> {
throw new Error("未实现搜索股票代码");
throw new Error(ERROR_UNDEFINED_SEARCH_STOCK);
}
};

View File

@ -1,3 +1,7 @@
// Utils
import { COMMON_SH, COMMON_SZ, COMMON_HK, COMMON_US } from "@stocks/base/utils/constant";
import { ERROR_UNDEFINED_API_CODE, ERROR_UNDEFINED_SZ_API_CODE, ERROR_UNDEFINED_SH_API_CODE, ERROR_UNDEFINED_HK_API_CODE, ERROR_UNDEFINED_US_API_CODE, ERROR_API_CODE } from "@utils/constant";
// Types
import ApiCodeTransform from "types/stocks/transforms/api-code";
@ -10,7 +14,23 @@ class BaseApiCodeTransform implements ApiCodeTransform {
* @param code
*/
public transform(code: string): string {
throw new Error("未实现股票代码转换统一代码");
if (code.indexOf(COMMON_SH) === 0) {
return this.SHTransform(code);
}
if (code.indexOf(COMMON_SZ) === 0) {
return this.SZTransform(code);
}
if (code.indexOf(COMMON_HK) === 0) {
return this.HKTransform(code);
}
if (code.indexOf(COMMON_US) === 0) {
return this.USTransform(code);
}
throw new Error(ERROR_API_CODE);
}
/**
@ -26,7 +46,7 @@ class BaseApiCodeTransform implements ApiCodeTransform {
* @param code
*/
public SZTransform(code: string): string {
throw new Error("未实现深交所股票代码转换统一代码");
throw new Error(ERROR_UNDEFINED_SZ_API_CODE);
}
/**
@ -34,7 +54,7 @@ class BaseApiCodeTransform implements ApiCodeTransform {
* @param code
*/
public SHTransform(code: string): string {
throw new Error("未实现上交所股票代码转换统一代码");
throw new Error(ERROR_UNDEFINED_SH_API_CODE);
}
/**
@ -42,7 +62,7 @@ class BaseApiCodeTransform implements ApiCodeTransform {
* @param code
*/
public HKTransform(code: string): string {
throw new Error("未实现港交所股票代码转换统一代码");
throw new Error(ERROR_UNDEFINED_HK_API_CODE);
}
/**
@ -50,7 +70,7 @@ class BaseApiCodeTransform implements ApiCodeTransform {
* @param code
*/
public USTransform(code: string): string {
throw new Error("未实现美交所股票代码转换统一代码");
throw new Error(ERROR_UNDEFINED_US_API_CODE);
}
}

View File

@ -1,8 +1,9 @@
// Utils
import { ERROR_COMMON_CODE, ERROR_UNDEFINED_SZ_COMMON_CODE, ERROR_UNDEFINED_SH_COMMON_CODE, ERROR_UNDEFINED_HK_COMMON_CODE, ERROR_UNDEFINED_US_COMMON_CODE } from "@utils/constant";
// Types
import CommonCodeTransform from "types/stocks/transforms/common-code";
// Utils
import { COMMON_SH, COMMON_SZ, COMMON_HK, COMMON_US } from "@stocks/base/utils/constant";
import { COMMON_SH, COMMON_SZ, COMMON_HK, COMMON_US } from "../utils/constant";
/**
*
@ -29,7 +30,7 @@ class BaseCommonCodeTransform implements CommonCodeTransform {
return this.USTransform(code);
}
throw new Error("请检查统一代码是否正确");
throw new Error(ERROR_COMMON_CODE);
}
/**
@ -45,11 +46,7 @@ class BaseCommonCodeTransform implements CommonCodeTransform {
* @param code
*/
public SZTransform(code: string): string {
if (code.indexOf(COMMON_SZ) !== 0) {
throw new Error("请检查统一代码是否正确");
}
throw new Error("未实现深交所统一代码转换股票代码");
throw new Error(ERROR_UNDEFINED_SZ_COMMON_CODE);
}
/**
@ -57,11 +54,7 @@ class BaseCommonCodeTransform implements CommonCodeTransform {
* @param code
*/
public SHTransform(code: string): string {
if (code.indexOf(COMMON_SH) !== 0) {
throw new Error("请检查统一代码是否正确");
}
throw new Error("未实现上交所统一代码转换股票代码");
throw new Error(ERROR_UNDEFINED_SH_COMMON_CODE);
}
/**
@ -69,11 +62,7 @@ class BaseCommonCodeTransform implements CommonCodeTransform {
* @param code
*/
public HKTransform(code: string): string {
if (code.indexOf(COMMON_HK) !== 0) {
throw new Error("请检查统一代码是否正确");
}
throw new Error("未实现港交所统一代码转换股票代码");
throw new Error(ERROR_UNDEFINED_HK_COMMON_CODE);
}
/**
@ -81,11 +70,7 @@ class BaseCommonCodeTransform implements CommonCodeTransform {
* @param code
*/
public USTransform(code: string): string {
if (code.indexOf(COMMON_US) !== 0) {
throw new Error("请检查统一代码是否正确");
}
throw new Error("未实现美交所统一代码转换股票代码");
throw new Error(ERROR_UNDEFINED_US_COMMON_CODE);
}
}

View File

@ -1,3 +1,6 @@
// Utils
import { ERROR_UNDEFINED_GET_CODE, ERROR_UNDEFINED_GET_NAME, ERROR_UNDEFINED_GET_NOW, ERROR_UNDEFINED_GET_LOW, ERROR_UNDEFINED_GET_HIGH, ERROR_UNDEFINED_GET_YESTERDAY, ERROR_UNDEFINED_GET_PERCENT, ERROR_UNDEFINED_GET_STOCK } from "@utils/constant";
// Types
import Stock from "types/utils/stock";
@ -9,65 +12,56 @@ class BaseStockTransform {
*
*/
getCode(): string {
throw new Error('未实现获取代码');
throw new Error(ERROR_UNDEFINED_GET_CODE);
}
/**
*
*/
getName(): string {
throw new Error('未实现获取名称');
throw new Error(ERROR_UNDEFINED_GET_NAME);
}
/**
*
*/
getNow(): number {
throw new Error('未实现获取现价');
throw new Error(ERROR_UNDEFINED_GET_NOW);
}
/**
*
*/
getLow(): number {
throw new Error('未实现获取最低价');
throw new Error(ERROR_UNDEFINED_GET_LOW);
}
/**
*
*/
getHigh(): number {
throw new Error('未实现获取最高价');
throw new Error(ERROR_UNDEFINED_GET_HIGH);
}
/**
*
*/
getYesterday(): number {
throw new Error('未实现获取昨日收盘价');
throw new Error(ERROR_UNDEFINED_GET_YESTERDAY);
}
/**
*
*/
getPercent(): number {
throw new Error('未实现获取涨跌');
throw new Error(ERROR_UNDEFINED_GET_PERCENT);
}
/**
*
*/
getStock(): Stock {
return {
code: this.getCode(),
name: this.getName(),
percent: this.getPercent(),
now: this.getNow(),
low: this.getLow(),
high: this.getHigh(),
yesterday: this.getYesterday(),
};
throw new Error(ERROR_UNDEFINED_GET_STOCK);
}
}

View File

@ -1,16 +1,6 @@
// 默认交易所常量
export const COMMON_SZ = "SZ"; // 深交所相关常量
export const COMMON_SH = "SH"; // 上交所相关常量
export const COMMON_HK = "HK"; // 港交所相关常量
export const COMMON_US = "US"; // 美交所相关常量
// 默认股票数据常量
export const DEFAULT_STOCK = {
code: '---',
name: '---',
percent: 0,
now: 0,
low: 0,
high: 0,
yesterday: 0,
};

View File

@ -2,15 +2,20 @@
const Base = require("stocks/base").default;
describe("【基础】股票代码接口", () => {
const {
ERROR_UNDEFINED_GET_STOCK,
ERROR_UNDEFINED_GET_STOCKS,
} = require("utils/constant");
it("需要获取的股票代码", async () => {
await expect(Base.getStock("SZ000000"))
.rejects
.toThrow(new Error("未实现获取股票数据"));
.toThrow(new Error(ERROR_UNDEFINED_GET_STOCK));
});
it("需要获取的股票代码组", async () => {
await expect(Base.getStocks(["SZ000000"]))
.rejects
.toThrow(new Error("未实现获取股票数据组"));
.toThrow(new Error(ERROR_UNDEFINED_GET_STOCKS));
});
});

View File

@ -2,69 +2,65 @@
const BaseApiCodeTransform = require("stocks/base/transforms/api-code").default;
describe("【基础】股票代码转换统一代码", () => {
it("深交所股票代码转换统一代码", async () => {
expect(() => (new BaseApiCodeTransform()).SZTransform("000000"))
.toThrow(new Error("未实现深交所股票代码转换统一代码"));
const {
ERROR_API_CODE,
ERROR_UNDEFINED_SZ_API_CODE,
ERROR_UNDEFINED_SH_API_CODE,
ERROR_UNDEFINED_HK_API_CODE,
ERROR_UNDEFINED_US_API_CODE
} = require("utils/constant");
it("深交所股票代码转换统一代码", async () => {
expect(() => (new BaseApiCodeTransform()).SZTransform("SZ000000"))
.toThrow(new Error("未实现深交所股票代码转换统一代码"));
.toThrow(new Error(ERROR_UNDEFINED_SZ_API_CODE));
});
it("上交所股票代码转换统一代码", async () => {
expect(() => (new BaseApiCodeTransform()).SHTransform("000000"))
.toThrow(new Error("未实现上交所股票代码转换统一代码"));
expect(() => (new BaseApiCodeTransform()).SHTransform("SH000000"))
.toThrow(new Error("未实现上交所股票代码转换统一代码"));
.toThrow(new Error(ERROR_UNDEFINED_SH_API_CODE));
});
it("港交所股票代码转换统一代码", async () => {
expect(() => new BaseApiCodeTransform().HKTransform("000000"))
.toThrow(new Error("未实现港交所股票代码转换统一代码"));
expect(() => new BaseApiCodeTransform().HKTransform("HK000000"))
.toThrow(new Error("未实现港交所股票代码转换统一代码"));
.toThrow(new Error(ERROR_UNDEFINED_HK_API_CODE));
});
it("美交所股票代码转换统一代码", async () => {
expect(() => (new BaseApiCodeTransform()).USTransform("000000"))
.toThrow(new Error("未实现美交所股票代码转换统一代码"));
expect(() => (new BaseApiCodeTransform()).USTransform("US000000"))
.toThrow(new Error("未实现美交所股票代码转换统一代码"));
.toThrow(new Error(ERROR_UNDEFINED_US_API_CODE));
});
it("交易所股票代码转换统一代码", async () => {
expect(() => (new BaseApiCodeTransform()).transform("000000"))
.toThrow(new Error("未实现股票代码转换统一代码"));
.toThrow(new Error(ERROR_API_CODE));
expect(() => (new BaseApiCodeTransform()).transform("SZ000000"))
.toThrow(new Error("未实现股票代码转换统一代码"));
.toThrow(new Error(ERROR_UNDEFINED_SZ_API_CODE));
expect(() => (new BaseApiCodeTransform()).transform("SH000000"))
.toThrow(new Error("未实现股票代码转换统一代码"));
.toThrow(new Error(ERROR_UNDEFINED_SH_API_CODE));
expect(() => (new BaseApiCodeTransform()).transform("HK000000"))
.toThrow(new Error("未实现股票代码转换统一代码"));
.toThrow(new Error(ERROR_UNDEFINED_HK_API_CODE));
expect(() => (new BaseApiCodeTransform()).transform("US000000"))
.toThrow(new Error("未实现股票代码转换统一代码"));
.toThrow(new Error(ERROR_UNDEFINED_US_API_CODE));
});
it("交易所股票代码组转换统一代码组", async () => {
expect(() => (new BaseApiCodeTransform()).transforms(["000000"]))
.toThrow(new Error("未实现股票代码转换统一代码"));
.toThrow(new Error(ERROR_API_CODE));
expect(() => (new BaseApiCodeTransform()).transforms(["SZ000000"]))
.toThrow(new Error("未实现股票代码转换统一代码"));
.toThrow(new Error(ERROR_UNDEFINED_SZ_API_CODE));
expect(() => (new BaseApiCodeTransform()).transforms(["SH000000"]))
.toThrow(new Error("未实现股票代码转换统一代码"));
.toThrow(new Error(ERROR_UNDEFINED_SH_API_CODE));
expect(() => (new BaseApiCodeTransform()).transforms(["HK000000"]))
.toThrow(new Error("未实现股票代码转换统一代码"));
.toThrow(new Error(ERROR_UNDEFINED_HK_API_CODE));
expect(() => (new BaseApiCodeTransform()).transforms(["US000000"]))
.toThrow(new Error("未实现股票代码转换统一代码"));
.toThrow(new Error(ERROR_UNDEFINED_US_API_CODE));
});
});

View File

@ -2,69 +2,65 @@
const BaseCommonCodeTransform = require("stocks/base/transforms/common-code").default;
describe("【基础】统一代码转换股票代码", () => {
it("深交所统一代码转换股票代码", async () => {
expect(() => (new BaseCommonCodeTransform()).SZTransform("000000"))
.toThrow(new Error("请检查统一代码是否正确"));
const {
ERROR_COMMON_CODE,
ERROR_UNDEFINED_SZ_COMMON_CODE,
ERROR_UNDEFINED_SH_COMMON_CODE,
ERROR_UNDEFINED_HK_COMMON_CODE,
ERROR_UNDEFINED_US_COMMON_CODE
} = require("utils/constant");
it("深交所统一代码转换股票代码", async () => {
expect(() => (new BaseCommonCodeTransform()).SZTransform("SZ000000"))
.toThrow(new Error("未实现深交所统一代码转换股票代码"));
.toThrow(new Error(ERROR_UNDEFINED_SZ_COMMON_CODE));
});
it("上交所统一代码转换股票代码", async () => {
expect(() => (new BaseCommonCodeTransform()).SHTransform("000000"))
.toThrow(new Error("请检查统一代码是否正确"));
expect(() => (new BaseCommonCodeTransform()).SHTransform("SH000000"))
.toThrow(new Error("未实现上交所统一代码转换股票代码"));
.toThrow(new Error(ERROR_UNDEFINED_SH_COMMON_CODE));
});
it("港交所统一代码转换股票代码", async () => {
expect(() => new BaseCommonCodeTransform().HKTransform("000000"))
.toThrow(new Error("请检查统一代码是否正确"));
expect(() => new BaseCommonCodeTransform().HKTransform("HK000000"))
.toThrow(new Error("未实现港交所统一代码转换股票代码"));
.toThrow(new Error(ERROR_UNDEFINED_HK_COMMON_CODE));
});
it("美交所统一代码转换股票代码", async () => {
expect(() => (new BaseCommonCodeTransform()).USTransform("000000"))
.toThrow(new Error("请检查统一代码是否正确"));
expect(() => (new BaseCommonCodeTransform()).USTransform("US000000"))
.toThrow(new Error("未实现美交所统一代码转换股票代码"));
.toThrow(new Error(ERROR_UNDEFINED_US_COMMON_CODE));
});
it("交易所统一代码转换股票代码", async () => {
expect(() => (new BaseCommonCodeTransform()).transform("000000"))
.toThrow(new Error("请检查统一代码是否正确"));
.toThrow(new Error(ERROR_COMMON_CODE));
expect(() => (new BaseCommonCodeTransform()).transform("SZ000000"))
.toThrow(new Error("未实现深交所统一代码转换股票代码"));
.toThrow(new Error(ERROR_UNDEFINED_SZ_COMMON_CODE));
expect(() => (new BaseCommonCodeTransform()).transform("SH000000"))
.toThrow(new Error("未实现上交所统一代码转换股票代码"));
.toThrow(new Error(ERROR_UNDEFINED_SH_COMMON_CODE));
expect(() => (new BaseCommonCodeTransform()).transform("HK000000"))
.toThrow(new Error("未实现港交所统一代码转换股票代码"));
.toThrow(new Error(ERROR_UNDEFINED_HK_COMMON_CODE));
expect(() => (new BaseCommonCodeTransform()).transform("US000000"))
.toThrow(new Error("未实现美交所统一代码转换股票代码"));
.toThrow(new Error(ERROR_UNDEFINED_US_COMMON_CODE));
});
it("交易所统一代码组转换股票代码组", async () => {
expect(() => (new BaseCommonCodeTransform()).transforms(["000000"]))
.toThrow(new Error("请检查统一代码是否正确"));
.toThrow(new Error(ERROR_COMMON_CODE));
expect(() => (new BaseCommonCodeTransform()).transforms(["SZ000000"]))
.toThrow(new Error("未实现深交所统一代码转换股票代码"));
.toThrow(new Error(ERROR_UNDEFINED_SZ_COMMON_CODE));
expect(() => (new BaseCommonCodeTransform()).transforms(["SH000000"]))
.toThrow(new Error("未实现上交所统一代码转换股票代码"));
expect(() => (new BaseCommonCodeTransform()).transforms(["HK000000"]))
.toThrow(new Error("未实现港交所统一代码转换股票代码"));
.toThrow(new Error(ERROR_UNDEFINED_HK_COMMON_CODE));
expect(() => (new BaseCommonCodeTransform()).transforms(["US000000"]))
.toThrow(new Error("未实现美交所统一代码转换股票代码"));
.toThrow(new Error(ERROR_UNDEFINED_US_COMMON_CODE));
});
});

View File

@ -2,43 +2,54 @@
const BaseStockTransform = require("stocks/base/transforms/stock").default;
describe("【基础】股票数据转换测试", () => {
const {
ERROR_UNDEFINED_GET_CODE,
ERROR_UNDEFINED_GET_NAME,
ERROR_UNDEFINED_GET_NOW,
ERROR_UNDEFINED_GET_LOW,
ERROR_UNDEFINED_GET_HIGH,
ERROR_UNDEFINED_GET_STOCK,
ERROR_UNDEFINED_GET_PERCENT,
ERROR_UNDEFINED_GET_YESTERDAY,
} = require("utils/constant");
it("获取股票代码", async () => {
expect(() => (new BaseStockTransform()).getCode())
.toThrow(new Error("未实现获取代码"));
.toThrow(new Error(ERROR_UNDEFINED_GET_CODE));
});
it("获取股票名称", async () => {
expect(() => (new BaseStockTransform()).getName())
.toThrow(new Error("未实现获取名称"));
.toThrow(new Error(ERROR_UNDEFINED_GET_NAME));
});
it("获取股票现价", async () => {
expect(() => (new BaseStockTransform()).getNow())
.toThrow(new Error("未实现获取现价"));
.toThrow(new Error(ERROR_UNDEFINED_GET_NOW));
});
it("获取股票最低价", async () => {
expect(() => (new BaseStockTransform()).getLow())
.toThrow(new Error("未实现获取最低价"));
.toThrow(new Error(ERROR_UNDEFINED_GET_LOW));
});
it("获取股票最高价", async () => {
expect(() => (new BaseStockTransform()).getHigh())
.toThrow(new Error("未实现获取最高价"));
.toThrow(new Error(ERROR_UNDEFINED_GET_HIGH));
});
it("获取股票昨日收盘价", async () => {
expect(() => (new BaseStockTransform()).getYesterday())
.toThrow(new Error("未实现获取昨日收盘价"));
.toThrow(new Error(ERROR_UNDEFINED_GET_YESTERDAY));
});
it("获取股票涨跌", async () => {
expect(() => (new BaseStockTransform()).getPercent())
.toThrow(new Error("未实现获取涨跌"));
.toThrow(new Error(ERROR_UNDEFINED_GET_PERCENT));
});
it("获取股票数据", async () => {
expect(() => (new BaseStockTransform()).getStock())
.toThrow();
.toThrow(new Error(ERROR_UNDEFINED_GET_STOCK));
});
});