Beispiel #1
0
        public void MapWithDateTimeStruct()
        {
            var rowMapper = new RowMapper <Entities.Race>();

            var mockedDataRecord = A.Fake <IDataRecord>();

            var expectedInt    = 15;
            var expectedString = "Test";
            var expectedDate   = new DateTime(2019, 11, 13);

            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("Id")]).Returns(expectedInt);
            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("RaceTypeId")]).Returns(expectedInt);
            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("VenueId")]).Returns(expectedInt);
            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("FirstStartListId")]).Returns(expectedInt);
            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("SecondStartListId")]).Returns(expectedInt);
            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("NumberOfSensors")]).Returns(expectedInt);
            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("Description")]).Returns(expectedString);
            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("Date")]).Returns(expectedDate);
            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("GenderSpecificRaceId")]).Returns(expectedInt);

            var mappedStartPosition = rowMapper.Map(mockedDataRecord);

            Assert.Equal(expectedInt, mappedStartPosition.Id);
            Assert.Equal(expectedInt, mappedStartPosition.RaceTypeId);
            Assert.Equal(expectedInt, mappedStartPosition.VenueId);
            Assert.Equal(expectedInt, mappedStartPosition.FirstStartListId);
            Assert.Equal(expectedInt, mappedStartPosition.SecondStartListId);
            Assert.Equal(expectedInt, mappedStartPosition.NumberOfSensors);
            Assert.Equal(expectedString, mappedStartPosition.Description);
            Assert.Equal(expectedDate, mappedStartPosition.Date);
            Assert.Equal(expectedInt, mappedStartPosition.GenderSpecificRaceId);
        }
Beispiel #2
0
        public async Task <IEnumerable <T> > QueryObjectSetAsync <T>(
            string sqlQuery,
            RowMapper <T> rowMapper,
            params QueryParameter[] queryParameters) where T : new()
        {
            using var dbConnection = await connectionFactory.CreateConnectionAsync()
                                     .ConfigureAwait(false);

            using var dbCommand = dbConnection.CreateCommand();

            dbCommand.CommandText = sqlQuery;
            dbCommand.AddParameters(queryParameters);

            var resultItems = new List <T>();

            using (var dbDataReader = await dbCommand.ExecuteReaderAsync().ConfigureAwait(false))
            {
                while (await dbDataReader.ReadAsync().ConfigureAwait(false))
                {
                    resultItems.Add(rowMapper.Map(dbDataReader));
                }
            }

            return(resultItems);
        }
Beispiel #3
0
        private IList <T> ProcessMap(ExcelWorksheet sheet)
        {
            var result = new List <T>();

            if (sheet.Dimension == null)
            {
                throw new TableMappingException(string.Format(Strings.XlsxPageEmpty, _sheetName), 0);
            }

            var header = new string[sheet.Dimension.End.Column];

            if (MappingOptions.HasHeader)
            {
                for (var column = 1; column <= sheet.Dimension.End.Column; column++)
                {
                    header[column - 1] = sheet.Cells[1, column]
                                         ?.Text
                                         ?.Replace("\r", string.Empty)
                                         .Replace("\n", string.Empty)
                                         .Trim()
                                         .ToLower() ?? string.Empty;
                }
            }

            int headerCorrect = (MappingOptions.HasHeader ? 1 : 0);
            int startRow      = 1 + headerCorrect;
            int row           = 0;
            int indexRow      = row + headerCorrect;

            for (var rowI = startRow; rowI <= sheet.Dimension.End.Row; rowI++, row++, indexRow++)
            {
                ThrowIfRowsLimitEnabled(row, indexRow);

                var rowResult = new string[sheet.Dimension.End.Column];

                for (var column = 1; column <= sheet.Dimension.End.Column; column++)
                {
                    var value = sheet.Cells[rowI, column]?.Value?.ToString();

                    if (MappingOptions.Trim)
                    {
                        value = value?.Trim();
                    }

                    rowResult[column - 1] = value;
                }

                var entity = RowMapper.Map(rowResult, header, indexRow, MappingOptions.SuppressConvertTypeErrors);
                ValidateRow(entity, indexRow);
                result.Add(entity);
            }

            return(result);
        }
Beispiel #4
0
        private void ProcessImport(TextFieldParser parser, CancellationToken?ct = null)
        {
            int row      = 0;
            int indexRow = row + (MappingOptions.HasHeader ? 1 : 0);

            string[] header = { };


            while (!parser.EndOfData)
            {
                if (ct.HasValue && ct.Value.IsCancellationRequested)
                {
                    ct.Value.ThrowIfCancellationRequested();
                }

                ThrowIfRowsLimitEnabled(indexRow);

                if (row == 0)
                {
                    if (MappingOptions.HasHeader)
                    {
                        header = parser.ReadFields();
                        header = header?.Select(x => x.ToLower()).ToArray()
                                 ?? throw new TableMappingException(Strings.HeaderRowIsEmpty, indexRow);
                    }
                }
                else
                {
                    var fields = parser.ReadFields();
                    if (fields == null)
                    {
                        continue;
                    }

                    var entity = RowMapper.Map(fields, header, indexRow, MappingOptions.SuppressConvertTypeErrors);

                    ValidateRow(entity, indexRow);

                    RowSaver.SaveRow(entity);
                }
                row++;
                indexRow++;
            }

            RowSaver.SaveRemainder();
        }
Beispiel #5
0
        public void MapWithNullRow()
        {
            var rowMapper = new RowMapper <Entities.StartPosition>();

            try
            {
                rowMapper.Map(null);
                Assert.False(true, "ArgumentNullException expected");
            }
            catch (ArgumentNullException e)
            {
                string parameterName = rowMapper.GetType()
                                       .GetMethod(nameof(rowMapper.Map))
                                       .GetParameters()
                                       .Select(p => p.Name)
                                       .FirstOrDefault();

                Assert.Contains(parameterName, e.Message, StringComparison.InvariantCultureIgnoreCase);
            }
        }
Beispiel #6
0
        private IList <T> ProcessMap(TextFieldParser parser)
        {
            int row      = 0;
            int indexRow = row + (MappingOptions.HasHeader ? 1 : 0);

            string[] header = { };

            var result = new List <T>();

            while (!parser.EndOfData)
            {
                ThrowIfRowsLimitEnabled(row, indexRow);

                if (row == 0)
                {
                    if (MappingOptions.HasHeader)
                    {
                        header = parser.ReadFields();
                        header = header?.Select(x => x.ToLower()).ToArray()
                                 ?? throw new TableMappingException(Strings.HeaderRowIsEmpty, indexRow);
                    }
                }
                else
                {
                    var fields = parser.ReadFields();
                    if (fields == null)
                    {
                        continue;
                    }

                    var entity = RowMapper.Map(fields, header, indexRow, MappingOptions.SuppressConvertTypeErrors);

                    ValidateRow(entity, indexRow);

                    result.Add(entity);
                }
                row++;
                indexRow++;
            }
            return(result);
        }
Beispiel #7
0
        public void MapStartPosition()
        {
            var rowMapper = new RowMapper <Entities.StartPosition>();

            var mockedDataRecord = A.Fake <IDataRecord>();

            int expectedId          = 500;
            int expectedStartListId = 501;
            int expectedSkierId     = 502;
            int expectedPosition    = 503;

            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("Id")]).Returns(expectedId);
            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("StartListId")]).Returns(expectedStartListId);
            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("SkierId")]).Returns(expectedSkierId);
            A.CallTo(() => mockedDataRecord[A <string> .That.IsEqualTo("Position")]).Returns(expectedPosition);

            var mappedStartPosition = rowMapper.Map(mockedDataRecord);

            Assert.Equal(expectedId, mappedStartPosition.Id);
            Assert.Equal(expectedStartListId, mappedStartPosition.StartListId);
            Assert.Equal(expectedSkierId, mappedStartPosition.SkierId);
            Assert.Equal(expectedPosition, mappedStartPosition.Position);
        }