Ejemplo n.º 1
0
        public void FileFormatValidator_WarnShorthandConflictsTo()
        {
            var src = new[]
            {
                new FileFormatModel
                {
                    ObservationDate = new DateTime(2010, 01, 05),
                    Shorthand       = "Q2_15",
                    From            = new DateTime(2015, 04, 01),
                    To    = new DateTime(2015, 06, 29),
                    Price = 0.598m
                }
            };

            var test = new FileFormatValidator(src);
            var rez  = test.ValidateAndFix();

            test.ValidationWarnings.Length.ShouldBe(1);
            test.ValidationErrors.ShouldBeNull();
            rez.Length.ShouldBe(1);
            rez[0].ObservationDate.ShouldBe(src[0].ObservationDate.GetValueOrDefault());
            rez[0].Shorthand.ShouldBe(src[0].Shorthand);
            rez[0].From.ShouldBe(src[0].From.GetValueOrDefault());
            rez[0].To.ShouldBe(src[0].To.GetValueOrDefault());
            rez[0].Price.ShouldBe(src[0].Price.GetValueOrDefault());
        }
Ejemplo n.º 2
0
        public void FileFormatValidator_WarnFixFromAndTo()
        {
            var src = new[]
            {
                new FileFormatModel
                {
                    ObservationDate = new DateTime(2010, 01, 05),
                    Shorthand       = "Q3_13",
                    From            = null,
                    To    = null,
                    Price = 0.575m
                }
            };

            var test = new FileFormatValidator(src);
            var rez  = test.ValidateAndFix();

            test.ValidationWarnings.Length.ShouldBe(2);
            test.ValidationErrors.ShouldBeNull();
            rez.Length.ShouldBe(1);
            rez[0].ObservationDate.ShouldBe(src[0].ObservationDate.GetValueOrDefault());
            rez[0].Shorthand.ShouldBe(src[0].Shorthand);
            rez[0].From.ShouldBe(new DateTime(2013, 07, 01));
            rez[0].To.ShouldBe(new DateTime(2013, 09, 30));
            rez[0].Price.ShouldBe(src[0].Price.GetValueOrDefault());
        }
Ejemplo n.º 3
0
        public void FileFormatValidator_WarnSuspiciousObservationDate()
        {
            var src = new[]
            {
                new FileFormatModel
                {
                    ObservationDate = new DateTime(1900, 01, 14),
                    Shorthand       = "Q3_14",
                    From            = new DateTime(2014, 07, 01),
                    To    = new DateTime(2014, 09, 30),
                    Price = 0.575m
                }
            };

            var test = new FileFormatValidator(src);
            var rez  = test.ValidateAndFix();

            test.ValidationWarnings.Length.ShouldBe(1);
            test.ValidationErrors.ShouldBeNull();
            rez.Length.ShouldBe(1);
            rez[0].ObservationDate.ShouldBe(src[0].ObservationDate.GetValueOrDefault());
            rez[0].Shorthand.ShouldBe(src[0].Shorthand);
            rez[0].From.ShouldBe(src[0].From.GetValueOrDefault());
            rez[0].To.ShouldBe(src[0].To.GetValueOrDefault());
            rez[0].Price.ShouldBe(src[0].Price.GetValueOrDefault());
        }
Ejemplo n.º 4
0
        public void FileFormatValidator_Correct()
        {
            var src = new[]
            {
                new FileFormatModel
                {
                    ObservationDate = new DateTime(2009, 01, 02),
                    Shorthand       = "Q2_09",
                    From            = new DateTime(2009, 04, 01),
                    To    = new DateTime(2009, 06, 30),
                    Price = 0.46925m
                }
            };

            var test = new FileFormatValidator(src);
            var rez  = test.ValidateAndFix();

            test.ValidationWarnings.ShouldBeNull();
            test.ValidationErrors.ShouldBeNull();
            rez.Length.ShouldBe(1);
            rez[0].ObservationDate.ShouldBe(src[0].ObservationDate.GetValueOrDefault());
            rez[0].Shorthand.ShouldBe(src[0].Shorthand);
            rez[0].From.ShouldBe(src[0].From.GetValueOrDefault());
            rez[0].To.ShouldBe(src[0].To.GetValueOrDefault());
            rez[0].Price.ShouldBe(src[0].Price.GetValueOrDefault());
        }
Ejemplo n.º 5
0
        public void FileFormatValidator_ErrNoObservationDate()
        {
            var src = new[]
            {
                new FileFormatModel
                {
                    ObservationDate = null,
                    Shorthand       = "Q1_13",
                    From            = new DateTime(2013, 01, 01),
                    To    = new DateTime(2013, 03, 31),
                    Price = 0.685m
                }
            };

            var test = new FileFormatValidator(src);
            var rez  = test.ValidateAndFix();

            rez.Length.ShouldBe(0);
            test.ValidationWarnings.ShouldBeNull();
            test.ValidationErrors.Length.ShouldBe(1);
        }
Ejemplo n.º 6
0
        public void FileFormatValidator_ErrWrongShorthand()
        {
            var src = new[]
            {
                new FileFormatModel
                {
                    ObservationDate = new DateTime(2009, 02, 25),
                    Shorthand       = null,
                    From            = new DateTime(2014, 04, 01),
                    To    = new DateTime(2014, 06, 30),
                    Price = 0.578m
                }
            };

            var test = new FileFormatValidator(src);
            var rez  = test.ValidateAndFix();

            rez.Length.ShouldBe(0);
            test.ValidationWarnings.ShouldBeNull();
            test.ValidationErrors.Length.ShouldBe(1);
        }
Ejemplo n.º 7
0
        public void FileFormatValidator_ErrorIsPriority()
        {
            var src = new[]
            {
                new FileFormatModel
                {
                    ObservationDate = new DateTime(2010, 01, 05),
                    Shorthand       = "Q3_13",
                    From            = null,
                    To    = null,
                    Price = 0m
                }
            };

            var test = new FileFormatValidator(src);
            var rez  = test.ValidateAndFix();

            test.ValidationWarnings.ShouldBeNull();
            test.ValidationErrors.Length.ShouldBe(1);
            rez.Length.ShouldBe(0);
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            if (args.Length != 2 || args [0] == ParameterHelpOption)
            {
                Console.WriteLine(@"Run the app with parameters:
PivotQuotes.exe <src> <dst>
Where:
src - source csv file name (tabular)
dst - destination file name (grid)

Press ENTER to finish.");
                Console.ReadLine();
                return;
            }

            // change locale format settings. Works starting from .Net 4.5
            // be careful when copying this to ASP.NET, thought

            var ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();

            ci.DateTimeFormat.DateSeparator         = "/";
            ci.NumberFormat.NumberDecimalSeparator  = ".";
            CultureInfo.DefaultThreadCurrentCulture = ci;

            // read source data

            Console.WriteLine("Reading source file...");

            var csvEngine = new FileHelperEngine <FileFormatModel>();

            csvEngine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;
            csvEngine.AfterReadRecord       += CsvEngine_AfterReadRecord;

            FileFormatModel[] srcData;
            try
            {
                srcData = csvEngine.ReadFile(args[0]);
            }
            catch (Exception ex)
            {
                Console.WriteLine($@"There was a problem opening source file. {ex.Message}

Press ENTER to finish.");
                Console.ReadLine();
                return;
            }

            // show data read errors (lines were skipped)

            if (csvEngine.ErrorManager.HasErrors)
            {
                Console.WriteLine("Following lines with errors were skipped:");
                foreach (var error in csvEngine.ErrorManager.Errors)
                {
                    Console.WriteLine($"Line {error.LineNumber}, {error.ExceptionInfo.Message}");
                }

                Console.Write("Do you want to continue with wrong data skipped? [y/N]:");
                if (!WaitForContinueConfirmation())
                {
                    return;
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            Console.WriteLine("Validating source data...");

            // some additional validation

            var validator = new FileFormatValidator(srcData);
            var fixedData = validator.ValidateAndFix();

            if (validator.ValidationErrors != null)
            {
                Console.WriteLine("There are unrecoverable validation errors:");
                foreach (var error in validator.ValidationErrors)
                {
                    Console.WriteLine(error);
                }
                Console.Write(@"Please fix the data and run the tool again.

Do you want to continue with wrong data skipped? [y/N]:");
                if (!WaitForContinueConfirmation())
                {
                    return;
                }
            }

            Console.WriteLine();
            Console.WriteLine();

            if (validator.ValidationWarnings != null)
            {
                Console.WriteLine("FYI: there were some data fixed by program, please review:");
                foreach (var warning in validator.ValidationWarnings)
                {
                    Console.WriteLine(warning);
                }
                Console.WriteLine(@"If something is wrongly fixed, please fix the data manually and run the tool again.");
            }

            // pivot creation

            Console.WriteLine();
            Console.WriteLine("Creating pivot grid...");

            // sort pivot headers in correct order ('Q3_07' is _before_ 'Q1_10')
            var pivotHeaders = fixedData
                               .Select(x => x.Shorthand)
                               .Distinct()
                               .Select(x => new ShorthandWrapper(x))
                               .OrderBy(x => x.Year)
                               .ThenBy(x => x.Quarter)
                               .ToArray();

            var pivotLines = fixedData
                             .GroupBy(x => x.ObservationDate.GetValueOrDefault())
                             .OrderBy(grp => grp.Key);

            using (var file = new StreamWriter(args[1]))
            {
                // write headers
                foreach (var header in pivotHeaders)
                {
                    file.Write(",");
                    file.Write(header.SrcValue);
                }
                file.WriteLine();

                // write lines
                foreach (var line in pivotLines)
                {
                    var dateStr = line.Key.ToString("dd/MM/yyyy");
                    file.Write($"{dateStr},");

                    // write cells
                    foreach (var header in pivotHeaders)
                    {
                        var cellvalue = line.Where(x => x.Shorthand == header.SrcValue).ToArray();
                        if (cellvalue.Length > 0)
                        {
                            file.Write(cellvalue[0].Price.GetValueOrDefault());
                        }
                        if (cellvalue.Length > 1)
                        {
                            Console.WriteLine($"Found more than one price value for ObservationDate={dateStr} and Shorthand={header.SrcValue}");
                        }
                        file.Write(",");
                    }

                    file.WriteLine();
                }
            }

            Console.WriteLine($@"Resulting data written to {args[1]}.

Press ENTER to finish.");
            Console.ReadLine();
        }