Exemplo n.º 1
0
        public void Test_ReadSheet_Data(int firstRow, int?lastRow, int?expectedCount)
        {
            var service = GetService();
            DataFileReaderSettings settings;

            using (Stream stream = SheetTestHelper.GetStream("TestSheet.xlsx"))
            {
                settings = SheetTestHelper.GetSettings(stream, "TestSheet");
                settings.FirstDataRowNumber = 10;
                settings.LastDataRowNumber  = lastRow;
            }
            using (Stream stream = SheetTestHelper.GetStream("TestSheet.xlsx"))
                using (IObjectsReader reader = service.OpenDataFile(stream, settings))
                {
                    int count = 0;
                    foreach (IObjectReader obj in reader.GetObjects( ))
                    {
                        string column   = "A";
                        string dataType = obj.GetString(column);
                        Assert.That(dataType, Is.Not.Null.Or.Empty);
                        count++;
                        if (count == 20)
                        {
                            break; // that's enough
                        }
                    }
                    Assert.That(count, Is.GreaterThan(0));
                    if (expectedCount != null)
                    {
                        Assert.That(count, Is.EqualTo(expectedCount.Value));
                    }
                }
        }
Exemplo n.º 2
0
        public void Test_ReadSheet_NoSheets( )
        {
            var service = GetService( );
            DataFileReaderSettings settings = new DataFileReaderSettings( );

            using (Stream stream = SheetTestHelper.GetStream("NoSheets.xlsx"))
                using (IObjectsReader reader = service.OpenDataFile(stream, settings))
                {
                    int count = reader.GetObjects( ).Count( );
                    Assert.That(count, Is.EqualTo(0));
                }
        }
Exemplo n.º 3
0
        public void Test_ReadSheet_BlankRows(string sheet, int?lastRow, int expected)
        {
            var service = GetService( );
            DataFileReaderSettings settings;

            using (Stream stream = SheetTestHelper.GetStream("Test File.xlsx"))
            {
                settings = SheetTestHelper.GetSettings(stream, sheet);
                settings.LastDataRowNumber = lastRow;
            }
            using (Stream stream = SheetTestHelper.GetStream("Test File.xlsx"))
                using (IObjectsReader reader = service.OpenDataFile(stream, settings))
                {
                    int count = reader.GetObjects( ).Count( );
                    Assert.That(count, Is.EqualTo(expected));
                }
        }
Exemplo n.º 4
0
        private List <IObjectReader> TestRecords(string csv, DataFileReaderSettings settings, int expectedCount)
        {
            var service = GetService( );
            List <IObjectReader> objects;

            using (Stream stream = SheetTestHelper.GetCsvStream(csv))
                using (IObjectsReader objectsReader = service.OpenDataFile(stream, settings ?? new DataFileReaderSettings {
                    ImportFormat = ImportFormat.Tab
                }))
                {
                    objects = objectsReader.GetObjects( ).ToList( );
                }

            Assert.That(objects, Is.Not.Null, "objects");
            Assert.That(objects.Count, Is.EqualTo(expectedCount));
            return(objects);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Start processing an import run, after checking has been performed.
        /// </summary>
        /// <param name="importRun">A writable import run entity. Caller will save.</param>
        internal void StartImportSafe(ImportRun importRun)
        {
            ImportConfig importConfig = importRun.ImportConfigUsed;

            if (importConfig == null)
            {
                throw new ConnectorConfigException("Import configuration could not be loaded.");
            }
            if (importConfig.ImportConfigMapping == null)
            {
                throw new ConnectorConfigException("Import configuration has no mapping.");
            }

            IObjectsReader         recordsReader = null;
            IReaderToEntityAdapter entityAdapter;
            IImportReporter        importRunReporter;
            IRecordImporter        recordImporter;
            ICancellationWatcher   cancellationWatcher;
            BatchRunner            batchRunner;

            try
            {
                // Reads records out of the file to count
                recordsReader = GetRecordsReader(importRun, importConfig);
                importRun.ImportRecordsTotal = recordsReader.GetObjects( ).Count( );
                importRun.Save( );

                // Re-reads records out of the file for importing
                recordsReader = GetRecordsReader(importRun, importConfig);

                // Writes records into entities
                entityAdapter = GetEntityAdapter(importConfig);

                // Create a reporter to process progress notifications
                importRunReporter = new ImportRunReporter(importRun);

                // Create a reporter to process progress notifications
                cancellationWatcher = new ImportRunCancellationWatcher(importRun);

                // Activate record impoter
                bool testRun = importRun.ImportTestRun == true;
                recordImporter = _recordImporterActivator(entityAdapter, importRunReporter, importConfig.ImportConfigMapping, testRun);
                if (recordImporter == null)
                {
                    throw new Exception("recordImporter failed to activate.");
                }

                // Connects the reader to the writer
                batchRunner = new BatchRunner
                {
                    ObjectsReader       = recordsReader,
                    RecordImporter      = recordImporter,
                    CancellationWatcher = cancellationWatcher
                };

                // Go! Run as user
                using (new SecurityBypassContext(false))
                {
                    batchRunner.ProcessAll( );
                }
            }
            finally
            {
                recordsReader?.Dispose( );
            }
        }
Exemplo n.º 6
0
        public void Test_Scenarios(int rowNum)
        {
            var service = GetService( );
            DataFileReaderSettings settings;

            using (Stream stream = SheetTestHelper.GetStream("TestSheet.xlsx"))     // IMPORTANT: Ensure TestRowNumbers has the right number of rows
            {
                settings = SheetTestHelper.GetSettings(stream, "TestSheet");
                settings.FirstDataRowNumber = rowNum;
            }
            using (Stream stream = SheetTestHelper.GetStream("TestSheet.xlsx"))
                using (IObjectsReader reader = service.OpenDataFile(stream, settings))
                {
                    IObjectReader obj = reader.GetObjects().First();

                    string   actualColRef    = "D";
                    string[] expectedColumns = { "E", "G", "I", "K", "M", "O", "Q" };

                    foreach (string expectedColumn in expectedColumns)
                    {
                        string expectedNative = obj.GetString(expectedColumn);

                        if (string.IsNullOrEmpty(expectedNative))
                        {
                            continue;
                        }

                        switch (expectedColumn)
                        {
                        case "E": // String
                            string actualString     = obj.GetString(actualColRef);
                            string actualSingleLine = StringHelpers.ToSingleLine(actualString);
                            Assert.That(actualSingleLine, Is.EqualTo(expectedNative));
                            break;

                        case "G": // Number
                            int?actualNumber   = obj.GetInt(actualColRef);
                            int expectedNumber = int.Parse(expectedNative, CultureInfo.InvariantCulture);
                            Assert.That(actualNumber, Is.EqualTo(expectedNumber));
                            break;

                        case "I": // Decimal
                            decimal?actualDecimal   = obj.GetDecimal(actualColRef);
                            decimal expectedDecimal = decimal.Parse(expectedNative, CultureInfo.InvariantCulture);
                            Assert.That(actualDecimal, Is.EqualTo(expectedDecimal));
                            break;

                        case "K": // Boolean
                            bool?actualBool   = obj.GetBoolean(actualColRef);
                            bool expectedBool = expectedNative == "Yes" || expectedNative != "No" && bool.Parse(expectedNative);
                            Assert.That(actualBool, Is.EqualTo(expectedBool));
                            break;

                        case "M": // DateTime
                            DateTime?actualDateTime   = obj.GetDateTime(actualColRef);
                            DateTime expectedDateTime = DateTime.Parse(expectedNative, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
                            Assert.That(actualDateTime, Is.EqualTo(expectedDateTime));
                            break;

                        case "O": // Date
                            DateTime?actualDate   = obj.GetDate(actualColRef);
                            DateTime expectedDate = DateTime.Parse(expectedNative, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
                            Assert.That(actualDate, Is.EqualTo(expectedDate));
                            break;

                        case "Q": // Time
                            DateTime?actualTime   = obj.GetTime(actualColRef);
                            DateTime expectedTime = DateTime.Parse(expectedNative, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
                            Assert.That(actualTime, Is.EqualTo(expectedTime));
                            break;
                        }
                    }
                }
        }