CqlTestFixture.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Abc.Zebus.Directory.Cassandra.Cql;
  2. using Abc.Zebus.Util;
  3. using Cassandra;
  4. using Cassandra.Data.EntityContext;
  5. using Cassandra.Data.Linq;
  6. using Moq;
  7. using NUnit.Framework;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Reflection;
  12. namespace Abc.Zebus.Directory.Cassandra.Tests.Cql
  13. {
  14. // TODO: This entire namespace belongs in Zebus.Testing but it would require the creation of a Zebus.Shared, and we are not ready for that just yet
  15. public abstract class CqlTestFixture<TDataContext, TConfig>
  16. where TDataContext : CqlDataContext<TConfig>
  17. where TConfig : class, ICassandraConfiguration
  18. {
  19. private readonly string _keySpace = "UnitTestingKeyspace_" + Guid.NewGuid().ToString().Substring(0, 8);
  20. private readonly CassandraCqlSessionManager _sessionManager = new CassandraCqlSessionManager();
  21. protected Cluster Cluster { get; private set; }
  22. public TDataContext DataContext { get; private set; }
  23. protected ISession Session { get; private set; }
  24. public Mock<TConfig> ConfigurationMock { get; private set; }
  25. protected abstract string Hosts { get; }
  26. protected abstract string LocalDataCenter { get; }
  27. [TestFixtureSetUp]
  28. public void CreateSchema()
  29. {
  30. Diagnostics.CassandraTraceSwitch.Level = TraceLevel.Verbose;
  31. Diagnostics.CassandraStackTraceIncluded = true;
  32. ConfigurationMock = CreateConfigurationMock();
  33. var strategyReplicationProperty = ReplicationStrategies.CreateNetworkTopologyStrategyReplicationProperty(new Dictionary<string, int> { { LocalDataCenter, 1 } });
  34. Cluster = _sessionManager.GetCluster(ConfigurationMock.Object);
  35. Cluster.ConnectAndCreateDefaultKeyspaceIfNotExists(strategyReplicationProperty).Dispose();
  36. Session = _sessionManager.GetSession(ConfigurationMock.Object);
  37. DataContext = CreateDataContext();
  38. DataContext.CreateTablesIfNotExist();
  39. }
  40. private TDataContext CreateDataContext()
  41. {
  42. // To enhance if needed
  43. return (TDataContext)Activator.CreateInstance(typeof(TDataContext), _sessionManager, ConfigurationMock.Object);
  44. }
  45. private Mock<TConfig> CreateConfigurationMock()
  46. {
  47. return new CassandraConfigurationMock<TConfig>(Hosts, _keySpace, LocalDataCenter, 5.Seconds());
  48. }
  49. [TestFixtureTearDown]
  50. public void DropSchema()
  51. {
  52. Session.Execute(new SimpleStatement(string.Format("drop keyspace \"{0}\";", _keySpace)));
  53. _sessionManager.Dispose();
  54. }
  55. [TearDown]
  56. public void TruncateAllColumnFamilies()
  57. {
  58. var tableNames = GetTableNames();
  59. foreach (var name in tableNames)
  60. Session.Execute(new SimpleStatement(string.Format("truncate {0};", name)));
  61. }
  62. private IEnumerable<string> GetTableNames()
  63. {
  64. var fieldInfo = typeof(Context).GetField("_tables", BindingFlags.Instance | BindingFlags.NonPublic);
  65. if (fieldInfo == null)
  66. yield break;
  67. dynamic tableDictionary = fieldInfo.GetValue(DataContext);
  68. foreach (var tableName in tableDictionary.Keys)
  69. {
  70. yield return tableName;
  71. }
  72. }
  73. }
  74. }