Files
zszq-trs/YLErpUnitTest/Modules/VolatilityModule/VolatilityQueryServiceTest.cs
T
2024-05-09 14:06:26 +08:00

143 lines
4.6 KiB
C#

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using YLErp.DBModels;
namespace YLErp.Modules.VolatilityModule
{
[TestClass]
public class VolatilityQueryServiceTest : YLUnitTestBase
{
readonly VolatilityQueryService service;
public VolatilityQueryServiceTest()
{
service = new VolatilityQueryService(new OptUserInfo(0, "UnitTest"));
}
[TestMethod("获取单个标的的曲面波动率")]
public void TestGetVolatility()
{
var date = new DateTime(2020, 4, 20);
//标的不存在的情况下获取不到波动率
var vols = service.GetVolatility(new SingleVolatilityRequest
{
QuotationDate = date,
TradeVolWithBidAsk = true,
UnderlyingId = 1,
UnderlyingCode = "TA006",
UserGroup = "",
VolType = "交易"
});
Assert.AreEqual(vols.Count(), 0);
//同源标的不存在波动率的情况下获取默认波动率
var vols2 = service.GetVolatility(new SingleVolatilityRequest
{
QuotationDate = date,
TradeVolWithBidAsk = true,
UnderlyingCode = "AP005",
UserGroup = "",
VolType = "交易"
});
Assert.AreEqual(vols2.Count(), 3);
Assert.AreEqual(vols2.First().VolTable[0].Vol, 0.3);
Assert.AreEqual(vols2.First().QuotationDate, date);
var vols3 = service.GetVolatility(new SingleVolatilityRequest
{
QuotationDate = date,
TradeVolWithBidAsk = true,
UnderlyingId = 14,
//UnderlyingCode = "AP005",
UserGroup = "",
VolType = "交易"
});
Assert.AreEqual(vols3.Count(), 3);
Assert.AreEqual(vols3.First().VolTable[0].Vol, 0.3);
Assert.AreEqual(vols3.First().QuotationDate, date);
//标的已过期的情况下返回波动率为0的默认波动率
var vols4 = service.GetVolatility(new SingleVolatilityRequest
{
QuotationDate = date,
TradeVolWithBidAsk = true,
UnderlyingCode = "RB2003",
UserGroup = "",
VolType = "交易"
});
Assert.AreEqual(vols4.Count(), 3);
Assert.AreEqual(vols4.First().VolTable[0].Vol, 0);
Assert.AreEqual(vols4.First().QuotationDate, date);
}
[TestMethod("获取单个标的的曲面波动率2")]
public void TestGetVolatility2()
{
var date = DateTime.Today;
var vols = service.GetVolatility(new SingleVolatilityRequest
{
QuotationDate = date,
TradeVolWithBidAsk = true,
UnderlyingCode = "AL00",
VolType = "交易"
});
Assert.AreEqual(vols.Count(), 0);
}
[TestMethod("获取批量标的的曲面波动率")]
public void TestGetVolatilities()
{
var vols = service.GetVolatilities(new BatchVolatilityRequest
{
QuotationDate = DateTime.Today,
TradeVolWithBidAsk = true,
UserGroup = string.Empty,
VolType = "交易"
}, true);
}
[TestMethod("验证波动率复制")]
public void TestMissingVolatilities()
{
var un = GetUnderlyingManager();
AddClearSQL<volatility>($"{nameof(volatility.ContractCode)}='{un.UnderlyingCode}'");
var vols = service.GetVolatility(new SingleVolatilityRequest
{
QuotationDate = DateTime.Today,
TradeVolWithBidAsk = false,
UserGroup = string.Empty,
VolType = "交易",
UnderlyingCode = un.UnderlyingCode,
UnderlyingId = un.id
}, true);
Assert.IsTrue(vols.Count() == 1 && vols.First().VolTable[0].Vol == 0.3);
var vols2 = service.GetVolatility(new SingleVolatilityRequest
{
QuotationDate = DateTime.Today,
TradeVolWithBidAsk = true,
UserGroup = string.Empty,
VolType = "交易",
UnderlyingCode = un.UnderlyingCode,
UnderlyingId = un.id
}, true);
Assert.IsTrue(vols2.Count() == 3 && vols2.First().VolTable[0].Vol == 0.3);
}
}
}