PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Sources/CsvReader.Azure.Tests/Utility.cs

https://github.com/tpwalke2/DataTable
C# | 85 lines | 65 code | 12 blank | 8 comment | 3 complexity | fa350fed830008fd26674ba1c996fed7 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.WindowsAzure.StorageClient;
  6. using System.Data.Services.Client;
  7. using Xunit;
  8. using System.IO;
  9. using DataAccess;
  10. using Microsoft.WindowsAzure;
  11. namespace CsvReader.Azure.Tests
  12. {
  13. internal static class Utility
  14. {
  15. public static T Lookup<T>(CloudTableClient tableClient, string tableName, string partitionKey, string rowKey) where T : TableServiceEntity
  16. {
  17. TableServiceContext ctx = tableClient.GetDataServiceContext();
  18. // Azure will special case this lookup pattern for a single entity.
  19. // See http://blogs.msdn.com/b/windowsazurestorage/archive/2010/11/06/how-to-get-most-out-of-windows-azure-tables.aspx
  20. try
  21. {
  22. // This will throw DataServiceQueryException if not found. (as opposed to return an empty query)
  23. var x = from row in ctx.CreateQuery<T>(tableName)
  24. where row.PartitionKey == partitionKey && row.RowKey == rowKey
  25. select row;
  26. var x2 = x.AsTableServiceQuery<T>();
  27. return x2.First();
  28. }
  29. catch (DataServiceQueryException)
  30. {
  31. // Not found.
  32. return null;
  33. }
  34. }
  35. // Table will come back sorted by (parition, row key)
  36. // Integers don't sort nicely as strings.
  37. public static T[] ReadTable<T>(CloudStorageAccount account, string tableName) where T : TableServiceEntity
  38. {
  39. CloudTableClient tableClient = account.CreateCloudTableClient();
  40. TableServiceContext ctx = tableClient.GetDataServiceContext();
  41. var query = from row in ctx.CreateQuery<T>(tableName) select row;
  42. var query2 = query.AsTableServiceQuery<T>();
  43. // Verify table matches source
  44. T[] result = query2.ToArray();
  45. return result;
  46. }
  47. // Do string comparison, ignoring ignoring newline (\r\n vs just \n)
  48. public static void AssertEquals(string content, string actual)
  49. {
  50. content = content.Replace("\r\n", "\n");
  51. actual = actual.Replace("\r\n", "\n");
  52. Assert.Equal(content, actual);
  53. }
  54. public static void AssertEquals(string content, DataTable dt)
  55. {
  56. string actual = ToString(dt);
  57. AssertEquals(content, actual);
  58. }
  59. public static void AssertEquals(DataTable tableExpected, DataTable tableActual)
  60. {
  61. string actual = ToString(tableActual);
  62. string content = ToString(tableExpected);
  63. AssertEquals(content, actual);
  64. }
  65. private static string ToString(DataTable dt)
  66. {
  67. StringWriter sw = new StringWriter();
  68. dt.SaveToStream(sw);
  69. string actual = sw.ToString();
  70. return actual;
  71. }
  72. }
  73. }