Wednesday, January 11, 2012

a utility I wrote and its test

the utility:

using System;

using System.Collections.Generic;

using System.Linq;

using OurApp.Core.Entities;

namespace OurApp.Web.UI.Util

{

   public class ProgramPlanShuffleListFiltrationHelper

   {

      public string SerializedProgramPlansForPrograms { get; private set; }

      public string SerializedProgramPlansForSampleTypes { get; private set; }

      private Dictionary<Program, List<ProgramPlan>> _programPlansForPrograms;

      private Dictionary<SampleType, List<ProgramPlan>>

            _programPlansForSampleTypes;

   

      public ProgramPlanShuffleListFiltrationHelper(List<Program> programs,

            List<ProgramPlan> programPlans, List<SampleType> sampleTypes)

      {

         _programPlansForPrograms = programs.ToDictionary(program => program, program

               => new List<ProgramPlan>());

         _programPlansForSampleTypes = sampleTypes.ToDictionary(sampleType =>

               sampleType, sampleType => new List<ProgramPlan>());

         PopulateProgramPlansForPrograms(programPlans);

         PopulateProgramPlansForSampleTypes(programPlans, sampleTypes);

         MakeDataTransferObjectForPrograms(programs);

         MakeDataTransferObjectForSampleTypes(sampleTypes);

      }

   

      private void PopulateProgramPlansForPrograms(IEnumerable<ProgramPlan>

            programPlans)

      {

         foreach (ProgramPlan programPlan in programPlans)

         {

            List<ProgramPlan> dummyList =

                  _programPlansForPrograms[programPlan.Program];

            dummyList.Add(programPlan);

            _programPlansForPrograms[programPlan.Program] = dummyList;

         }

      }

   

      private void PopulateProgramPlansForSampleTypes(IEnumerable<ProgramPlan>

            programPlans, ICollection<SampleType> sampleTypes)

      {

         foreach (ProgramPlan programPlan in programPlans)

         {

            foreach (SampleType sampleType in programPlan.SampleTypes)

            {

               if (sampleTypes.Count > 0 && sampleTypes.Contains(sampleType))

               {

                  List<ProgramPlan> dummyList

                        = _programPlansForSampleTypes[sampleType];

                  dummyList.Add(programPlan);

                  _programPlansForSampleTypes[sampleType] = dummyList;

               }

            }

         }

      }

   

      private void MakeDataTransferObjectForPrograms(IEnumerable<Program> programs)

      {

         Dictionary<Guid, Dictionary<Guid, string>> outerDictionary = new Dictionary<Guid,

               Dictionary<Guid, string>>();

         foreach (Program program in programs)

         {

            Dictionary<Guid, string> innerDictionary = new Dictionary<Guid, string>();

            foreach (ProgramPlan programPlan in _programPlansForPrograms[program])

            {

               string name = program.Name + "." +

                     programPlan.NameStaticAttribute.

                     GetSelectedValueWithDefault(programPlan.Id).Value;

               innerDictionary.Add(programPlan.Id, name);

            }

            outerDictionary.Add(program.Id, innerDictionary);

         }

         SerializedProgramPlansForPrograms =

               Newtonsoft.Json.JsonConvert.SerializeObject(outerDictionary);

      }

   

      private void MakeDataTransferObjectForSampleTypes(IEnumerable<SampleType>

            sampleTypes)

      {

         Dictionary<Guid, List<Guid>> dictionary = new Dictionary<Guid, List<Guid>>();

         foreach (SampleType sampleType in

               sampleTypes)

         {

            List<Guid> list = new List<Guid>();

            foreach (ProgramPlan programPlan in

                  _programPlansForSampleTypes[sampleType])

            {

               list.Add(programPlan.Id);

            }

            dictionary.Add(sampleType.Id, list);

         }

         SerializedProgramPlansForSampleTypes =

               Newtonsoft.Json.JsonConvert.SerializeObject(dictionary);

      }

   }

}

 
 

the test:

using System.Collections.Generic;

using OurApp.Core.Entities;

using OurApp.Data.Tests.Support;

using OurApp.Web.UI.Util;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace OurApp.Data.Tests.Utils

{

   [TestClass]

   public class ProgramPlanShuffleListFiltrationHelperTest : DataIntegrationTestBase

   {

      [TestMethod]

      public void test_facets_of_ProgramPlanShuffleListFiltrationHelper()

      {

         
//arrange

         Category silicon = new Category { Name = "Silicon", IdPrefix = "S" };

         silicon.Save();

         SampleType cpu = new SampleType { Name = "CPU", IdPrefix = "CPU", Category

               = silicon };

         cpu.Save();

         SampleType gpu = new SampleType { Name = "GPU", IdPrefix = "GPU", Category

               = silicon };

         gpu.Save();

         SampleType chipset = new SampleType { Name = "Chipset", IdPrefix = "Chipset",

               Category = silicon };

         chipset.Save();

   

         ProgramPlan alpha = new ProgramPlan { CanOrder = true, SampleTypes = new

               List<SampleType> { cpu, gpu, chipset } };

         alpha.Save();

         alpha.NameStaticAttribute.CreateAndAddAvailableValue("Alpha")

               .SelectValueForEntity(alpha.Id);

         alpha.Save();

   

         ProgramPlan beta = new ProgramPlan { CanOrder = true, SampleTypes = new

               List<SampleType> { cpu } };

         beta.Save();

         beta.NameStaticAttribute.CreateAndAddAvailableValue("Beta")

               .SelectValueForEntity(beta.Id);

         beta.Save();

   

         ProgramPlan gamma = new ProgramPlan { CanOrder = true, SampleTypes = new

               List<SampleType> { cpu, gpu } };

         gamma.Save();

         gamma.NameStaticAttribute.CreateAndAddAvailableValue("Gamma")

               .SelectValueForEntity(gamma.Id);

         gamma.Save();

   

         ProgramPlan delta = new ProgramPlan { CanOrder = true, SampleTypes = new

               List<SampleType> { gpu } };

         delta.Save();

         delta.NameStaticAttribute.CreateAndAddAvailableValue("Delta")

               .SelectValueForEntity(delta.Id);

         delta.Save();

   

         ProgramPlan epsilon = new ProgramPlan { CanOrder = true, SampleTypes = new

               List<SampleType> { cpu, gpu } };

         epsilon.Save();

         epsilon.NameStaticAttribute.CreateAndAddAvailableValue("Epsilon")

               .SelectValueForEntity(epsilon.Id);

         epsilon.Save();

   

         ProgramPlan zeta = new ProgramPlan { CanOrder = true, SampleTypes = new

               List<SampleType> { cpu, gpu } };

         zeta.Save();

         zeta.NameStaticAttribute.CreateAndAddAvailableValue("Zeta")

               .SelectValueForEntity(zeta.Id);

         zeta.Save();

   

         ProgramPlan eta = new ProgramPlan { CanOrder = true, SampleTypes = new

               List<SampleType> { cpu, gpu, chipset } };

         eta.Save();

         eta.NameStaticAttribute.CreateAndAddAvailableValue("Eta")

               .SelectValueForEntity(eta.Id);

         eta.Save();

   

         Program moneyMaker = new Program { Name = "MoneyMaker", ProgramPlans =

               new List<ProgramPlan>() { alpha, beta, gamma, delta } };

         moneyMaker.Save();

         Program crowdPleaser = new Program { Name = "CrowdPleaser", ProgramPlans =

               new List<ProgramPlan>() { epsilon, zeta, eta } };

         crowdPleaser.Save();

   

         alpha.Program = moneyMaker;

         alpha.Save();

         beta.Program = moneyMaker;

         beta.Save();

         gamma.Program = moneyMaker;

         gamma.Save();

         delta.Program = moneyMaker;

         delta.Save();

         epsilon.Program = crowdPleaser;

         epsilon.Save();

         zeta.Program = crowdPleaser;

         zeta.Save();

         eta.Program = crowdPleaser;

         eta.Save();

   

         
//act

         ProgramPlanShuffleListFiltrationHelper programPlanShuffleListFiltrationHelper =

               new ProgramPlanShuffleListFiltrationHelper(new List<Program> { moneyMaker,

               crowdPleaser }, new List<ProgramPlan> { alpha, beta, gamma, delta, epsilon,

               zeta, eta }, new List<SampleType> { cpu, gpu, chipset });

   

         
//assert

         Assert.AreEqual(programPlanShuffleListFiltrationHelper.

               SerializedProgramPlansForPrograms, "{\"" + moneyMaker.Id + "\":{\"" +

               alpha.Id + "\":\"MoneyMaker.Alpha\",\"" + beta.Id + "\":\"MoneyMaker.Beta\",\""

               + gamma.Id + "\":\"MoneyMaker.Gamma\",\"" + delta.Id +

               "\":\"MoneyMaker.Delta\"},\"" + crowdPleaser.Id + "\":{\"" + epsilon.Id +

               "\":\"CrowdPleaser.Epsilon\",\"" + zeta.Id + "\":\"CrowdPleaser.Zeta\",\""

               + eta.Id + "\":\"CrowdPleaser.Eta\"}}");

         Assert.AreEqual(programPlanShuffleListFiltrationHelper.

               SerializedProgramPlansForSampleTypes, "{\"" + cpu.Id + "\":[\"" + alpha.Id +

               "\",\"" + beta.Id + "\",\"" + gamma.Id + "\",\"" + epsilon.Id + "\",\"" + zeta.Id +

               "\",\"" + eta.Id + "\"],\"" + gpu.Id + "\":[\"" + alpha.Id + "\",\"" + gamma.Id +

               "\",\"" + delta.Id + "\",\"" + epsilon.Id + "\",\"" + zeta.Id + "\",\"" + eta.Id + "\"],\""

               + chipset.Id + "\":[\"" + alpha.Id + "\",\"" + eta.Id + "\"]}");

      }

   }

}

No comments:

Post a Comment