public async Task FetchLatestProducers_WithProducerRegisteredTwiceIn2015_ReturnsOnlyLatestProducer()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                ModelHelper helper = new ModelHelper(database.Model);

                // Arrange
                Model.Scheme scheme = helper.CreateScheme();

                Model.MemberUpload memberUpload1 = helper.CreateMemberUpload(scheme);
                memberUpload1.ComplianceYear = 2015;
                memberUpload1.IsSubmitted = true;

                Model.ProducerSubmission producer1 = helper.CreateProducerAsCompany(memberUpload1, "AAAAA");
                producer1.UpdatedDate = new DateTime(2015, 1, 1);

                Model.MemberUpload memberUpload2 = helper.CreateMemberUpload(scheme);
                memberUpload2.ComplianceYear = 2015;
                memberUpload2.IsSubmitted = true;

                Model.ProducerSubmission producer2 = helper.CreateProducerAsCompany(memberUpload2, "AAAAA");
                producer2.UpdatedDate = new DateTime(2015, 1, 2);

                database.Model.SaveChanges();

                // Act
                FetchProducerSearchResultsForCacheDataAccess dataAccess = new FetchProducerSearchResultsForCacheDataAccess(database.WeeeContext);
                var results = await dataAccess.FetchLatestProducers();

                // Assert
                Assert.NotNull(results);
                Assert.Contains(results, p => p.RegistrationNumber == "AAAAA" && p.Name == producer2.Business.Company.Name);
                Assert.DoesNotContain(results, p => p.RegistrationNumber == "AAAAA" && p.Name == producer1.Business.Company.Name);
            }
        }
        public void FetchSumOfExistingCharges_WithProducerWithNoChargesInSpecifiedYear_ReturnsZero()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                // Arrange
                ModelHelper helper = new ModelHelper(database.Model);

                Scheme scheme = helper.CreateScheme();

                MemberUpload memberUpload = helper.CreateMemberUpload(scheme);
                memberUpload.ComplianceYear = 2015;
                memberUpload.IsSubmitted = true;

                ProducerSubmission producer = helper.CreateProducerAsCompany(memberUpload, "AAAAA");
                producer.ChargeThisUpdate = 1;

                database.Model.SaveChanges();

                ProducerChargeCalculatorDataAccess dataAccess = new ProducerChargeCalculatorDataAccess(database.WeeeContext);

                // Act
                decimal result = dataAccess.FetchSumOfExistingCharges(scheme.ApprovalNumber, "AAAAA", 2016);

                // Assert
                Assert.Equal(0, result);
            }
        }
        public void GetLatestProducerForComplianceYearAndScheme_ComplianceYearDoesNotMatch_ReturnsNull()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                ModelHelper helper = new ModelHelper(database.Model);

                // Arrange
                Scheme scheme = helper.CreateScheme();

                MemberUpload memberUpload = helper.CreateMemberUpload(scheme);
                memberUpload.ComplianceYear = 2016;
                memberUpload.IsSubmitted = true;

                Weee.Tests.Core.Model.ProducerSubmission producer = helper.CreateProducerAsCompany(memberUpload, "AAAAAAA");

                database.Model.SaveChanges();

                ProducerQuerySet querySet = ProducerQuerySet(database.WeeeContext);

                // Act
                var result = querySet.GetLatestProducerForComplianceYearAndScheme("AAAAAAA", "2017", scheme.OrganisationId);

                // Assert
                Assert.Null(result);
            }
        }
        public void FetchSumOfExistingCharges_WithSameProducerWithDifferentSchemesInSameYear_ReturnsChargesPerScheme()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                // Arrange
                ModelHelper helper = new ModelHelper(database.Model);

                Scheme scheme1 = helper.CreateScheme();
                scheme1.ApprovalNumber = "FirstScheme";

                MemberUpload memberUpload1 = helper.CreateMemberUpload(scheme1);
                memberUpload1.ComplianceYear = 2016;
                memberUpload1.IsSubmitted = true;

                ProducerSubmission producer1 = helper.CreateProducerAsCompany(memberUpload1, "AAAAA");
                producer1.ChargeThisUpdate = 1;
                
                Scheme scheme2 = helper.CreateScheme();
                scheme2.ApprovalNumber = "SecondScheme";

                MemberUpload memberUpload2 = helper.CreateMemberUpload(scheme2);
                memberUpload2.ComplianceYear = 2016;
                memberUpload2.IsSubmitted = true;

                ProducerSubmission producer2 = helper.CreateProducerAsCompany(memberUpload2, "AAAAA");
                producer2.ChargeThisUpdate = 2;

                database.Model.SaveChanges();

                ProducerChargeCalculatorDataAccess dataAccess = new ProducerChargeCalculatorDataAccess(database.WeeeContext);

                // Act
                decimal producer1Charge = dataAccess.FetchSumOfExistingCharges("FirstScheme", "AAAAA", 2016);
                decimal producer2Charge = dataAccess.FetchSumOfExistingCharges("SecondScheme", "AAAAA", 2016);

                // Assert
                Assert.Equal(1, producer1Charge);
                Assert.Equal(2, producer2Charge);
            }
        }
        public void FetchSumOfExistingCharges_WithProducerWithSeveralCharges_ReturnsSumOfChargeAmounts()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                // Arrange
                ModelHelper helper = new ModelHelper(database.Model);

                Scheme scheme = helper.CreateScheme();

                MemberUpload memberUpload1 = helper.CreateMemberUpload(scheme);
                memberUpload1.ComplianceYear = 2016;
                memberUpload1.IsSubmitted = true;

                ProducerSubmission producer1 = helper.CreateProducerAsCompany(memberUpload1, "AAAAA");
                producer1.ChargeThisUpdate = 1;

                MemberUpload memberUpload2 = helper.CreateMemberUpload(scheme);
                memberUpload2.ComplianceYear = 2016;
                memberUpload2.IsSubmitted = true;

                ProducerSubmission producer2 = helper.CreateProducerAsCompany(memberUpload2, "AAAAA");
                producer2.ChargeThisUpdate = 2;

                MemberUpload memberUpload3 = helper.CreateMemberUpload(scheme);
                memberUpload3.ComplianceYear = 2016;
                memberUpload3.IsSubmitted = true;

                ProducerSubmission producer3 = helper.CreateProducerAsCompany(memberUpload3, "AAAAA");
                producer3.ChargeThisUpdate = 3;

                database.Model.SaveChanges();

                ProducerChargeCalculatorDataAccess dataAccess = new ProducerChargeCalculatorDataAccess(database.WeeeContext);

                // Act
                decimal result = dataAccess.FetchSumOfExistingCharges(scheme.ApprovalNumber, "AAAAA", 2016);

                // Assert
                Assert.Equal(6, result);
            }
        }
        public void GetLatestCompanyProducers_ReturnsCurrentForComplianceYearCompaniesOnly()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                ModelHelper helper = new ModelHelper(database.Model);

                // Arrange
                Scheme scheme1 = helper.CreateScheme();

                MemberUpload memberUpload1 = helper.CreateMemberUpload(scheme1);
                memberUpload1.ComplianceYear = 2015;
                memberUpload1.IsSubmitted = true;

                ProducerSubmission companyProducer1 = helper.CreateProducerAsCompany(memberUpload1, "AA");

                ProducerSubmission companyProducer2 = helper.CreateProducerAsCompany(memberUpload1, "BB");

                ProducerSubmission companyProducer3 = helper.CreateProducerAsCompany(memberUpload1, "AA");

                database.Model.SaveChanges();

                // Act
                var result = ProducerQuerySet(database.WeeeContext).GetLatestCompanyProducers();

                // Assert
                Assert.DoesNotContain(result, p => p.Id == companyProducer1.Id);
                Assert.Contains(result, p => p.Id == companyProducer2.Id);
                Assert.Contains(result, p => p.Id == companyProducer3.Id);
            }
        }
        public void GetLatestCompanyProducers_ReturnsCompaniesOnly()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                ModelHelper helper = new ModelHelper(database.Model);

                // Arrange
                Scheme scheme1 = helper.CreateScheme();

                MemberUpload memberUpload1 = helper.CreateMemberUpload(scheme1);
                memberUpload1.ComplianceYear = 2015;
                memberUpload1.IsSubmitted = true;

                Weee.Tests.Core.Model.ProducerSubmission companyProducer1 = helper.CreateProducerAsCompany(memberUpload1, "AAAAAAA");

                Weee.Tests.Core.Model.ProducerSubmission partnershipProducer = helper.CreateProducerAsPartnership(memberUpload1, "PPP1");

                Weee.Tests.Core.Model.ProducerSubmission soleTraderProducer = helper.CreateProducerAsSoleTrader(memberUpload1, "SSS1");

                Weee.Tests.Core.Model.ProducerSubmission companyProducer2 = helper.CreateProducerAsCompany(memberUpload1, "AAAAAAB");

                database.Model.SaveChanges();

                // Act
                var result = ProducerQuerySet(database.WeeeContext).GetLatestCompanyProducers();

                // Assert
                Assert.Contains(result, p => p.Id == companyProducer1.Id);
                Assert.Contains(result, p => p.Id == companyProducer2.Id);
                Assert.DoesNotContain(result, p => p.Id == soleTraderProducer.Id);
                Assert.DoesNotContain(result, p => p.Id == partnershipProducer.Id);
            }
        }
        public void GetProducerForOtherSchemeAndObligationType_ForAnotherSchemeSameObligationType_ReturnsAnotherSchemeProducer()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                ModelHelper helper = new ModelHelper(database.Model);

                // Arrange
                Scheme scheme1 = helper.CreateScheme();
                
                Scheme scheme2 = helper.CreateScheme();

                MemberUpload memberUpload1 = helper.CreateMemberUpload(scheme2);
                memberUpload1.ComplianceYear = 2015;
                memberUpload1.IsSubmitted = true;

                Weee.Tests.Core.Model.ProducerSubmission producer1 = helper.CreateProducerAsCompany(memberUpload1, "AAAAAAA");
                producer1.ObligationType = "B2C";

                database.Model.SaveChanges();

                ProducerQuerySet querySet = ProducerQuerySet(database.WeeeContext);

                // Act
                var result = querySet.GetProducerForOtherSchemeAndObligationType("AAAAAAA", "2015", scheme1.OrganisationId, ObligationType.B2C);

                // Assert
                Assert.NotNull(result);
                Assert.Equal(producer1.Id, result.Id);
            }
        }
        public void GetAllRegistrationNumbers_ReturnsDistinctRegistrationNumbers()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                ModelHelper helper = new ModelHelper(database.Model);

                // Arrange
                Scheme scheme = helper.CreateScheme();

                MemberUpload memberUpload1 = helper.CreateMemberUpload(scheme);
                memberUpload1.ComplianceYear = 2015;
                memberUpload1.IsSubmitted = false;

                Weee.Tests.Core.Model.ProducerSubmission producer1 = helper.CreateProducerAsCompany(memberUpload1, "AAAAAAA");

                MemberUpload memberUpload2 = helper.CreateMemberUpload(scheme);
                memberUpload2.ComplianceYear = 2015;
                memberUpload2.IsSubmitted = true;

                Weee.Tests.Core.Model.ProducerSubmission producer2 = helper.CreateProducerAsCompany(memberUpload2, "AAAAAAA");

                database.Model.SaveChanges();

                ProducerQuerySet querySet = ProducerQuerySet(database.WeeeContext);

                // Act
                List<string> results = querySet.GetAllRegistrationNumbers();

                // Assert
                Assert.NotNull(results);
                Assert.Equal(1, results.Count(r => r == "AAAAAAA"));
            }
        }
        public void GetLatestProducerFromPreviousComplianceYears_TwoProducerEntriesIn2015_AndOneIn2014_ReturnsLatestProducerByUpdatedDateIn2015()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                ModelHelper helper = new ModelHelper(database.Model);

                // Arrange
                Scheme scheme = helper.CreateScheme();

                MemberUpload memberUpload1 = helper.CreateMemberUpload(scheme);
                memberUpload1.ComplianceYear = 2015;
                memberUpload1.IsSubmitted = true;

                Weee.Tests.Core.Model.ProducerSubmission producer1 = helper.CreateProducerAsCompany(memberUpload1, "AAAAAAA");
                producer1.UpdatedDate = new DateTime(2015, 1, 1);

                MemberUpload memberUpload2 = helper.CreateMemberUpload(scheme);
                memberUpload2.ComplianceYear = 2015;
                memberUpload2.IsSubmitted = true;

                Weee.Tests.Core.Model.ProducerSubmission producer2 = helper.CreateProducerAsCompany(memberUpload2, "AAAAAAA");
                producer2.UpdatedDate = new DateTime(2015, 1, 2);

                MemberUpload memberUpload3 = helper.CreateMemberUpload(scheme);
                memberUpload3.ComplianceYear = 2014;
                memberUpload3.IsSubmitted = true;

                Weee.Tests.Core.Model.ProducerSubmission producer3 = helper.CreateProducerAsCompany(memberUpload3, "AAAAAAA");
                producer3.UpdatedDate = new DateTime(2014, 1, 1);

                database.Model.SaveChanges();

                ProducerQuerySet querySet = ProducerQuerySet(database.WeeeContext);

                // Act
                var result = querySet.GetLatestProducerFromPreviousComplianceYears("AAAAAAA");

                // Assert
                Assert.NotNull(result);
                Assert.Equal(producer2.Id, result.Id);
            }
        }
        public async Task FetchLatestProducers_WithThreeProducers_ReturnsProducersOrderedByRegistrationNumber()
        {
            using (DatabaseWrapper database = new DatabaseWrapper())
            {
                ModelHelper helper = new ModelHelper(database.Model);

                // Arrange
                Model.Scheme scheme = helper.CreateScheme();

                Model.MemberUpload memberUpload1 = helper.CreateMemberUpload(scheme);
                memberUpload1.ComplianceYear = 2015;
                memberUpload1.IsSubmitted = true;

                Model.ProducerSubmission producer1 = helper.CreateProducerAsCompany(memberUpload1, "AAAAA");

                Model.MemberUpload memberUpload2 = helper.CreateMemberUpload(scheme);
                memberUpload2.ComplianceYear = 2015;
                memberUpload2.IsSubmitted = true;

                Model.ProducerSubmission producer2 = helper.CreateProducerAsCompany(memberUpload2, "CCCCC");

                Model.MemberUpload memberUpload3 = helper.CreateMemberUpload(scheme);
                memberUpload3.ComplianceYear = 2015;
                memberUpload3.IsSubmitted = true;

                Model.ProducerSubmission producer3 = helper.CreateProducerAsCompany(memberUpload3, "BBBBB");

                database.Model.SaveChanges();

                // Act
                FetchProducerSearchResultsForCacheDataAccess dataAccess = new FetchProducerSearchResultsForCacheDataAccess(database.WeeeContext);
                var results = await dataAccess.FetchLatestProducers();

                // Assert
                Assert.NotNull(results);

                var result1 = results.SingleOrDefault(r => r.RegistrationNumber == "AAAAA");
                var result2 = results.SingleOrDefault(r => r.RegistrationNumber == "BBBBB");
                var result3 = results.SingleOrDefault(r => r.RegistrationNumber == "CCCCC");

                Assert.NotNull(result1);
                Assert.NotNull(result2);
                Assert.NotNull(result3);

                int result1index = results.IndexOf(result1);
                int result2index = results.IndexOf(result2);
                int result3index = results.IndexOf(result3);

                Assert.True(result1index < result2index);
                Assert.True(result2index < result3index);
            }
        }