public void ProcessValue_DBNull_to_NullabeType(Type targetType) { var result = ValueMapper.ProcessValue(targetType, DBNull.Value); result.Should().Be(null); ValidateIsAssignable(targetType, result); }
public void ProcessValue_Null_to_DateTime() { var result = ValueMapper.ProcessValue(typeof(DateTime), null); result.Should().Be(default(DateTime)); var date = default(DateTime); }
//[InlineData(MyEnum.One, "1.0")] public void ProcessValue_value_to_Enum(Type targetType, object expected, object value) { var result = ValueMapper.ProcessValue(targetType, value); result.Should().BeOfType(targetType); result.Should().Be(expected); }
public static T ExecuteScalar <T>(this DbConnection connection, string commandText, object[] parameters = null, DbTransaction transaction = null, IExceptionProcessor exceptionProcessor = null) { var result = ExecuteScalar(connection, commandText, parameters, transaction, exceptionProcessor); return(ValueMapper.ProcessValue <T>(result)); }
public void ProcessValue_byteArray_to_Guid() { var targetType = typeof(Guid); var expected = Guid.NewGuid(); var value = expected.ToByteArray(); ValueMapper.ProcessValue(targetType, value).Should().Be(expected); }
public void ProcessValue_byteArray_to_string() { var targetType = typeof(string); var expected = "something"; var value = Encoding.UTF8.GetBytes(expected); ValueMapper.ProcessValue(targetType, value).Should().Be(expected); }
public void ProcessValue_empty_string_to_NullableType(Type targetType) { var value = ""; var result = ValueMapper.ProcessValue(targetType, value); Output.WriteLine(result?.ToString() ?? "<null>"); result.Should().BeNull(); ValidateIsAssignable(targetType, result); }
private static object Insert(this DbConnection connection, object data, string tableName, IFieldInfoCollection fields, DbTransaction transaction = null, OnConflictOption option = OnConflictOption.Default, ICommandBuilder commandBuilder = null, IExceptionProcessor exceptionProcessor = null, object keyValue = null, bool persistKeyvalue = true) { if (data == null) { throw new ArgumentNullException("data"); } commandBuilder = commandBuilder ?? DefaultCommandBuilder; using (var command = connection.CreateCommand()) { commandBuilder.BuildInsert(command, data, tableName, fields, option: option, keyValue: keyValue, persistKeyvalue: persistKeyvalue); try { var ptData = data as IPersistanceTracking; ptData?.OnInserting(); var id = command.ExecuteScalar(); ptData?.OnInserted(); var pkField = fields.PrimaryKeyField; if (pkField != null) { var value = ValueMapper.ProcessValue(pkField.RunTimeType, id); pkField.SetFieldValue(data, value); return(value); } else { return(id); } } catch (Exception ex) { if (exceptionProcessor is null) { throw; } else { throw exceptionProcessor.ProcessException(ex, connection, (string)null, transaction); } } } }
public void ProcessValue_string_to_DateTime(string value) { var targetType = typeof(DateTime); var expected = DateTime.Parse(value); var result = ValueMapper.ProcessValue(targetType, value); result.Should().BeOfType <DateTime>(); ((DateTime)result).Should().BeCloseTo(expected, 1000); }
// ReadData depends on this behavior public void ProcessValue_jiberrishString_to_NullableGuid() { var value = "jibberish"; Action a = () => { ValueMapper.ProcessValue(typeof(Guid?), value).Should().BeNull(); }; a.Should().Throw <FormatException>(); }
public void ProcessValue_value_to_Type(Type targetType, object value, object expected) { var result = ValueMapper.ProcessValue(targetType, value); result.Should().Be(expected); if (result != null) { result.GetType().Should().Be(Nullable.GetUnderlyingType(targetType) ?? targetType); } ValidateIsAssignable(targetType, result); }
public void ProcessValue_empty_string_to_DateTime() { var targetType = typeof(DateTime); var expected = default(DateTime); var value = ""; var result = ValueMapper.ProcessValue(targetType, value); result.Should().BeOfType <DateTime>(); ((DateTime)result).Should().BeCloseTo(expected, 1000); }
public void ProcessValue_DBnull_to_valueType(Type targetType) { var value = DBNull.Value; var expected = Activator.CreateInstance(targetType); var result = ValueMapper.ProcessValue(targetType, value); ValidateIsAssignable(targetType, result); if (expected == null) { expected = value; } result.Should().Be(expected); Output.WriteLine(result?.ToString() ?? "<null>"); }
public static IEnumerable <TResult> QueryScalar2 <TResult>(this DbConnection connection, string commandText, object paramaters = null, DbTransaction transaction = null, IExceptionProcessor exceptionProcessor = null) { var targetType = typeof(TResult); using (var command = connection.CreateCommand()) { command.CommandText = commandText; command.AddParams(paramaters); using (var reader = connection.ExecuteReader(command, transaction: transaction, exceptionProcessor: exceptionProcessor)) { while (reader.Read()) { TResult value = default(TResult); try { var dbValue = reader.GetValue(0); value = (TResult)ValueMapper.ProcessValue(targetType, dbValue); } catch (Exception e) { if (exceptionProcessor != null) { throw exceptionProcessor.ProcessException(e, connection, commandText, transaction); } else { throw; } } yield return(value); } } } }
public void ProcessValue_DBNull_to_string() { var result = ValueMapper.ProcessValue(typeof(string), DBNull.Value); result.Should().Be(null); }
public void ProcessValue_DBNull_to_DateTime() { var result = ValueMapper.ProcessValue(typeof(DateTime), DBNull.Value); result.Should().Be(default(DateTime)); }
public void ProcessValue_jiberrishString_to_NullableValue(Type targetType) { var value = "jibberish"; ValueMapper.ProcessValue(targetType, value).Should().BeNull(); }