public void TestUnsupportedOperations() { var generator = new TestFactoryBuilder().Build(); Assert.Throws <AccessorGeneratorException>(() => generator.Create <object>()); Assert.Throws <AccessorGeneratorException>(() => generator.Create <IInvalidMethodAccessor>()); }
public void TestReplace() { using (TestDatabase.Initialize() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "BBB" }) .InsertData(new DataEntity { Id = 2, Name = "CCC" }) .InsertData(new DataEntity { Id = 3, Name = "AAA" })) { var generator = new TestFactoryBuilder() .UseFileDatabase() .SetSql("SELECT * FROM Data ORDER BY /*# sort */") .Build(); var accessor = generator.Create <IRawAccessor>(); var list = accessor.QueryData("Id"); Assert.Equal(1, list[0].Id); list = accessor.QueryData("Id DESC"); Assert.Equal(3, list[0].Id); list = accessor.QueryData("Name DESC"); Assert.Equal(2, list[0].Id); } }
public void TestTransaction() { using (var con = TestDatabase.Initialize() .SetupDataTable()) { var generator = new TestFactoryBuilder() .SetSql("INSERT INTO Data (Id, Name) VALUES (/*@ id */1, /*@ name */'test')") .Build(); var accessor = generator.Create <ITransactionAccessor>(); con.Open(); using (var tx = con.BeginTransaction()) { var effect = accessor.Execute(tx, 1L, "xxx"); Assert.Equal(1, effect); tx.Rollback(); } var entity = con.QueryData(1L); Assert.Null(entity); using (var tx = con.BeginTransaction()) { var effect = accessor.Execute(tx, 1L, "xxx"); Assert.Equal(1, effect); tx.Commit(); } entity = con.QueryData(1L); Assert.NotNull(entity); } }
public void TestObjectParameter() { var generator = new TestFactoryBuilder() .Build(); var accessor = generator.Create <IObjectProcedureAccessor>(); var con = new MockDbConnection(); con.SetupCommand(cmd => { cmd.Executing = c => { Assert.Equal("PROC", c.CommandText); Assert.Equal(1, c.Parameters["param1"].Value); Assert.Equal(2, c.Parameters["param2"].Value); c.Parameters["param2"].Value = 3; c.Parameters["param3"].Value = 4; c.Parameters.OfType <MockDbParameter>().First(x => x.Direction == ParameterDirection.ReturnValue).Value = 5; }; cmd.SetupResult(100); }); var param2 = (object)2; var ret = accessor.Call(con, 1, ref param2, out var param3); Assert.Equal(3, param2); Assert.Equal(4, param3); Assert.Equal(5, ret); }
public async ValueTask TestExecuteReaderSimpleAsync() { await using (TestDatabase.Initialize() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "Data-1" }) .InsertData(new DataEntity { Id = 2, Name = "Data-2" })) { var generator = new TestFactoryBuilder() .UseFileDatabase() .SetSql("SELECT * FROM Data ORDER BY Id") .Build(); var accessor = generator.Create <IExecuteReaderSimpleAsyncAccessor>(); using (var reader = await accessor.ExecuteReaderAsync()) { Assert.True(reader.Read()); Assert.True(reader.Read()); Assert.False(reader.Read()); } } }
public void TestTypeHandlerForEnumUnderlyingType() { var generator = new TestFactoryBuilder() .SetSql("/*@ value */") .Config(config => { config.ConfigureTypeHandlers(handlers => { handlers[typeof(int)] = new ToStringTypeHandler(); }); }) .Build(); var accessor = generator.Create <IEnumAccessor>(); var con = new MockDbConnection(); con.SetupCommand(cmd => { cmd.Executing = c => { Assert.Equal(DbType.String, c.Parameters[0].DbType); Assert.Equal(nameof(Value.One), c.Parameters[0].Value); }; cmd.SetupResult(1); }); accessor.Execute(con, Value.One); }
public void TestCustomUsing() { using (TestDatabase.Initialize() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "Data-1" }) .InsertData(new DataEntity { Id = 2, Name = "Data-2" }) .InsertData(new DataEntity { Id = 3, Name = "Data-3" })) { var generator = new TestFactoryBuilder() .UseFileDatabase() .SetSql( "/*!using Smart.Mock */" + "SELECT * FROM Data" + "/*% if (CustomScriptHelper.HasValue(id)) { */" + "WHERE Id >= /*@ id */0" + "/*% } */") .Build(); var accessor = generator.Create <IDynamicAccessor>(); var list = accessor.QueryData(null); Assert.Equal(3, list.Count); list = accessor.QueryData(2); Assert.Equal(2, list.Count); } }
public void TestForEach() { var generator = new TestFactoryBuilder() .SetSql( "WHERE ((1 = 0)" + "/*% foreach (var p in parameters) { */" + "OR (Key1 = /*@ p.Key1 */ 1 AND Key2 = /*@ p.Key2 */ 'a')" + "/*% } */" + ")") .Build(); var accessor = generator.Create <IForEachAccessor>(); var con = new MockDbConnection(); con.SetupCommand(cmd => { cmd.Executing = c => { Assert.Equal( "WHERE ((1 = 0) OR (Key1 = @_dp0 AND Key2 = @_dp1) OR (Key1 = @_dp2 AND Key2 = @_dp3) OR (Key1 = @_dp4 AND Key2 = @_dp5))", c.CommandText); Assert.Equal(6, c.Parameters.Count); Assert.Equal("@_dp0", c.Parameters[0].ParameterName); Assert.Equal(DbType.Int32, c.Parameters[0].DbType); Assert.Equal(1, c.Parameters[0].Value); Assert.Equal("@_dp1", c.Parameters[1].ParameterName); Assert.Equal(DbType.String, c.Parameters[1].DbType); Assert.Equal("a", c.Parameters[1].Value); Assert.Equal("@_dp2", c.Parameters[2].ParameterName); Assert.Equal(DbType.Int32, c.Parameters[2].DbType); Assert.Equal(2, c.Parameters[2].Value); Assert.Equal("@_dp3", c.Parameters[3].ParameterName); Assert.Equal(DbType.String, c.Parameters[3].DbType); Assert.Equal("b", c.Parameters[3].Value); Assert.Equal("@_dp4", c.Parameters[4].ParameterName); Assert.Equal(DbType.Int32, c.Parameters[4].DbType); Assert.Equal(3, c.Parameters[4].Value); Assert.Equal("@_dp5", c.Parameters[5].ParameterName); Assert.Equal(DbType.String, c.Parameters[5].DbType); Assert.Equal("c", c.Parameters[5].Value); }; cmd.SetupResult(1); }); var parameters = new[] { new Parameter { Key1 = 1, Key2 = "a" }, new Parameter { Key1 = 2, Key2 = "b" }, new Parameter { Key1 = 3, Key2 = "c" } }; accessor.Execute(con, parameters); }
public void TestArrayMixed() { using (TestDatabase.Initialize() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "AAA" }) .InsertData(new DataEntity { Id = 2, Name = "AAA" }) .InsertData(new DataEntity { Id = 3, Name = "BBB" }) .InsertData(new DataEntity { Id = 4, Name = "BBB" })) { var generator = new TestFactoryBuilder() .UseFileDatabase() .SetSql("SELECT * FROM Data WHERE Id IN /*@ ids */(2, 4) AND Name = /*@ name */'AAA'") .Build(); var accessor = generator.Create <IInArrayMixedAccessor>(); var list = accessor.QueryData(new[] { 2, 4 }, "AAA"); Assert.Equal(1, list.Count); } }
public void TestSimple() { var generator = new TestFactoryBuilder() .SetSql("/*% var id = value; */ WHERE Id = /*@ id */1") .Build(); var accessor = generator.Create <ISimpleAccessor>(); var con = new MockDbConnection(); con.SetupCommand(cmd => { cmd.Executing = c => { Assert.Equal(DbType.Int32, c.Parameters[0].DbType); Assert.Equal(1, c.Parameters[0].Value); }; cmd.SetupResult(1); }); con.SetupCommand(cmd => { cmd.Executing = c => { Assert.Equal(DbType.Int32, c.Parameters[0].DbType); Assert.Equal(2, c.Parameters[0].Value); }; cmd.SetupResult(1); }); accessor.Execute(con, 1); accessor.Execute(con, 2); }
public void TestQueryFirstOrDefaultWithConnection() { using (var con = TestDatabase.Initialize() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "Data-1" })) { var generator = new TestFactoryBuilder() .SetSql("SELECT * FROM Data WHERE Id = /*@ id */1") .Build(); var accessor = generator.Create <IQueryFirstOrDefaultWithConnectionAccessor>(); con.Open(); var entity = accessor.QueryFirstOrDefault(con, 1L); Assert.Equal(ConnectionState.Open, con.State); Assert.NotNull(entity); Assert.Equal(1, entity.Id); Assert.Equal("Data-1", entity.Name); entity = accessor.QueryFirstOrDefault(con, 2L); Assert.Null(entity); } }
public void PropertySelect() { using (TestDatabase.Initialize() .SetupDataTable()) { var generator = new TestFactoryBuilder() .UseMemoryDatabase() .SetSql( "SELECT " + "1 AS ColumnName1, " + "2 AS columnName2, " + "3 AS column_name3, " + "4 AS COLUMN_NAME4, " + "5 AS column_name5, " + "6 AS ColumnName6, " + "7 AS ColumnName7") .Build(); var accessor = generator.Create <IPropertySelectorAccessor>(); var entity = accessor.QueryFirstOrDefault(); Assert.NotNull(entity); Assert.Equal(1, entity.ColumnName1); Assert.Equal(2, entity.ColumnName2); Assert.Equal(3, entity.ColumnName3); Assert.Equal(4, entity.ColumnName4); Assert.Equal(5, entity.ColumnName5); Assert.Equal(0, entity.ColumnName6); } }
public void TestListHandler() { var generator = new TestFactoryBuilder() .SetSql("/*% var ids = values; */ WHERE Id IN /*@ ids */(1)") .Config(config => { config.ConfigureTypeHandlers(c => { c[typeof(string)] = new DummyTypeHandler(); }); }) .Build(); var accessor = generator.Create <IListHandlerAccessor>(); var con = new MockDbConnection(); con.SetupCommand(cmd => { cmd.Executing = c => { Assert.Equal(5, c.Parameters[0].Size); Assert.Equal(5, c.Parameters[1].Size); }; cmd.SetupResult(0); }); accessor.Execute(con, new List <string>(new[] { "1", "2" })); var controller = (IEngineController)generator.Engine; Assert.Equal(1, controller.Diagnostics.DynamicSetupCacheCount); controller.ClearDynamicSetupCache(); Assert.Equal(0, controller.Diagnostics.DynamicSetupCacheCount); }
public async ValueTask TestQueryFirstOrDefaultSimpleAsync() { await using (TestDatabase.Initialize() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "Data-1" })) { var generator = new TestFactoryBuilder() .UseFileDatabase() .SetSql("SELECT * FROM Data WHERE Id = /*@ id */1") .Build(); var accessor = generator.Create <IQueryFirstOrDefaultSimpleAsyncAccessor>(); var entity = await accessor.QueryFirstOrDefaultAsync(1L); Assert.NotNull(entity); Assert.Equal(1, entity.Id); Assert.Equal("Data-1", entity.Name); entity = await accessor.QueryFirstOrDefaultAsync(2L); Assert.Null(entity); } }
public void TestProviderForQuery() { using (TestDatabase.Initialize() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "Data-1" }) .InsertData(new DataEntity { Id = 2, Name = "Data-2" })) using (TestDatabase.Initialize2() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "Data-1" })) { var generator = new TestFactoryBuilder() .UseMultipleDatabase() .SetSql("SELECT * FROM Data") .Build(); var accessor = generator.Create <IProviderForQueryAccessor>(); var count = accessor.Query().Count(); Assert.Equal(2, count); var count2 = accessor.Query2().Count(); Assert.Equal(1, count2); } }
public async ValueTask TestQueryBufferdSimpleAsync() { await using (TestDatabase.Initialize() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "Data-1" }) .InsertData(new DataEntity { Id = 2, Name = "Data-2" })) { var generator = new TestFactoryBuilder() .UseFileDatabase() .SetSql("SELECT * FROM Data ORDER BY Id") .Build(); var accessor = generator.Create <IQuerySimpleAsyncAccessor>(); var list = await accessor.QueryBufferdAsync(); Assert.Equal(2, list.Count); Assert.Equal(1, list[0].Id); Assert.Equal("Data-1", list[0].Name); Assert.Equal(2, list[1].Id); Assert.Equal("Data-2", list[1].Name); } }
public async ValueTask TestExecuteReaderWithConnectionAsync() { await using (var con = TestDatabase.Initialize() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "Data-1" }) .InsertData(new DataEntity { Id = 2, Name = "Data-2" })) { var generator = new TestFactoryBuilder() .SetSql("SELECT * FROM Data ORDER BY Id") .Build(); con.Open(); var accessor = generator.Create <IExecuteReaderWithConnectionAsyncAccessor>(); Assert.Equal(ConnectionState.Open, con.State); using (var reader = await accessor.ExecuteReaderAsync(con)) { Assert.True(reader.Read()); Assert.True(reader.Read()); Assert.False(reader.Read()); } Assert.Equal(ConnectionState.Open, con.State); } }
public async ValueTask TestQueryNonBufferdWithConnectionAsync() { await using (var con = TestDatabase.Initialize() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "Data-1" }) .InsertData(new DataEntity { Id = 2, Name = "Data-2" })) { var generator = new TestFactoryBuilder() .SetSql("SELECT * FROM Data ORDER BY Id") .Build(); var accessor = generator.Create <IQueryWithConnectionAsyncAccessor>(); con.Open(); var list = await accessor.QueryNonBufferdAsync(con).ToListAsync(); Assert.Equal(ConnectionState.Open, con.State); Assert.Equal(2, list.Count); Assert.Equal(1, list[0].Id); Assert.Equal("Data-1", list[0].Name); Assert.Equal(2, list[1].Id); Assert.Equal("Data-2", list[1].Name); } }
public void TestInvalidTimeoutParameter() { var generator = new TestFactoryBuilder() .SetSql(string.Empty) .Build(); Assert.Throws <AccessorGeneratorException>(() => generator.Create <IInvalidTimeoutParameterAccessor>()); }
public void TestQueryFirstOrDefaultInvalid() { var generator = new TestFactoryBuilder() .SetSql(string.Empty) .Build(); Assert.Throws <AccessorGeneratorException>(() => generator.Create <IQueryFirstOrDefaultInvalidAccessor>()); }
public void TestExecuteInvalidAsync() { var generator = new TestFactoryBuilder() .SetSql(string.Empty) .Build(); Assert.Throws <AccessorGeneratorException>(() => generator.Create <IExecuteInvalidAsyncAccessor>()); }
public void TestInvalidSqlSize() { var generator = new TestFactoryBuilder() .SetSql("SELECT * FROM Data WHERE /*% // dummy */") .Build(); var accessor = generator.Create <IInvalidSqlSizeAccessor>(); Assert.Throws <ArgumentOutOfRangeException>(() => accessor.Execute(new MockDbConnection())); }
public void TestSetupFailedList() { var generator = new TestFactoryBuilder() .SetSql("/*@ value */") .Config(config => { config.ConfigureTypeMap(map => map.Clear()); }) .Build(); Assert.Throws <AccessorGeneratorException>(() => generator.Create <ISetupFailedListAccessor>()); }
public void TestParameterConvert() { var generator = new TestFactoryBuilder() .Build(); var accessor = generator.Create <IParameterAccessor>(); var con = new MockDbConnection(); con.SetupCommand(cmd => { cmd.Executing = c => { Assert.Equal(13, c.Parameters.Count); Assert.Equal(1, c.Parameters[nameof(Parameter.Value1)].Value); Assert.Equal(1, c.Parameters[nameof(Parameter.Value2)].Value); Assert.Equal(DBNull.Value, c.Parameters[nameof(Parameter.Value3)].Value); Assert.Equal(Value.One, c.Parameters[nameof(Parameter.Value4)].Value); Assert.Equal(Value.One, c.Parameters[nameof(Parameter.Value5)].Value); Assert.Equal(DBNull.Value, c.Parameters[nameof(Parameter.Value6)].Value); c.Parameters[nameof(Parameter.Output1)].Value = 1; c.Parameters[nameof(Parameter.Output2)].Value = DBNull.Value; c.Parameters[nameof(Parameter.Output3)].Value = 1; c.Parameters[nameof(Parameter.Output4)].Value = DBNull.Value; c.Parameters[nameof(Parameter.Output5)].Value = 1; c.Parameters[nameof(Parameter.Output6)].Value = DBNull.Value; c.Parameters[nameof(Parameter.Output7)].Value = 1; }; cmd.SetupResult(100); }); var parameter = new Parameter { Value1 = 1, Value2 = 1, Value3 = null, Value4 = Value.One, Value5 = Value.One, Value6 = null }; accessor.Call(con, parameter); Assert.Equal(1, parameter.Output1); Assert.Equal(0, parameter.Output2); Assert.Equal(1, parameter.Output3); Assert.Null(parameter.Output4); Assert.Equal(Value.One, parameter.Output5); Assert.Null(parameter.Output6); Assert.Equal("1", parameter.Output7); }
public void TestSetupFailedList() { var generator = new TestFactoryBuilder() .SetSql("/*% var ids = values; */ WHERE Id IN /*@ ids */(1)") .Config(config => { config.ConfigureTypeMap(map => map.Clear()); }) .Build(); var accessor = generator.Create <ISetupFailedListAccessor>(); Assert.Throws <TargetInvocationException>(() => accessor.Execute(new MockDbConnection(), new List <string>(new[] { "1", "2" }))); }
public void TestSetupFailed() { var generator = new TestFactoryBuilder() .SetSql("/*% var id = value; */ WHERE Id = /*@ id */1") .Config(config => { config.ConfigureTypeMap(map => map.Clear()); }) .Build(); var accessor = generator.Create <ISetupFailedAccessor>(); Assert.Throws <TargetInvocationException>(() => accessor.Execute(new MockDbConnection(), "1")); }
public void TestExecuteScalarResultIsNull() { using (TestDatabase.Initialize()) { var generator = new TestFactoryBuilder() .UseMemoryDatabase() .SetSql("SELECT NULL") .Build(); var accessor = generator.Create <IExecuteScalarSimpleAccessor>(); var count = accessor.ExecuteScalar(); Assert.Equal(0, count); } }
public void TestExecuteVoid() { using (var con = TestDatabase.Initialize() .SetupDataTable()) { var generator = new TestFactoryBuilder() .UseFileDatabase() .SetSql("INSERT INTO Data (Id, Name) VALUES (/*@ id */1, /*@ name */'test')") .Build(); var accessor = generator.Create <IExecuteVoidAccessor>(); accessor.Execute(2, "xxx"); var entity = con.QueryData(2); Assert.NotNull(entity); } }
public async ValueTask TestExecuteScalarCancelAsync() { await using (TestDatabase.Initialize() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "Data-1" }) .InsertData(new DataEntity { Id = 2, Name = "Data-2" })) { var generator = new TestFactoryBuilder() .UseFileDatabase() .SetSql("SELECT COUNT(*) FROM Data") .Build(); var accessor = generator.Create <IExecuteScalarCancelAsyncAccessor>(); var count = await accessor.ExecuteScalarAsync(default);
public async ValueTask TestQueryBufferdCancelAsync() { await using (TestDatabase.Initialize() .SetupDataTable() .InsertData(new DataEntity { Id = 1, Name = "Data-1" }) .InsertData(new DataEntity { Id = 2, Name = "Data-2" })) { var generator = new TestFactoryBuilder() .UseFileDatabase() .SetSql("SELECT * FROM Data ORDER BY Id") .Build(); var accessor = generator.Create <IQueryCancelAsyncAccessor>(); var list = await accessor.QueryBufferdAsync(default);