| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using Abc.Zebus.Directory.Cassandra.Cql;
- using Abc.Zebus.Util;
- using Cassandra;
- using Cassandra.Data.EntityContext;
- using Cassandra.Data.Linq;
- using Moq;
- using NUnit.Framework;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Reflection;
- namespace Abc.Zebus.Directory.Cassandra.Tests.Cql
- {
- // 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
- public abstract class CqlTestFixture<TDataContext, TConfig>
- where TDataContext : CqlDataContext<TConfig>
- where TConfig : class, ICassandraConfiguration
- {
- private readonly string _keySpace = "UnitTestingKeyspace_" + Guid.NewGuid().ToString().Substring(0, 8);
- private readonly CassandraCqlSessionManager _sessionManager = new CassandraCqlSessionManager();
- protected Cluster Cluster { get; private set; }
- public TDataContext DataContext { get; private set; }
- protected ISession Session { get; private set; }
- public Mock<TConfig> ConfigurationMock { get; private set; }
- protected abstract string Hosts { get; }
- protected abstract string LocalDataCenter { get; }
- [TestFixtureSetUp]
- public void CreateSchema()
- {
- Diagnostics.CassandraTraceSwitch.Level = TraceLevel.Verbose;
- Diagnostics.CassandraStackTraceIncluded = true;
- ConfigurationMock = CreateConfigurationMock();
- var strategyReplicationProperty = ReplicationStrategies.CreateNetworkTopologyStrategyReplicationProperty(new Dictionary<string, int> { { LocalDataCenter, 1 } });
- Cluster = _sessionManager.GetCluster(ConfigurationMock.Object);
- Cluster.ConnectAndCreateDefaultKeyspaceIfNotExists(strategyReplicationProperty).Dispose();
- Session = _sessionManager.GetSession(ConfigurationMock.Object);
- DataContext = CreateDataContext();
- DataContext.CreateTablesIfNotExist();
- }
- private TDataContext CreateDataContext()
- {
- // To enhance if needed
- return (TDataContext)Activator.CreateInstance(typeof(TDataContext), _sessionManager, ConfigurationMock.Object);
- }
- private Mock<TConfig> CreateConfigurationMock()
- {
- return new CassandraConfigurationMock<TConfig>(Hosts, _keySpace, LocalDataCenter, 5.Seconds());
- }
- [TestFixtureTearDown]
- public void DropSchema()
- {
- Session.Execute(new SimpleStatement(string.Format("drop keyspace \"{0}\";", _keySpace)));
- _sessionManager.Dispose();
- }
- [TearDown]
- public void TruncateAllColumnFamilies()
- {
- var tableNames = GetTableNames();
- foreach (var name in tableNames)
- Session.Execute(new SimpleStatement(string.Format("truncate {0};", name)));
- }
- private IEnumerable<string> GetTableNames()
- {
- var fieldInfo = typeof(Context).GetField("_tables", BindingFlags.Instance | BindingFlags.NonPublic);
- if (fieldInfo == null)
- yield break;
- dynamic tableDictionary = fieldInfo.GetValue(DataContext);
- foreach (var tableName in tableDictionary.Keys)
- {
- yield return tableName;
- }
- }
- }
- }
|