public void CreateTest()
        {
            var ticker        = "1234";
            var latestQuarter = FiscalQuarterPeriod.Create(2020, 2);

            // latest
            Assert.AreEqual(TickerDayParameter.Create(ticker, LatestDayPeriod.GetInstance()), TickerPeriodParameterCreator.Create(ticker, "latest", null));

            // day
            Assert.AreEqual(TickerDayParameter.Create(ticker,
                                                      DayPeriod.Create(2020, 1, 1)), TickerPeriodParameterCreator.Create(ticker, "2020-01-01", null));


            // LYLQ
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, RelativeFiscalQuarterPeriod.Create(0, 0)), TickerPeriodParameterCreator.Create(ticker, "LYLQ", null));

            Assert.AreEqual(TickerQuarterParameter.Create(ticker, RelativeFiscalQuarterPeriod.Create(1, 0)), TickerPeriodParameterCreator.Create(ticker, "LY-1LQ", null));
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, RelativeFiscalQuarterPeriod.Create(0, 2)), TickerPeriodParameterCreator.Create(ticker, "LYLQ-2", null));
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, RelativeFiscalQuarterPeriod.Create(1, 2)), TickerPeriodParameterCreator.Create(ticker, "LY-1LQ-2", null));

            // Quarter
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, FiscalQuarterPeriod.Create(2020, 1)), TickerPeriodParameterCreator.Create(ticker, "2020Q1", null));
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, "2019", "LQ-1", FiscalQuarterPeriod.Create(2019, 1)), TickerPeriodParameterCreator.Create(ticker, "2019LQ-1", latestQuarter));
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, "2019", "LQ-2", FiscalQuarterPeriod.Create(2018, 4)), TickerPeriodParameterCreator.Create(ticker, "2019LQ-2", latestQuarter));
            Assert.AreEqual(TickerQuarterParameter.Create(ticker, "LY-2", "4", FiscalQuarterPeriod.Create(2018, 4)), TickerPeriodParameterCreator.Create(ticker, "LY-2Q4", latestQuarter));

            // others
            Assert.ThrowsException <ValidationError>(() => TickerPeriodParameterCreator.Create(ticker, "dummy", null));
        }
        public void ToApiV3ParametersTest()
        {
            var ticker     = "1234";
            var parameter1 = TickerDayParameter.Create(ticker, DayPeriod.Create(2021, 1, 1)).ToApiV3Parameters();

            Assert.AreEqual("2021-01-01", parameter1["date"]);

            var parameter2 = TickerDayParameter.Create(ticker, LatestDayPeriod.GetInstance()).ToApiV3Parameters();

            Assert.AreEqual("latest", parameter2["date"]);
        }
        public void CreateTest()
        {
            var ticker = "1234";
            var period = DayPeriod.Create(2021, 1, 1);

            Assert.AreEqual(period, TickerDayParameter.Create(ticker, period).GetPeriod());
            Assert.AreEqual(LatestDayPeriod.GetInstance(), TickerDayParameter.Create(ticker, "latest").GetPeriod());
            Assert.AreEqual(period, TickerDayParameter.Create(ticker, "2021-01-01").GetPeriod());

            // validation error
            Assert.ThrowsException <ValidationError>(() => TickerDayParameter.Create("dummy", period));
        }
示例#4
0
 public static ITickerPeriodParameter Create(string ticker, string periodParam, FiscalQuarterPeriod latestFiscalQuarterPeriod)
 {
     if (periodParam.Equals("latest"))
     {
         return(TickerDayParameter.Create(ticker, LatestDayPeriod.GetInstance()));
     }
     else if (PeriodRegularExpressionConfig.DayRegex.IsMatch(periodParam))
     {
         return(TickerDayParameter.Create(ticker, DayPeriod.Parse(periodParam)));
     }
     else if (PeriodRegularExpressionConfig.FiscalQuarterRegex.IsMatch(periodParam))
     {
         var period = FiscalQuarterPeriod.Parse(periodParam);
         return(TickerQuarterParameter.Create(ticker, period));
     }
     else if (PeriodRegularExpressionConfig.RelativeFiscalQuarterRegex.IsMatch(periodParam))
     {
         var period = RelativeFiscalQuarterPeriod.Parse(periodParam);
         return(TickerQuarterParameter.Create(ticker, period));
     }
     else if (PeriodRegularExpressionConfig.BCodeUdfFiscalQuarterInputRegex.IsMatch(periodParam))
     {
         var match = PeriodRegularExpressionConfig.BCodeUdfFiscalQuarterInputRegex
                     .Match(periodParam);
         var fy = match.Groups["fiscalYear"].Value.Trim();
         var fq = match.Groups["fiscalQuarter"].Value.Trim();
         if (fy.Contains("LY"))
         {
             var prevYears = RelativeFiscalQuarterPeriod.ParseRelativeValue("years", match);
             if (prevYears > latestFiscalQuarterPeriod.Year)
             {
                 throw new ValidationError($"{prevYears} is bigger than {latestFiscalQuarterPeriod.Year}");
             }
             var fiscalQuarter = fq.Replace("Q", "");
             var period        = FiscalQuarterPeriod.Create(latestFiscalQuarterPeriod.Year - prevYears, uint.Parse(fiscalQuarter));
             return(TickerQuarterParameter.Create(ticker, fy, fiscalQuarter, period));
         }
         else if (fq.Contains("LQ"))
         {
             var prevQuarters = RelativeFiscalQuarterPeriod.ParseRelativeValue("quarters", match);
             var period       = FiscalQuarterPeriod.Create(uint.Parse(fy), latestFiscalQuarterPeriod.Quarter).Before(0, prevQuarters);
             return(TickerQuarterParameter.Create(ticker, fy, fq, period));
         }
         else
         {
             throw new ValidationError($"{periodParam} is not supported input format");
         }
     }
     else
     {
         throw new ValidationError($"{periodParam} is not supported input format");
     }
 }
        public void ResolveTest()
        {
            var quarterDict = new SupportedTierDictionary <FiscalQuarterPeriod>();
            var dayDict     = new SupportedTierDictionary <DayPeriod>();

            quarterDict.Add(company.Ticker, company.SupportedQuarterRanges);
            dayDict.Add(company.Ticker, company.SupportedDailyRanges);
            var resolver = new PeriodSupportedTierResolver(null, null, quarterDict, dayDict);

            // snapshot
            Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(ticker, Snapshot.GetInstance(), false, false));

            // relative quarter periods
            Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(ticker, RelativeFiscalQuarterPeriod.CreateLatest(), false, false));
            Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(ticker, RelativeFiscalQuarterPeriod.Create(5, 1), false, false));
            Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(ticker, RelativeFiscalQuarterPeriod.Create(5, 2), false, false));
            Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(ticker, RelativeFiscalQuarterPeriod.Create(5, 3), false, false));
            Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(ticker, RelativeFiscalQuarterPeriod.Create(11, 2), false, false));
            Assert.AreEqual(SupportedTier.None, resolver.Resolve(ticker, RelativeFiscalQuarterPeriod.Create(11, 3), false, false));
            Assert.AreEqual(SupportedTier.None, resolver.Resolve(ticker, RelativeFiscalQuarterPeriod.Create(11, 4), false, false));

            // latest day
            Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(ticker, LatestDayPeriod.GetInstance(), false, false));


            // quarter
            Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(ticker, fixedOldestQuarter, false, false));
            Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(ticker, fixedOldestQuarter.Next() as FiscalQuarterPeriod, false, false));
            Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(ticker, fixedLatestQuarter, false, false));
            Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(ticker, ondemandOldestQuarter, false, false));
            Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(ticker, ondemandOldestQuarter.Next() as FiscalQuarterPeriod, false, false));
            Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(ticker, ondemandLatestQuarter, false, false));
            Assert.AreEqual(SupportedTier.None, resolver.Resolve(ticker, ondemandLatestQuarter.Next() as FiscalQuarterPeriod, false, false));

            // day
            Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(ticker, ondemandLatestDay, false, false));
            Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(ticker, fixedTierLatestDay, false, false));
            Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(ticker, fixedTierOldestDay, false, false));
            Assert.AreEqual(SupportedTier.FixedTier, resolver.Resolve(ticker, fixedTierOldestDay.Next(), false, false));
            Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(ticker, fixedTierOldestDay.Prev(), false, false));
            Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(ticker, ondemandOldestDay, false, false));
            Assert.AreEqual(SupportedTier.OndemandTier, resolver.Resolve(ticker, ondemandOldestDay.Next(), false, false));
            Assert.AreEqual(SupportedTier.None, resolver.Resolve(ticker, ondemandOldestDay.Prev(), false, false));
            Assert.AreEqual(SupportedTier.None, resolver.Resolve(ticker, fixedTierLatestDay.Next(), false, false));
            Assert.AreEqual(SupportedTier.None, resolver.Resolve(ticker, ondemandLatestDay.Next(), false, false));
        }
        public void GetHashCodeTest()
        {
            var a = TickerDayParameter.Create("1234", "2021-01-01").GetHashCode();
            var b = TickerDayParameter.Create("1234", DayPeriod.Create(2021, 1, 1)).GetHashCode();
            var c = TickerDayParameter.Create("2345", DayPeriod.Create(2021, 1, 1)).GetHashCode();
            var d = TickerDayParameter.Create("1234", DayPeriod.Create(2021, 1, 2)).GetHashCode();
            var f = TickerDayParameter.Create("1234", LatestDayPeriod.GetInstance()).GetHashCode();
            var g = TickerDayParameter.Create("1234", LatestDayPeriod.GetInstance()).GetHashCode();
            var h = TickerDayParameter.Create("2345", LatestDayPeriod.GetInstance()).GetHashCode();

            Assert.AreEqual(a, b);
            Assert.AreNotEqual(a, c);
            Assert.AreNotEqual(a, d);
            Assert.AreNotEqual(a, f);
            Assert.AreEqual(f, g);
            Assert.AreNotEqual(f, h);
        }
        public ITickerPeriodParameter Build()
        {
            switch (dataType)
            {
            case DataTypeConfig.Indicator:
                return(TickerEmptyPeriodParameter.Create(ticker, Snapshot.GetInstance()));

            case DataTypeConfig.Quarter:
                if (string.IsNullOrWhiteSpace(parameter1) && string.IsNullOrWhiteSpace(parameter2))
                {
                    return(TickerQuarterParameter.Create(ticker, RelativeFiscalQuarterPeriod.CreateLatest()));
                }
                else
                {
                    return(TickerQuarterParameter.Create(ticker, FiscalQuarterPeriod.Create(parameter1, parameter2)));
                }

            case DataTypeConfig.Daily:
                return(TickerDayParameter.Create(ticker, LatestDayPeriod.GetInstance()));

            default:
                throw new NotSupportedDataTypeException($"dataType ={dataType} is not supported.");
            }
        }
 public static TickerDayParameter Create(string ticker, string dateParam)
 {
     JpTickerValidator.Validate(ticker);
     if (dateParam.Equals(ApiRequestParamConfig.ValueLatest))
     {
         return(new TickerDayParameter(ticker, ApiRequestParamConfig.ValueLatest, LatestDayPeriod.GetInstance()));
     }
     else if (PeriodRegularExpressionConfig.DayRegex.IsMatch(dateParam))
     {
         return(new TickerDayParameter(ticker, dateParam, DayPeriod.Parse(dateParam)));
     }
     else
     {
         throw new ValidationError($"input {dateParam} is not supported format");
     }
 }
        public void CreateGetDailyRequestTest()
        {
            // use ondemand
            var ticker    = "6501";
            var parameter = TickerDayParameter.Create(ticker, DayPeriod.Create(2021, 1, 1));
            var request   = BuffettCodeApiV3RequestCreator.CreateGetDailyRequest(parameter, true);

            Assert.AreEqual(BuffettCodeApiV3Config.ENDPOINT_ONDEMAND_DAILY, request.EndPoint);
            Assert.AreEqual(ticker, request.Parameters["ticker"]);
            Assert.AreEqual("2021-01-01", request.Parameters["date"]);

            // not use ondemand
            request = BuffettCodeApiV3RequestCreator.CreateGetDailyRequest(parameter, false);
            Assert.AreEqual(BuffettCodeApiV3Config.ENDPOINT_DAILY, request.EndPoint);
            Assert.AreEqual(ticker, request.Parameters["ticker"]);
            Assert.AreEqual("2021-01-01", request.Parameters["date"]);

            // latest case
            request = BuffettCodeApiV3RequestCreator.CreateGetDailyRequest(TickerDayParameter.Create(ticker, LatestDayPeriod.GetInstance()), false);
            Assert.AreEqual(BuffettCodeApiV3Config.ENDPOINT_DAILY, request.EndPoint);
            Assert.AreEqual(ticker, request.Parameters["ticker"]);
            Assert.AreEqual("latest", request.Parameters["date"]);
        }
        public void FindAvailableTierTest()
        {
            var tierResolver = CreatTierResolver();
            var helper       = new ApiTaskHelper(tierResolver);

            // enable ondemand endpoint
            // quarter
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedOldestQuarter, true, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedOldestQuarter.Next() as FiscalQuarterPeriod, true, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedLatestQuarter, true, true, true));
            Assert.AreEqual(SupportedTier.OndemandTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandOldestQuarter, true, true, true));
            Assert.AreEqual(SupportedTier.OndemandTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandOldestQuarter.Next() as FiscalQuarterPeriod, true, true, true));
            Assert.AreEqual(SupportedTier.OndemandTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandLatestQuarter, true, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, RelativeFiscalQuarterPeriod.CreateLatest(), true, true, true));

            Assert.ThrowsException <NotSupportedTierException>(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandLatestQuarter.Next() as FiscalQuarterPeriod, true, true, true));

            // daily
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Daily, ticker, fixedTierOldestDay, true, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Daily, ticker, fixedTierOldestDay.Next() as DayPeriod, true, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Daily, ticker, fixedTierLatestDay, true, true, true));
            Assert.AreEqual(SupportedTier.OndemandTier, helper.FindAvailableTier(DataTypeConfig.Daily, ticker, ondemandOldestDay, true, true, true));
            Assert.AreEqual(SupportedTier.OndemandTier, helper.FindAvailableTier(DataTypeConfig.Daily, ticker, ondemandOldestDay.Next() as DayPeriod, true, true, true));
            Assert.AreEqual(SupportedTier.OndemandTier, helper.FindAvailableTier(DataTypeConfig.Daily, ticker, ondemandLatestDay, true, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Daily, ticker, LatestDayPeriod.GetInstance(), true, true, true));
            Assert.ThrowsException <NotSupportedTierException>(() => helper.FindAvailableTier(DataTypeConfig.Daily, ticker, ondemandLatestDay.Next() as DayPeriod, true, true, true));


            // disabled ondemand endpoint
            // quarter
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedOldestQuarter, false, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedOldestQuarter.Next() as FiscalQuarterPeriod, false, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, fixedLatestQuarter, false, true, true));
            Assert.ThrowsException <NotSupportedTierException>(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandOldestQuarter, false, true, true));
            Assert.ThrowsException <NotSupportedTierException>(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandOldestQuarter.Next() as FiscalQuarterPeriod, false, true, true));
            Assert.ThrowsException <NotSupportedTierException>(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandLatestQuarter, false, true, true));
            Assert.ThrowsException <NotSupportedTierException>(() => helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, ondemandLatestQuarter.Next() as FiscalQuarterPeriod, false, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Quarter, ticker, RelativeFiscalQuarterPeriod.CreateLatest(), false, true, true));

            // daily
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Daily, ticker, fixedTierOldestDay, false, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Daily, ticker, fixedTierOldestDay.Next() as DayPeriod, false, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Daily, ticker, fixedTierLatestDay, false, true, true));
            Assert.ThrowsException <NotSupportedTierException>(() => helper.FindAvailableTier(DataTypeConfig.Daily, ticker, ondemandLatestDay, false, true, true));
            Assert.ThrowsException <NotSupportedTierException>(() => helper.FindAvailableTier(DataTypeConfig.Daily, ticker, ondemandLatestDay.Next() as DayPeriod, false, true, true));
            Assert.ThrowsException <NotSupportedTierException>(() => helper.FindAvailableTier(DataTypeConfig.Daily, ticker, ondemandLatestDay, false, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Daily, ticker, LatestDayPeriod.GetInstance(), false, true, true));
            Assert.ThrowsException <NotSupportedTierException>(() => helper.FindAvailableTier(DataTypeConfig.Daily, ticker, ondemandLatestDay.Next() as DayPeriod, false, true, true));

            // company is always fixed tier
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Company, ticker, Snapshot.GetInstance(), true, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Company, ticker, Snapshot.GetInstance(), false, true, true));

            // indicator is always fixed tier
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Indicator, ticker, Snapshot.GetInstance(), true, true, true));
            Assert.AreEqual(SupportedTier.FixedTier, helper.FindAvailableTier(DataTypeConfig.Indicator, ticker, Snapshot.GetInstance(), false, true, true));
        }