Пример #1
0
int main(int argc, char *argv[]) {
	auto db = get_database(argc, argv);
	test_init(8);
	try {
		db->execute("CREATE TABLE rusqltest (`value` INT(2) NOT NULL)");
		pass("could run CREATE TABLE");
	} catch(std::exception &e) {
		diag(e);
		fail("could run CREATE TABLE");
	}
	test_start_try(1);
	try {
		auto res = db->query("SHOW TABLES");
		test(contains(res, "rusqltest"), "rusqltest table created");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	test_start_try(1);
	try {
		// SELECT on empty database
		auto res = db->query("SELECT * FROM rusqltest");
		test(!res, "no result rows in empty table");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	test_start_try(3);
	try {
		db->execute("INSERT INTO rusqltest (`value`) VALUES (27)");
		auto res = db->query("SELECT * FROM rusqltest");
		test(res, "result row in empty table");
		test(res.get_uint64(0) == 27, "correct result row in empty table");
		test(res.get_uint64("value") == 27, "correct result row by name in empty table");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	try {
		db->execute("DROP TABLE rusqltest");
		pass("could run DROP TABLE");
	} catch(std::exception &e) {
		diag(e);
		fail("could run DROP TABLE");
	}
	test_start_try(1);
	try {
		auto res = db->query("SHOW TABLES");
		test(!contains(res, "rusqltest"), "rusqltest table dropped");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();
	return 0;
}
Пример #2
0
int main(int argc, char *argv[]) {
	auto db = get_database(argc, argv);
	test_init(4);

	const  int16_t negative_int = -16000;
	const uint16_t high_int     = 34000;

	db->execute("CREATE TABLE rusqltest (`value` SMALLINT(3) NOT NULL)");
	test_start_try(2);
	try {
		db->execute("INSERT INTO rusqltest VALUES (?)", negative_int);
		pass("store negative int");
		auto res = db->select_query("SELECT * FROM rusqltest");
		test(res.get<int16_t>(0) == negative_int, "retrieve negative int");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();
	db->execute("DROP TABLE rusqltest");

	db->execute("CREATE TABLE rusqltest (`value` SMALLINT(3) UNSIGNED NOT NULL)");
	test_start_try(2);
	try {
		db->execute("INSERT INTO rusqltest VALUES (?)", high_int);
		pass("store high int");
		auto res = db->select_query("SELECT * FROM rusqltest");
		diag(res.get_string(0));
		test(res.get<uint16_t>(0) == high_int, "retrieve high int");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();
	db->execute("DROP TABLE rusqltest");

	return 0;
}
Пример #3
0
int main(int argc, char *argv[]) {
	auto db = get_database(argc, argv);
	test_init(14);
	try {
		db->execute("CREATE TABLE rusqltest (`value` INT(2) NOT NULL)");
		pass("could run CREATE TABLE");
	} catch(std::exception &e) {
		diag(e);
		fail("could run CREATE TABLE");
	}
	test_start_try(1);
	try {
		auto res = db->select_query("SHOW TABLES");
		test(contains(res, "rusqltest"), "rusqltest table created");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	test_start_try(1);
	try {
		// SELECT on empty database
		auto res = db->select_query("SELECT * FROM rusqltest");
		test(!res, "no result rows in empty table");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	test_start_try(3);
	try {
		db->execute("INSERT INTO rusqltest (`value`) VALUES (27)");
		auto res = db->select_query("SELECT * FROM rusqltest");
		test(res, "result row in empty table");
		test(res.get_uint64(0) == 27, "correct result row in empty table");
		test(res.get_uint64("value") == 27, "correct result row by name in empty table");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	try {
		db->execute("DROP TABLE rusqltest");
		pass("could run DROP TABLE");
	} catch(std::exception &e) {
		diag(e);
		fail("could run DROP TABLE");
	}
	test_start_try(1);
	try {
		auto res = db->select_query("SHOW TABLES");
		test(!contains(res, "rusqltest"), "rusqltest table dropped");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	try {
		db->query("DROP TABLE nonexistant");
		fail("DROP nonexistant table throws");
	} catch(std::exception &e) {
		pass("DROP nonexistant table throws");
	}

	try {
		db->select_query("SELECT 1");
		pass("Next query succeeds");
	} catch(std::exception &e) {
		fail("Next query succeeds");
	}

	try {
		db->select_query("CREATE DATABASE nonexistant2");
		fail("CREATE statement in select_query() throws");
	} catch(std::exception &e) {
		diag(e);
		pass("CREATE statement in select_query() throws");
	}

	try {
		db->execute("DROP DATABASE nonexistant2");
	} catch(...) {}

	try {
		db->query("SELECT 1");
		fail("SELECT query in query() throws");
	} catch(std::exception &e) {
		diag(e);
		pass("SELECT query in query() throws");
	}

	test_start_try(2);
	try {
		auto res = db->select_query("SELECT 234");
		test(res.get_uint64(0) == 234, "next SELECT statement after query() gives correct result");
		res.next();
		test(!res, "next SELECT statement after query() gives only one result");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	return 0;
}
Пример #4
0
int main(int argc, char *argv[]) {
	auto db = get_database(argc, argv);
	test_init(23);

	// TODO: after named_bind() is added, throw if it is called without execute()
	// TODO: throw if get() was called without fetch()
	// TODO: throw if get() was called without bind_results() (or once it's added, named_bind())
	// TODO: throw if get() was called with a nonexistant field

	db->execute("CREATE TABLE rusqltest (`id` INT(10) NOT NULL, `value` VARCHAR(10) NOT NULL)");
	db->execute("INSERT INTO rusqltest VALUES (?, ?), (?, ?), (?, ?)", 5, "a", 6, "b", 7, "c");

	test_start_try(10);
	try {
		auto statement = db->execute("SELECT * FROM rusqltest");

		statement.bind_all_self();

		test(statement.fetch(), "first result");
		test(statement.get<int>("id") == 5, "first result int");
		test(statement.get<std::string>("value") == "a", "first result string");
		test(statement.fetch(), "second result");
		test(statement.get<int>("id") == 6, "second result int");
		test(statement.get<std::string>("value") == "b", "second result string");
		test(statement.fetch(), "third result");
		test(statement.get<std::string>("value") == "c", "third result string");
		test(statement.get<int>("id") == 7, "third result int");
		test(!statement.fetch(), "end of results");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	// test NULL columns
	db->execute("DROP TABLE rusqltest");
	db->execute("CREATE TABLE rusqltest (`id` INT(10) NOT NULL, `value` VARCHAR(10) NULL)");
	db->execute("INSERT INTO rusqltest VALUES (?, ?), (?, NULL), (?, NULL)", 5, "a", 6, 7);

	test_start_try(13);
	try {
		auto statement = db->execute("SELECT * FROM rusqltest");

		statement.bind_all_self();

		test(statement.fetch(), "first result");
		test(statement.get<int>("id") == 5, "first result int");
		test(statement.get<std::string>("value") == "a", "first result string");
		boost::optional<std::string> placeholder;
		placeholder = statement.get<decltype(placeholder)>("value");
		test(placeholder, "first result string is set");
		test(*placeholder == "a", "first result string is correct");

		test(statement.fetch(), "second result");
		test(statement.get<int>("id") == 6, "second result int");
		placeholder = statement.get<decltype(placeholder)>("value");
		test(!placeholder, "second result string is not set");

		bool threw = false;
		try {
			statement.get<std::string>("value");
		} catch(std::exception &e) {
			threw = true;
		}
		test(threw, "get<string> doesn't return");

		test(statement.fetch(), "third result");
		placeholder = statement.get<decltype(placeholder)>("value");
		test(!placeholder, "third result string is not set");
		test(statement.get<int>("id") == 7, "third result int");
		test(!statement.fetch(), "end of results");
	} catch(std::exception &e) {
		diag(e);
	}
	test_finish_try();

	return 0;
}