SqlDataAdapterTest.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. [Test]
  359. public void GetFillParametersTest ()
  360. {
  361. string query = "select id, type_bit from numeric_family where id > @param1";
  362. adapter = new SqlDataAdapter (query, connectionString);
  363. IDataParameter[] param = adapter.GetFillParameters ();
  364. Assert.AreEqual (0, param.Length, "#1 size shud be 0");
  365. SqlParameter param1 = new SqlParameter ();
  366. param1.ParameterName = "@param1";
  367. param1.Value = 2;
  368. adapter.SelectCommand.Parameters.Add (param1);
  369. param = adapter.GetFillParameters ();
  370. Assert.AreEqual (1, param.Length, "#2 count shud be 1");
  371. Assert.AreEqual (param1, param[0], "#3 Params shud be equal");
  372. }
  373. [Test]
  374. public void FillSchemaTest ()
  375. {
  376. string query = "";
  377. // Test if connection is closed if excepton occurs during fill schema
  378. query = "select * from invalid_table";
  379. adapter = new SqlDataAdapter (query, connectionString);
  380. data = new DataSet ("test");
  381. try {
  382. adapter.FillSchema (data , SchemaType.Source);
  383. }catch (Exception e){
  384. if ( adapter.SelectCommand.Connection.State != ConnectionState.Closed)
  385. {
  386. Assert.Fail ("#0 Conn shud be closed if exception occurs");
  387. adapter.SelectCommand.Connection.Close();
  388. }
  389. }
  390. // Test Primary Key is set (since primary key column returned)
  391. query = "select id, type_int from numeric_family where id=1";
  392. adapter = new SqlDataAdapter (query, connectionString);
  393. data = new DataSet ("test1");
  394. adapter.FillSchema (data , SchemaType.Source);
  395. Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length,
  396. "#1 Primary Key property must be set");
  397. // Test Primary Key is not set (since primary key column is returned)
  398. query = "select type_bit, type_int from numeric_family where id=1";
  399. adapter = new SqlDataAdapter (query, connectionString);
  400. data = new DataSet ("test2");
  401. adapter.FillSchema (data, SchemaType.Source);
  402. Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length,
  403. "#2 Primary Key property should not be set");
  404. // Test multiple tables are created for a batch query
  405. query = "Select id ,type_bit from numeric_family;" ;
  406. query += "Select id,type_bit,type_int from numeric_family;";
  407. data = new DataSet ("test3");
  408. adapter = new SqlDataAdapter (query, connectionString);
  409. adapter.FillSchema (data , SchemaType.Source);
  410. Assert.AreEqual (2 , data.Tables.Count , "#3 A table shud be created for each Result Set");
  411. Assert.AreEqual (2 , data.Tables[0].Columns.Count , "#4 should have 2 columns");
  412. Assert.AreEqual (3 , data.Tables[1].Columns.Count , "#5 Should have 3 columns");
  413. // Test if table names and column names are filled correctly
  414. query = "select 10,20 from numeric_family;" ;
  415. query += "select 10,20 from numeric_family;";
  416. adapter = new SqlDataAdapter (query, connectionString);
  417. data = new DataSet ("test4");
  418. try {
  419. adapter.FillSchema (data , SchemaType.Source);
  420. }catch (Exception e){
  421. Assert.Fail ("#3 Unexpected Exception : " + e);
  422. }
  423. Assert.AreEqual ( "Table", data.Tables[0].TableName);
  424. Assert.AreEqual ( "Table1", data.Tables[1].TableName);
  425. Assert.AreEqual ( "Column1", data.Tables[0].Columns[0].ColumnName,
  426. "#6 Unnamed col shud be named as 'ColumnN'");
  427. Assert.AreEqual ( "Column2", data.Tables[0].Columns[1].ColumnName,
  428. "#7 Unnamed col shud be named as 'ColumnN'");
  429. Assert.AreEqual ( "Column1", data.Tables[1].Columns[0].ColumnName,
  430. "#8 Unnamed col shud be named as 'ColumnN'");
  431. Assert.AreEqual ( "Column2", data.Tables[1].Columns[1].ColumnName,
  432. "#9 Unnamed col shud be named as 'ColumnN'");
  433. Assert.AreEqual (ConnectionState.Closed, adapter.SelectCommand.Connection.State,
  434. "#10 Connection shud be closed");
  435. // Test if mapping works correctly
  436. // doesent work in both mono and msdotnet
  437. // gotto check if something is wrong
  438. /*
  439. query = "select id,type_bit from numeric_family";
  440. adapter = new SqlDataAdapter (query, connectionString);
  441. data = new DataSet ("test");
  442. DataTable table = data.Tables.Add ("numeric_family_1");
  443. table.Columns.Add ("id");
  444. table.Columns.Add ("type_bit");
  445. DataTableMapping map = adapter.TableMappings.Add("numeric_family_1",
  446. "numeric_family");
  447. map.ColumnMappings.Add ("id", "id_1");
  448. map.ColumnMappings.Add ("type_bit", "type_bit_1");
  449. adapter.FillSchema (data, SchemaType.Source, "numeric_family");
  450. foreach (DataTable tab in data.Tables){
  451. Console.WriteLine ("Table == {0}",tab.TableName);
  452. foreach (DataColumn col in tab.Columns)
  453. Console.WriteLine (" Col = {0} " , col.ColumnName);
  454. }
  455. */
  456. }
  457. [Test]
  458. public void MissingSchemaActionTest ()
  459. {
  460. adapter = new SqlDataAdapter (
  461. "select id,type_bit,type_int from numeric_family where id<=4",
  462. connectionString);
  463. data = new DataSet ();
  464. Assert.AreEqual (MissingSchemaAction.Add, adapter.MissingSchemaAction,
  465. "#1 Default Value");
  466. adapter.Fill (data);
  467. Assert.AreEqual (1, data.Tables.Count , "#1 One table shud be populated");
  468. Assert.AreEqual (3, data.Tables[0].Columns.Count, "#2 Missing cols are added");
  469. Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length, "#3 Default Value");
  470. adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  471. data.Reset();
  472. adapter.Fill (data);
  473. Assert.AreEqual (3, data.Tables[0].Columns.Count,
  474. "#4 Missing cols are added");
  475. Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length, "#5 Default Value");
  476. adapter.MissingSchemaAction = MissingSchemaAction.Ignore ;
  477. data.Reset ();
  478. adapter.Fill (data);
  479. Assert.AreEqual (0, data.Tables.Count, "#6 Data shud be ignored");
  480. adapter.MissingSchemaAction = MissingSchemaAction.Error ;
  481. data.Reset();
  482. try {
  483. adapter.Fill (data);
  484. Assert.Fail ("#8 Exception shud be thrown: Schema Mismatch");
  485. }catch (AssertionException e) {
  486. throw e;
  487. }catch (Exception e){
  488. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  489. "#9 Incorrect Exception : "+e);
  490. }
  491. // Test for invalid MissingSchema Value
  492. try {
  493. adapter.MissingSchemaAction = (MissingSchemaAction)(-5000);
  494. Assert.Fail ("#10 Exception shud be thrown: Invalid Value");
  495. }catch (AssertionException e){
  496. throw e;
  497. }catch (Exception e){
  498. Assert.AreEqual (typeof(ArgumentException), e.GetType(),
  499. "#11 Incorrect Exception : " +e);
  500. }
  501. // Tests if Data is filled correctly if schema is defined
  502. // manually and MissingSchemaAction.Error is set
  503. adapter.MissingSchemaAction = MissingSchemaAction.Error;
  504. data.Reset();
  505. DataTable table = data.Tables.Add ("Table");
  506. table.Columns.Add ("id");
  507. table.Columns.Add ("type_bit");
  508. table.Columns.Add ("type_int");
  509. try {
  510. adapter.Fill (data);
  511. Assert.AreEqual (1, data.Tables.Count, "#12");
  512. Assert.AreEqual (4, data.Tables[0].Rows.Count, "#13");
  513. }catch (Exception e) {
  514. Assert.Fail ("#12 Unexpected Exception : " + e);
  515. }
  516. }
  517. [Test]
  518. public void MissingMappingActionTest ()
  519. {
  520. adapter = new SqlDataAdapter ("select id,type_bit from numeric_family where id=1",
  521. connectionString);
  522. data = new DataSet ();
  523. Assert.AreEqual (adapter.MissingMappingAction,
  524. MissingMappingAction.Passthrough,
  525. "#1 Default Value");
  526. adapter.Fill(data);
  527. Assert.AreEqual (1, data.Tables.Count,
  528. "#2 One Table shud be created");
  529. Assert.AreEqual (2, data.Tables[0].Columns.Count,
  530. "#3 Two Cols shud be created");
  531. adapter.MissingMappingAction = MissingMappingAction.Ignore;
  532. data.Reset ();
  533. adapter.Fill (data);
  534. Assert.AreEqual (0, data.Tables.Count, "#4 No table shud be created");
  535. adapter.MissingMappingAction = MissingMappingAction.Error;
  536. data.Reset ();
  537. try {
  538. adapter.Fill (data);
  539. Assert.Fail ("#5 Exception shud be thrown : Mapping is missing");
  540. }catch (AssertionException e){
  541. throw e;
  542. }catch (Exception e) {
  543. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  544. "#6 Incorrect Exception : " + e);
  545. }
  546. try {
  547. adapter.MissingMappingAction = (MissingMappingAction)(-5000);
  548. Assert.Fail ("#7 Exception shud be thrown : Invalid Value");
  549. }catch (AssertionException e){
  550. throw e;
  551. }catch (Exception e){
  552. Assert.AreEqual (typeof(ArgumentException), e.GetType(),
  553. "#8 Incorrect Exception : " +e);
  554. }
  555. // Test if mapping the column and table names works correctly
  556. adapter.MissingMappingAction = MissingMappingAction.Error;
  557. data.Reset ();
  558. DataTable table = data.Tables.Add ("numeric_family_1");
  559. table.Columns.Add ("id_1");
  560. table.Columns.Add ("type_bit_1");
  561. table.Columns.Add ("type_int_1");
  562. DataTableMapping tableMap = adapter.TableMappings.Add ("numeric_family",
  563. "numeric_family_1");
  564. tableMap.ColumnMappings.Add ("id", "id_1");
  565. tableMap.ColumnMappings.Add ("type_bit", "type_bit_1");
  566. tableMap.ColumnMappings.Add ("type_int", "type_int_1");
  567. adapter.Fill (data,"numeric_family");
  568. Assert.AreEqual (1, data.Tables.Count ,
  569. "#8 The DataTable shud be correctly mapped");
  570. Assert.AreEqual (3, data.Tables[0].Columns.Count,
  571. "#9 The DataColumns shud be corectly mapped");
  572. Assert.AreEqual (1, data.Tables[0].Rows.Count,
  573. "#10 Data shud be populated if mapping is correct");
  574. }
  575. // Test case for bug #76433
  576. [Test]
  577. public void FillSchema_ValuesTest()
  578. {
  579. SqlConnection conn = new SqlConnection(connectionString);
  580. using (conn) {
  581. conn.Open();
  582. IDbCommand command = conn.CreateCommand();
  583. // Create Temp Table
  584. String cmd = "Create Table #tmp_TestTable (" ;
  585. cmd += "Field1 DECIMAL (10) NOT NULL,";
  586. cmd += "Field2 DECIMAL(19))";
  587. command.CommandText = cmd;
  588. command.ExecuteNonQuery();
  589. DataSet dataSet = new DataSet();
  590. string selectString = "SELECT * FROM #tmp_TestTable";
  591. IDbDataAdapter dataAdapter = new SqlDataAdapter (
  592. selectString,conn);
  593. dataAdapter.FillSchema(dataSet, SchemaType.Mapped);
  594. Assert.AreEqual (1, dataSet.Tables.Count, "#1");
  595. DataColumn col = dataSet.Tables[0].Columns[0];
  596. Assert.IsFalse (dataSet.Tables[0].Columns[0].AllowDBNull,"#2");
  597. Assert.IsTrue (dataSet.Tables[0].Columns[1].AllowDBNull,"#3");
  598. }
  599. }
  600. [Test]
  601. public void Fill_CheckSchema ()
  602. {
  603. SqlConnection conn = new SqlConnection(connectionString);
  604. using (conn) {
  605. conn.Open();
  606. IDbCommand command = conn.CreateCommand();
  607. // Create Temp Table
  608. String cmd = "Create Table #tmp_TestTable (" ;
  609. cmd += "id int primary key,";
  610. cmd += "field int not null)";
  611. command.CommandText = cmd;
  612. command.ExecuteNonQuery();
  613. DataSet dataSet = new DataSet();
  614. string selectString = "SELECT * from #tmp_TestTable";
  615. IDbDataAdapter dataAdapter = new SqlDataAdapter (
  616. selectString,conn);
  617. dataAdapter.Fill (dataSet);
  618. Assert.IsTrue (dataSet.Tables[0].Columns[1].AllowDBNull, "#1");
  619. Assert.AreEqual (0, dataSet.Tables[0].PrimaryKey.Length, "#2");
  620. dataSet.Reset ();
  621. dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey ;
  622. dataAdapter.Fill (dataSet);
  623. Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#3");
  624. Assert.AreEqual (1, dataSet.Tables[0].PrimaryKey.Length, "#4");
  625. }
  626. }
  627. [Test]
  628. public void FillSchema_CheckSchema ()
  629. {
  630. SqlConnection conn = new SqlConnection(connectionString);
  631. using (conn) {
  632. conn.Open();
  633. IDbCommand command = conn.CreateCommand();
  634. // Create Temp Table
  635. String cmd = "Create Table #tmp_TestTable (" ;
  636. cmd += "id int primary key,";
  637. cmd += "field int not null)";
  638. command.CommandText = cmd;
  639. command.ExecuteNonQuery();
  640. DataSet dataSet = new DataSet();
  641. string selectString = "SELECT * from #tmp_TestTable";
  642. IDbDataAdapter dataAdapter = new SqlDataAdapter (
  643. selectString,conn);
  644. dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
  645. Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#1");
  646. dataSet.Reset ();
  647. dataAdapter.MissingSchemaAction = MissingSchemaAction.Add;
  648. dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
  649. Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#2");
  650. dataSet.Reset ();
  651. dataAdapter.MissingSchemaAction = MissingSchemaAction.Ignore;
  652. dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
  653. Assert.AreEqual (0, dataSet.Tables.Count, "#3");
  654. dataSet.Reset ();
  655. dataAdapter.MissingSchemaAction = MissingSchemaAction.Error;
  656. try {
  657. dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
  658. Assert.Fail ("#4 Error should be thrown");
  659. } catch (InvalidOperationException e) {
  660. }
  661. }
  662. }
  663. }
  664. }