Merge pull request #1253 from esdrubal/datetimeiso8601
[mono.git] / mcs / class / System.Data / Test / System.Data.Common / DbDataReaderTest.cs
1 // DbDataReaderTest.cs - NUnit Test Cases for DbDataReader class
2 //
3 // Author:
4 //      Mika Aalto (mika@aalto.pro)
5 //
6 // Copyright (C) 2014 Mika Aalto
7 //
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:
15 //
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 //
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.
26 //
27
28
29 using NUnit.Framework;
30 using System;
31 using System.Data;
32 using System.Data.Common;
33 using System.IO;
34
35 namespace MonoTests.System.Data.Common
36 {
37         [TestFixture]
38         public class DbDataReaderTest
39         {
40                 DbDataReaderMock dataReader;
41
42                 [SetUp]
43                 public void SetUp ()
44                 {
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[]));
49
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 });
53
54                         dataReader = new DbDataReaderMock (testData);
55
56                         Assert.AreEqual (3, testData.Rows.Count);
57                 }
58
59                 [TearDown]
60                 public void TearDown ()
61                 {
62                 }
63
64                 #if NET_4_5
65                 [Test]
66                 public void GetFieldValueTest ()
67                 {
68                         //First row
69                         dataReader.Read ();
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);
76                         }
77
78                         //Second row where data row column value is DBNull
79                         dataReader.Read ();
80                         Assert.AreEqual ("row_2", dataReader.GetFieldValue<string> (0), "#4");
81                         try {
82                                 actual_data = dataReader.GetFieldValue<byte[]> (1);
83                                 Assert.Fail ("GetFieldValue method should throw InvalidCastException for DBNull values #5");
84                         } catch (InvalidCastException) {
85                                 //This is expected
86                         }
87
88                         //Third row
89                         dataReader.Read ();
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");
95                 }
96
97                 [Test]
98                 public void GetStreamTest ()
99                 {
100                         int testColOrdinal = 1;
101                         byte[] buffer = new byte[1024];
102
103                         dataReader.Read ();
104                         Stream stream = dataReader.GetStream (testColOrdinal);
105                         Assert.IsNotNull (stream, "Stream from datareader is null #1");
106
107                         //Read stream content to byte buffer
108                         int data_length = stream.Read (buffer, 0, buffer.Length);
109
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);
115                         }
116
117                         //Get DBNull value stream
118                         Assert.IsTrue (dataReader.Read ());
119                         stream = dataReader.GetStream (testColOrdinal);
120                         Assert.AreEqual (0, stream.Length, "#4");
121
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");
128                 }
129
130                 [Test]
131                 public void GetTextReader ()
132                 {
133                         int testColOrdinal = 0;
134
135                         //Read first row
136                         dataReader.Read ();
137                         TextReader textReader = dataReader.GetTextReader (testColOrdinal);
138                         Assert.IsNotNull (textReader, "return value from datareader GetTextReader method is null #1");
139
140                         string txt = textReader.ReadToEnd ();
141                         Assert.AreEqual ("row_1", txt, "#2");
142
143                         //Move to second row
144                         Assert.IsTrue (dataReader.Read ());
145                         textReader = dataReader.GetTextReader (testColOrdinal);
146                         txt = textReader.ReadToEnd ();
147                         Assert.AreEqual ("row_2", txt, "#3");
148
149                         //Move to third row
150                         Assert.IsTrue (dataReader.Read ());
151                         textReader = dataReader.GetTextReader (testColOrdinal);
152                         txt = textReader.ReadToEnd ();
153                         Assert.AreEqual ("row_3", txt, "#4");
154
155                         Assert.IsFalse (dataReader.Read (), "#5");
156                 }
157
158                 #endif //NET_4_5
159         }
160 }
161