Exemplo n.º 1
0
        public int ImportCsv(string csvFile, CsvConfig csvCfg)
        {
            string name = Path.GetFileNameWithoutExtension(csvFile);
            _Command.CommandText = string.Format("drop table if exists {0}", name);
            _Command.ExecuteNonQuery();

            int cnt = 0;
            using (StreamReader reader = new StreamReader(csvFile))
            {
                string line = reader.ReadLine();
                if (string.IsNullOrWhiteSpace(line))
                {
                    return -1;
                }
                string[] header = line.Split(csvCfg.Seperator);
                StringBuilder create = new StringBuilder();
                StringBuilder insert = new StringBuilder();
                create.Append(string.Format("CREATE TABLE {0} (", name));
                insert.Append(string.Format("INSERT INTO {0} (", name));
                for (int i = 0; i < header.Length; i += 1)
                {
                    line = (header[i] ?? "").Trim();
                    if (!Regex.IsMatch(line, "^[\\w]+$"))
                    {
                        return -1;
                    }
                    create.Append(string.Format("{0} VARCHAR(8),", line));
                    insert.Append(string.Format("{0},", line));
                }
                create.Remove(create.Length - 1, 1).Append(')');
                _Command.CommandText = create.ToString();
                _Command.ExecuteNonQuery();
                create.Clear();

                insert.Remove(insert.Length - 1, 1).Append(") VALUES ");
                string sql = insert.ToString();
                insert.Clear();

                IDbTransaction trans = _Connect.BeginTransaction();
                string[] detail;
                int idx;
                int max;
                line = reader.ReadLine();
                while (line != null)
                {
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        line = reader.ReadLine();
                        continue;
                    }
                    idx = 0;

                    insert.Append(sql).Append('(');
                    detail = line.Split(csvCfg.Seperator);
                    max = detail.Length <= header.Length ? detail.Length : header.Length;
                    while (idx < max)
                    {
                        insert.Append(string.Format("'{0}',", detail[idx]));
                        idx += 1;
                    }
                    while (idx < header.Length)
                    {
                        insert.Append("null,");
                        idx += 1;
                    }
                    insert.Remove(insert.Length - 1, 1).Append(')');
                    _Command.CommandText = insert.ToString();
                    _Command.ExecuteNonQuery();
                    insert.Clear();
                    cnt += 1;

                    line = reader.ReadLine();
                }
                trans.Commit();

                //Main.ShowAlert(string.Format("成功导入 {0} 条记录!", cnt));
            }
            return cnt;
        }
Exemplo n.º 2
0
        public void TestBackup()
        {
            var directoryInfo = new DirectoryInfo("TestCsv");
            var directoryPath = directoryInfo.FullName;

            try {
                directoryInfo.Refresh();
                if (directoryInfo.Exists)
                {
                    directoryInfo.Delete(recursive: true);
                    directoryInfo.Refresh();
                }

                directoryInfo.Create();
                directoryInfo.Refresh();
                var activeDirectory   = directoryInfo.CreateSubdirectory("Active");
                var backupDirectory   = directoryInfo.CreateSubdirectory("Backup");
                var backupPeriodicity = 2;
                var backupCopies      = 3;
                var csvConfig         = new CsvConfig(activeDirectory.FullName, backupDirectory.FullName, backupPeriodicity, backupCopies);
                var now    = new DateTime(2000, 12, 11);
                var result = Csv.Backup(csvConfig, now);
                Assert.AreEqual(@$ "0 files copied from {directoryPath}\Active 
to {directoryPath}\Backup\csv20001211.", result);

                now    = now.AddDays(1);
                result = Csv.Backup(csvConfig, now);
                Assert.AreEqual(@"Last backup: 11.12.00; Today: 12.12.00; Next backup: 13.12.00;", result);

                File.WriteAllText(activeDirectory.FullName + @"\Data.csv", "some text");
                now    = now.AddDays(1);
                result = Csv.Backup(csvConfig, now);
                Assert.AreEqual(@$ "1 files copied from {directoryPath}\Active 
to {directoryPath}\Backup\csv20001213.", result);

                now    = now.AddDays(1);
                result = Csv.Backup(csvConfig, now);
                Assert.AreEqual(@"Last backup: 13.12.00; Today: 14.12.00; Next backup: 15.12.00;", result);

                File.WriteAllText(activeDirectory.FullName + @"\Data1.csv", "other text");
                now    = now.AddDays(1);
                result = Csv.Backup(csvConfig, now);
                Assert.AreEqual(@$ "2 files copied from {directoryPath}\Active 
to {directoryPath}\Backup\csv20001215.", result);

                File.WriteAllText(activeDirectory.FullName + @"\Data1.csv", "other text");
                now    = now.AddDays(2);
                result = Csv.Backup(csvConfig, now);
                Assert.AreEqual(@$ "2 files copied from {directoryPath}\Active 
to {directoryPath}\Backup\csv20001217.", result);

                File.WriteAllText(activeDirectory.FullName + @"\Data1.csv", "other text");
                now    = now.AddDays(2);
                result = Csv.Backup(csvConfig, now);
                Assert.AreEqual(@$ "Directory {directoryPath}\Backup\csv20001211 deleted
2 files copied from {directoryPath}\Active 
to {directoryPath}\Backup\csv20001219.", result);
            } finally {
                directoryInfo.Delete(recursive: true);
            }
        }
Exemplo n.º 3
0
 public int ExportCsv(string csvFile, CsvConfig csvCfg)
 {
     return 0;
 }
Exemplo n.º 4
0
 public void TearDown()
 {
     CsvConfig.Reset();
 }
 public void TestFixtureTearDown()
 {
     CsvConfig <TableItem> .Reset();
 }
Exemplo n.º 6
0
        public void TestCsvReader()
        {
            var directoryInfo = new DirectoryInfo("TestCsv");

            try {
                if (directoryInfo.Exists)
                {
                    directoryInfo.Delete(recursive: true);
                    directoryInfo.Refresh();
                }

                directoryInfo.Create();
                directoryInfo.Refresh();

                var csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException);
                var fileName  = csvConfig.DirectoryPath + @"\TestCsvReader.csv";

                var expectedInts         = new List <int>();
                var expectedNullInts     = new List <int?>();
                var expectedLongs        = new List <long>();
                var expectedDecimals     = new List <decimal?>();
                var expectedDecimalNulls = new List <decimal?>();
                using (var fileStream = new FileStream(fileName, FileMode.Create)) {
                    using var streamWriter = new StreamWriter(fileStream);
                    streamWriter.Write(csvConfig.LineCharAdd);
                    streamWriter.Write("0" + csvConfig.Delimiter);
                    streamWriter.Write("1" + csvConfig.Delimiter);
                    streamWriter.WriteLine();

                    streamWriter.Write(csvConfig.LineCharAdd);
                    writeInt(streamWriter, int.MaxValue, expectedInts, csvConfig.Delimiter);
                    writeInt(streamWriter, 1, expectedInts, csvConfig.Delimiter);
                    writeInt(streamWriter, 0, expectedInts, csvConfig.Delimiter);
                    writeInt(streamWriter, -1, expectedInts, csvConfig.Delimiter);
                    writeInt(streamWriter, -10, expectedInts, csvConfig.Delimiter);
                    writeInt(streamWriter, -111, expectedInts, csvConfig.Delimiter);
                    writeInt(streamWriter, int.MinValue, expectedInts, csvConfig.Delimiter);
                    streamWriter.WriteLine();

                    streamWriter.Write(csvConfig.LineCharAdd);
                    writeIntNull(streamWriter, null, expectedNullInts, csvConfig.Delimiter);
                    writeIntNull(streamWriter, int.MaxValue, expectedNullInts, csvConfig.Delimiter);
                    writeIntNull(streamWriter, 1, expectedNullInts, csvConfig.Delimiter);
                    writeIntNull(streamWriter, 0, expectedNullInts, csvConfig.Delimiter);
                    writeIntNull(streamWriter, -1, expectedNullInts, csvConfig.Delimiter);
                    writeIntNull(streamWriter, -10, expectedNullInts, csvConfig.Delimiter);
                    writeIntNull(streamWriter, -111, expectedNullInts, csvConfig.Delimiter);
                    writeIntNull(streamWriter, int.MinValue, expectedNullInts, csvConfig.Delimiter);
                    streamWriter.WriteLine();

                    streamWriter.Write(csvConfig.LineCharAdd);
                    writeLong(streamWriter, long.MaxValue, expectedLongs, csvConfig.Delimiter);
                    writeLong(streamWriter, 1, expectedLongs, csvConfig.Delimiter);
                    writeLong(streamWriter, 0, expectedLongs, csvConfig.Delimiter);
                    writeLong(streamWriter, -1, expectedLongs, csvConfig.Delimiter);
                    writeLong(streamWriter, -10, expectedLongs, csvConfig.Delimiter);
                    writeLong(streamWriter, -111, expectedLongs, csvConfig.Delimiter);
                    writeLong(streamWriter, long.MinValue, expectedLongs, csvConfig.Delimiter);
                    streamWriter.WriteLine();

                    streamWriter.Write(csvConfig.LineCharAdd);
                    writeDecimal(streamWriter, decimal.MaxValue, expectedDecimals, csvConfig.Delimiter);
                    writeDecimal(streamWriter, 1234567890.1234567890m, expectedDecimals, csvConfig.Delimiter);
                    writeDecimal(streamWriter, 1234567890.12m, expectedDecimals, csvConfig.Delimiter);
                    writeDecimal(streamWriter, decimal.One, expectedDecimals, csvConfig.Delimiter);
                    writeDecimal(streamWriter, decimal.Zero, expectedDecimals, csvConfig.Delimiter);
                    writeDecimal(streamWriter, decimal.MinusOne, expectedDecimals, csvConfig.Delimiter);
                    writeDecimal(streamWriter, -1234567890.12m, expectedDecimals, csvConfig.Delimiter);
                    writeDecimal(streamWriter, -1234567890.1234567890m, expectedDecimals, csvConfig.Delimiter);
                    writeDecimal(streamWriter, decimal.MinValue, expectedDecimals, csvConfig.Delimiter);
                    streamWriter.WriteLine();

                    streamWriter.Write(csvConfig.LineCharAdd);
                    writeDecimal(streamWriter, null, expectedDecimalNulls, csvConfig.Delimiter);
                    writeDecimal(streamWriter, decimal.MaxValue, expectedDecimalNulls, csvConfig.Delimiter);
                    writeDecimal(streamWriter, 1234567890.1234567890m, expectedDecimalNulls, csvConfig.Delimiter);
                    writeDecimal(streamWriter, 1234567890.12m, expectedDecimalNulls, csvConfig.Delimiter);
                    writeDecimal(streamWriter, decimal.One, expectedDecimalNulls, csvConfig.Delimiter);
                    writeDecimal(streamWriter, decimal.Zero, expectedDecimalNulls, csvConfig.Delimiter);
                    writeDecimal(streamWriter, decimal.MinusOne, expectedDecimalNulls, csvConfig.Delimiter);
                    writeDecimal(streamWriter, -1234567890.12m, expectedDecimalNulls, csvConfig.Delimiter);
                    writeDecimal(streamWriter, -1234567890.1234567890m, expectedDecimalNulls, csvConfig.Delimiter);
                    writeDecimal(streamWriter, decimal.MinValue, expectedDecimalNulls, csvConfig.Delimiter);
                    streamWriter.WriteLine();

                    streamWriter.Write(csvConfig.LineCharAdd);
                    streamWriter.Write("a" + csvConfig.Delimiter);
                    streamWriter.Write("Ä" + csvConfig.Delimiter);
                    streamWriter.Write("☹" + csvConfig.Delimiter);
                    streamWriter.WriteLine();

                    streamWriter.Write(csvConfig.LineCharAdd);
                    streamWriter.Write(csvConfig.Delimiter);
                    streamWriter.Write("a" + csvConfig.Delimiter);
                    streamWriter.Write("abc" + csvConfig.Delimiter);
                    streamWriter.Write("Ä" + csvConfig.Delimiter);
                    streamWriter.Write("aÄ" + csvConfig.Delimiter);
                    streamWriter.Write("abcÄ ☹de" + csvConfig.Delimiter);
                    streamWriter.WriteLine();

                    streamWriter.Write(csvConfig.LineCharAdd);
                    streamWriter.Write("31.12.9999" + csvConfig.Delimiter);
                    streamWriter.Write("1.1.0001" + csvConfig.Delimiter);
                    streamWriter.WriteLine();

                    streamWriter.Write("0" + csvConfig.Delimiter);
                    streamWriter.Write("0:0:1" + csvConfig.Delimiter);
                    streamWriter.Write("0:1:1" + csvConfig.Delimiter);
                    streamWriter.Write("1:1:1" + csvConfig.Delimiter);
                    streamWriter.Write("23" + csvConfig.Delimiter);
                    streamWriter.Write("23:59" + csvConfig.Delimiter);
                    streamWriter.Write("23:59:59" + csvConfig.Delimiter);
                    streamWriter.WriteLine();

                    streamWriter.Write("3155378975999999999" + csvConfig.Delimiter); //DateTime.MaxValue
                    streamWriter.Write("0" + csvConfig.Delimiter);                   //DateTime.MinValue
                    streamWriter.Write("630822816000000000" + csvConfig.Delimiter);  //new DateTime(2000, 1, 1)
                    streamWriter.Write("630822852610010000" + csvConfig.Delimiter);  //new DateTime(2000, 1, 1, 1, 1, 1, 1)
                    streamWriter.Write("633978144000000000" + csvConfig.Delimiter);  //new DateTime(2009, 12, 31)
                    streamWriter.Write("633979007999990000" + csvConfig.Delimiter);  //new DateTime(2009, 12, 31, 23, 59, 59, 999)
                    streamWriter.WriteLine();

                    streamWriter.Write(csvConfig.LineCharAdd);
                    streamWriter.Write("31.12.9999 23:59:59" + csvConfig.Delimiter);
                    streamWriter.Write("1.1.0001 0:0:1" + csvConfig.Delimiter);
                    streamWriter.Write("31.1.2000 0:12" + csvConfig.Delimiter);
                    streamWriter.Write("31.1.2001 12" + csvConfig.Delimiter);
                    streamWriter.WriteLine();

                    for (int i = -csvConfig.BufferSize; i < csvConfig.BufferSize; i++)
                    {
                        streamWriter.WriteLine(csvConfig.LineCharAdd + i.ToString() + csvConfig.Delimiter);
                    }
                }

                string s;
                using (var fileStream = new FileStream(fileName, FileMode.Open)) {
                    using var streamReader = new StreamReader(fileStream);
                    s = streamReader.ReadToEnd();
                }

                int maxLineLenght = 150;
                using var csvReader = new CsvReader(fileName, csvConfig, maxLineLenght);
                //bool
                Assert.AreEqual(csvConfig.LineCharAdd, csvReader.ReadFirstLineChar());
                Assert.AreEqual(false, csvReader.ReadBool());
                Assert.AreEqual(true, csvReader.ReadBool());
                csvReader.ReadEndOfLine();
                Assert.IsFalse(csvReader.IsEof);

                //int
                Assert.AreEqual(csvConfig.LineCharAdd, csvReader.ReadFirstLineChar());
                foreach (var expectedInt in expectedInts)
                {
                    Assert.AreEqual(expectedInt, csvReader.ReadInt());
                    Assert.IsFalse(csvReader.IsEndOfFileReached());
                    Assert.IsFalse(csvReader.IsEof);
                }
                csvReader.ReadEndOfLine();
                Assert.IsFalse(csvReader.IsEof);

                //int?
                Assert.AreEqual(csvConfig.LineCharAdd, csvReader.ReadFirstLineChar());
                foreach (var expectedInt in expectedNullInts)
                {
                    var actualInt = csvReader.ReadIntNull();
                    if (expectedInt.HasValue)
                    {
                        Assert.AreEqual(expectedInt, actualInt);
                    }
                    else
                    {
                        Assert.IsNull(actualInt);
                    }
                    Assert.IsFalse(csvReader.IsEndOfFileReached());
                    Assert.IsFalse(csvReader.IsEof);
                }
                csvReader.ReadEndOfLine();
                Assert.IsFalse(csvReader.IsEof);

                //long
                Assert.AreEqual(csvConfig.LineCharAdd, csvReader.ReadFirstLineChar());
                foreach (var expectedLong in expectedLongs)
                {
                    Assert.AreEqual(expectedLong, csvReader.ReadLong());
                    Assert.IsFalse(csvReader.IsEndOfFileReached());
                    Assert.IsFalse(csvReader.IsEof);
                }
                csvReader.ReadEndOfLine();
                Assert.IsFalse(csvReader.IsEof);

                //decimal
                Assert.AreEqual(csvConfig.LineCharAdd, csvReader.ReadFirstLineChar());
                foreach (var expectedDecimal in expectedDecimals)
                {
                    Assert.AreEqual(expectedDecimal, csvReader.ReadDecimal());
                    Assert.IsFalse(csvReader.IsEndOfFileReached());
                    Assert.IsFalse(csvReader.IsEof);
                }
                csvReader.ReadEndOfLine();
                Assert.IsFalse(csvReader.IsEof);

                //decimal or null
                Assert.AreEqual(csvConfig.LineCharAdd, csvReader.ReadFirstLineChar());
                foreach (var expectedDecimal in expectedDecimalNulls)
                {
                    Assert.AreEqual(expectedDecimal, csvReader.ReadDecimalNull());
                    Assert.IsFalse(csvReader.IsEndOfFileReached());
                    Assert.IsFalse(csvReader.IsEof);
                }
                csvReader.ReadEndOfLine();
                Assert.IsFalse(csvReader.IsEof);

                //char
                Assert.AreEqual(csvConfig.LineCharAdd, csvReader.ReadFirstLineChar());
                Assert.AreEqual('a', csvReader.ReadChar());
                Assert.AreEqual('Ä', csvReader.ReadChar());
                Assert.AreEqual('☹', csvReader.ReadChar());
                csvReader.ReadEndOfLine();
                Assert.IsFalse(csvReader.IsEof);

                //string?
                Assert.AreEqual(csvConfig.LineCharAdd, csvReader.ReadFirstLineChar());
                Assert.IsNull(csvReader.ReadStringNull());
                Assert.AreEqual("a", csvReader.ReadString());
                Assert.AreEqual("abc", csvReader.ReadString());
                Assert.AreEqual("Ä", csvReader.ReadString());
                Assert.AreEqual("aÄ", csvReader.ReadString());
                Assert.AreEqual("abcÄ ☹de", csvReader.ReadString());
                csvReader.ReadEndOfLine();

                //Date
                Assert.AreEqual(csvConfig.LineCharAdd, csvReader.ReadFirstLineChar());
                Assert.AreEqual(DateTime.MaxValue.Date, csvReader.ReadDate());
                Assert.AreEqual(DateTime.MinValue.Date, csvReader.ReadDate());
                csvReader.ReadEndOfLine();

                //Time
                Assert.AreEqual(new TimeSpan(0, 0, 0), csvReader.ReadTime());
                Assert.AreEqual(new TimeSpan(0, 0, 1), csvReader.ReadTime());
                Assert.AreEqual(new TimeSpan(0, 1, 1), csvReader.ReadTime());
                Assert.AreEqual(new TimeSpan(1, 1, 1), csvReader.ReadTime());
                Assert.AreEqual(new TimeSpan(23, 0, 0), csvReader.ReadTime());
                Assert.AreEqual(new TimeSpan(23, 59, 0), csvReader.ReadTime());
                Assert.AreEqual(new TimeSpan(23, 59, 59), csvReader.ReadTime());
                csvReader.ReadEndOfLine();

                //DateTime
                Assert.AreEqual(DateTime.MaxValue, csvReader.ReadDateTimeTicks());
                Assert.AreEqual(DateTime.MinValue, csvReader.ReadDateTimeTicks());
                Assert.AreEqual(new DateTime(2000, 1, 1), csvReader.ReadDateTimeTicks());
                Assert.AreEqual(new DateTime(2000, 1, 1, 1, 1, 1, 1), csvReader.ReadDateTimeTicks());
                Assert.AreEqual(new DateTime(2009, 12, 31), csvReader.ReadDateTimeTicks());
                Assert.AreEqual(new DateTime(2009, 12, 31, 23, 59, 59, 999), csvReader.ReadDateTimeTicks());
                csvReader.ReadEndOfLine();

                //DateSeconds
                Assert.AreEqual(csvConfig.LineCharAdd, csvReader.ReadFirstLineChar());
                Assert.AreEqual(new DateTime(9999, 12, 31, 23, 59, 59, 0), csvReader.ReadDateSeconds());
                Assert.AreEqual(new DateTime(1, 1, 1, 0, 0, 1, 0), csvReader.ReadDateSeconds());
                Assert.AreEqual(new DateTime(2000, 1, 31, 0, 12, 0, 0), csvReader.ReadDateSeconds());
                Assert.AreEqual(new DateTime(2001, 1, 31, 12, 0, 0, 0), csvReader.ReadDateSeconds());
                csvReader.ReadEndOfLine();

                //more than 1 buffer data
                for (int i = -csvConfig.BufferSize; i < csvConfig.BufferSize; i++)
                {
                    Assert.AreEqual(csvConfig.LineCharAdd, csvReader.ReadFirstLineChar());
                    Assert.AreEqual(i, csvReader.ReadInt());
                    Assert.IsFalse(csvReader.IsEndOfFileReached());
                    Assert.IsFalse(csvReader.IsEof);
                    csvReader.ReadEndOfLine();
                    if (i < csvConfig.BufferSize - 1)
                    {
                        Assert.IsFalse(csvReader.IsEndOfFileReached());
                        Assert.IsFalse(csvReader.IsEof);
                    }
                    else
                    {
                        Assert.IsTrue(csvReader.IsEndOfFileReached());
                        Assert.IsTrue(csvReader.IsEof);
                    }
                }
            } finally {
                directoryInfo.Delete(recursive: true);
            }
        }
        public void TestCsvWriterTransaction()
        {
            var directoryInfo = new DirectoryInfo("TestCsv");

            try {
                if (directoryInfo.Exists)
                {
                    directoryInfo.Delete(recursive: true);
                    directoryInfo.Refresh();
                }

                directoryInfo.Create();
                directoryInfo.Refresh();

                var csvConfig    = new CsvConfig(directoryInfo.FullName, reportException: reportException);
                var fileName     = csvConfig.DirectoryPath + @"\TestCsvWriterTransaction.csv";
                var header       = "Some long header";
                var someText     = "Some Text followed by character a: ";
                var aCharacter   = 'a';
                var expectedText =
                    header + Environment.NewLine +
                    someText + csvConfig.Delimiter + aCharacter + csvConfig.Delimiter + Environment.NewLine;
                using (var csvWriter = new CsvWriter(fileName, csvConfig, 250)) {
                    csvWriter.WriteLine(header);
                    csvWriter.StartTransaction();
                    csvWriter.StartNewLine();
                    csvWriter.Write(someText);
                    csvWriter.Write(aCharacter);
                    csvWriter.WriteEndOfLine();
                    csvWriter.CommitTransaction();
                }
                assert(fileName, expectedText);

                header        = "Header";
                someText      = "b: ";
                aCharacter    = 'b';
                expectedText +=
                    header + Environment.NewLine;
                using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None,
                                                       csvConfig.BufferSize, FileOptions.SequentialScan))
                {
                    fileStream.Position = fileStream.Length;
                    using var csvWriter = new CsvWriter(null, csvConfig, 250, fileStream);
                    csvWriter.WriteLine(header);
                    csvWriter.StartTransaction();
                    csvWriter.StartNewLine();
                    csvWriter.Write(someText);
                    csvWriter.Write(aCharacter);
                    csvWriter.WriteEndOfLine();
                    csvWriter.RollbackTransaction();
                }
                assert(fileName, expectedText);

                header        = "Header 3";
                someText      = "CCCCC: ";
                aCharacter    = 'c';
                expectedText +=
                    header + Environment.NewLine;
                using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None,
                                                       csvConfig.BufferSize, FileOptions.SequentialScan))
                {
                    fileStream.Position = fileStream.Length;
                    using var csvWriter = new CsvWriter(null, csvConfig, 250, fileStream);
                    csvWriter.WriteLine(header);
                    csvWriter.StartTransaction();
                    csvWriter.StartNewLine();
                    csvWriter.Write(someText);
                    csvWriter.RollbackTransaction();
                }
                assert(fileName, expectedText);

                header        = "Header 4";
                someText      = "DDDD: ";
                aCharacter    = 'd';
                expectedText +=
                    header + Environment.NewLine +
                    someText + csvConfig.Delimiter + aCharacter + csvConfig.Delimiter + Environment.NewLine;
                using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None,
                                                       csvConfig.BufferSize, FileOptions.SequentialScan))
                {
                    fileStream.Position = fileStream.Length;
                    using var csvWriter = new CsvWriter(null, csvConfig, 250, fileStream);
                    csvWriter.WriteLine(header);
                    csvWriter.StartTransaction();
                    csvWriter.StartNewLine();
                    csvWriter.Write("text gets rolled back");
                    csvWriter.RollbackTransaction();
                    csvWriter.StartNewLine();
                    csvWriter.Write(someText);
                    csvWriter.Write(aCharacter);
                    csvWriter.WriteEndOfLine();
                }
                assert(fileName, expectedText);

                header        = "Header 5";
                someText      = "EEEE: ";
                aCharacter    = 'e';
                expectedText += header + Environment.NewLine;
                using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None,
                                                       csvConfig.BufferSize, FileOptions.SequentialScan))
                {
                    fileStream.Position = fileStream.Length;
                    using var csvWriter = new CsvWriter(null, csvConfig, 250, fileStream);
                    csvWriter.WriteLine(header);
                    try {
                        csvWriter.StartTransaction();
                        csvWriter.StartNewLine();
                        csvWriter.Write("WriteDate which has also time throws exception, catch executes rollback");
                        csvWriter.WriteDate(new DateTime(1999, 9, 29, 19, 59, 59));
                    } catch (Exception) {
                        csvWriter.RollbackTransaction();
                    }
                }
                assert(fileName, expectedText);

                header        = "Header 6";
                someText      = "FFFF: ";
                aCharacter    = 'f';
                expectedText +=
                    header + Environment.NewLine +
                    someText + csvConfig.Delimiter + aCharacter + csvConfig.Delimiter + Environment.NewLine;
                using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None,
                                                       csvConfig.BufferSize, FileOptions.SequentialScan)) {
                    fileStream.Position = fileStream.Length;
                    using var csvWriter = new CsvWriter(null, csvConfig, 250, fileStream);
                    csvWriter.WriteLine(header);
                    try {
                        csvWriter.StartTransaction();
                        csvWriter.StartNewLine();
                        csvWriter.Write("WriteDate which has also time throws exception, catch executes rollback");
                        csvWriter.WriteDate(new DateTime(1999, 9, 29, 19, 59, 59));
                    } catch (Exception) {
                        csvWriter.RollbackTransaction();
                    }
                    csvWriter.StartNewLine();
                    csvWriter.Write(someText);
                    csvWriter.Write(aCharacter);
                    csvWriter.WriteEndOfLine();
                }
                assert(fileName, expectedText);


                header     = "Header 7";
                someText   = "GGGGGG: ";
                aCharacter = 'g';
                var longString = new string('a', 1024 - 3);
                expectedText += header + Environment.NewLine;
                using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None,
                                                       csvConfig.BufferSize, FileOptions.SequentialScan))
                {
                    fileStream.Position = fileStream.Length;
                    using var csvWriter = new CsvWriter(null, csvConfig, 1024, fileStream);
                    csvWriter.WriteLine(header);
                    csvWriter.StartTransaction();
                    //write more than CsvWriter buffer can hold;
                    for (int i = 0; i < 5; i++)
                    {
                        csvWriter.WriteLine(longString);
                    }
                    csvWriter.StartNewLine();
                    csvWriter.Write(someText);
                    csvWriter.Write(aCharacter);
                    csvWriter.WriteEndOfLine();
                    csvWriter.RollbackTransaction();
                }
                assert(fileName, expectedText);
            } finally {
                directoryInfo.Delete(recursive: true);
            }
        }
Exemplo n.º 8
0
        public void TestSingleChild()
        {
            /* Test explanation:
             * items can be stored or not stored, legal combinations:
             * child variable name | parent variable name
             * --------------------+--------------------
             * child_: not stored  | parent_: not stored
             * child__: not stored | parent__:stored
             * child: stored       | parent: stored
             * child___: stored    | parent___: not stored    this combination is illegal. Test: is an exception thrown ?
             *
             * for each combination above some test code is written
             *
             * parent variabls names:
             * Parent: the parent property in the child is not nullable
             * ParentN: the parent property in the child is nullable
             * ParentR: the parent property in the child is readonly
             * ParentNR: the parent property in the child is nullable and readonly
             *
             * each activity like create, update or delete is done first in a rooled back transaction and none of the data should
             * be changed, then the same activity is executed in a commited transactions and the data should change accordingly.
             *
             * After a transaction has been committed, the datacontext gets disposed, opened again and verified, that the data
             * is still the same. This is done twice, first using the .bak files, then the .csv files. There is one exception:
             * stored parents might have some not stored children. In the new data context, those parents have no longer those
             * children.
             *
             * For convenience, the variables parent0, parentN1, child1, etc. contain always the data from the latest data context. They
             * get updated, each time assertDataDisposeDCRecreateDCassertData() gets called.
             *
             * child_, child__, parent_ and parent___ are not stored in the data context. They do not get updated by
             * assertDataDisposeDCRecreateDCassertData() and can therefore contain children or parents stored in previous data
             * contexts.
             */
            var directoryInfo = new DirectoryInfo("TestCsv");

            if (directoryInfo.Exists)
            {
                directoryInfo.Delete(recursive: true);
                directoryInfo.Refresh();
            }

            directoryInfo.Create();


            try {
                csvConfig         = new CsvConfig(directoryInfo.FullName, reportException: reportException);
                bakCsvFileSwapper = new BakCsvFileSwapper(csvConfig);
                DC.Trace          = dcTrace;
                dc = new DC(csvConfig);
                assertData("");

                // Create
                // ======
                // Create parent
                // -------------
                traceHeader("create not stored parent");
                dc.StartTransaction();
                var parent0_ = new SingleChildParent("p_Temp", isStoring: false);
                dc.RollbackTransaction();
                assertData("");

                dc.StartTransaction();
                parent0_ = new SingleChildParent("p_", isStoring: false);
                dc.CommitTransaction();
                assertDataDisposeDCRecreateDCassertData("");


                traceHeader("create stored parent");
                dc.StartTransaction();
                parent0 = new SingleChildParent("p0Temp", isStoring: true);
                dc.RollbackTransaction();
                assertData("");

                dc.StartTransaction();
                parent0 = new SingleChildParent("p0", isStoring: true);
                dc.CommitTransaction();
                assertDataDisposeDCRecreateDCassertData("p0|");

                // Create child with Parent==parent0, ParentN==null, ParentR==parentR0, ParentNR=parentNR0
                // ---------------------------------------------------------------------------------------
                traceHeader("create not stored child with not stored parent");
                var parentR0_  = new SingleChildParentR("pR0_", isStoring: false);
                var parentNR0_ = new SingleChildParentNR("pRN0_", isStoring: false);
                dc.StartTransaction();
                var child0_ = new SingleChildChild("c0_Temp", parent0_, null, parentR0_, parentNR0_, isStoring: false);
                dc.RollbackTransaction();
                Assert.IsNull(parent0_.Child);
                Assert.IsNull(parentR0_.Child);
                Assert.IsNull(parentNR0_.Child);
                assertData("p0|");

                dc.StartTransaction();
                child0_ = new SingleChildChild("c0_", parent0_, null, parentR0_, parentNR0_, isStoring: false);
                dc.CommitTransaction();
                Assert.AreEqual("c0_", child0_.Text);
                Assert.AreEqual(child0_, parent0_.Child);
                Assert.AreEqual(child0_, parentR0_.Child);
                Assert.AreEqual(child0_, parentNR0_.Child);
                assertDataDisposeDCRecreateDCassertData("p0|");


                traceHeader("create not stored child with stored parent");
                parent0__   = new SingleChildParent("p0__", isStoring: true);
                parentR0__  = new SingleChildParentR("pR0__", isStoring: true);
                parentNR0__ = new SingleChildParentNR("pNR0__", isStoring: true);
                dc.StartTransaction();
                var child0__ = new SingleChildChild("c0__Temp", parent0__, null, parentR0__, parentNR0__, isStoring: false);
                dc.RollbackTransaction();
                Assert.IsNull(parent0__.Child);
                Assert.IsNull(parentR0__.Child);
                Assert.IsNull(parentNR0__.Child);
                assertData("p0|p0__|pR0__|pNR0__|");

                dc.StartTransaction();
                child0__ = new SingleChildChild("c0__", parent0__, null, parentR0__, parentNR0__, isStoring: false);
                dc.CommitTransaction();
                Assert.AreEqual("c0__", child0__.Text);
                Assert.AreEqual(parent0__, child0__.Parent);
                Assert.AreEqual(parentR0__, child0__.ParentR);
                Assert.AreEqual(parentNR0__, child0__.ParentNR);
                assertDataDisposeDCRecreateDCassertData("p0|p0__:c0__|pR0__:c0__|pNR0__:c0__|", "p0|p0__|pR0__|pNR0__|");


                traceHeader("create stored child with stored parent");
                parentR0  = new SingleChildParentR("pR0", isStoring: true);
                parentNR0 = new SingleChildParentNR("pNR0", isStoring: true);
                dc.StartTransaction();
                child0 = new SingleChildChild("c0Temp", parent0, null, parentR0, parentNR0, isStoring: true);
                dc.RollbackTransaction();
                assertData("p0|p0__|pR0__|pR0|pNR0__|pNR0|");

                dc.StartTransaction();
                child0 = new SingleChildChild("c0", parent0, null, parentR0, parentNR0, isStoring: true);
                dc.CommitTransaction();
                assertDataDisposeDCRecreateDCassertData("p0:c0|p0__|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|");

                //Fail to create stored child with not stored parent
                traceHeader("fail to create stored child with not stored parents");
                try {
                    var parent0___   = new SingleChildParent("p0___", isStoring: false);
                    var parentN0___  = new SingleChildParentN("pN0___", isStoring: false);
                    var parentR0___  = new SingleChildParentR("pR0___", isStoring: false);
                    var parentNR0___ = new SingleChildParentNR("pRN0___", isStoring: false);
                    var child___     = new SingleChildChild("failed child", parent0___, parentN0___, parentR0___, parentNR0___);
                    Assert.Fail();
                } catch {
                }
                //Todo: Ideally, an exception during create, store or remove should not change any data. Is additional code needed undoing
                //any potentially changed data ?
                //Assert.AreEqual(0, parent0_.Children.Count);
                assertDataDisposeDCRecreateDCassertData("p0:c0|p0__|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|");

                // Update
                // ======
                // Update child.Parent to parent1, child.ParentN to parent0N
                // ---------------------------------------------------------

                traceHeader("not stored child: update with not stored parents");
                var parent1_  = new SingleChildParent("p1_", isStoring: false);
                var parentN0_ = new SingleChildParentN("pN0___", isStoring: false);
                dc.StartTransaction();
                child0_.Update("c0_.1Temp", parent1_, parentN0_);
                dc.RollbackTransaction();
                Assert.AreEqual(child0_, parent0_.Child);
                Assert.IsNull(parentN0_.Child);
                Assert.IsNull(parent1_.Child);
                assertData("p0:c0|p0__|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|");

                dc.StartTransaction();
                child0_.Update("c0_.1", parent1_, parentN0_);
                dc.CommitTransaction();
                Assert.IsNull(parent0_.Child);
                Assert.AreEqual(child0_, parentN0_.Child);
                Assert.AreEqual(child0_, parent1_.Child);
                assertDataDisposeDCRecreateDCassertData("p0:c0|p0__|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|");


                traceHeader("not stored child: update with stored parents");
                parent1__  = new SingleChildParent("p1__", isStoring: true);
                parentN0__ = new SingleChildParentN("pN0__", isStoring: true);
                dc.StartTransaction();
                child0__.Update("c0__.1Temp", parent1__, parentN0__);
                dc.RollbackTransaction();
                Assert.AreEqual("c0__", child0__.Text);
                Assert.AreEqual(parent0__.Text, child0__.Parent.Text);
                Assert.IsNull(child0__.ParentN);
                assertData("p0:c0|p0__|p1__|pN0__|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|");

                dc.StartTransaction();
                child0__.Update("c0__.1", parent1__, parentN0__);
                dc.CommitTransaction();
                Assert.AreEqual("c0__.1", child0__.Text);
                Assert.AreEqual(parent1__, child0__.Parent);
                Assert.AreEqual(parentN0__, child0__.ParentN);
                assertDataDisposeDCRecreateDCassertData(
                    "p0:c0|p0__|p1__:c0__.1|pN0__:c0__.1|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|",
                    "p0:c0|p0__|p1__|pN0__|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|");


                traceHeader("stored child: update with stored parents");
                parent1  = new SingleChildParent("p1", isStoring: true);
                parentN0 = new SingleChildParentN("pN0", isStoring: true);
                dc.StartTransaction();
                child0.Update("c0.1Temp", parent1, parentN0);
                dc.RollbackTransaction();
                assertData("p0:c0|p0__|p1__|p1|pN0__|pN0|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|");

                dc.StartTransaction();
                child0.Update("c0.1", parent1, parentN0);
                dc.CommitTransaction();
                assertDataDisposeDCRecreateDCassertData("p0|p0__|p1__|p1:c0.1|pN0__|pN0:c0.1|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|");

                // Update child.ParentN to parent1N
                // --------------------------------

                traceHeader("not stored child: update not stored ParentN");
                var parentN1_ = new SingleChildParentN("pN1_", isStoring: false);
                dc.StartTransaction();
                child0_.Update("c0_.2Temp", parent1_, parentN1_);
                dc.RollbackTransaction();
                Assert.AreEqual("c0_.1", child0_.Text);
                Assert.AreEqual(child0_, parent1_.Child);
                Assert.AreEqual(child0_, parentN0_.Child);
                Assert.IsNull(parentN1_.Child);
                assertData("p0|p0__|p1__|p1:c0.1|pN0__|pN0:c0.1|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|");

                dc.StartTransaction();
                child0_.Update("c0_.2", parent1_, parentN1_);
                dc.CommitTransaction();
                Assert.AreEqual("c0_.2", child0_.Text);
                Assert.IsNull(parent0_.Child);
                Assert.IsNull(parentN0_.Child);
                Assert.AreEqual(child0_, parent1_.Child);
                Assert.AreEqual(child0_, parentN1_.Child);
                assertDataDisposeDCRecreateDCassertData("p0|p0__|p1__|p1:c0.1|pN0__|pN0:c0.1|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|");


                traceHeader("not stored child: update stored ParentN");
                parentN1__ = new SingleChildParentN("pN1__", isStoring: true);
                dc.StartTransaction();
                child0__.Update("c0__.2Temp", parent1__, parentN1__);
                dc.RollbackTransaction();
                Assert.AreEqual("c0__.1", child0__.Text);
                Assert.AreEqual(parent1__.Text, child0__.Parent.Text);
                Assert.AreEqual(parentN0__.Text, child0__.ParentN !.Text);
                assertDataDisposeDCRecreateDCassertData(
                    "p0|p0__|p1__|p1:c0.1|pN0__|pN0:c0.1|pN1__|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|");

                dc.StartTransaction();
                child0__.Update("c0__.2", parent1__, parentN1__);
                dc.CommitTransaction();
                Assert.AreEqual("c0__.2", child0__.Text);
                Assert.AreEqual(parent1__, child0__.Parent);
                Assert.AreEqual(parentN1__, child0__.ParentN);
                assertDataDisposeDCRecreateDCassertData(
                    "p0|p0__|p1__:c0__.2|p1:c0.1|pN0__|pN0:c0.1|pN1__:c0__.2|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|",
                    "p0|p0__|p1__|p1:c0.1|pN0__|pN0:c0.1|pN1__|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|");


                traceHeader("stored child: update stored ParentN");
                parentN1 = new SingleChildParentN("pN1", isStoring: true);
                dc.StartTransaction();
                child0.Update("c0.2Temp", parent1, parentN1);
                dc.RollbackTransaction();
                assertData("p0|p0__|p1__|p1:c0.1|pN0__|pN0:c0.1|pN1__|pN1|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|");

                dc.StartTransaction();
                child0.Update("c0.2", parent1, parentN1);
                dc.CommitTransaction();
                assertDataDisposeDCRecreateDCassertData(
                    "p0|p0__|p1__|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|");

                // Update child.ParentN to null
                // ----------------------------

                traceHeader("not stored child: update not stored ParentN to null");
                dc.StartTransaction();
                child0_.Update("c0_.3Temp", parent1_, null);
                dc.RollbackTransaction();
                Assert.AreEqual("c0_.2", child0_.Text);
                Assert.AreEqual(child0_, parent1_.Child);
                Assert.AreEqual(child0_, parentN1_.Child);
                assertData("p0|p0__|p1__|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|");

                dc.StartTransaction();
                child0_.Update("c0_.3", parent1_, null);
                dc.CommitTransaction();
                Assert.AreEqual("c0_.3", child0_.Text);
                Assert.IsNull(parentN0_.Child);
                Assert.AreEqual(child0_, parent1_.Child);
                assertDataDisposeDCRecreateDCassertData(
                    "p0|p0__|p1__|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|");


                traceHeader("not stored child: update stored ParentN to null");
                dc.StartTransaction();
                child0__.Update("c0__.3Temp", parent1__, null);
                dc.RollbackTransaction();
                Assert.AreEqual("c0__.2", child0__.Text);
                Assert.AreEqual(parent1__.Text, child0__.Parent.Text);
                Assert.AreEqual(parentN1__.Text, child0__.ParentN !.Text);
                assertDataDisposeDCRecreateDCassertData(
                    "p0|p0__|p1__|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|");

                dc.StartTransaction();
                child0__.Update("c0__.3", parent1__, null);
                dc.CommitTransaction();
                Assert.AreEqual("c0__.3", child0__.Text);
                Assert.AreEqual(parent1__, child0__.Parent);
                Assert.IsNull(child0__.ParentN);
                assertDataDisposeDCRecreateDCassertData(
                    "p0|p0__|p1__:c0__.3|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|",
                    "p0|p0__|p1__|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|");


                traceHeader("stored child: update stored ParentN to null");
                dc.StartTransaction();
                child0.Update("c0.3Temp", parent1, null);
                dc.RollbackTransaction();
                assertData("p0|p0__|p1__|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|");

                dc.StartTransaction();
                child0.Update("c0.3", parent1, null);
                dc.CommitTransaction();
                assertDataDisposeDCRecreateDCassertData(
                    "p0|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1|pR0__|pR0:c0.3|pNR0__|pNR0:c0.3|c0.3:p1,pR0,pNR0|");


                // Release
                // =======
                // Fail to release parent with stored child
                // ----------------------------------------
                traceHeader("stored child: fail to release parent");
                try {
                    parent1.Release();
                    Assert.Fail();
                } catch {
                }
                assertData("p0|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1|pR0__|pR0:c0.3|pNR0__|pNR0:c0.3|c0.3:p1,pR0,pNR0|");
                bakCsvFileSwapper.DeleteBakFiles();

                //Release child
                //-------------
                traceHeader("stored child: release child");
                parentR1  = new SingleChildParentR("pR1", isStoring: true);
                parentNR1 = new SingleChildParentNR("pNR1", isStoring: true);
                child1    = new SingleChildChild("c1", parent0, parentN1, parentR1, parentNR1, isStoring: true);
                assertDataDisposeDCRecreateDCassertData(
                    "p0:c1|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1:c1|pR0__|pR0:c0.3|pR1:c1|pNR0__|pNR0:c0.3|pNR1:c1|c0.3:p1,pR0,pNR0|c1:p0,pN1,pR1,pNR1|");
                dc.StartTransaction();
                child1.Release();
                dc.RollbackTransaction();
                assertData("p0:c1|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1:c1|pR0__|pR0:c0.3|pR1:c1|pNR0__|pNR0:c0.3|pNR1:c1|c0.3:p1,pR0,pNR0|c1:p0,pN1,pR1,pNR1|");

                dc.StartTransaction();
                child1.Release();
                dc.CommitTransaction();
                child1 = null;
                assertDataDisposeDCRecreateDCassertData(
                    "p0:c1|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1:c1|pR0__|pR0:c0.3|pR1:c1|pNR0__|pNR0:c0.3|pNR1:c1|c0.3:p1,pR0,pNR0|",
                    "p0" + "|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1" + "|pR0__|pR0:c0.3|pR1" + "|pNR0__|pNR0:c0.3|pNR1" + "|c0.3:p1,pR0,pNR0|");

                //Release parent
                //--------------
                traceHeader("stored parent: release parent");
                dc.StartTransaction();
                parent0.Release();
                dc.RollbackTransaction();
                assertData("p0" + "|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1" + "|pR0__|pR0:c0.3|pR1" + "|pNR0__|pNR0:c0.3|pNR1" + "|c0.3:p1,pR0,pNR0|");

                dc.StartTransaction();
                parent0.Release();
                dc.CommitTransaction();
                parent0 = null;
                assertDataDisposeDCRecreateDCassertData(
                    "p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1|pR0__|pR0:c0.3|pR1|pNR0__|pNR0:c0.3|pNR1|c0.3:p1,pR0,pNR0|");
            } finally {
                DC.DisposeData();
            }
        }
Exemplo n.º 9
0
        public void TestPlayingList()
        {
            try {
                var directoryInfo = new DirectoryInfo("TestCsv");
                if (directoryInfo.Exists)
                {
                    directoryInfo.Delete(recursive: true);
                    directoryInfo.Refresh();
                }

                directoryInfo.Create();

                csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException);
                _         = new DC(csvConfig);
                var location = new Location("SomePath", "SomeLocation");

                //create and delete playlist
                var playlistRecordA = createPlayList();
                release(playlistRecordA);
                increaseGeneration();

                //create and delete track
                var trackRecordA = createTrack();
                release(trackRecordA);
                increaseGeneration();

                //assign new track to new playlist, delete playlistTrack, track, playlist
                playlistRecordA = createPlayList();
                trackRecordA    = createTrack();
                var playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA);
                release(playlistTrackRecordA);
                release(trackRecordA);
                release(playlistRecordA);
                increaseGeneration();

                //assign new track to new playlist, delete playlistTrack track, playlist
                playlistRecordA      = createPlayList();
                trackRecordA         = createTrack();
                playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA);
                release(trackRecordA);
                release(playlistRecordA);
                increaseGeneration();

                //assign new track to new playlist, delete playlistTrack playlist, track
                playlistRecordA      = createPlayList();
                trackRecordA         = createTrack();
                playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA);
                release(playlistRecordA);
                release(trackRecordA);
                increaseGeneration();

                //create new playlist, track, playlistTrack, playingList
                //delete playlist, track
                playlistRecordA      = createPlayList();
                trackRecordA         = createTrack();
                playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA);
                var playingListRecordA = createPlayingList(playlistRecordA);
                release(playlistRecordA);
                release(trackRecordA);
                increaseGeneration();

                //create new playlist, track, playlistTrack, playingList
                //delete track, playlist
                playlistRecordA      = createPlayList();
                trackRecordA         = createTrack();
                playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA);
                playingListRecordA   = createPlayingList(playlistRecordA);
                release(trackRecordA);
                release(playlistRecordA);
                increaseGeneration();

                //create new playlist, track, playlistTrack, playingList
                //delete playlistTrack, track, playlist
                playlistRecordA      = createPlayList();
                trackRecordA         = createTrack();
                playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA);
                playingListRecordA   = createPlayingList(playlistRecordA);
                release(playlistTrackRecordA);
                release(trackRecordA);
                release(playlistRecordA);
                increaseGeneration();

                //create new playlist with 3 tracks, 3 PlaylistTracks, playingList
                //play 1 track
                //delete first playlistTrack
                //delete second playlistTrack
                playlistRecordA      = createPlayList();
                trackRecordA         = createTrack();
                playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA);
                var trackRecordB         = createTrack();
                var playlistTrackRecordB = createPlaylistTrack(playlistRecordA, trackRecordB);
                var trackRecordC         = createTrack();
                var playlistTrackRecordC = createPlaylistTrack(playlistRecordA, trackRecordC);
                playingListRecordA = createPlayingList(playlistRecordA);
                playNextTrack(playingListRecordA, trackRecordA);
                release(playlistTrackRecordA);
                release(playlistTrackRecordB);

                //playlist contains only 1 record. Keep playing it
                playNextTrack(playingListRecordA, trackRecordC);
                playNextTrack(playingListRecordA, trackRecordC);

                //create second playinglist with same 3 tracks
                //delete 3. track
                var playlistRecordB = createPlayList();
                _ = createPlaylistTrack(playlistRecordB, trackRecordA);
                _ = createPlaylistTrack(playlistRecordB, trackRecordB);
                _ = createPlaylistTrack(playlistRecordB, trackRecordC);

                //Testing of playNextTrack from empty PlayingList cannot be tested, because disposeCreateDC() makes empty PlayingList
                //disapear
                //release(trackRecordC);
                //playNextTrack(playingListRecordA, null);


                //increaseGeneration();


                Assert.IsTrue(true);//Just for setting a breakpoint
            } finally {
                DC.DisposeData();
            }
        }
Exemplo n.º 10
0
        public void TestDataStoreCSVRestore()
        {
            var directoryInfo = new DirectoryInfo("TestCsv");

            if (directoryInfo.Exists)
            {
                directoryInfo.Delete(recursive: true);
                directoryInfo.Refresh();
            }

            directoryInfo.Create();
            directoryInfo.Refresh();

            var csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException);
            var dataStore = new DataStoreCSV <TestItemCsv>(
                null,
                1,
                csvConfig !,
                TestItemCsv.MaxLineLength,
                TestItemCsv.Headers,
                TestItemCsv.SetKey,
                TestItemCsv.Create,
                null,
                TestItemCsv.Update,
                TestItemCsv.Write,
                TestItemCsv.Disconnect,
                TestItem.RollbackItemNew,
                TestItem.RollbackItemStore,
                TestItem.RollbackItemUpdate,
                TestItem.RollbackItemRelease,
                areInstancesUpdatable: true,
                areInstancesReleasable: true);

            try {
                var testItem0 = new TestItemCsv("testItem0");
                dataStore.Add(testItem0);
                var testItem1 = new TestItemCsv("testItem1");
                dataStore.Add(testItem1);
                var testItem2 = new TestItemCsv("testItem2");
                dataStore.Add(testItem2);
                testItem1.Remove(dataStore);
                testItem2.Update("testItem2 updated", dataStore);
                var expectedtestItem0 = testItem0.ToString();
                var expectedtestItem2 = testItem2.ToString();
                dataStore.Dispose();

                directoryInfo.Refresh();
                File.Delete(directoryInfo.FullName + @"\TestItemCsv.csv");
                File.Move(directoryInfo.FullName + @"\TestItemCsv.bak", directoryInfo.FullName + @"\TestItemCsv.csv");

                dataStore = new DataStoreCSV <TestItemCsv>(
                    null,
                    1,
                    csvConfig !,
                    TestItemCsv.MaxLineLength,
                    TestItemCsv.Headers,
                    TestItemCsv.SetKey,
                    TestItemCsv.Create,
                    null,
                    TestItemCsv.Update,
                    TestItemCsv.Write,
                    TestItemCsv.Disconnect,
                    TestItem.RollbackItemNew,
                    TestItem.RollbackItemStore,
                    TestItem.RollbackItemUpdate,
                    TestItem.RollbackItemRelease,
                    areInstancesUpdatable: true,
                    areInstancesReleasable: true);
                Assert.AreEqual(expectedtestItem0, dataStore[0].ToString());
                Assert.AreEqual(expectedtestItem2, dataStore[2].ToString());
            } finally {
                dataStore?.Dispose();
            }
        }
Exemplo n.º 11
0
        public void TestControlChar()
        {
            var directoryInfo = new DirectoryInfo("ControlCharTest");

            try {
                if (directoryInfo.Exists)
                {
                    directoryInfo.Delete(recursive: true);
                    directoryInfo.Refresh();
                }

                directoryInfo.Create();
                directoryInfo.Refresh();

                var    csvConfig     = new CsvConfig(directoryInfo.FullName, reportException: reportException);
                var    fileName      = csvConfig.DirectoryPath + @"\ControlCharTest.csv";
                var    header        = "Characters from ASCII 0 up to '!'";
                var    characters    = new char[34];
                var    maxLineLenght = 250;
                string?nullString    = null;
                string asciiString;
                var    utfString = "Smiley: 😀;Tab: \t; CR: \r; LF: \n; Slash: \\; Burmese: ၑ;";
                using (var csvWriter = new CsvWriter(fileName, csvConfig, maxLineLenght)) {
                    csvWriter.WriteLine(header);
                    csvWriter.StartNewLine();
                    for (int i = 0; i < characters.Length; i++)
                    {
                        var c = (char)i;
                        characters[i] = c;
                        csvWriter.Write(c);
                    }
                    csvWriter.WriteEndOfLine();

                    csvWriter.StartNewLine();
                    csvWriter.Write(nullString);
                    csvWriter.Write("");
                    csvWriter.Write(" ");
                    csvWriter.Write("a");
                    csvWriter.Write(csvConfig.Delimiter.ToString());
                    csvWriter.Write("\\");
                    csvWriter.Write("\\n");
                    csvWriter.Write("\\r");
                    csvWriter.Write("\\r\\n");
                    csvWriter.Write("😀");
                    csvWriter.Write("ၑ");
                    asciiString = new string(characters);
                    csvWriter.Write(asciiString);
                    csvWriter.Write(utfString);
                    csvWriter.WriteEndOfLine();
                }

                using var csvReader = new CsvReader(fileName, csvConfig, maxLineLenght);
                var line = csvReader.ReadLine();
                Assert.AreEqual(header, line);

                for (int i = 0; i < characters.Length; i++)
                {
                    var c = csvReader.ReadChar();
                    characters[i] = c;
                    Assert.AreEqual(characters[i], c);
                }
                csvReader.ReadEndOfLine();
                Assert.IsNull(csvReader.ReadStringNull());
                Assert.AreEqual("", csvReader.ReadStringNull());
                Assert.AreEqual(" ", csvReader.ReadStringNull());
                Assert.AreEqual("a", csvReader.ReadStringNull());
                Assert.AreEqual(csvConfig.Delimiter.ToString(), csvReader.ReadStringNull());
                Assert.AreEqual("\\", csvReader.ReadStringNull());
                Assert.AreEqual("\\n", csvReader.ReadStringNull());
                Assert.AreEqual("\\r", csvReader.ReadStringNull());
                Assert.AreEqual("\\r\\n", csvReader.ReadStringNull());
                Assert.AreEqual("😀", csvReader.ReadStringNull());
                Assert.AreEqual("ၑ", csvReader.ReadStringNull());
                Assert.AreEqual(asciiString, csvReader.ReadStringNull());
                Assert.AreEqual(utfString, csvReader.ReadStringNull());
            } finally {
                directoryInfo.Delete(recursive: true);
            }
        }
Exemplo n.º 12
0
        public void TestCsvWriter()
        {
            var directoryInfo = new DirectoryInfo("TestCsv");

            try {
                if (directoryInfo.Exists)
                {
                    directoryInfo.Delete(recursive: true);
                    directoryInfo.Refresh();
                }

                directoryInfo.Create();
                directoryInfo.Refresh();

                var csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException);
                var fileName  = csvConfig.DirectoryPath + @"\TestCsvWriterInt.csv";
                using (var csvWriter = new CsvWriter(fileName, csvConfig, 250)) {
                    csvWriter.WriteLine("Some header");

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.Write(false);
                    csvWriter.Write(true);
                    csvWriter.WriteEndOfLine();

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.Write(int.MaxValue);
                    csvWriter.Write(1);
                    csvWriter.Write(0);
                    csvWriter.Write(-1);
                    csvWriter.Write(int.MinValue);
                    csvWriter.WriteEndOfLine();

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.Write((int?)null);
                    csvWriter.Write((int?)int.MaxValue);
                    csvWriter.Write((int?)1);
                    csvWriter.Write((int?)0);
                    csvWriter.Write((int?)-1);
                    csvWriter.Write((int?)int.MinValue);
                    csvWriter.WriteEndOfLine();

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.Write(long.MaxValue);
                    csvWriter.Write(1L);
                    csvWriter.Write(0L);
                    csvWriter.Write(-1L);
                    csvWriter.Write(long.MinValue);
                    csvWriter.WriteEndOfLine();

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.Write(decimal.MaxValue, 0);
                    csvWriter.Write(decimal.MaxValue);
                    csvWriter.Write(1234567890.12345678m, 8);
                    csvWriter.Write(30m, 2);
                    csvWriter.Write(1.1m, 1);
                    csvWriter.Write(1.1m, 0);
                    csvWriter.Write(1m, 1);
                    csvWriter.Write(decimal.One);
                    csvWriter.Write(0.9m, 1);
                    csvWriter.Write(0.9m, 0);
                    csvWriter.Write(0.4m, 0);
                    csvWriter.Write(decimal.Zero);
                    csvWriter.Write(-0.4m, 0);
                    csvWriter.Write(-0.9m, 0);
                    csvWriter.Write(-0.9m, 3);
                    csvWriter.Write(decimal.MinusOne);
                    csvWriter.Write(-1m, 1);
                    csvWriter.Write(-1.1m, 0);
                    csvWriter.Write(-1.1m, 1);
                    csvWriter.Write(-1234567890.12345678m, 8);
                    csvWriter.Write(decimal.MinValue);
                    csvWriter.Write(decimal.MinValue, 0);
                    csvWriter.Write((decimal)Math.PI);
                    csvWriter.WriteEndOfLine();

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.Write((decimal?)null, 0);
                    csvWriter.Write((decimal?)decimal.MaxValue, 0);
                    csvWriter.Write((decimal?)decimal.MaxValue);
                    csvWriter.Write((decimal?)1234567890.12345678m, 8);
                    csvWriter.Write((decimal?)30m, 2);
                    csvWriter.Write((decimal?)1.1m, 1);
                    csvWriter.Write((decimal?)1.1m, 0);
                    csvWriter.Write((decimal?)1m, 1);
                    csvWriter.Write((decimal?)decimal.One);
                    csvWriter.Write((decimal?)0.9m, 1);
                    csvWriter.Write((decimal?)0.9m, 0);
                    csvWriter.Write((decimal?)0.4m, 0);
                    csvWriter.Write((decimal?)decimal.Zero);
                    csvWriter.Write((decimal?)-0.4m, 0);
                    csvWriter.Write((decimal?)-0.9m, 0);
                    csvWriter.Write((decimal?)-0.9m, 3);
                    csvWriter.Write((decimal?)decimal.MinusOne);
                    csvWriter.Write((decimal?)-1m, 1);
                    csvWriter.Write((decimal?)-1.1m, 0);
                    csvWriter.Write((decimal?)-1.1m, 1);
                    csvWriter.Write((decimal?)-1234567890.12345678m, 8);
                    csvWriter.Write((decimal?)decimal.MinValue);
                    csvWriter.Write((decimal?)decimal.MinValue, 0);
                    csvWriter.Write((decimal?)Math.PI);
                    csvWriter.WriteEndOfLine();

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.Write('a'); //
                    csvWriter.Write('Ä'); //
                    csvWriter.Write('☹'); // Smiley with white frowning face
                    csvWriter.WriteEndOfLine();

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.Write((string?)null);
                    csvWriter.Write("");
                    csvWriter.Write("abc");
                    csvWriter.Write("Ä");
                    csvWriter.Write("aÄ☹");
                    csvWriter.WriteEndOfLine();

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.WriteDate(DateTime.MaxValue.Date);
                    csvWriter.WriteDate(DateTime.MinValue.Date);
                    csvWriter.WriteDate(new DateTime(2000, 1, 1));
                    csvWriter.WriteDate(new DateTime(2009, 12, 31));
                    csvWriter.WriteDate(new DateTime(2010, 1, 1));
                    csvWriter.WriteDate(new DateTime(2019, 12, 31));
                    csvWriter.WriteDate(new DateTime(2020, 1, 1));
                    csvWriter.WriteDate(new DateTime(2029, 12, 31));
                    csvWriter.WriteDate(new DateTime(2020, 1, 1));
                    csvWriter.WriteDate(new DateTime(2120, 1, 1));
                    csvWriter.WriteEndOfLine();

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.WriteTime(new TimeSpan(0, 0, 0));
                    csvWriter.WriteTime(new TimeSpan(0, 0, 1));
                    csvWriter.WriteTime(new TimeSpan(0, 1, 1));
                    csvWriter.WriteTime(new TimeSpan(1, 1, 1));
                    csvWriter.WriteTime(new TimeSpan(23, 0, 0));
                    csvWriter.WriteTime(new TimeSpan(23, 59, 0));
                    csvWriter.WriteTime(new TimeSpan(23, 59, 59));
                    csvWriter.WriteEndOfLine();

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.WriteDateTimeTicks(DateTime.MaxValue);
                    csvWriter.WriteDateTimeTicks(DateTime.MinValue);
                    csvWriter.WriteDateTimeTicks(new DateTime(2000, 1, 1));
                    csvWriter.WriteDateTimeTicks(new DateTime(2000, 1, 1, 1, 1, 1, 1));
                    csvWriter.WriteDateTimeTicks(new DateTime(2009, 12, 31));
                    csvWriter.WriteDateTimeTicks(new DateTime(2009, 12, 31, 23, 59, 59, 999));
                    csvWriter.WriteEndOfLine();

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.WriteDateMinutes(new DateTime(2000, 1, 1, 1, 1, 0, 0));
                    csvWriter.WriteDateMinutes(new DateTime(2009, 12, 31, 23, 59, 0, 0));
                    csvWriter.WriteDateMinutes(new DateTime(3009, 12, 31, 23, 0, 0, 0));
                    csvWriter.WriteDateMinutes(new DateTime(4009, 12, 31, 23, 0, 30, 0));
                    csvWriter.WriteEndOfLine();

                    csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                    csvWriter.WriteDateSeconds(new DateTime(2000, 1, 1, 1, 1, 1, 0));
                    csvWriter.WriteDateSeconds(new DateTime(2009, 12, 31, 23, 59, 59, 0));
                    csvWriter.WriteDateSeconds(new DateTime(3009, 12, 31, 23, 59, 0, 0));
                    csvWriter.WriteDateSeconds(new DateTime(4009, 12, 31, 23, 0, 0, 0));
                    csvWriter.WriteEndOfLine();

                    for (int i = -csvConfig.BufferSize; i < csvConfig.BufferSize; i++)
                    {
                        csvWriter.WriteFirstLineChar(csvConfig.LineCharAdd);
                        csvWriter.Write(i / 1000m, 2);
                        csvWriter.WriteEndOfLine();
                    }
                }

                using var fileStream   = new FileStream(fileName, FileMode.Open);
                using var streamReader = new StreamReader(fileStream);
                var line = streamReader.ReadLine();
                Assert.AreEqual("Some header", line);

                line = streamReader.ReadLine();
                Assert.AreEqual(csvConfig.LineCharAdd, line ![0]);