// Test not yet to scale re: total data points.
TEST_F(ProjectFixture,Profile_DeserializeAnalysis) {
  // ETH@20121108 - When this test is running reasonably well, replace the beginning of
  // UpdateAnalysis with this.
  ProjectDatabase db = getPopulatedDatabase(100,500,false,toPath("./UpdateAnalysis"));
  AnalysisRecordVector records = AnalysisRecord::getAnalysisRecords(db);
  ASSERT_EQ(1u,records.size());
  AnalysisRecord record = records[0];

  // time the process of deserializing an analysis
  ptime start = microsec_clock::local_time();
  Analysis analysis = record.analysis();
  time_duration deserializeTime = microsec_clock::local_time() - start;

  std::cout << "Time: " << to_simple_string(deserializeTime) << std::endl;
}
  // mimic code in AnalysisDriver to avoid adding AnalysisDriver as a dependency

  // time the process of retrieving response function data
  ptime start = microsec_clock::local_time();
  db.unloadUnusedCleanRecords();
  ASSERT_TRUE(db.startTransaction());
  DataPointRecordVector dataPointRecords = DataPointRecord::getDataPointRecords(db);
  EXPECT_EQ(500u,dataPointRecords.size());
  BOOST_FOREACH(project::DataPointRecord& dataPointRecord,dataPointRecords) {
    // in AnalysisDriver, removes DataPoint directories, but they don't exist here
    db.removeRecord(dataPointRecord);
  }
  db.save();
  ASSERT_TRUE(db.commitTransaction());
  AnalysisRecordVector analysisRecords = AnalysisRecord::getAnalysisRecords(db);
  ASSERT_EQ(1u,analysisRecords.size());
  Analysis analysis = analysisRecords[0].analysis();
  analysis.clearAllResults();
  db.unloadUnusedCleanRecords();
  ASSERT_TRUE(db.startTransaction());
  AnalysisRecord analysisRecord(analysis,db);
  db.save();
  ASSERT_TRUE(db.commitTransaction());
  time_duration clearTime = microsec_clock::local_time() - start;

  std::cout << "Time: " << to_simple_string(clearTime) << std::endl;
}

TEST_F(ProjectFixture,Profile_OpenDatabaseWithUpdate) {
  openstudio::path dbFilename = toPath("MS-BESX-2_Heating_DDACE-Random_1000.osp");
TEST_F(ProjectFixture,DataPoint_DataPointRecord_Synchronization) {

  // create analysis with one data point
  ProjectDatabase database = getCleanDatabase("DataPoint_DataPointRecord_Synchronization");

  analysis::Analysis analysis(
      "Test Analysis",
      analysis::Problem("Test Problem",analysis::VariableVector(),runmanager::Workflow()),
      analysis::DesignOfExperiments(analysis::DesignOfExperimentsOptions(
          analysis::DesignOfExperimentsType::FullFactorial)),
      FileReference(toPath("in.osm")));

  std::vector<QVariant> variableValues;
  analysis::DataPoint original = analysis.problem().createDataPoint(variableValues).get();
  analysis.addDataPoint(original);

  // save analysis to database
  {
    bool didStartTransaction = database.startTransaction();
    AnalysisRecord analysisRecord(analysis,database);

    DataPointRecordVector dataPointRecords = DataPointRecord::getDataPointRecords(database);
    ASSERT_EQ(1u,dataPointRecords.size());
    DataPointRecord dataPointRecord = dataPointRecords[0];
    EXPECT_FALSE(dataPointRecord.isComplete());

    database.save();
    if (didStartTransaction) {
      database.commitTransaction();
    }
  }

  // reload analysis, change data point, save back to database
  // verify that change shows up in database
  OptionalAnalysis loadedAnalysis;
  {
    AnalysisRecordVector loadedAnalysisRecords = AnalysisRecord::getAnalysisRecords(database);
    ASSERT_EQ(1u,loadedAnalysisRecords.size());
    loadedAnalysis = loadedAnalysisRecords[0].analysis();

    analysis::DataPointVector loadedDataPoints = loadedAnalysis->dataPoints();
    ASSERT_EQ(1u,loadedDataPoints.size());
    loadedDataPoints[0].getImpl<analysis::detail::DataPoint_Impl>()->markComplete();
    EXPECT_TRUE(loadedDataPoints[0].isComplete());
  }
  ASSERT_TRUE(loadedAnalysis);

  {
    bool didStartTransaction = database.startTransaction();
    database.unloadUnusedCleanRecords();
    AnalysisRecord analysisRecord(*loadedAnalysis,database);
    database.save();
    if (didStartTransaction) {
      database.commitTransaction();
    }
    EXPECT_EQ(1u,AnalysisRecord::getAnalysisRecords(database).size());

    DataPointRecordVector dataPointRecords = DataPointRecord::getDataPointRecords(database);
    ASSERT_EQ(1u,dataPointRecords.size());
    DataPointRecord dataPointRecord = dataPointRecords[0];
    EXPECT_TRUE(dataPointRecord.isComplete());
  }

}