DbDataReaderTest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // DbDataReaderTest.cs - NUnit Test Cases for DbDataReader class
  2. //
  3. // Author:
  4. // Mika Aalto ([email protected])
  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. #if NET_2_0
  28. using NUnit.Framework;
  29. using System;
  30. using System.Data;
  31. using System.Data.Common;
  32. using System.IO;
  33. namespace Test.System.Data.Common
  34. {
  35. [TestFixture]
  36. public class DbDataReaderTest
  37. {
  38. DbDataReaderMock dataReader;
  39. [SetUp]
  40. public void SetUp ()
  41. {
  42. //Setup test data table
  43. DataTable testData = new DataTable ();
  44. testData.Columns.Add ("text_col", typeof(string));
  45. testData.Columns.Add ("binary_col", typeof(byte[]));
  46. testData.Rows.Add ("row_1", new byte[] { 0xde, 0xad, 0xbe, 0xef });
  47. testData.Rows.Add ("row_2", DBNull.Value);
  48. testData.Rows.Add ("row_3", new byte[] { 0x00 });
  49. dataReader = new DbDataReaderMock (testData);
  50. Assert.AreEqual (3, testData.Rows.Count);
  51. }
  52. [TearDown]
  53. public void TearDown ()
  54. {
  55. }
  56. #if NET_4_5
  57. [Test]
  58. public void GetFieldValueTest ()
  59. {
  60. //First row
  61. dataReader.Read ();
  62. Assert.AreEqual ("row_1", dataReader.GetFieldValue<string> (0), "#1");
  63. byte[] expected_data = new byte[] { 0xde, 0xad, 0xbe, 0xef };
  64. byte[] actual_data = dataReader.GetFieldValue<byte[]> (1);
  65. Assert.AreEqual (expected_data.Length, actual_data.Length, "#2");
  66. for (int i = 0; i < expected_data.Length; i++) {
  67. Assert.AreEqual (expected_data [i], actual_data [i], "#3 at index " + i);
  68. }
  69. //Second row where data row column value is DBNull
  70. dataReader.Read ();
  71. Assert.AreEqual ("row_2", dataReader.GetFieldValue<string> (0), "#4");
  72. try {
  73. actual_data = dataReader.GetFieldValue<byte[]> (1);
  74. Assert.Fail ("GetFieldValue method should throw InvalidCastException for DBNull values #5");
  75. } catch (InvalidCastException) {
  76. //This is expected
  77. }
  78. //Thord row
  79. dataReader.Read ();
  80. Assert.AreEqual ("row_3", dataReader.GetFieldValue<string> (0), "#6");
  81. expected_data = new byte[] { 0x00 };
  82. actual_data = dataReader.GetFieldValue<byte[]> (1);
  83. Assert.AreEqual (expected_data.Length, actual_data.Length, "#7");
  84. Assert.AreEqual (expected_data [0], actual_data [0], "#8");
  85. }
  86. [Test]
  87. public void GetStreamTest ()
  88. {
  89. int testColOrdinal = 1;
  90. byte[] buffer = new byte[1024];
  91. dataReader.Read ();
  92. Stream stream = dataReader.GetStream (testColOrdinal);
  93. Assert.IsNotNull (stream, "Stream from datareader is null #1");
  94. //Read stream content to byte buffer
  95. int data_length = stream.Read (buffer, 0, buffer.Length);
  96. //Verify that content is expected
  97. byte[] expected = new byte[] { 0xde, 0xad, 0xbe, 0xef };
  98. Assert.AreEqual (expected.Length, data_length, "#2");
  99. for (int i = 0; i < expected.Length; i++) {
  100. Assert.AreEqual (expected [i], buffer [i], "#3 at index " + i);
  101. }
  102. //Get DBNull value stream
  103. Assert.IsTrue (dataReader.Read ());
  104. stream = dataReader.GetStream (testColOrdinal);
  105. Assert.AreEqual (0, stream.Length, "#4");
  106. //Get single byte value stream
  107. Assert.IsTrue (dataReader.Read ());
  108. stream = dataReader.GetStream (testColOrdinal);
  109. expected = new byte[] { 0x00 };
  110. Assert.AreEqual (expected.Length, stream.Length, "#5");
  111. Assert.AreEqual (expected [0], stream.ReadByte (), "#6");
  112. }
  113. [Test]
  114. public void GetTextReader ()
  115. {
  116. int testColOrdinal = 0;
  117. //Read first row
  118. dataReader.Read ();
  119. TextReader textReader = dataReader.GetTextReader (testColOrdinal);
  120. Assert.IsNotNull (textReader, "return value from datareader GetTextReader method is null #1");
  121. string txt = textReader.ReadToEnd ();
  122. Assert.AreEqual ("row_1", txt, "#2");
  123. //Move to second row
  124. Assert.IsTrue (dataReader.Read ());
  125. textReader = dataReader.GetTextReader (testColOrdinal);
  126. txt = textReader.ReadToEnd ();
  127. Assert.AreEqual ("row_2", txt, "#3");
  128. //Move to third row
  129. Assert.IsTrue (dataReader.Read ());
  130. textReader = dataReader.GetTextReader (testColOrdinal);
  131. txt = textReader.ReadToEnd ();
  132. Assert.AreEqual ("row_3", txt, "#4");
  133. Assert.IsFalse (dataReader.Read (), "#5");
  134. }
  135. #endif //NET_4_5
  136. }
  137. }
  138. #endif // NET_2_0