Esempio n. 1
0
        public DownloadTestBase()
        {
            options = new DbContextOptionsBuilder <WasteDataContext>()
                      .UseInMemoryDatabase(databaseName: "WasteDataDatabase" + counter++)
                      .Options;

            // Insert seed data into the database using one instance of the context
            using var context = new WasteDataContext(options);


            var device1 = new Device(Guid.Parse(Device1Guid), "OnePlus", 0, "10");
            var device2 = new Device(Guid.Parse(Device1Guid), "OnePlus", 0, "10");

            context.Devices.Add(device1);
            context.Devices.Add(device2);

            context.SaveChanges();

            for (int i = 0; i < 5; i++)
            {
                var totalBytesDownloaded = ((20 + i) * 1024 * 1024);
                var downloadTest         = new DownloadTest(totalBytesDownloaded, DateTime.Now.AddMinutes(i), DateTime.Now.AddMinutes(i - 2), false, "", "192.168.0.123", "BE");
                device1.AddDownloadTest(downloadTest);
            }

            for (int i = 0; i < 5; i++)
            {
                var totalBytesDownloaded = ((10 + i) * 1024 * 1024);
                var downloadTest         = new DownloadTest(totalBytesDownloaded, DateTime.Now.AddMinutes(i), DateTime.Now.AddMinutes(i - 2), false, "", "192.168.0.123", "BE");
                device1.AddDownloadTest(downloadTest);
            }

            context.SaveChanges();
        }
Esempio n. 2
0
        public async void GetRankingPositionByDeviceIdQuery_WhenCalledWithDevice2Guid_ReturnsRanking1()
        {
            using var context = new WasteDataContext(options);
            var mediator = new Mock <IMediator>();

            GetRankingPositionByDeviceIdQuery query = new GetRankingPositionByDeviceIdQuery {
                DeviceGuid = Guid.Parse(Device2Guid)
            };
            GetRankingPositionByDeviceIdQueryHandler handler = new GetRankingPositionByDeviceIdQueryHandler(context);

            var result = await handler.Handle(query, new System.Threading.CancellationToken());

            Assert.Equal(1, result.Position);
        }
        public void GetTop10DownloadTests_WhenCalled_ReturnsDevice1OnFirstPlace()
        {
            var expectedPositionDevice = Device1Guid;

            using var context = new WasteDataContext(options);
            var mediator             = new Mock <IMediator>();
            var sqlConnectionFactory = new Mock <ISqlConnectionFactory>();

            GetTop10DownloadTestsQuery        query   = new GetTop10DownloadTestsQuery();
            GetTop10DownloadTestsQueryHandler handler = new GetTop10DownloadTestsQueryHandler(sqlConnectionFactory.Object);

            //TODO how to test Dapper query

            //var result = handler.Handle(query, new System.Threading.CancellationToken()).Result;

            //var listOrderedByTotalBytesDownloadedDesc = result.OrderByDescending(p => p.TotalBytesDownloaded);

            //var deviceFirstPosition = listOrderedByTotalBytesDownloadedDesc.First();

            //Assert.Equal(expectedPositionDevice, deviceFirstPosition.DeviceGuid.ToString());
        }
Esempio n. 4
0
        public async void AddDownloadTestCommand_WhenCalled_AddsDeviceAndDownloadTest()
        {
            using var context = new WasteDataContext(options);
            var mediator    = new Mock <IMediator>();
            var httpService = new Mock <IHttpService>();

            var newDeviceGuid = Guid.NewGuid();
            var totalBytes    = 100 * 1024 * 1024;

            AddDownloadTestCommand command = new AddDownloadTestCommand
            {
                DownloadTest = new AddDownloadTestDto
                {
                    DeviceGuid           = newDeviceGuid,
                    DeviceName           = "TestName1",
                    ConnectionName       = "OurWifi",
                    StartDate            = DateTime.Now,
                    EndDate              = DateTime.Now.AddMinutes(-2),
                    IpAddress            = "94.111.111.59",
                    IsWifi               = false,
                    OsId                 = 0,
                    OsVersion            = "10",
                    TotalBytesDownloaded = totalBytes
                }
            };

            httpService.Setup(p => p.GetCountryByIpAddress("94.111.111.59")).ReturnsAsync("BE");

            AddDownloadTestCommandHandler addDownloadTestCommandHandler = new AddDownloadTestCommandHandler(context, httpService.Object);

            await addDownloadTestCommandHandler.Handle(command, new System.Threading.CancellationToken());

            var device = context.Devices.Include(p => p.DownloadTests).SingleOrDefault(p => p.DeviceGuid == newDeviceGuid);

            Assert.NotNull(device);

            Assert.Collection(device.DownloadTests, item => Assert.Equal(totalBytes, item.TotalBytesDownloaded));
        }