Ejemplo n.º 1
0
        public void TraitImportExportTest()
        {
            //  WorkingDir wd = new WorkingDir(Utili.GetCurrentMethodAndClass());
            using (var db1 = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
            {
                var sim1 = new Simulator(db1.ConnectionString);
                using (var db2 = new DatabaseSetup(Utili.GetCurrentMethodAndClass()))
                {
                    var sim2 = new Simulator(db2.ConnectionString);
                    //check if the basic thing is identical

                    /*sim1.HouseholdTraits[0].Should().BeEquivalentTo(sim2.HouseholdTraits[0], o => o.Excluding(
                     *  x => IsInvalidMember(x)
                     *  ).IgnoringCyclicReferences());*/


                    //check import
                    List <HouseholdTrait.JsonDto> dto1 = new List <HouseholdTrait.JsonDto>();
                    foreach (var trait in sim1.HouseholdTraits.It)
                    {
                        dto1.Add(trait.GetJson());
                    }

                    sim2.HouseholdTraits.DeleteItem(sim2.HouseholdTraits[0]);
                    var result = HouseholdTrait.ImportObjectFromJson(sim2, dto1);
                    if (sim1.HouseholdTraits.It.Count != sim2.HouseholdTraits.It.Count)
                    {
                        throw new LPGException("count not equal");
                    }
                    sim2.HouseholdTraits.It.Sort();
                    sim1.HouseholdTraits[0].Should().BeEquivalentTo(result[0], o => o.IgnoringCyclicReferences()
                                                                    .Using <IRelevantGuidProvider>(x => x.Subject.RelevantGuid.Should().BeEquivalentTo(x.Expectation.RelevantGuid)).WhenTypeIs <IRelevantGuidProvider>()
                                                                    .Excluding(x => IsInvalidMember(x)
                                                                               ));
                    result.Should().HaveCount(1);
                    sim1.HouseholdTraits[0].Name.Should().BeEquivalentTo(sim2.HouseholdTraits[0].Name);
                    sim1.HouseholdTraits[0].Should().BeEquivalentTo(sim2.HouseholdTraits[0], o => o.IgnoringCyclicReferences()
                                                                    .Using <IRelevantGuidProvider>(x => x.Subject.RelevantGuid.Should().BeEquivalentTo(x.Expectation.RelevantGuid)).WhenTypeIs <IRelevantGuidProvider>()
                                                                    .Excluding(x => IsInvalidMember(x)
                                                                               ));
                }
            }
        }
        public bool Import([NotNull] JsonDatabaseImportOptions calcDirectoryOptions)
        {
            _calculationProfiler.StartPart(Utili.GetCurrentMethodAndClass());
            string jsonFileName = calcDirectoryOptions.Input;

            if (jsonFileName == null)
            {
                Logger.Error("No file was set.");
                return(false);
            }

            if (!File.Exists(jsonFileName))
            {
                Logger.Error("File does not exist");
                return(false);
            }
            Logger.Info("Loading...");
            var sim = new Simulator(_connectionString);

            Logger.Info("Loading finished.");
            switch (calcDirectoryOptions.Type)
            {
            case TypesToProcess.HouseholdTemplates: {
                string json = File.ReadAllText(jsonFileName);
                var    hhts = JsonConvert.DeserializeObject <List <HouseholdTemplate.JsonDto> >(json);
                Logger.Info("Started loading " + hhts.Count + " household templates");
                HouseholdTemplate.ImportObjectFromJson(sim, hhts);
                Logger.Info("Finished loading " + hhts.Count + " household templates");
                break;
            }

            case TypesToProcess.ModularHouseholds: {
                string json = File.ReadAllText(jsonFileName);
                var    hhts = JsonConvert.DeserializeObject <List <ModularHousehold.JsonModularHousehold> >(json);
                Logger.Info("Started loading " + hhts.Count + " households");
                ModularHousehold.ImportObjectFromJson(sim, hhts);
                Logger.Info("Finished loading " + hhts.Count + " households");
                break;
            }

            case TypesToProcess.None:
                throw new LPGException("You need to set a type that you want to process");

            case TypesToProcess.HouseholdTraits: {
                string json = File.ReadAllText(jsonFileName);
                var    hhts = JsonConvert.DeserializeObject <List <HouseholdTrait.JsonDto> >(json);
                Logger.Info("Started loading " + hhts.Count + " traits");
                HouseholdTrait.ImportObjectFromJson(sim, hhts);
                Logger.Info("Finished loading " + hhts.Count + " traits");
            }
            break;

            case TypesToProcess.HouseholdTraitsWithDeviceCategories:
                throw new LPGException("You need to set a type that you want to process");

            default:
                throw new ArgumentOutOfRangeException(nameof(JsonDatabaseImportOptions));
            }

            _calculationProfiler.StopPart(Utili.GetCurrentMethodAndClass());
            return(true);
        }