All pastes #279876 Raw Edit

Nagappan

public text v1 · immutable
·published 2006-12-15 11:50 UTC
rendered paste body
		[Test]
		public void FillSchemaTest ()
		{
			string query = ""; 	

			// Test if connection is closed if excepton occurs during fill schema 
			query = "select * from invalid_table"; 
			adapter = new SqlDataAdapter (query, connectionString);
			data = new DataSet ("test");
			try {
				adapter.FillSchema (data , SchemaType.Source);
			}catch (Exception e){
				if ( adapter.SelectCommand.Connection.State != ConnectionState.Closed)
				{
					Assert.Fail ("#0 Conn shud be closed if exception occurs");
					adapter.SelectCommand.Connection.Close();
				}
			}
		
			// Test Primary Key is set (since primary key column returned)	
			query = "select id, type_int from numeric_family where id=1";	
			adapter = new SqlDataAdapter (query, connectionString);
			data = new DataSet ("test1");
			adapter.FillSchema (data , SchemaType.Source);

			Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length,
				"#1 Primary Key property must be set");
	
			// Test Primary Key is not set (since primary key column is returned)	
			query = "select type_bit, type_int from numeric_family where id=1"; 	
			adapter = new SqlDataAdapter (query, connectionString);
			data = new DataSet ("test2");
			adapter.FillSchema (data, SchemaType.Source);
			Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length,
				"#2 Primary Key property should not be set");

			// Test multiple tables are created for a batch query
			query = "Select id ,type_bit from numeric_family;" ;
			query += "Select id,type_bit,type_int from numeric_family;"; 
			data = new DataSet ("test3");
			adapter = new SqlDataAdapter (query, connectionString);
			adapter.FillSchema (data , SchemaType.Source);
			Assert.AreEqual (2 , data.Tables.Count , "#3 A table shud be created for each Result Set");
			Assert.AreEqual (2 , data.Tables[0].Columns.Count , "#4 should have 2 columns");
			Assert.AreEqual (3 , data.Tables[1].Columns.Count , "#5 Should have 3 columns");

			// Test if table names and column names  are filled correctly
			query = "select 10,20 from numeric_family;" ;
			query += "select 10,20 from numeric_family;";
			adapter = new SqlDataAdapter (query, connectionString);
			data = new DataSet ("test4");
			try {
				adapter.FillSchema (data , SchemaType.Source);
			}catch (Exception e){
				Assert.Fail ("#3 Unexpected Exception : " + e); 
			}
			Assert.AreEqual ( "Table", data.Tables[0].TableName);
			Assert.AreEqual ( "Table1", data.Tables[1].TableName);
			Assert.AreEqual ( "Column1", data.Tables[0].Columns[0].ColumnName,
				"#6 Unnamed col shud be named as 'ColumnN'");
			Assert.AreEqual ( "Column2", data.Tables[0].Columns[1].ColumnName,
				"#7 Unnamed col shud be named as 'ColumnN'");
			Assert.AreEqual ( "Column1", data.Tables[1].Columns[0].ColumnName,
				"#8 Unnamed col shud be named as 'ColumnN'");
			Assert.AreEqual ( "Column2", data.Tables[1].Columns[1].ColumnName,
				"#9 Unnamed col shud be named as 'ColumnN'");
			Assert.AreEqual (ConnectionState.Closed, adapter.SelectCommand.Connection.State,
				"#10 Connection shud be closed");
			
			// Test if mapping works correctly  
			// doesent work in both mono and msdotnet
			// gotto check if something is wrong 
			/*
			query = "select id,type_bit from numeric_family";  
			adapter = new SqlDataAdapter (query, connectionString);
			data = new DataSet ("test");
			DataTable table = data.Tables.Add ("numeric_family_1");
			table.Columns.Add ("id");
			table.Columns.Add ("type_bit");
			DataTableMapping map = adapter.TableMappings.Add("numeric_family_1",
							"numeric_family");
			map.ColumnMappings.Add ("id", "id_1");
			map.ColumnMappings.Add ("type_bit", "type_bit_1");
			adapter.FillSchema (data, SchemaType.Source, "numeric_family");
			foreach (DataTable tab in data.Tables){
				Console.WriteLine ("Table == {0}",tab.TableName);
				foreach (DataColumn col in tab.Columns)
					Console.WriteLine (" Col = {0} " , col.ColumnName);
			}			
			*/
		}