DbDataReaderTest.cs 5.0 KB

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