public async void StatisticsHomePage_Packages_ValidateReportStructureAndAvailability()
        {
            JArray report = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "Downloads", 42 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "Downloads", 64 },
                }
            };

            var fakePackageReport = report.ToString();

            var fakeReportService = new Mock <IReportService>();

            fakeReportService.Setup(x => x.Load("recentpopularity.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageReport, DateTime.UtcNow)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.Packages()).Model;

            int sum = 0;

            foreach (var item in model.DownloadPackagesAll)
            {
                sum += item.Downloads;
            }

            Assert.Equal <int>(106, sum);
        }
        public async void StatisticsHomePage_ValidateReportStructureAndAvailability()
        {
            var fakePackageReport = "[{\"PackageId\":\"A\",\"Downloads\":1},{\"PackageId\":\"B\",\"Downloads\":2}]";
            var fakePackageVersionReport = "[{\"PackageId\":\"A\",\"PackageVersion\":\"1.0\",\"Downloads\":3},{\"PackageId\":\"A\",\"PackageVersion\":\"1.1\",\"Downloads\":4},{\"PackageId\":\"B\",\"PackageVersion\":\"1.0\",\"Downloads\":5}]";

            var fakeReportService = new Mock<IReportService>();

            fakeReportService.Setup(x => x.Load("RecentPopularity.json")).Returns(Task.FromResult(fakePackageReport));
            fakeReportService.Setup(x => x.Load("RecentPopularityDetail.json")).Returns(Task.FromResult(fakePackageVersionReport));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult) await controller.Index()).Model;

            int sum = 0;

            if (model.IsDownloadPackageAvailable)
            {
                foreach (var item in model.DownloadPackagesSummary)
                {
                    sum += item.Downloads;
                }
            }

            if (model.IsDownloadPackageDetailAvailable)
            {
                foreach (var item in model.DownloadPackageVersionsSummary)
                {
                    sum += item.Downloads;
                }
            }

            Assert.Equal<int>(15, sum);
        }
        public async void StatisticsHomePage_Per_Package_ValidateModel()
        {
            string PackageId = "A";

            var fakeReportService = new Mock <IReportService>();

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            TestUtility.SetupUrlHelperForUrlGeneration(controller);

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.PackageDownloadsByVersion(PackageId, new[] { GalleryConstants.StatisticsDimensions.Version })).Model;

            Assert.Equal(PackageId, model.PackageId);
        }
        public static StatisticsController CreateController(Mock <IAggregateStatsService> aggregateStatsService, Mock <HttpRequestBase> request = null)
        {
            request = request ?? new Mock <HttpRequestBase>();

            var context = new Mock <HttpContextBase>();

            context.SetupGet(s => s.Request).Returns(request.Object);

            var controller = new StatisticsController(aggregateStatsService.Object);

            controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);

            return(controller);
        }
        public async void StatisticsHomePage_ValidateFullReportStructureAndAvailability()
        {
            var fakePackageReport = "[{\"PackageId\":\"A\",\"Downloads\":1},{\"PackageId\":\"B\",\"Downloads\":2}]";
            var fakePackageVersionReport = "[{\"PackageId\":\"A\",\"PackageVersion\":\"1.0\",\"Downloads\":3},{\"PackageId\":\"A\",\"PackageVersion\":\"1.1\",\"Downloads\":4},{\"PackageId\":\"B\",\"PackageVersion\":\"1.0\",\"Downloads\":5}]";

            var fakeReportService = new Mock<IReportService>();

            fakeReportService.Setup(x => x.Load("RecentPopularity.json")).Returns(Task.FromResult(fakePackageReport));
            fakeReportService.Setup(x => x.Load("RecentPopularityDetail.json")).Returns(Task.FromResult(fakePackageVersionReport));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult) await controller.Index()).Model;

            if (model.IsDownloadPackageAvailable)
            {
                foreach (var item in model.DownloadPackagesSummary)
                {
                    if (item.PackageId == "A" && item.Downloads == 1)
                    {
                        continue;
                    }
                    else if (item.PackageId == "B" && item.Downloads == 2)
                    {
                        continue;
                    }
                    throw new Exception("unexpected data in Package report");
                }
            }

            if (model.IsDownloadPackageDetailAvailable)
            {
                foreach (var item in model.DownloadPackageVersionsSummary)
                {
                    if (item.PackageId == "A" && item.PackageVersion == "1.0" & item.Downloads == 3)
                    {
                        continue;
                    }
                    if (item.PackageId == "A" && item.PackageVersion == "1.1" & item.Downloads == 4)
                    {
                        continue;
                    }
                    if (item.PackageId == "B" && item.PackageVersion == "1.0" & item.Downloads == 5)
                    {
                        continue;
                    }
                    throw new Exception("unexpected data in Package report");
                }
            }
        }
        public async Task StatisticsHomePage_PackageVersions_ValidateReportStructureAndAvailability()
        {
            JArray report = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 22 },
                },
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.1" },
                    { "Downloads", 20 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 64 },
                }
            };

            var fakePackageVersionReport = report.ToString();

            var fakeReportService = new Mock <IReportService>();
            var updatedUtc1       = new DateTime(2002, 01, 01, 10, 20, 30);
            var updatedUtc2       = new DateTime(2001, 01, 01, 10, 20, 30);

            fakeReportService.Setup(x => x.Load("recentpopularitydetail.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageVersionReport, updatedUtc1)));
            fakeReportService.Setup(x => x.Load("recentcommunitypopularitydetail.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageVersionReport, updatedUtc2)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.PackageVersions()).Model;

            long sum = 0;

            foreach (var item in model.DownloadPackageVersionsAll)
            {
                sum += item.Downloads;
            }

            Assert.Equal(106, sum);
            Assert.True(model.LastUpdatedUtc.HasValue);
            Assert.Equal(updatedUtc1, model.LastUpdatedUtc.Value);
        }
        public async void Statistics_By_Client_Operation_ValidateModel()
        {
            string PackageId      = "A";
            string PackageVersion = "2.0";

            var fakeReportService = new Mock <IReportService>();

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            TestUtility.SetupUrlHelperForUrlGeneration(controller);

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.PackageDownloadsDetail(PackageId, PackageVersion, new string[] { "ClientName" })).Model;

            Assert.Equal(PackageId, model.PackageId);
            Assert.Equal(PackageVersion, model.PackageVersion);
        }
        public async Task StatisticsDownloadByVersionAction_ReturnsHttpOkIfPackageExists()
        {
            const string PackageId = "A";

            var fakeStatisticsService = new Mock <IStatisticsService>();

            fakeStatisticsService
            .Setup(service => service.GetPackageDownloadsByVersion(PackageId))
            .Returns(Task.FromResult(new StatisticsPackagesReport()));

            var controller = new StatisticsController(fakeStatisticsService.Object);

            TestUtility.SetupUrlHelperForUrlGeneration(controller);

            await controller.PackageDownloadsByVersionReport(PackageId, It.IsAny <string[]>());

            Assert.Equal(200, controller.Response.StatusCode);
        }
        public async Task StatisticsDownloadByVersionAction_ReturnsHttpNotFoundIfPackageDoesntExist()
        {
            const string PackageId = "A";

            var fakeStatisticsService = new Mock <IStatisticsService>();

            fakeStatisticsService
            .Setup(service => service.GetPackageDownloadsByVersion(PackageId))
            .Throws <StatisticsReportNotFoundException>();

            var controller = new StatisticsController(fakeStatisticsService.Object);

            TestUtility.SetupUrlHelperForUrlGeneration(controller);

            await controller.PackageDownloadsByVersionReport(PackageId, It.IsAny <string[]>());

            Assert.Equal(404, controller.Response.StatusCode);
        }
        public async void StatisticsHomePage_Packages_Negative_ValidateThrowOnInvalidStructure()
        {
            JArray report = new JArray
            {
                new JObject
                {
                    { "Lala", "A" },
                    { "Downloads", 303 }
                }
            };

            var fakePackageReport = "[{\"Lala\":\"A\",\"Downloads\":303}]";

            var fakeReportService = new Mock <IReportService>();

            fakeReportService.Setup(x => x.Load("RecentPopularity.json")).Returns(Task.FromResult(fakePackageReport));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            bool hasException = false;

            try
            {
                var model = (StatisticsPackagesViewModel)((ViewResult)await controller.Packages()).Model;
                hasException = false;
            }
            catch (Exception)
            {
                //  we don't care too much about the exact type of the exception
                hasException = true;
            }

            if (!hasException)
            {
                throw new Exception("this exception thrown because expected exception was not thrown");
            }
        }
        public async void StatisticsHomePage_Per_Package_ValidateReportStructureAndAvailability()
        {
            string PackageId = "A";

            var fakeReport = "[{\"PackageVersion\":\"1.0\",\"Downloads\":101},{\"PackageVersion\":\"2.1\",\"Downloads\":202}]";

            var fakeReportService = new Mock<IReportService>();

            fakeReportService.Setup(x => x.Load("RecentPopularity_" + PackageId + ".json")).Returns(Task.FromResult(fakeReport));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult) await controller.PackageDownloadsByVersion(PackageId)).Model;

            int sum = 0;

            foreach (var item in model.PackageDownloadsByVersion)
            {
                sum += item.Downloads;
            }

            Assert.Equal<int>(303, sum);
            Assert.Equal<int>(303, model.TotalPackageDownloads);
        }
        public async void StatisticsHomePage_Packages_ValidateReportStructureAndAvailability()
        {
            var fakePackageReport = "[{\"PackageId\":\"A\",\"Downloads\":42},{\"PackageId\":\"B\",\"Downloads\":64}]";

            var fakeReportService = new Mock<IReportService>();

            fakeReportService.Setup(x => x.Load("RecentPopularity.json")).Returns(Task.FromResult(fakePackageReport));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult) await controller.Packages()).Model;

            int sum = 0;

            foreach (var item in model.DownloadPackagesAll)
            {
                sum += item.Downloads;
            }

            Assert.Equal<int>(106, sum);
        }
        public async void StatisticsHomePage_ValidateFullReportStructureAndAvailability()
        {
            JArray report1 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "Downloads", 1 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "Downloads", 2 },
                }
            };

            JArray report2 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 3 },
                },
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.1" },
                    { "Downloads", 4 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 5 },
                }
            };

            JArray report3 = new JArray
            {
                new JObject
                {
                    { "ClientMajorVersion", 0 },
                    { "ClientMinorVersion", 0 },
                    { "Downloads", 1349 }
                },
                new JObject
                {
                    { "ClientMajorVersion", 1 },
                    { "ClientMinorVersion", 0 },
                    { "Downloads", 1349 }
                }
            };

            JArray report4 = new JArray
            {
                new JObject
                {
                    { "Year", 2012 },
                    { "MonthOfYear", 11 },
                    { "Downloads", 5383767 }
                },
                new JObject
                {
                    { "Year", 2012 },
                    { "MonthOfYear", 12 },
                    { "Downloads", 5383767 }
                }
            };

            var fakePackageReport = report1.ToString();
            var fakePackageVersionReport = report2.ToString();
            var fakeNuGetClientVersion = report3.ToString();
            var fakeLast6Months = report4.ToString();

            var fakeReportService = new Mock<IReportService>();

            fakeReportService.Setup(x => x.Load("RecentPopularity.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageReport, DateTime.UtcNow)));
            fakeReportService.Setup(x => x.Load("RecentPopularityDetail.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageVersionReport, DateTime.UtcNow)));
            fakeReportService.Setup(x => x.Load("NuGetClientVersion.json")).Returns(Task.FromResult(new StatisticsReport(fakeNuGetClientVersion, DateTime.UtcNow)));
            fakeReportService.Setup(x => x.Load("Last6Months.json")).Returns(Task.FromResult(new StatisticsReport(fakeLast6Months, DateTime.UtcNow)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult) await controller.Index()).Model;

            if (model.IsDownloadPackageAvailable)
            {
                foreach (var item in model.DownloadPackagesSummary)
                {
                    if (item.PackageId == "A" && item.Downloads == 1)
                    {
                        continue;
                    }
                    else if (item.PackageId == "B" && item.Downloads == 2)
                    {
                        continue;
                    }
                    throw new Exception("unexpected data in Package report");
                }
            }

            if (model.IsDownloadPackageDetailAvailable)
            {
                foreach (var item in model.DownloadPackageVersionsSummary)
                {
                    if (item.PackageId == "A" && item.PackageVersion == "1.0" & item.Downloads == 3)
                    {
                        continue;
                    }
                    if (item.PackageId == "A" && item.PackageVersion == "1.1" & item.Downloads == 4)
                    {
                        continue;
                    }
                    if (item.PackageId == "B" && item.PackageVersion == "1.0" & item.Downloads == 5)
                    {
                        continue;
                    }
                    throw new Exception("unexpected data in Package report");
                }
            }
        }
        public async void Statistics_By_Client_Operation_ValidateReportStructureAndAvailability()
        {
            string PackageId      = "A";
            string PackageVersion = "2.0";

            JObject report = new JObject
            {
                { "Downloads", 603 },
                { "Items", new JArray
                  {
                      new JObject
                      {
                          { "Version", "1.0" },
                          { "Downloads", 101 },
                          { "Items", new JArray
                            {
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "Install" },
                                    { "Downloads", 101 }
                                },
                            } }
                      },
                      new JObject
                      {
                          { "Version", "2.0" },
                          { "Downloads", 502 },
                          { "Items", new JArray
                            {
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "Install" },
                                    { "Downloads", 201 }
                                },
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "unknow" },
                                    { "Downloads", 301 }
                                }
                            } }
                      },
                  } }
            };

            var fakeReport = report.ToString();

            var fakeReportService = new Mock <IReportService>();

            string reportName = "recentpopularity/RecentPopularityDetail_" + PackageId + ".json";

            reportName = reportName.ToLowerInvariant();

            var updatedUtc = new DateTime(2001, 01, 01, 10, 20, 30);

            fakeReportService.Setup(x => x.Load(reportName)).Returns(Task.FromResult(new StatisticsReport(fakeReport, updatedUtc)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            TestUtility.SetupUrlHelperForUrlGeneration(controller, new Uri("http://nuget.org"));

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.PackageDownloadsDetail(PackageId, PackageVersion, new string[] { "ClientName" })).Model;

            int sum = 0;

            foreach (var row in model.Report.Table)
            {
                sum += int.Parse(row[row.GetLength(0) - 1].Data);
            }

            Assert.Equal(502, sum);
            Assert.Equal("502", model.Report.Total);
            Assert.True(model.LastUpdatedUtc.HasValue);
            Assert.Equal(updatedUtc, model.LastUpdatedUtc.Value);
        }
        public async void StatisticsHomePage_ValidateReportStructureAndAvailability()
        {
            JArray report1 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "Downloads", 1 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "Downloads", 2 },
                }
            };

            JArray report2 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 3 },
                },
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.1" },
                    { "Downloads", 4 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 5 },
                }
            };

            JArray report3 = new JArray
            {
                new JObject
                {
                    { "ClientMajorVersion", 0 },
                    { "ClientMinorVersion", 0 },
                    { "Downloads", 1349 }
                },
                new JObject
                {
                    { "ClientMajorVersion", 1 },
                    { "ClientMinorVersion", 0 },
                    { "Downloads", 1349 }
                }
            };

            JArray report4 = new JArray
            {
                new JObject
                {
                    { "Year", 2012 },
                    { "MonthOfYear", 11 },
                    { "Downloads", 5383767 }
                },
                new JObject
                {
                    { "Year", 2012 },
                    { "MonthOfYear", 12 },
                    { "Downloads", 5383767 }
                }
            };

            var fakePackageReport = report1.ToString();
            var fakePackageVersionReport = report2.ToString();
            var fakeNuGetClientVersion = report3.ToString();
            var fakeLast6Months = report4.ToString();

            var fakeReportService = new Mock<IReportService>();

            var updatedUtc = new DateTime(2001, 01, 01, 10, 20, 30);

            fakeReportService.Setup(x => x.Load("RecentPopularity.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageReport, DateTime.MinValue)));
            fakeReportService.Setup(x => x.Load("RecentPopularityDetail.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageVersionReport, null)));
            fakeReportService.Setup(x => x.Load("NuGetClientVersion.json")).Returns(Task.FromResult(new StatisticsReport(fakeNuGetClientVersion, DateTime.MinValue)));
            fakeReportService.Setup(x => x.Load("Last6Months.json")).Returns(Task.FromResult(new StatisticsReport(fakeLast6Months, updatedUtc)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult) await controller.Index()).Model;

            int sum = 0;

            if (model.IsDownloadPackageAvailable)
            {
                foreach (var item in model.DownloadPackagesSummary)
                {
                    sum += item.Downloads;
                }
            }

            if (model.IsDownloadPackageDetailAvailable)
            {
                foreach (var item in model.DownloadPackageVersionsSummary)
                {
                    sum += item.Downloads;
                }
            }

            Assert.Equal(15, sum);
            Assert.True(model.LastUpdatedUtc.HasValue);
            Assert.Equal(updatedUtc, model.LastUpdatedUtc.Value);
        }
        public async void StatisticsHomePage_PackageVersions_ValidateReportStructureAndAvailability()
        {
            JArray report = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 22 },
                },
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.1" },
                    { "Downloads", 20 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 64 },
                }
            };

            var fakePackageVersionReport = report.ToString();

            var fakeReportService = new Mock<IReportService>();
            var updatedUtc = new DateTime(2001, 01, 01, 10, 20, 30);

            fakeReportService.Setup(x => x.Load("RecentPopularityDetail.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageVersionReport, updatedUtc)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult) await controller.PackageVersions()).Model;

            int sum = 0;

            foreach (var item in model.DownloadPackageVersionsAll)
            {
                sum += item.Downloads;
            }

            Assert.Equal(106, sum);
            Assert.True(model.LastUpdatedUtc.HasValue);
            Assert.Equal(updatedUtc, model.LastUpdatedUtc.Value);
        }
        public static StatisticsController CreateController(Mock<IAggregateStatsService> aggregateStatsService, Mock<HttpRequestBase> request = null)
        {
            request = request ?? new Mock<HttpRequestBase>();

            var context = new Mock<HttpContextBase>();
            context.SetupGet(s => s.Request).Returns(request.Object);

            var controller = new StatisticsController(aggregateStatsService.Object);
            controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);

            return controller;
        }
Ejemplo n.º 18
0
        public async void StatisticsHomePage_Per_Package_ValidateReportStructureAndAvailability()
        {
            string PackageId = "A";

            JObject report = new JObject
            {
                { "Downloads", 603 },
                { "Items", new JArray
                  {
                      new JObject
                      {
                          { "Version", "1.0" },
                          { "Downloads", 101 },
                          { "Items", new JArray
                            {
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "Install" },
                                    { "Downloads", 101 }
                                },
                            } }
                      },
                      new JObject
                      {
                          { "Version", "2.0" },
                          { "Downloads", 502 },
                          { "Items", new JArray
                            {
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "Install" },
                                    { "Downloads", 201 }
                                },
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "unknow" },
                                    { "Downloads", 301 }
                                }
                            } }
                      },
                  } }
            };

            var fakeReport = report.ToString();

            var fakeReportService = new Mock <IReportService>();

            string reportName = "RecentPopularityDetail_" + PackageId + ".json";

            reportName = reportName.ToLowerInvariant();

            fakeReportService.Setup(x => x.Load(reportName)).Returns(Task.FromResult(fakeReport));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            TestUtility.SetupUrlHelperForUrlGeneration(controller, new Uri("http://nuget.org"));

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.PackageDownloadsByVersion(PackageId, new string[] { "Version" })).Model;

            int sum = 0;

            foreach (var row in model.Report.Table)
            {
                sum += int.Parse(row[row.GetLength(0) - 1].Data);
            }

            Assert.Equal <int>(603, sum);
            Assert.Equal <string>("603", model.Report.Total);
        }
Ejemplo n.º 19
0
        public async void StatisticsHomePage_ValidateReportStructureAndAvailability()
        {
            JArray report1 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "Downloads", 1 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "Downloads", 2 },
                }
            };

            JArray report2 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 3 },
                },
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.1" },
                    { "Downloads", 4 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 5 },
                }
            };

            JArray report3 = new JArray
            {
                new JObject
                {
                    { "ClientMajorVersion", 0 },
                    { "ClientMinorVersion", 0 },
                    { "Downloads", 1349 }
                },
                new JObject
                {
                    { "ClientMajorVersion", 1 },
                    { "ClientMinorVersion", 0 },
                    { "Downloads", 1349 }
                }
            };

            JArray report4 = new JArray
            {
                new JObject
                {
                    { "Year", 2012 },
                    { "MonthOfYear", 11 },
                    { "Downloads", 5383767 }
                },
                new JObject
                {
                    { "Year", 2012 },
                    { "MonthOfYear", 12 },
                    { "Downloads", 5383767 }
                }
            };

            var fakePackageReport        = report1.ToString();
            var fakePackageVersionReport = report2.ToString();
            var fakeNuGetClientVersion   = report3.ToString();
            var fakeLast6Months          = report4.ToString();

            var fakeReportService = new Mock <IReportService>();

            fakeReportService.Setup(x => x.Load("RecentPopularity.json")).Returns(Task.FromResult(fakePackageReport));
            fakeReportService.Setup(x => x.Load("RecentPopularityDetail.json")).Returns(Task.FromResult(fakePackageVersionReport));
            fakeReportService.Setup(x => x.Load("NuGetClientVersion.json")).Returns(Task.FromResult(fakeNuGetClientVersion));
            fakeReportService.Setup(x => x.Load("Last6Months.json")).Returns(Task.FromResult(fakeLast6Months));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.Index()).Model;

            int sum = 0;

            if (model.IsDownloadPackageAvailable)
            {
                foreach (var item in model.DownloadPackagesSummary)
                {
                    sum += item.Downloads;
                }
            }

            if (model.IsDownloadPackageDetailAvailable)
            {
                foreach (var item in model.DownloadPackageVersionsSummary)
                {
                    sum += item.Downloads;
                }
            }

            Assert.Equal <int>(15, sum);
        }
        public async void StatisticsHomePage_ValidateFullReportStructureAndAvailability()
        {
            JArray report1 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "Downloads", 1 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "Downloads", 2 },
                }
            };

            JArray report2 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 3 },
                },
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.1" },
                    { "Downloads", 4 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 5 },
                }
            };

            var fakePackageReport        = report1.ToString();
            var fakePackageVersionReport = report2.ToString();

            var fakeReportService = new Mock <IReportService>();

            fakeReportService.Setup(x => x.Load("RecentPopularity.json")).Returns(Task.FromResult(fakePackageReport));
            fakeReportService.Setup(x => x.Load("RecentPopularityDetail.json")).Returns(Task.FromResult(fakePackageVersionReport));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.Index()).Model;

            if (model.IsDownloadPackageAvailable)
            {
                foreach (var item in model.DownloadPackagesSummary)
                {
                    if (item.PackageId == "A" && item.Downloads == 1)
                    {
                        continue;
                    }
                    else if (item.PackageId == "B" && item.Downloads == 2)
                    {
                        continue;
                    }
                    throw new Exception("unexpected data in Package report");
                }
            }

            if (model.IsDownloadPackageDetailAvailable)
            {
                foreach (var item in model.DownloadPackageVersionsSummary)
                {
                    if (item.PackageId == "A" && item.PackageVersion == "1.0" & item.Downloads == 3)
                    {
                        continue;
                    }
                    if (item.PackageId == "A" && item.PackageVersion == "1.1" & item.Downloads == 4)
                    {
                        continue;
                    }
                    if (item.PackageId == "B" && item.PackageVersion == "1.0" & item.Downloads == 5)
                    {
                        continue;
                    }
                    throw new Exception("unexpected data in Package report");
                }
            }
        }
        public async void StatisticsHomePage_Packages_ValidateReportStructureAndAvailability()
        {
            JArray report = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "Downloads", 42 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "Downloads", 64 },
                }
            };

            var fakePackageReport = report.ToString();

            var fakeReportService = new Mock<IReportService>();

            fakeReportService.Setup(x => x.Load("RecentPopularity.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageReport, DateTime.UtcNow)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult) await controller.Packages()).Model;

            int sum = 0;

            foreach (var item in model.DownloadPackagesAll)
            {
                sum += item.Downloads;
            }

            Assert.Equal<int>(106, sum);
        }
        public async Task StatisticsHomePage_Per_Package_ValidateReportStructureAndAvailabilityInvalidGroupBy()
        {
            string PackageId = "A";

            JObject report = new JObject
            {
                { "Downloads", 603 },
                { "Items", new JArray
                  {
                      new JObject
                      {
                          { "Version", "1.0" },
                          { "Downloads", 101 },
                          { "Items", new JArray
                            {
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "Install" },
                                    { "Downloads", 101 }
                                },
                            } }
                      },
                      new JObject
                      {
                          { "Version", "2.0" },
                          { "Downloads", 502 },
                          { "Items", new JArray
                            {
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "Install" },
                                    { "Downloads", 201 }
                                },
                                new JObject
                                {
                                    { "ClientName", "NuGet" },
                                    { "ClientVersion", "2.1" },
                                    { "Operation", "unknown" },
                                    { "Downloads", 301 }
                                }
                            } }
                      },
                  } }
            };

            var fakeReport = report.ToString();

            var fakeReportService = new Mock <IReportService>();

            string reportName = "recentpopularity/RecentPopularityDetail_" + PackageId + ".json";

            reportName = reportName.ToLowerInvariant();

            var updatedUtc = new DateTime(2001, 01, 01, 10, 20, 30);

            fakeReportService.Setup(x => x.Load(reportName)).Returns(Task.FromResult(new StatisticsReport(fakeReport, updatedUtc)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            TestUtility.SetupUrlHelperForUrlGeneration(controller);

            var invalidDimension = "this_dimension_does_not_exist";

            var actualReport = (StatisticsPackagesReport)((JsonResult)await controller.PackageDownloadsByVersionReport(PackageId, new[] { GalleryConstants.StatisticsDimensions.Version, invalidDimension })).Data;

            int sum = 0;

            foreach (var row in actualReport.Table)
            {
                sum += int.Parse(row[row.GetLength(0) - 1].Data);
            }

            Assert.Equal(603, sum);
            Assert.Equal(603, actualReport.Total);
            Assert.True(actualReport.LastUpdatedUtc.HasValue);
            Assert.Equal(updatedUtc, actualReport.LastUpdatedUtc.Value);
            Assert.DoesNotContain(invalidDimension, actualReport.Columns);
        }
Ejemplo n.º 23
0
        public async void StatisticsHomePage_ValidateReportStructureAndAvailability()
        {
            JArray report1 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "Downloads", 1 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "Downloads", 2 },
                }
            };

            JArray report2 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 3 },
                },
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.1" },
                    { "Downloads", 4 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 5 },
                }
            };

            var fakePackageReport = report1.ToString();
            var fakePackageVersionReport = report2.ToString();

            var fakeReportService = new Mock<IReportService>();

            fakeReportService.Setup(x => x.Load("RecentPopularity.json")).Returns(Task.FromResult(fakePackageReport));
            fakeReportService.Setup(x => x.Load("RecentPopularityDetail.json")).Returns(Task.FromResult(fakePackageVersionReport));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult) await controller.Index()).Model;

            int sum = 0;

            if (model.IsDownloadPackageAvailable)
            {
                foreach (var item in model.DownloadPackagesSummary)
                {
                    sum += item.Downloads;
                }
            }

            if (model.IsDownloadPackageDetailAvailable)
            {
                foreach (var item in model.DownloadPackageVersionsSummary)
                {
                    sum += item.Downloads;
                }
            }

            Assert.Equal<int>(15, sum);
        }
        public async void StatisticsHomePage_Packages_Negative_ValidateThrowOnInvalidStructure()
        {
            var fakePackageReport = "[{\"Lala\":\"A\",\"Downloads\":303}]";

            var fakeReportService = new Mock<IReportService>();

            fakeReportService.Setup(x => x.Load("RecentPopularity.json")).Returns(Task.FromResult(fakePackageReport));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            bool hasException = false;

            try
            {
                var model = (StatisticsPackagesViewModel)((ViewResult) await controller.Packages()).Model;
                hasException = false;
            }
            catch (Exception)
            {
                //  we don't care too much about the exact type of the exception
                hasException = true;
            }

            if (!hasException)
            {
                throw new Exception("this exception thrown because expected exception was not thrown");
            }
        }
        public async void StatisticsHomePage_ValidateFullReportStructureAndAvailability()
        {
            JArray report1 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "Downloads", 1 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "Downloads", 2 },
                }
            };

            JArray report2 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 3 },
                },
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.1" },
                    { "Downloads", 4 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 5 },
                }
            };

            JArray report3 = new JArray
            {
                new JObject
                {
                    { "ClientMajorVersion", 0 },
                    { "ClientMinorVersion", 0 },
                    { "Downloads", 1349 }
                },
                new JObject
                {
                    { "ClientMajorVersion", 1 },
                    { "ClientMinorVersion", 0 },
                    { "Downloads", 1349 }
                }
            };

            JArray report4 = new JArray
            {
                new JObject
                {
                    { "Year", 2012 },
                    { "WeekOfYear", 11 },
                    { "Downloads", 5383767 }
                },
                new JObject
                {
                    { "Year", 2012 },
                    { "WeekOfYear", 12 },
                    { "Downloads", 5383767 }
                }
            };

            var fakePackageReport        = report1.ToString();
            var fakePackageVersionReport = report2.ToString();
            var fakeNuGetClientVersion   = report3.ToString();
            var fakeLast6Weeks           = report4.ToString();

            var fakeReportService = new Mock <IReportService>();

            fakeReportService.Setup(x => x.Load("recentpopularity.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageReport, DateTime.UtcNow)));
            fakeReportService.Setup(x => x.Load("recentpopularitydetail.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageVersionReport, DateTime.UtcNow)));
            fakeReportService.Setup(x => x.Load("nugetclientversion.json")).Returns(Task.FromResult(new StatisticsReport(fakeNuGetClientVersion, DateTime.UtcNow)));
            fakeReportService.Setup(x => x.Load("last6weeks.json")).Returns(Task.FromResult(new StatisticsReport(fakeLast6Weeks, DateTime.UtcNow)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.Index()).Model;

            if (model.IsDownloadPackageAvailable)
            {
                foreach (var item in model.DownloadPackagesSummary)
                {
                    if (item.PackageId == "A" && item.Downloads == 1)
                    {
                        continue;
                    }
                    else if (item.PackageId == "B" && item.Downloads == 2)
                    {
                        continue;
                    }
                    throw new Exception("unexpected data in Package report");
                }
            }

            if (model.IsDownloadPackageDetailAvailable)
            {
                foreach (var item in model.DownloadPackageVersionsSummary)
                {
                    if (item.PackageId == "A" && item.PackageVersion == "1.0" & item.Downloads == 3)
                    {
                        continue;
                    }
                    if (item.PackageId == "A" && item.PackageVersion == "1.1" & item.Downloads == 4)
                    {
                        continue;
                    }
                    if (item.PackageId == "B" && item.PackageVersion == "1.0" & item.Downloads == 5)
                    {
                        continue;
                    }
                    throw new Exception("unexpected data in Package report");
                }
            }
        }
        public async void StatisticsHomePage_ValidateReportStructureAndAvailability()
        {
            JArray report1 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "Downloads", 1 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "Downloads", 2 },
                }
            };

            JArray report2 = new JArray
            {
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 3 },
                },
                new JObject
                {
                    { "PackageId", "A" },
                    { "PackageVersion", "1.1" },
                    { "Downloads", 4 },
                },
                new JObject
                {
                    { "PackageId", "B" },
                    { "PackageVersion", "1.0" },
                    { "Downloads", 5 },
                }
            };

            JArray report3 = new JArray
            {
                new JObject
                {
                    { "ClientMajorVersion", 0 },
                    { "ClientMinorVersion", 0 },
                    { "Downloads", 1349 }
                },
                new JObject
                {
                    { "ClientMajorVersion", 1 },
                    { "ClientMinorVersion", 0 },
                    { "Downloads", 1349 }
                }
            };

            JArray report4 = new JArray
            {
                new JObject
                {
                    { "Year", 2012 },
                    { "WeekOfYear", 41 },
                    { "Downloads", 5383767 }
                },
                new JObject
                {
                    { "Year", 2012 },
                    { "WeekOfYear", 42 },
                    { "Downloads", 5383767 }
                }
            };

            var fakePackageReport        = report1.ToString();
            var fakePackageVersionReport = report2.ToString();
            var fakeNuGetClientVersion   = report3.ToString();
            var fakeLast6Weeks           = report4.ToString();

            var fakeReportService = new Mock <IReportService>();

            var updatedUtc = new DateTime(2001, 01, 01, 10, 20, 30);

            fakeReportService.Setup(x => x.Load("recentpopularity.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageReport, DateTime.MinValue)));
            fakeReportService.Setup(x => x.Load("recentpopularitydetail.json")).Returns(Task.FromResult(new StatisticsReport(fakePackageVersionReport, null)));
            fakeReportService.Setup(x => x.Load("nugetclientversion.json")).Returns(Task.FromResult(new StatisticsReport(fakeNuGetClientVersion, DateTime.MinValue)));
            fakeReportService.Setup(x => x.Load("last6weeks.json")).Returns(Task.FromResult(new StatisticsReport(fakeLast6Weeks, updatedUtc)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.Index()).Model;

            int sum = 0;

            if (model.IsDownloadPackageAvailable)
            {
                foreach (var item in model.DownloadPackagesSummary)
                {
                    sum += item.Downloads;
                }
            }

            if (model.IsDownloadPackageDetailAvailable)
            {
                foreach (var item in model.DownloadPackageVersionsSummary)
                {
                    sum += item.Downloads;
                }
            }

            Assert.Equal(15, sum);
            Assert.True(model.LastUpdatedUtc.HasValue);
            Assert.Equal(updatedUtc, model.LastUpdatedUtc.Value);
        }
        public async void Statistics_By_Client_Operation_ValidateReportStructureAndAvailability()
        {
            string PackageId = "A";
            string PackageVersion = "2.0";

            JObject report = new JObject
            {
                { "Downloads", 603 },
                { "Items", new JArray
                    {
                        new JObject
                        {
                            { "Version", "1.0" },
                            { "Downloads", 101 },
                            { "Items", new JArray 
                                {
                                    new JObject
                                    {
                                        { "ClientName", "NuGet" },
                                        { "ClientVersion", "2.1" },
                                        { "Operation", "Install" },
                                        { "Downloads", 101 }
                                    },
                                }
                            }
                        },
                        new JObject
                        {
                            { "Version", "2.0" },
                            { "Downloads", 502 },
                            { "Items", new JArray
                                {
                                    new JObject
                                    {
                                        { "ClientName", "NuGet" },
                                        { "ClientVersion", "2.1" },
                                        { "Operation", "Install" },
                                        { "Downloads", 201 }
                                    },
                                    new JObject
                                    {
                                        { "ClientName", "NuGet" },
                                        { "ClientVersion", "2.1" },
                                        { "Operation", "unknow" },
                                        { "Downloads", 301 }
                                    }
                                }
                            }
                        },
                    }
                }
            };

            var fakeReport = report.ToString();

            var fakeReportService = new Mock<IReportService>();

            string reportName = "RecentPopularityDetail_" + PackageId + ".json";
            reportName = reportName.ToLowerInvariant();

            var updatedUtc = new DateTime(2001, 01, 01, 10, 20, 30);
            fakeReportService.Setup(x => x.Load(reportName)).Returns(Task.FromResult(new StatisticsReport(fakeReport, updatedUtc)));

            var controller = new StatisticsController(new JsonStatisticsService(fakeReportService.Object));

            TestUtility.SetupUrlHelperForUrlGeneration(controller, new Uri("http://nuget.org"));

            var model = (StatisticsPackagesViewModel)((ViewResult)await controller.PackageDownloadsDetail(PackageId, PackageVersion, new string[] { "ClientName" })).Model;

            int sum = 0;

            foreach (var row in model.Report.Table)
            {
                sum += int.Parse(row[row.GetLength(0) - 1].Data);
            }

            Assert.Equal(502, sum);
            Assert.Equal("502", model.Report.Total);
            Assert.True(model.LastUpdatedUtc.HasValue);
            Assert.Equal(updatedUtc, model.LastUpdatedUtc.Value);
        }