Example #1
0
/**
 * @brief Tests import of ImportNewData.
 */
void TestCreateAsset::testImport() {
	QDate startDate = QDate::fromString("2014-01-01", "yyyy-MM-dd");
	QDate endDate = QDate::fromString("2014-02-01", "yyyy-MM-dd");
	Asset b = Asset("Gogolea", "../Examples/Gogolea_test.csv", "Yahoo", startDate, endDate);
	CreateAsset algo = CreateAsset();
	algo.import(b, "../../Examples/table.csv");
	SessionSaver::getInstance()->saveAsset(b);

	QString data;
	QFile importedCSV(b.getAbsolutePathToFile());
	QStringList dataRows;
	QStringList dataRow;

	if (importedCSV.open(QFile::ReadOnly)) {
		data = importedCSV.readAll();
		dataRows = data.split("\n");
		importedCSV.close();
	}
	QCOMPARE(dataRows.size(), 23);
	// Checks first date:
	dataRow = dataRows.at(1).split(",");
	QVERIFY(endDate >= QDate::fromString(dataRow[0], "yyyy-MM-dd"));
	// Checks last date:
	dataRow = dataRows.at(dataRows.size()-2).split(",");
	QVERIFY(startDate <= QDate::fromString(dataRow[0], "yyyy-MM-dd"));

	Asset *a = SessionBuilder::getInstance()->buildAsset("Gogolea");
	QVERIFY(a->getFile() == "../Examples/Gogolea_test.csv");
	QVERIFY(a->getStartDate() == startDate);
	QVERIFY(a->getEndDate() == endDate);
	QVERIFY(a->getName() == "Gogolea");
	QVERIFY(a->getOrigin() == "Yahoo");
	QVERIFY(AssetsFactory::getInstance()->retrieveAsset("Gogolea") != NULL);

	//Verify the day of week
	int size = dataRows.size();
	for (int x =1; x < size; x++) {
		dataRow = dataRows.at(x).split(",");
		QVERIFY((QDate::fromString(dataRow[0], "yyyy-MM-dd").dayOfWeek() <= 5));
	}
	// Deletes the database file:
	QFile databaseFile(SessionSaver::getInstance()->getDatabaseFile());
	databaseFile.remove();
}
Example #2
0
/**
* @brief Creates a new file with selected data
* @param asset The asset created
* @param file The file where are located the values.
* @throw ImportException The data is not valid
*/
void ImportNewData::import(const Asset &asset, const QString& file) const {
	QString data;
	QFile importedCSV(file);
	QStringList rowOfData;
	QStringList rowData;
	data.clear();
	rowOfData.clear();
	rowData.clear();
	QRegExp date_regex("^(20|19)[0-9]{2}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$");
	QRegExp value_regex("^([0-9]+)([.])([0-9][0-9])$");
	QDate previousDate = QDate::fromString("2999-01-01", "yyyy-MM-dd");
	int data_index;
	if (asset.getOrigin() == "ProjectVaR") {
		data_index = 1;
	} else {
		data_index = 6;
	}

	if (importedCSV.open(QFile::ReadOnly)) {
		data = importedCSV.readAll();
		rowOfData = data.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
		importedCSV.close();
	}

	//FILE CREATION OF IMPORTED DATA
	// Do unique names
	QFile fileCreated(asset.getFile());
	// The file is open in write-only mode and we check the opening
	if (!fileCreated.open(QIODevice::WriteOnly | QIODevice::Text)) {
		return;
	}
	QTextStream flux(&fileCreated);
	flux.setCodec("UTF-8");
	QDate endDate = asset.getEndDate();
	QDate startDate = asset.getStartDate();
	rowData = rowOfData.at(0).split(",");
	if (!(rowData.count() < data_index)) {
		if (!((QString) rowData[0]).isEmpty() && !((QString)rowData[data_index]).isEmpty()) {
			flux << rowData[0] << "," << rowData[data_index] << "\n";
			// x = 1 to avoid the first line with labels
			for (int x =1; x < rowOfData.size()-1; x++) {
				rowData = rowOfData.at(x).split(",");
				//Check dates and values are correct
				if(date_regex.exactMatch(rowData[0]) && value_regex.exactMatch(rowData[data_index])) {
					QDate currentDate = QDate::fromString(rowData[0], "yyyy-MM-dd");
					//checks the order of dates
					if(previousDate > currentDate) {
						previousDate = currentDate;
						//checks if we are on still in the range of dates
						if ((endDate >= currentDate)) {
							if(startDate > currentDate) {
								break;
							}
							flux << rowData[0] << "," << rowData[data_index] << "\n";
						}
					} else {
						throw CreateAssetException("Dates are not sorted");
						return;
					}

				} else {
					throw CreateAssetException("The data is invalid");
					return;
				}
			}
		} else {
			throw CreateAssetException("Header is missing");
			return;
		}
	} else {
		throw CreateAssetException("Wrong file type");
		return;
	}
	fileCreated.close();
}