public async Task Given_existing_projection_When_projecting_Then_projection_is_updated_And_it_has_data()
        {
            var dependencies = Init(nameof(FunctionUsageActorTests) +
                                    nameof(
                                        Given_existing_projection_When_projecting_Then_projection_is_updated_And_it_has_data
                                        ));
            var context = dependencies.CreateFunctionUsageContext();

            var eventName = "testEvent";

            context.Projections.Add(new Projection
            {
                Event     = eventName,
                Name      = KnownProjectionsNames.FunctionUsage, Sequence = 11,
                Projector = nameof(FunctionsUsageProjector)
            });

            context.FunctionsUsage.Add(new FunctionUsage
            {
                FunctionName     = "testFunction",
                InvocationsCount = 2,
                CalculatorName   = "calcA",
                Period           = TimeSpan.FromMinutes(1),
                PeriodEnd        = DateTimeOffset.Now,
                PeriodStart      = DateTimeOffset.Now - TimeSpan.FromMinutes(1)
            });


            var actor = Sys.ActorOf(Props.Create <FunctionsUsageProjector>(eventName, null));

            actor.Tell(ProjectorActorProtocol.Start.Instance);
            ExpectMsg <ProjectorActorProtocol.Next>();

            var usage = new Sequenced <CalculatorActor.CalculationPerformed>()
            {
                Message  = new CalculatorActor.CalculationPerformed("calcA", "123+1", null, new [] { "Add" }),
                Sequence = 200,
            };

            actor.Tell(usage);

            ExpectMsg <ProjectorActorProtocol.Next>();

            var query      = dependencies.CreateFindProjectionQuery();
            var projection = query.Execute(KnownProjectionsNames.FunctionUsage,
                                           nameof(FunctionsUsageProjector),
                                           eventName);

            //we have an existing projection
            Assert.Equal(usage.Sequence, projection.Sequence);

            //and it contains data
            var usageQuery    = new FunctionsUsageQuery(dependencies.CreateFunctionUsageContext());
            var functionUsage = await usageQuery.Execute("calcA", periodStart : usage.Message.Occured - TimeSpan.FromMinutes(2));

            functionUsage.Should().BeEquivalentTo(new FunctionTotalUsage
            {
                InvocationsCount = 1, FunctionName = "Add"
            });
        }
        public async Task Given_not_existing_projection_When_projecting_Then_projection_is_created_And_has_data()
        {
            var dependencies = Init(nameof(FunctionUsageActorTests) +
                                    nameof(
                                        Given_not_existing_projection_When_projecting_Then_projection_is_created_And_has_data
                                        ));

            var eventName = "testEvent";

            var actor = Sys.ActorOf(Props.Create <FunctionsUsageProjector>(eventName, null));

            actor.Tell(ProjectorActorProtocol.Start.Instance);
            ExpectMsg <ProjectorActorProtocol.Next>();

            var sequenced = new Sequenced <CalculatorActor.CalculationPerformed>
            {
                Sequence = 200,
                Message  = new CalculatorActor.CalculationPerformed("calcA",
                                                                    "123+5", null, new[] { "Add", "Subtract" })
            };

            actor.Tell(sequenced);
            ExpectMsg <ProjectorActorProtocol.Next>();

            var query      = dependencies.CreateFindProjectionQuery();
            var projection = query.Execute(KnownProjectionsNames.FunctionUsage,
                                           nameof(FunctionsUsageProjector),
                                           eventName);

            //we have an existing projection
            Assert.Equal(sequenced.Sequence, projection.Sequence);

            //and it contains data
            var usageQuery    = new FunctionsUsageQuery(dependencies.CreateFunctionUsageContext());
            var functionUsage = await usageQuery.Execute("calcA");

            functionUsage.Should().BeEquivalentTo(new[]
            {
                new FunctionUsage
                {
                    InvocationsCount = 1,
                    FunctionName     = sequenced.Message.FunctionsUsed[0],
                    Period           = TimeSpan.FromMinutes(1),
                    CalculatorName   = sequenced.Message.CalculatorId,
                    PeriodStart      = sequenced.Message.Occured.ToMinutePeriodBegin(),
                    PeriodEnd        = sequenced.Message.Occured.ToMinutePeriodEnd()
                },
                new FunctionUsage
                {
                    InvocationsCount = 1,
                    FunctionName     = sequenced.Message.FunctionsUsed[1],
                    Period           = TimeSpan.FromMinutes(1),
                    CalculatorName   = sequenced.Message.CalculatorId,
                    PeriodStart      = sequenced.Message.Occured.ToMinutePeriodBegin(),
                    PeriodEnd        = sequenced.Message.Occured.ToMinutePeriodEnd()
                }
            });
        }
Пример #3
0
        public async Task Given_context_with_data_When_executing_query_with_filter_Then_it_returns_filtered_data()
        {
            var options = new DbContextOptionsBuilder <FunctionUsageContext>()
                          .UseInMemoryDatabase(nameof(FunctionUsageQueryTests) +
                                               nameof(
                                                   Given_context_with_data_When_executing_query_with_filter_Then_it_returns_filtered_data
                                                   )).Options;

            var context     = new FunctionUsageContext(options);
            var query       = new FunctionsUsageQuery(context);
            var periodStart = DateTimeOffset.Now;

            var functions = new[]
            {
                new FunctionUsage
                {
                    FunctionName     = "testFunc",
                    InvocationsCount = 1,
                    CalculatorName   = "calcA",
                    Period           = TimeSpan.FromMinutes(1),
                    PeriodStart      = periodStart,
                    PeriodEnd        = periodStart + TimeSpan.FromMinutes(1)
                },
                new FunctionUsage
                {
                    FunctionName     = "myFunc",
                    InvocationsCount = 2,
                    CalculatorName   = "calcB",
                    Period           = TimeSpan.FromHours(1),
                    PeriodStart      = periodStart,
                    PeriodEnd        = periodStart + TimeSpan.FromHours(1)
                },
                new FunctionUsage
                {
                    FunctionName     = "myFunc",
                    InvocationsCount = 2,
                    CalculatorName   = "calcB",
                    Period           = TimeSpan.FromHours(1),
                    PeriodStart      = periodStart - TimeSpan.FromHours(1),
                    PeriodEnd        = periodStart
                },
            };

            context.FunctionsUsage.AddRange(functions);
            await context.SaveChangesAsync();

            var res = await query.Execute("calcB", DateTimeOffset.Now - TimeSpan.FromHours(2), DateTimeOffset.Now);

            Assert.Equal(functions[2], res.Single());
        }
Пример #4
0
        public async Task Given_empty_context_When_executing_query_Then_it_should_return_nothing()
        {
            var options = new DbContextOptionsBuilder <FunctionUsageContext>()
                          .UseInMemoryDatabase(nameof(FunctionUsageQueryTests) +
                                               nameof(Given_empty_context_When_executing_query_Then_it_should_return_nothing))
                          .Options;

            var context = new FunctionUsageContext(options);
            var query   = new FunctionsUsageQuery(context);

            var res = await query.Execute("");

            Assert.Empty(res);
        }