Skip to content

Commit

Permalink
tests rewritten using xUnit library
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomáš Pozler committed Oct 17, 2023
1 parent efb4836 commit a9c7e1c
Show file tree
Hide file tree
Showing 8 changed files with 1,455 additions and 92 deletions.
33 changes: 33 additions & 0 deletions EvitaDB.TestX/BaseTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using EvitaDB.Client;
using Xunit.Abstractions;

namespace EvitaDB.TestX;

public abstract class BaseTest : IClassFixture<SetupFixture>, IAsyncLifetime
{
protected readonly ITestOutputHelper _outputHelper;
protected readonly SetupFixture _setupFixture;

protected EvitaClient? _client;

private const int RandomSeed = 42;
protected static readonly Random Random = new(RandomSeed);
protected const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss zzz";

public BaseTest(ITestOutputHelper outputHelper, SetupFixture setupFixture)
{
_outputHelper = outputHelper;
_setupFixture = setupFixture;
}

public async Task InitializeAsync()
{
_client = await _setupFixture.GetClient();
}

public Task DisposeAsync()
{
_setupFixture.ReturnClient(_client!);
return Task.CompletedTask;
}
}
210 changes: 210 additions & 0 deletions EvitaDB.TestX/EvitaClientDemoQueryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
using System.Globalization;
using EvitaDB.Client;
using EvitaDB.Client.Config;
using EvitaDB.Client.DataTypes;
using EvitaDB.Client.Models;
using EvitaDB.Client.Models.Data;
using EvitaDB.Client.Models.Data.Structure;
using EvitaDB.Client.Queries.Order;
using EvitaDB.Client.Queries.Requires;
using EvitaDB.TestX.Utils;
using static EvitaDB.Client.Queries.IQueryConstraints;

namespace EvitaDB.TestX;

public class EvitaClientDemoQueryTest : IDisposable
{
private static readonly EvitaClientConfiguration? EvitaClientConfiguration = new EvitaClientConfiguration.Builder()
.SetHost("demo.evitadb.io")
.SetPort(5556)
.SetUseGeneratedCertificate(false)
.SetUsingTrustedRootCaCertificate(true)
.Build();

private static EvitaClient? _client;

private const string ExistingCatalogWithData = "evita";

public EvitaClientDemoQueryTest()
{
_client = new EvitaClient(EvitaClientConfiguration!);
}

[Fact]
public void ShouldBeAbleToQueryCatalogWithDataAndGetDataChunkOfEntityReferences()
{
EvitaEntityReferenceResponse referenceResponse = _client!.QueryCatalog(ExistingCatalogWithData,
session => session.Query<EvitaEntityReferenceResponse, EntityReference>(
Query(
Collection("Product"),
FilterBy(
And(
Not(
AttributeContains("url", "bla")
),
Or(
EntityPrimaryKeyInSet(677, 678, 679, 680),
And(
PriceBetween(1.2m, 1000),
PriceValidIn(DateTimeOffset.Now),
PriceInPriceLists("basic", "vip"),
PriceInCurrency(new Currency("CZK"))
)
)
)
),
OrderBy(
PriceNatural(OrderDirection.Desc)
),
Require(
Page(1, 20),
DataInLocales(new CultureInfo("en-US"), new CultureInfo("cs-CZ")),
QueryTelemetry()
)
)
));

Assert.Equal(20, referenceResponse.RecordPage.Data!.Count);
Assert.True(referenceResponse.RecordPage.Data.All(x => x is {Type: "Product", PrimaryKey: > 0}));
Assert.Equal(1, referenceResponse.ExtraResults.Count);
Assert.Equal(typeof(Client.Models.ExtraResults.QueryTelemetry), referenceResponse.ExtraResults.Values.ToList()[0].GetType());
}

[Fact]
public void ShouldBeAbleToQueryCatalogWithDataAndGetDataChunkOfSealedEntities()
{
EvitaEntityResponse entityResponse = _client!.QueryCatalog(ExistingCatalogWithData, session =>
session.Query<EvitaEntityResponse, ISealedEntity>(
Query(
Collection("Product"),
FilterBy(
And(
Not(
AttributeContains("url", "bla")
),
Or(
EntityPrimaryKeyInSet(677, 678, 679, 680),
And(
PriceBetween(102.2m, 10000),
PriceValidIn(DateTimeOffset.Now),
PriceInPriceLists("basic", "vip"),
PriceInCurrency(new Currency("CZK"))
)
)
)
),
OrderBy(
PriceNatural(OrderDirection.Desc)
),
Require(
Page(1, 20),
EntityFetch(AttributeContentAll(), ReferenceContentAll(), PriceContentAll()),
DataInLocales(new CultureInfo("en-US"), new CultureInfo("cs-CZ")),
QueryTelemetry()
)
)
));

Assert.Equal(20, entityResponse.RecordPage.Data!.Count);
Assert.Contains(entityResponse.RecordPage.Data, x => x.GetAttributeValues().Any());
Assert.Contains(entityResponse.RecordPage.Data, x => x.GetReferences().Any());
Assert.Contains(entityResponse.RecordPage.Data, x => x.GetPrices().Any());

Assert.Equal(1, entityResponse.ExtraResults.Count);
Assert.Equal(typeof(Client.Models.ExtraResults.QueryTelemetry), entityResponse.ExtraResults.Values.ToList()[0].GetType());
}

[Fact]
public void ShouldBeAbleToExecuteComplexQueryAndGetResults()
{
EvitaEntityResponse evitaEntityResponse = _client!.QueryCatalog(ExistingCatalogWithData,
session => session.Query<EvitaEntityResponse, ISealedEntity>(
Query(
Collection("Product"),
FilterBy(
Or(
AttributeInSet("visibility", "yes", "no"),
EntityLocaleEquals(new CultureInfo("cs-CZ")),
HierarchyWithin("categories", null, ExcludingRoot()),
Not(
ReferenceHaving(
"groups",
And(
AttributeInRange("assignmentValidity", DateTimeOffset.Now),
AttributeInRange("assignmentValidity", DateTimeOffset.Now.AddDays(7))
)
)
),
And(
PriceInPriceLists("vip", "basic"),
PriceInCurrency("CZK"),
PriceBetween(100m, 200.5m),
PriceValidNow()
),
AttributeBetween("frekvence-od", 20m, 95.3m),
UserFilter(
FacetHaving("variantParameters", EntityPrimaryKeyInSet(1, 2, 3))
)
)),
OrderBy(
AttributeNatural("orderedQuantity", OrderDirection.Desc),
PriceNatural(OrderDirection.Asc),
ReferenceProperty(
"groups", Random()
)
),
Require(
EntityFetch(
AssociatedDataContentAll(),
AttributeContentAll(),
PriceContentAll(),
ReferenceContentWithAttributes(
"relatedProducts",
FilterBy(AttributeContains("category", "w")),
OrderBy(EntityProperty(AttributeNatural("orderedQuantity", OrderDirection.Asc))),
EntityFetch(AttributeContentAll())
),
HierarchyContent(StopAt(Distance(2)), EntityFetch(AttributeContentAll()))
),
FacetSummaryOfReference(
"parameterValues",
FacetStatisticsDepth.Impact,
FilterBy(AttributeContains("code", "a")),
OrderBy(AttributeNatural(Data.AttributeName, OrderDirection.Desc))
),
FacetGroupsDisjunction("productSetItems", FilterBy(AttributeGreaterThanEquals("order", 5))),
AttributeHistogram(20, "response-time", "thickness"),
PriceHistogram(10),
QueryTelemetry(),
HierarchyOfReference(
"categories",
FromNode(
"x",
Node(FilterBy(AttributeEquals("code", "portables"))),
EntityFetch(AttributeContent("categoryType")),
StopAt(Distance(1)),
Statistics(StatisticsType.ChildrenCount, StatisticsType.QueriedEntityCount)
),
FromNode(
"y",
Node(FilterBy(AttributeContains("code", "laptops"))),
EntityFetch(AttributeContent("status")),
StopAt(Distance(2)),
Statistics(StatisticsType.ChildrenCount, StatisticsType.QueriedEntityCount)
)
),
PriceType(QueryPriceMode.WithoutTax),
Strip(0, 20)
)
)
)
);

Assert.True(evitaEntityResponse.RecordData.Count > 0);
}

public void Dispose()
{
_client?.Dispose();
}
}
Loading

0 comments on commit a9c7e1c

Please sign in to comment.