|
| 1 | +#if !NETCOREAPP1_1_2 |
| 2 | +using System; |
| 3 | +using System.Data; |
| 4 | +using Dapper; |
| 5 | +using MySql.Data.MySqlClient; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace SideBySide |
| 9 | +{ |
| 10 | + public class CommandBuilderTests : IClassFixture<DatabaseFixture>, IDisposable |
| 11 | + { |
| 12 | + public CommandBuilderTests(DatabaseFixture database) |
| 13 | + { |
| 14 | + m_database = database; |
| 15 | + m_database.Connection.Open(); |
| 16 | + } |
| 17 | + |
| 18 | + public void Dispose() |
| 19 | + { |
| 20 | + m_database.Connection.Close(); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void Insert() |
| 25 | + { |
| 26 | + m_database.Connection.Execute(@"drop table if exists command_builder_insert; |
| 27 | +create table command_builder_insert |
| 28 | +( |
| 29 | + id int not null primary key, |
| 30 | + value varchar(100) |
| 31 | +);"); |
| 32 | + using (var dataAdapter = new MySqlDataAdapter("select * from command_builder_insert", m_database.Connection)) |
| 33 | + using (new MySqlCommandBuilder(dataAdapter)) |
| 34 | + { |
| 35 | + var dataTable = new DataTable(); |
| 36 | + dataAdapter.Fill(dataTable); |
| 37 | + |
| 38 | + var row = dataTable.NewRow(); |
| 39 | + row["id"] = 1; |
| 40 | + row["value"] = "inserted"; |
| 41 | + dataTable.Rows.Add(row); |
| 42 | + |
| 43 | + dataAdapter.Update(dataTable); |
| 44 | + } |
| 45 | + |
| 46 | + Assert.Equal(1, m_database.Connection.ExecuteScalar<int>("select count(*) from command_builder_insert;")); |
| 47 | + Assert.Equal("inserted", m_database.Connection.ExecuteScalar<string>("select value from command_builder_insert where id = 1;")); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void Update() |
| 52 | + { |
| 53 | + m_database.Connection.Execute(@"drop table if exists command_builder_update; |
| 54 | +create table command_builder_update |
| 55 | +( |
| 56 | + id int not null primary key, |
| 57 | + value varchar(100) |
| 58 | +); |
| 59 | +insert into command_builder_update values(1, 'one'), (2, 'two'); |
| 60 | +"); |
| 61 | + using (var dataAdapter = new MySqlDataAdapter("select * from command_builder_update", m_database.Connection)) |
| 62 | + using (new MySqlCommandBuilder(dataAdapter)) |
| 63 | + { |
| 64 | + var dataTable = new DataTable(); |
| 65 | + dataAdapter.Fill(dataTable); |
| 66 | + |
| 67 | + dataTable.Rows[0]["value"] = "updated"; |
| 68 | + dataAdapter.Update(dataTable); |
| 69 | + } |
| 70 | + |
| 71 | + Assert.Equal("updated", m_database.Connection.ExecuteScalar<string>("select value from command_builder_update where id = 1;")); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void Delete() |
| 76 | + { |
| 77 | + m_database.Connection.Execute(@"drop table if exists command_builder_delete; |
| 78 | +create table command_builder_delete |
| 79 | +( |
| 80 | + id int not null primary key, |
| 81 | + value varchar(100) |
| 82 | +); |
| 83 | +insert into command_builder_delete values(1, 'one'), (2, 'two'); |
| 84 | +"); |
| 85 | + using (var dataAdapter = new MySqlDataAdapter("select * from command_builder_delete", m_database.Connection)) |
| 86 | + using (new MySqlCommandBuilder(dataAdapter)) |
| 87 | + { |
| 88 | + var dataTable = new DataTable(); |
| 89 | + dataAdapter.Fill(dataTable); |
| 90 | + |
| 91 | + dataTable.Rows[0].Delete(); |
| 92 | + |
| 93 | + dataAdapter.Update(dataTable); |
| 94 | + } |
| 95 | + |
| 96 | + Assert.Equal(1, m_database.Connection.ExecuteScalar<int>("select count(*) from command_builder_delete;")); |
| 97 | + } |
| 98 | + |
| 99 | + [Theory] |
| 100 | + [InlineData("test", "`test`")] |
| 101 | + [InlineData("te`st", "`te``st`")] |
| 102 | + [InlineData("`test`", "```test```" |
| 103 | +#if BASELINE |
| 104 | + , Skip = "Doesn't quote leading quotes" |
| 105 | +#endif |
| 106 | + )] |
| 107 | + public void QuoteIdentifier(string input, string expected) |
| 108 | + { |
| 109 | + var cb = new MySqlCommandBuilder(); |
| 110 | + Assert.Equal(expected, cb.QuoteIdentifier(input)); |
| 111 | + } |
| 112 | + |
| 113 | + [Theory] |
| 114 | + [InlineData("test", "test")] |
| 115 | + [InlineData("`test`", "test")] |
| 116 | + [InlineData("`te``st`", "te`st")] |
| 117 | + [InlineData("```test```", "`test`")] |
| 118 | + public void UnquoteIdentifier(string input, string expected) |
| 119 | + { |
| 120 | + var cb = new MySqlCommandBuilder(); |
| 121 | + Assert.Equal(expected, cb.UnquoteIdentifier(input)); |
| 122 | + } |
| 123 | + |
| 124 | + readonly DatabaseFixture m_database; |
| 125 | + } |
| 126 | +} |
| 127 | +#endif |
0 commit comments