1 // DbDataReaderTest.cs - NUnit Test Cases for DbDataReader class
4 // Mika Aalto (mika@aalto.pro)
6 // Copyright (C) 2014 Mika Aalto
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using NUnit.Framework;
32 using System.Data.Common;
35 namespace MonoTests.System.Data.Common
38 public class DbDataReaderTest
40 DbDataReaderMock dataReader;
45 //Setup test data table
46 DataTable testData = new DataTable ();
47 testData.Columns.Add ("text_col", typeof(string));
48 testData.Columns.Add ("binary_col", typeof(byte[]));
50 testData.Rows.Add ("row_1", new byte[] { 0xde, 0xad, 0xbe, 0xef });
51 testData.Rows.Add ("row_2", DBNull.Value);
52 testData.Rows.Add ("row_3", new byte[] { 0x00 });
54 dataReader = new DbDataReaderMock (testData);
56 Assert.AreEqual (3, testData.Rows.Count);
60 public void TearDown ()
66 public void GetFieldValueTest ()
70 Assert.AreEqual ("row_1", dataReader.GetFieldValue<string> (0), "#1");
71 byte[] expected_data = new byte[] { 0xde, 0xad, 0xbe, 0xef };
72 byte[] actual_data = dataReader.GetFieldValue<byte[]> (1);
73 Assert.AreEqual (expected_data.Length, actual_data.Length, "#2");
74 for (int i = 0; i < expected_data.Length; i++) {
75 Assert.AreEqual (expected_data [i], actual_data [i], "#3 at index " + i);
78 //Second row where data row column value is DBNull
80 Assert.AreEqual ("row_2", dataReader.GetFieldValue<string> (0), "#4");
82 actual_data = dataReader.GetFieldValue<byte[]> (1);
83 Assert.Fail ("GetFieldValue method should throw InvalidCastException for DBNull values #5");
84 } catch (InvalidCastException) {
90 Assert.AreEqual ("row_3", dataReader.GetFieldValue<string> (0), "#6");
91 expected_data = new byte[] { 0x00 };
92 actual_data = dataReader.GetFieldValue<byte[]> (1);
93 Assert.AreEqual (expected_data.Length, actual_data.Length, "#7");
94 Assert.AreEqual (expected_data [0], actual_data [0], "#8");
98 public void GetStreamTest ()
100 int testColOrdinal = 1;
101 byte[] buffer = new byte[1024];
104 Stream stream = dataReader.GetStream (testColOrdinal);
105 Assert.IsNotNull (stream, "Stream from datareader is null #1");
107 //Read stream content to byte buffer
108 int data_length = stream.Read (buffer, 0, buffer.Length);
110 //Verify that content is expected
111 byte[] expected = new byte[] { 0xde, 0xad, 0xbe, 0xef };
112 Assert.AreEqual (expected.Length, data_length, "#2");
113 for (int i = 0; i < expected.Length; i++) {
114 Assert.AreEqual (expected [i], buffer [i], "#3 at index " + i);
117 //Get DBNull value stream
118 Assert.IsTrue (dataReader.Read ());
119 stream = dataReader.GetStream (testColOrdinal);
120 Assert.AreEqual (0, stream.Length, "#4");
122 //Get single byte value stream
123 Assert.IsTrue (dataReader.Read ());
124 stream = dataReader.GetStream (testColOrdinal);
125 expected = new byte[] { 0x00 };
126 Assert.AreEqual (expected.Length, stream.Length, "#5");
127 Assert.AreEqual (expected [0], stream.ReadByte (), "#6");
131 public void GetTextReader ()
133 int testColOrdinal = 0;
137 TextReader textReader = dataReader.GetTextReader (testColOrdinal);
138 Assert.IsNotNull (textReader, "return value from datareader GetTextReader method is null #1");
140 string txt = textReader.ReadToEnd ();
141 Assert.AreEqual ("row_1", txt, "#2");
144 Assert.IsTrue (dataReader.Read ());
145 textReader = dataReader.GetTextReader (testColOrdinal);
146 txt = textReader.ReadToEnd ();
147 Assert.AreEqual ("row_2", txt, "#3");
150 Assert.IsTrue (dataReader.Read ());
151 textReader = dataReader.GetTextReader (testColOrdinal);
152 txt = textReader.ReadToEnd ();
153 Assert.AreEqual ("row_3", txt, "#4");
155 Assert.IsFalse (dataReader.Read (), "#5");