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 ()
65 public void GetFieldValueTest ()
69 Assert.AreEqual ("row_1", dataReader.GetFieldValue<string> (0), "#1");
70 byte[] expected_data = new byte[] { 0xde, 0xad, 0xbe, 0xef };
71 byte[] actual_data = dataReader.GetFieldValue<byte[]> (1);
72 Assert.AreEqual (expected_data.Length, actual_data.Length, "#2");
73 for (int i = 0; i < expected_data.Length; i++) {
74 Assert.AreEqual (expected_data [i], actual_data [i], "#3 at index " + i);
77 //Second row where data row column value is DBNull
79 Assert.AreEqual ("row_2", dataReader.GetFieldValue<string> (0), "#4");
81 actual_data = dataReader.GetFieldValue<byte[]> (1);
82 Assert.Fail ("GetFieldValue method should throw InvalidCastException for DBNull values #5");
83 } catch (InvalidCastException) {
89 Assert.AreEqual ("row_3", dataReader.GetFieldValue<string> (0), "#6");
90 expected_data = new byte[] { 0x00 };
91 actual_data = dataReader.GetFieldValue<byte[]> (1);
92 Assert.AreEqual (expected_data.Length, actual_data.Length, "#7");
93 Assert.AreEqual (expected_data [0], actual_data [0], "#8");
97 public void GetStreamTest ()
99 int testColOrdinal = 1;
100 byte[] buffer = new byte[1024];
103 Stream stream = dataReader.GetStream (testColOrdinal);
104 Assert.IsNotNull (stream, "Stream from datareader is null #1");
106 //Read stream content to byte buffer
107 int data_length = stream.Read (buffer, 0, buffer.Length);
109 //Verify that content is expected
110 byte[] expected = new byte[] { 0xde, 0xad, 0xbe, 0xef };
111 Assert.AreEqual (expected.Length, data_length, "#2");
112 for (int i = 0; i < expected.Length; i++) {
113 Assert.AreEqual (expected [i], buffer [i], "#3 at index " + i);
116 //Get DBNull value stream
117 Assert.IsTrue (dataReader.Read ());
118 stream = dataReader.GetStream (testColOrdinal);
119 Assert.AreEqual (0, stream.Length, "#4");
121 //Get single byte value stream
122 Assert.IsTrue (dataReader.Read ());
123 stream = dataReader.GetStream (testColOrdinal);
124 expected = new byte[] { 0x00 };
125 Assert.AreEqual (expected.Length, stream.Length, "#5");
126 Assert.AreEqual (expected [0], stream.ReadByte (), "#6");
130 public void GetTextReader ()
132 int testColOrdinal = 0;
136 TextReader textReader = dataReader.GetTextReader (testColOrdinal);
137 Assert.IsNotNull (textReader, "return value from datareader GetTextReader method is null #1");
139 string txt = textReader.ReadToEnd ();
140 Assert.AreEqual ("row_1", txt, "#2");
143 Assert.IsTrue (dataReader.Read ());
144 textReader = dataReader.GetTextReader (testColOrdinal);
145 txt = textReader.ReadToEnd ();
146 Assert.AreEqual ("row_2", txt, "#3");
149 Assert.IsTrue (dataReader.Read ());
150 textReader = dataReader.GetTextReader (testColOrdinal);
151 txt = textReader.ReadToEnd ();
152 Assert.AreEqual ("row_3", txt, "#4");
154 Assert.IsFalse (dataReader.Read (), "#5");