Skip to content

Instantly share code, notes, and snippets.

@jrgcubano
Created August 31, 2018 12:29
Show Gist options
  • Save jrgcubano/83c627c0ff47420d7b067d1e0c6359af to your computer and use it in GitHub Desktop.
Save jrgcubano/83c627c0ff47420d7b067d1e0c6359af to your computer and use it in GitHub Desktop.
Xunit ES Acceptance Testing with CollectionFixture and Simple Fixture...
[CollectionDefinition(nameof(CarparkDataSourceCollection))]
public class CarparkDataSourceCollection : ICollectionFixture<CarparkDataSourceFixture>
{
// Intentionally left empty as collection fixture marker
}
public class CarparkDataSourceFixture : IDisposable
{
readonly ElasticsearchInside.Elasticsearch _elasticServer;
public CarparkDataSourceFixture()
{
_elasticServer = new ElasticsearchInside.Elasticsearch(server => server.SetPort(9333))
.ReadySync();
var connectionSettings = new ConnectionSettings(_elasticServer.Url)
.MapDefaultTypeIndices(m => m
.Add(typeof(CarParkEntity), "parkingapi-qa-entity-carpark"));
//.Add(typeof(ElasticCarparkCounterData), _indexSet.GetFormatted()));
ElasticClient = new ElasticClient(connectionSettings);
seedCarparksAnalyticsData(ElasticClient);
}
static List<T> getItems<T>(IElasticClient client, string file) =>
client.Serializer.Deserialize<List<T>>(
Assembly
.GetExecutingAssembly()
.GetManifestResourceStream($"Ylp.WebApi.Analytics.StandAloneTester.Carparks.Data.{file}.json"));
static void createIndex<T>(IElasticClient client, string indexName, string fileName) where T : class
{
var created = client.CreateIndex(indexName, desc => desc.Settings(s => s.NumberOfShards(1).NumberOfReplicas(0))
.Mappings(m => m.Map<T>(mm => mm.AutoMap())));
if (created.ServerError != null)
throw new Exception(created.ServerError.Error.Reason);
var items = getItems<T>(client, fileName);
client.Bulk(b => b.IndexMany(items, (d, item) => d.Index(indexName)));
}
static void seedCarparksAnalyticsData(IElasticClient client)
{
client.DeleteIndex("parkingapi-qa-entity-carpark");
createIndex<CarParkEntity>(client, "parkingapi-qa-entity-carpark", "carparks");
client.Refresh("parkingapi-qa-entity-carpark");
}
static void deleteIndices(IElasticClient client)
{
client.DeleteIndex("parkingapi-qa-entity-carpark");
}
public void Dispose()
{
_elasticServer?.Dispose();
}
public IElasticClient ElasticClient { get; }
}
public class CarparkExampleControllerFixture : IDisposable
{
readonly CarparkDataSourceFixture _dataSourceFixture;
public CarparkExampleControllerFixture(CarparkDataSourceFixture dataSourceFixture)
{
_dataSourceFixture = dataSourceFixture;
}
public void Dispose()
{
}
}
[Collection(nameof(CarparkDataSourceCollection))]
public class WhenExample1Test
{
readonly CarparkDataSourceFixture _dataSourceFixture;
readonly CarparkExampleControllerFixture _controllerFixture;
public WhenExample1Test(CarparkDataSourceFixture dataSourceFixture)
{
_dataSourceFixture = dataSourceFixture;
_controllerFixture = new CarparkExampleControllerFixture(dataSourceFixture);
}
[Fact]
public async Task Test11()
{
var result = _dataSourceFixture.ElasticClient.Search<CarParkEntity>(cp => cp);
var doc = result.Documents.Single(x => x.Id.Equals("4083faa4-27a1-4ace-b903-3ae4a2a162c0"));
doc.Id.Should().Be("4083faa4-27a1-4ace-b903-3ae4a2a162c0");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment