SqlDataAdapterTest.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. //
  2. // SqlDataAdapterTest.cs - NUnit Test Cases for testing the
  3. // SqlDataAdapter class
  4. // Author:
  5. // Umadevi S ([email protected])
  6. // Sureshkumar T ([email protected])
  7. // Senganal T ([email protected])
  8. //
  9. // Copyright (c) 2004 Novell Inc., and the individuals listed
  10. // on the ChangeLog entries.
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Data;
  33. using System.Data.Common;
  34. using System.Data.SqlClient;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Data.SqlClient
  37. {
  38. [TestFixture]
  39. [Category ("sqlserver")]
  40. public class SqlDataAdapterTest
  41. {
  42. SqlDataAdapter adapter =null;
  43. DataSet data = null ;
  44. string connectionString = ConnectionManager.Singleton.ConnectionString;
  45. SqlConnection conn = null;
  46. [Test]
  47. /**
  48. The below test will not run everytime, since the region id column is unique
  49. so change the regionid if you want the test to pass.
  50. **/
  51. public void UpdateTest () {
  52. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  53. try {
  54. ConnectionManager.Singleton.OpenConnection ();
  55. DataTable dt = new DataTable();
  56. SqlDataAdapter da = null;
  57. da = new SqlDataAdapter("Select * from employee;", conn);
  58. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  59. da.Fill(dt);
  60. DataRow dr = dt.NewRow();
  61. dr ["id"] = 6002;
  62. dr ["fname"] = "boston";
  63. dr ["dob"] = DateTime.Now.Subtract (new TimeSpan (20*365, 0, 0, 0));
  64. dr ["doj"] = DateTime.Now;
  65. dt.Rows.Add(dr);
  66. da.Update(dt);
  67. } finally {
  68. DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
  69. ConnectionManager.Singleton.CloseConnection ();
  70. }
  71. }
  72. /**
  73. This needs a errortable created as follows
  74. id uniqueidentifier,name char(10) , with values
  75. Guid name
  76. {A12...} NULL
  77. NULL bbbbbb
  78. **/
  79. [Test]
  80. public void NullGuidTest()
  81. {
  82. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  83. try {
  84. ConnectionManager.Singleton.OpenConnection ();
  85. DBHelper.ExecuteNonQuery (conn, "create table #tmp_guid_table ( " +
  86. " id uniqueidentifier default newid (), " +
  87. " name char (10))");
  88. DBHelper.ExecuteNonQuery (conn, "insert into #tmp_guid_table (name) values (null)");
  89. DBHelper.ExecuteNonQuery (conn, "insert into #tmp_guid_table (id, name) values (null, 'bbbb')");
  90. SqlDataAdapter da = new SqlDataAdapter("select * from #tmp_guid_table", conn);
  91. DataSet ds = new DataSet();
  92. da.Fill(ds);
  93. Assert.AreEqual (1, ds.Tables.Count, "#1");
  94. Assert.AreEqual (DBNull.Value, ds.Tables [0].Rows [1] ["id"], "#2");
  95. } finally {
  96. ConnectionManager.Singleton.CloseConnection ();
  97. }
  98. // the bug 68804 - is that the fill hangs!
  99. Assert.AreEqual("Done","Done");
  100. }
  101. [Test]
  102. public void DefaultConstructorTest ()
  103. {
  104. adapter = new SqlDataAdapter ();
  105. Assert.AreEqual (MissingMappingAction.Passthrough,
  106. adapter.MissingMappingAction,
  107. "#1 Missing Mapping acttion default to Passthrough");
  108. Assert.AreEqual (MissingSchemaAction.Add,
  109. adapter.MissingSchemaAction,
  110. "#2 Missing Schme action default to Add");
  111. }
  112. [Test]
  113. public void OverloadedConstructorsTest ()
  114. {
  115. SqlCommand selCmd = new SqlCommand ("Select * from numeric_family");
  116. adapter = new SqlDataAdapter (selCmd);
  117. Assert.AreEqual (MissingMappingAction.Passthrough,
  118. adapter.MissingMappingAction,
  119. "#1 Missing Mapping acttion default to Passthrough");
  120. Assert.AreEqual (MissingSchemaAction.Add,
  121. adapter.MissingSchemaAction,
  122. "#2 Missing Schme action default to Add");
  123. Assert.AreSame (selCmd, adapter.SelectCommand,
  124. "#3 Select Command shud be a ref to the arg passed");
  125. conn = new SqlConnection (connectionString);
  126. String selStr = "Select * from numeric_family";
  127. adapter = new SqlDataAdapter (selStr, conn);
  128. Assert.AreEqual (MissingMappingAction.Passthrough,
  129. adapter.MissingMappingAction,
  130. "#4 Missing Mapping acttion default to Passthrough");
  131. Assert.AreEqual (MissingSchemaAction.Add,
  132. adapter.MissingSchemaAction,
  133. "#5 Missing Schme action default to Add");
  134. Assert.AreSame (selStr, adapter.SelectCommand.CommandText,
  135. "#6 Select Command shud be a ref to the arg passed");
  136. Assert.AreSame (conn, adapter.SelectCommand.Connection,
  137. "#7 cmd.connection shud be t ref to connection obj");
  138. selStr = "Select * from numeric_family";
  139. adapter = new SqlDataAdapter (selStr, connectionString);
  140. Assert.AreEqual (MissingMappingAction.Passthrough,
  141. adapter.MissingMappingAction,
  142. "#8 Missing Mapping action shud default to Passthrough");
  143. Assert.AreEqual (MissingSchemaAction.Add,
  144. adapter.MissingSchemaAction,
  145. "#9 Missing Schema action shud default to Add");
  146. Assert.AreSame (selStr,
  147. adapter.SelectCommand.CommandText,
  148. "#10");
  149. Assert.AreEqual (connectionString,
  150. adapter.SelectCommand.Connection.ConnectionString,
  151. "#11 ");
  152. }
  153. [Test]
  154. public void Fill_Test_ConnState ()
  155. {
  156. //Check if Connection State is maintained correctly ..
  157. data = new DataSet ("test1");
  158. adapter = new SqlDataAdapter ("select id from numeric_family where id=1",
  159. connectionString);
  160. SqlCommand cmd = adapter.SelectCommand ;
  161. Assert.AreEqual (ConnectionState.Closed,
  162. cmd.Connection.State, "#1 Connection shud be in closed state");
  163. adapter.Fill (data);
  164. Assert.AreEqual (1, data.Tables.Count, "#2 One table shud be populated");
  165. Assert.AreEqual (ConnectionState.Closed, cmd.Connection.State,
  166. "#3 Connection shud be closed state");
  167. data = new DataSet ("test2");
  168. cmd.Connection.Open ();
  169. Assert.AreEqual (ConnectionState.Open, cmd.Connection.State,
  170. "#3 Connection shud be open");
  171. adapter.Fill (data);
  172. Assert.AreEqual (1, data.Tables.Count, "#4 One table shud be populated");
  173. Assert.AreEqual (ConnectionState.Open, cmd.Connection.State,
  174. "#5 Connection shud be open");
  175. cmd.Connection.Close ();
  176. // Test if connection is closed when exception occurs
  177. cmd.CommandText = "select id1 from numeric_family";
  178. try {
  179. adapter.Fill (data);
  180. }catch (Exception e) {
  181. if (cmd.Connection.State == ConnectionState.Open) {
  182. cmd.Connection.Close ();
  183. Assert.Fail ("# Connection Shud be Closed");
  184. }
  185. }
  186. }
  187. [Test]
  188. public void Fill_Test_Data ()
  189. {
  190. //Check if a table is created for each resultset
  191. String batchQuery = "Select id,type_bit,type_int from numeric_family;";
  192. batchQuery += "Select type_bit,type_bigint from numeric_family";
  193. adapter = new SqlDataAdapter (batchQuery, connectionString);
  194. data = new DataSet ("test1");
  195. adapter.Fill (data);
  196. Assert.AreEqual (2, data.Tables.Count,"#1 2 Table shud be created");
  197. //Check if Table and Col are named correctly for unnamed columns
  198. string query = "Select 10,20 from numeric_family;" ;
  199. query += "Select 10,20 from numeric_family";
  200. adapter = new SqlDataAdapter (query, connectionString);
  201. data = new DataSet ("test2");
  202. adapter.Fill (data);
  203. Assert.AreEqual (2, data.Tables.Count,
  204. "#2 2 Tables shud be created");
  205. Assert.AreEqual ("Table", data.Tables[0].TableName, "#3");
  206. Assert.AreEqual ("Table1", data.Tables[1].TableName, "#4");
  207. Assert.AreEqual ("Column1", data.Tables[0].Columns[0].ColumnName, "#5");
  208. Assert.AreEqual ("Column2", data.Tables[0].Columns[1].ColumnName, "#6");
  209. Assert.AreEqual ("Column1", data.Tables[1].Columns[0].ColumnName, "#7");
  210. Assert.AreEqual ("Column2", data.Tables[1].Columns[1].ColumnName, "#8");
  211. //Check if dup columns are named correctly
  212. query = "select A.id ,B.id , C.id from numeric_family A, ";
  213. query += "numeric_family B , numeric_family C";
  214. adapter = new SqlDataAdapter (query, connectionString);
  215. data = new DataSet ("test3");
  216. adapter.Fill (data);
  217. // NOTE msdotnet contradicts documented behavior
  218. // as per documentation the column names should be
  219. // id1,id2,id3 .. but msdotnet returns id,id1,id2
  220. Assert.AreEqual ("id", data.Tables[0].Columns[0].ColumnName,
  221. "#9 if colname is duplicated ,shud be col,col1,col2 etc");
  222. Assert.AreEqual ("id1", data.Tables[0].Columns[1].ColumnName,
  223. "#10 if colname is duplicated ,shud be col,col1,col2 etc");
  224. Assert.AreEqual ("id2", data.Tables[0].Columns[2].ColumnName,
  225. "#11 if colname is duplicated ,shud be col,col1,col2 etc");
  226. // Test if tables are created and named accordingly ,
  227. // but only for those queries returning result sets
  228. query = "update numeric_family set id=100 where id=50;";
  229. query += "select * from numeric_family";
  230. adapter = new SqlDataAdapter (query, connectionString);
  231. data = new DataSet ("test4");
  232. adapter.Fill (data);
  233. Assert.AreEqual (1 ,data.Tables.Count,
  234. "#12 Tables shud be named only for queries returning a resultset");
  235. Assert.AreEqual ("Table", data.Tables[0].TableName,
  236. "#13 The first resutlset shud have 'Table' as its name");
  237. // Test behavior with an outerjoin
  238. query = "select A.id,B.type_bit from numeric_family A LEFT OUTER JOIN ";
  239. query += "numeric_family B on A.id = B.type_bit";
  240. adapter = new SqlDataAdapter (query, connectionString);
  241. data = new DataSet ("test5");
  242. adapter.Fill (data);
  243. Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length,
  244. "#14 Primary Key shudnt be set if an outer join is performed");
  245. Assert.AreEqual (0, data.Tables[0].Constraints.Count,
  246. "#15 Constraints shudnt be set if an outer join is performed");
  247. adapter = new SqlDataAdapter ("select id from numeric_family",
  248. connectionString);
  249. data = new DataSet ("test6");
  250. adapter.Fill (data, 1, 1, "numeric_family");
  251. Assert.AreEqual (1, data.Tables[0].Rows.Count, "#16");
  252. Assert.AreEqual (2, data.Tables[0].Rows[0][0], "#17");
  253. // only one test for DataTable.. DataSet tests covers others
  254. adapter = new SqlDataAdapter ("select id from numeric_family",
  255. connectionString);
  256. DataTable table = new DataTable ("table1");
  257. adapter.Fill (table);
  258. Assert.AreEqual (4, table.Rows.Count , "#18");
  259. }
  260. [Test]
  261. public void Fill_Test_PriKey ()
  262. {
  263. // Test if Primary Key & Constraints Collection is correct
  264. adapter = new SqlDataAdapter ("select id,type_bit from numeric_family",
  265. connectionString);
  266. adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  267. data = new DataSet ("test1");
  268. adapter.Fill (data);
  269. Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length,
  270. "#1 Primary Key shud be set");
  271. Assert.AreEqual (1, data.Tables[0].Constraints.Count,
  272. "#2 Constraints shud be set");
  273. Assert.AreEqual (4, data.Tables[0].Rows.Count,
  274. "#3 No Of Rows shud be 4");
  275. // Test if data is correctly merged
  276. adapter.Fill (data);
  277. Assert.AreEqual (4, data.Tables[0].Rows.Count,
  278. "#4 No of Row shud still be 4");
  279. // Test if rows are appended and not merged
  280. // when primary key is not returned in the result-set
  281. string query = "Select type_int,type_bigint from numeric_family";
  282. adapter.SelectCommand.CommandText = query;
  283. data = new DataSet ("test2");
  284. adapter.Fill (data);
  285. Assert.AreEqual (4, data.Tables[0].Rows.Count,
  286. "#5 No of Rows shud be 4");
  287. adapter.Fill (data);
  288. Assert.AreEqual (8, data.Tables[0].Rows.Count,
  289. "#6 No of Rows shud double now");
  290. }
  291. [Test]
  292. public void Fill_Test_Exceptions ()
  293. {
  294. adapter = new SqlDataAdapter ("select * from numeric_family",
  295. connectionString);
  296. data = new DataSet ("test1");
  297. try {
  298. adapter.Fill (data, -1, 0, "numeric_family");
  299. Assert.Fail ("#1 Exception shud be thrown:Incorrect Arguments");
  300. }catch (AssertionException e){
  301. throw e;
  302. }catch (Exception e){
  303. Assert.AreEqual (typeof(ArgumentException), e.GetType(),
  304. "#2 Incorrect Exception : " + e);
  305. }
  306. // conn is not closed due to a bug..
  307. // can be removed later
  308. adapter.SelectCommand.Connection.Close ();
  309. try {
  310. adapter.Fill (data , 0 , -1 , "numeric_family");
  311. Assert.Fail ("#3 Exception shud be thrown:Incorrect Arguments");
  312. }catch (AssertionException e){
  313. throw e;
  314. }catch (Exception e){
  315. Assert.AreEqual (typeof(ArgumentException), e.GetType(),
  316. "#4 Incorrect Exception : " + e);
  317. }
  318. // conn is curr not closed.. can be removed later
  319. adapter.SelectCommand.Connection.Close ();
  320. /*
  321. // NOTE msdotnet contradicts documented behavior
  322. // InvalidOperationException is expected if table is not valid
  323. try {
  324. adapter.Fill (data , 0 , 0 , "invalid_talbe_name");
  325. }catch (InvalidOperationException e) {
  326. ex= e;
  327. }catch (Exception e){
  328. Assert.Fail ("#5 Exception shud be thrown : incorrect arugments ");
  329. }
  330. Assert.IsNotNull (ex , "#6 Exception shud be thrown : incorrect args ");
  331. adapter.SelectCommand.Connection.Close (); // tmp .. can be removed once the bug if fixed
  332. ex=null;
  333. */
  334. try {
  335. adapter.Fill ( null , 0 , 0 , "numeric_family");
  336. Assert.Fail ( "#7 Exception shud be thrown : Invalid Dataset");
  337. }catch (AssertionException e){
  338. throw e ;
  339. }catch (ArgumentNullException) {
  340. }catch (Exception e) {
  341. Assert.AreEqual (typeof(SystemException), e.GetType(),
  342. "#8 Incorrect Exception : " + e);
  343. }
  344. // conn is currently not being closed..
  345. //need to be removed once behavior is fixed
  346. adapter.SelectCommand.Connection.Close ();
  347. adapter.SelectCommand.Connection = null;
  348. try {
  349. adapter.Fill (data);
  350. Assert.Fail ("#9 Exception shud be thrown : Invalid Connection");
  351. }catch (AssertionException e){
  352. throw e;
  353. }catch (Exception e){
  354. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  355. "#10 Incorrect Exception : " + e);
  356. }
  357. }
  358. bool FillErrorContinue = false;
  359. [Test]
  360. public void Fill_Test_FillErrorTest ()
  361. {
  362. string query = "select type_bigint from numeric_family where id=1 or id=4 ";
  363. DataSet ds = new DataSet ();
  364. DataTable table = ds.Tables.Add ("test");
  365. table.Columns.Add ("col", typeof (int));
  366. adapter = new SqlDataAdapter (query, connectionString);
  367. DataTableMapping mapping = adapter.TableMappings.Add ("numeric_family", "test");
  368. mapping.ColumnMappings.Add ("type_bigint", "col");
  369. int count = 0;
  370. try {
  371. count = adapter.Fill (ds, "numeric_family");
  372. Assert.Fail ("#1 Overflow exception must be thrown");
  373. }catch (OverflowException e) {
  374. }
  375. Assert.AreEqual (0, ds.Tables [0].Rows.Count, "#2");
  376. Assert.AreEqual (0, count, "#3");
  377. adapter.FillError += new FillErrorEventHandler (ErrorHandler);
  378. FillErrorContinue = false;
  379. try {
  380. count = adapter.Fill (ds, "numeric_family");
  381. Assert.Fail ("#4 Overflow exception must be thrown");
  382. }catch (OverflowException e) {
  383. }
  384. Assert.AreEqual (0, ds.Tables [0].Rows.Count, "#5");
  385. Assert.AreEqual (0, count, "#6");
  386. FillErrorContinue = true;
  387. count = adapter.Fill (ds, "numeric_family");
  388. // 1 row shud be filled
  389. Assert.AreEqual (1, ds.Tables [0].Rows.Count, "#7");
  390. Assert.AreEqual (1, count, "#8");
  391. }
  392. void ErrorHandler (object sender, FillErrorEventArgs args)
  393. {
  394. args.Continue = FillErrorContinue;
  395. }
  396. [Test]
  397. public void GetFillParametersTest ()
  398. {
  399. string query = "select id, type_bit from numeric_family where id > @param1";
  400. adapter = new SqlDataAdapter (query, connectionString);
  401. IDataParameter[] param = adapter.GetFillParameters ();
  402. Assert.AreEqual (0, param.Length, "#1 size shud be 0");
  403. SqlParameter param1 = new SqlParameter ();
  404. param1.ParameterName = "@param1";
  405. param1.Value = 2;
  406. adapter.SelectCommand.Parameters.Add (param1);
  407. param = adapter.GetFillParameters ();
  408. Assert.AreEqual (1, param.Length, "#2 count shud be 1");
  409. Assert.AreEqual (param1, param[0], "#3 Params shud be equal");
  410. }
  411. [Test]
  412. public void FillSchemaTest ()
  413. {
  414. string query = "";
  415. // Test if connection is closed if excepton occurs during fill schema
  416. query = "select * from invalid_table";
  417. adapter = new SqlDataAdapter (query, connectionString);
  418. data = new DataSet ("test");
  419. try {
  420. adapter.FillSchema (data , SchemaType.Source);
  421. }catch (Exception e){
  422. if ( adapter.SelectCommand.Connection.State != ConnectionState.Closed)
  423. {
  424. Assert.Fail ("#0 Conn shud be closed if exception occurs");
  425. adapter.SelectCommand.Connection.Close();
  426. }
  427. }
  428. // Test Primary Key is set (since primary key column returned)
  429. query = "select id, type_int from numeric_family where id=1";
  430. adapter = new SqlDataAdapter (query, connectionString);
  431. data = new DataSet ("test1");
  432. adapter.FillSchema (data , SchemaType.Source);
  433. Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length,
  434. "#1 Primary Key property must be set");
  435. // Test Primary Key is not set (since primary key column is returned)
  436. query = "select type_bit, type_int from numeric_family where id=1";
  437. adapter = new SqlDataAdapter (query, connectionString);
  438. data = new DataSet ("test2");
  439. adapter.FillSchema (data, SchemaType.Source);
  440. Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length,
  441. "#2 Primary Key property should not be set");
  442. // Test multiple tables are created for a batch query
  443. query = "Select id ,type_bit from numeric_family;" ;
  444. query += "Select id,type_bit,type_int from numeric_family;";
  445. data = new DataSet ("test3");
  446. adapter = new SqlDataAdapter (query, connectionString);
  447. adapter.FillSchema (data , SchemaType.Source);
  448. Assert.AreEqual (2 , data.Tables.Count , "#3 A table shud be created for each Result Set");
  449. Assert.AreEqual (2 , data.Tables[0].Columns.Count , "#4 should have 2 columns");
  450. Assert.AreEqual (3 , data.Tables[1].Columns.Count , "#5 Should have 3 columns");
  451. // Test if table names and column names are filled correctly
  452. query = "select 10,20 from numeric_family;" ;
  453. query += "select 10,20 from numeric_family;";
  454. adapter = new SqlDataAdapter (query, connectionString);
  455. data = new DataSet ("test4");
  456. try {
  457. adapter.FillSchema (data , SchemaType.Source);
  458. }catch (Exception e){
  459. Assert.Fail ("#3 Unexpected Exception : " + e);
  460. }
  461. Assert.AreEqual ( "Table", data.Tables[0].TableName);
  462. Assert.AreEqual ( "Table1", data.Tables[1].TableName);
  463. Assert.AreEqual ( "Column1", data.Tables[0].Columns[0].ColumnName,
  464. "#6 Unnamed col shud be named as 'ColumnN'");
  465. Assert.AreEqual ( "Column2", data.Tables[0].Columns[1].ColumnName,
  466. "#7 Unnamed col shud be named as 'ColumnN'");
  467. Assert.AreEqual ( "Column1", data.Tables[1].Columns[0].ColumnName,
  468. "#8 Unnamed col shud be named as 'ColumnN'");
  469. Assert.AreEqual ( "Column2", data.Tables[1].Columns[1].ColumnName,
  470. "#9 Unnamed col shud be named as 'ColumnN'");
  471. Assert.AreEqual (ConnectionState.Closed, adapter.SelectCommand.Connection.State,
  472. "#10 Connection shud be closed");
  473. // Test if mapping works correctly
  474. // doesent work in both mono and msdotnet
  475. // gotto check if something is wrong
  476. /*
  477. query = "select id,type_bit from numeric_family";
  478. adapter = new SqlDataAdapter (query, connectionString);
  479. data = new DataSet ("test");
  480. DataTable table = data.Tables.Add ("numeric_family_1");
  481. table.Columns.Add ("id");
  482. table.Columns.Add ("type_bit");
  483. DataTableMapping map = adapter.TableMappings.Add("numeric_family_1",
  484. "numeric_family");
  485. map.ColumnMappings.Add ("id", "id_1");
  486. map.ColumnMappings.Add ("type_bit", "type_bit_1");
  487. adapter.FillSchema (data, SchemaType.Source, "numeric_family");
  488. foreach (DataTable tab in data.Tables){
  489. Console.WriteLine ("Table == {0}",tab.TableName);
  490. foreach (DataColumn col in tab.Columns)
  491. Console.WriteLine (" Col = {0} " , col.ColumnName);
  492. }
  493. */
  494. }
  495. [Test]
  496. public void MissingSchemaActionTest ()
  497. {
  498. adapter = new SqlDataAdapter (
  499. "select id,type_bit,type_int from numeric_family where id<=4",
  500. connectionString);
  501. data = new DataSet ();
  502. Assert.AreEqual (MissingSchemaAction.Add, adapter.MissingSchemaAction,
  503. "#1 Default Value");
  504. adapter.Fill (data);
  505. Assert.AreEqual (1, data.Tables.Count , "#1 One table shud be populated");
  506. Assert.AreEqual (3, data.Tables[0].Columns.Count, "#2 Missing cols are added");
  507. Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length, "#3 Default Value");
  508. adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  509. data.Reset();
  510. adapter.Fill (data);
  511. Assert.AreEqual (3, data.Tables[0].Columns.Count,
  512. "#4 Missing cols are added");
  513. Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length, "#5 Default Value");
  514. adapter.MissingSchemaAction = MissingSchemaAction.Ignore ;
  515. data.Reset ();
  516. adapter.Fill (data);
  517. Assert.AreEqual (0, data.Tables.Count, "#6 Data shud be ignored");
  518. adapter.MissingSchemaAction = MissingSchemaAction.Error ;
  519. data.Reset();
  520. try {
  521. adapter.Fill (data);
  522. Assert.Fail ("#8 Exception shud be thrown: Schema Mismatch");
  523. }catch (AssertionException e) {
  524. throw e;
  525. }catch (Exception e){
  526. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  527. "#9 Incorrect Exception : "+e);
  528. }
  529. // Test for invalid MissingSchema Value
  530. try {
  531. adapter.MissingSchemaAction = (MissingSchemaAction)(-5000);
  532. Assert.Fail ("#10 Exception shud be thrown: Invalid Value");
  533. }catch (AssertionException e){
  534. throw e;
  535. }catch (Exception e){
  536. Assert.AreEqual (typeof(ArgumentException), e.GetType(),
  537. "#11 Incorrect Exception : " +e);
  538. }
  539. // Tests if Data is filled correctly if schema is defined
  540. // manually and MissingSchemaAction.Error is set
  541. adapter.MissingSchemaAction = MissingSchemaAction.Error;
  542. data.Reset();
  543. DataTable table = data.Tables.Add ("Table");
  544. table.Columns.Add ("id");
  545. table.Columns.Add ("type_bit");
  546. table.Columns.Add ("type_int");
  547. try {
  548. adapter.Fill (data);
  549. Assert.AreEqual (1, data.Tables.Count, "#12");
  550. Assert.AreEqual (4, data.Tables[0].Rows.Count, "#13");
  551. }catch (Exception e) {
  552. Assert.Fail ("#12 Unexpected Exception : " + e);
  553. }
  554. }
  555. [Test]
  556. public void MissingMappingActionTest ()
  557. {
  558. adapter = new SqlDataAdapter ("select id,type_bit from numeric_family where id=1",
  559. connectionString);
  560. data = new DataSet ();
  561. Assert.AreEqual (adapter.MissingMappingAction,
  562. MissingMappingAction.Passthrough,
  563. "#1 Default Value");
  564. adapter.Fill(data);
  565. Assert.AreEqual (1, data.Tables.Count,
  566. "#2 One Table shud be created");
  567. Assert.AreEqual (2, data.Tables[0].Columns.Count,
  568. "#3 Two Cols shud be created");
  569. adapter.MissingMappingAction = MissingMappingAction.Ignore;
  570. data.Reset ();
  571. adapter.Fill (data);
  572. Assert.AreEqual (0, data.Tables.Count, "#4 No table shud be created");
  573. adapter.MissingMappingAction = MissingMappingAction.Error;
  574. data.Reset ();
  575. try {
  576. adapter.Fill (data);
  577. Assert.Fail ("#5 Exception shud be thrown : Mapping is missing");
  578. }catch (AssertionException e){
  579. throw e;
  580. }catch (Exception e) {
  581. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  582. "#6 Incorrect Exception : " + e);
  583. }
  584. try {
  585. adapter.MissingMappingAction = (MissingMappingAction)(-5000);
  586. Assert.Fail ("#7 Exception shud be thrown : Invalid Value");
  587. }catch (AssertionException e){
  588. throw e;
  589. }catch (Exception e){
  590. Assert.AreEqual (typeof(ArgumentException), e.GetType(),
  591. "#8 Incorrect Exception : " +e);
  592. }
  593. // Test if mapping the column and table names works correctly
  594. adapter.MissingMappingAction = MissingMappingAction.Error;
  595. data.Reset ();
  596. DataTable table = data.Tables.Add ("numeric_family_1");
  597. table.Columns.Add ("id_1");
  598. table.Columns.Add ("type_bit_1");
  599. table.Columns.Add ("type_int_1");
  600. DataTableMapping tableMap = adapter.TableMappings.Add ("numeric_family",
  601. "numeric_family_1");
  602. tableMap.ColumnMappings.Add ("id", "id_1");
  603. tableMap.ColumnMappings.Add ("type_bit", "type_bit_1");
  604. tableMap.ColumnMappings.Add ("type_int", "type_int_1");
  605. adapter.Fill (data,"numeric_family");
  606. Assert.AreEqual (1, data.Tables.Count ,
  607. "#8 The DataTable shud be correctly mapped");
  608. Assert.AreEqual (3, data.Tables[0].Columns.Count,
  609. "#9 The DataColumns shud be corectly mapped");
  610. Assert.AreEqual (1, data.Tables[0].Rows.Count,
  611. "#10 Data shud be populated if mapping is correct");
  612. }
  613. // Test case for bug #76433
  614. [Test]
  615. public void FillSchema_ValuesTest()
  616. {
  617. SqlConnection conn = new SqlConnection(connectionString);
  618. using (conn) {
  619. conn.Open();
  620. IDbCommand command = conn.CreateCommand();
  621. // Create Temp Table
  622. String cmd = "Create Table #tmp_TestTable (" ;
  623. cmd += "Field1 DECIMAL (10) NOT NULL,";
  624. cmd += "Field2 DECIMAL(19))";
  625. command.CommandText = cmd;
  626. command.ExecuteNonQuery();
  627. DataSet dataSet = new DataSet();
  628. string selectString = "SELECT * FROM #tmp_TestTable";
  629. IDbDataAdapter dataAdapter = new SqlDataAdapter (
  630. selectString,conn);
  631. dataAdapter.FillSchema(dataSet, SchemaType.Mapped);
  632. Assert.AreEqual (1, dataSet.Tables.Count, "#1");
  633. DataColumn col = dataSet.Tables[0].Columns[0];
  634. Assert.IsFalse (dataSet.Tables[0].Columns[0].AllowDBNull,"#2");
  635. Assert.IsTrue (dataSet.Tables[0].Columns[1].AllowDBNull,"#3");
  636. }
  637. }
  638. [Test]
  639. public void Fill_CheckSchema ()
  640. {
  641. SqlConnection conn = new SqlConnection(connectionString);
  642. using (conn) {
  643. conn.Open();
  644. IDbCommand command = conn.CreateCommand();
  645. // Create Temp Table
  646. String cmd = "Create Table #tmp_TestTable (" ;
  647. cmd += "id int primary key,";
  648. cmd += "field int not null)";
  649. command.CommandText = cmd;
  650. command.ExecuteNonQuery();
  651. DataSet dataSet = new DataSet();
  652. string selectString = "SELECT * from #tmp_TestTable";
  653. IDbDataAdapter dataAdapter = new SqlDataAdapter (
  654. selectString,conn);
  655. dataAdapter.Fill (dataSet);
  656. Assert.IsTrue (dataSet.Tables[0].Columns[1].AllowDBNull, "#1");
  657. Assert.AreEqual (0, dataSet.Tables[0].PrimaryKey.Length, "#2");
  658. dataSet.Reset ();
  659. dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey ;
  660. dataAdapter.Fill (dataSet);
  661. Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#3");
  662. Assert.AreEqual (1, dataSet.Tables[0].PrimaryKey.Length, "#4");
  663. }
  664. }
  665. [Test]
  666. public void FillSchema_CheckSchema ()
  667. {
  668. SqlConnection conn = new SqlConnection(connectionString);
  669. using (conn) {
  670. conn.Open();
  671. IDbCommand command = conn.CreateCommand();
  672. // Create Temp Table
  673. String cmd = "Create Table #tmp_TestTable (" ;
  674. cmd += "id int primary key,";
  675. cmd += "field int not null)";
  676. command.CommandText = cmd;
  677. command.ExecuteNonQuery();
  678. DataSet dataSet = new DataSet();
  679. string selectString = "SELECT * from #tmp_TestTable";
  680. IDbDataAdapter dataAdapter = new SqlDataAdapter (
  681. selectString,conn);
  682. dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
  683. Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#1");
  684. dataSet.Reset ();
  685. dataAdapter.MissingSchemaAction = MissingSchemaAction.Add;
  686. dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
  687. Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#2");
  688. dataSet.Reset ();
  689. dataAdapter.MissingSchemaAction = MissingSchemaAction.Ignore;
  690. dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
  691. Assert.AreEqual (0, dataSet.Tables.Count, "#3");
  692. dataSet.Reset ();
  693. dataAdapter.MissingSchemaAction = MissingSchemaAction.Error;
  694. try {
  695. dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
  696. Assert.Fail ("#4 Error should be thrown");
  697. } catch (InvalidOperationException e) {
  698. }
  699. }
  700. }
  701. }
  702. }