Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 900759d

Browse files
committed
Add sql test for UNIQUE
1 parent 2cbdeff commit 900759d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

test/sql/insert_sql_test.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,5 +523,54 @@ TEST_F(InsertSQLTests, InsertIntoSelectColumn) {
523523
txn_manager.CommitTransaction(txn);
524524
}
525525

526+
TEST_F(InsertSQLTests, UniqueColumn) {
527+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
528+
auto txn = txn_manager.BeginTransaction();
529+
catalog::Catalog::GetInstance()->CreateDatabase(DEFAULT_DB_NAME, txn);
530+
txn_manager.CommitTransaction(txn);
531+
532+
std::string create_table("CREATE TABLE t (id INTEGER NOT NULL PRIMARY KEY,"
533+
"st VARCHAR(15) NOT NULL UNIQUE);");
534+
TestingSQLUtil::ExecuteSQLQuery(create_table);
535+
536+
std::string ins_query_3("INSERT INTO t VALUES (3, 'abc');");
537+
538+
ResultType result;
539+
std::vector<std::string> ref_result;
540+
std::string result_query = "select st from t;";
541+
542+
// Single row, should succeed
543+
std::string ins_query_1("INSERT INTO t VALUES (1, 'abc');");
544+
result = TestingSQLUtil::ExecuteSQLQuery(ins_query_1);
545+
EXPECT_EQ(result, ResultType::SUCCESS);
546+
ref_result.push_back("abc");
547+
TestingSQLUtil::ExecuteSQLQueryAndCheckResult(result_query,
548+
ref_result,
549+
false);
550+
551+
// Second row, distinct from first, should succeed
552+
std::string ins_query_2("INSERT INTO t VALUES (2, 'def');");
553+
result = TestingSQLUtil::ExecuteSQLQuery(ins_query_2);
554+
EXPECT_EQ(result, ResultType::SUCCESS);
555+
ref_result.push_back("def");
556+
TestingSQLUtil::ExecuteSQLQueryAndCheckResult(result_query,
557+
ref_result,
558+
false);
559+
560+
// Third row, non-unique value for string, should fail
561+
std::string ins_query_3("INSERT INTO t VALUES (3, 'abc');");
562+
result = TestingSQLUtil::ExecuteSQLQuery(ins_query_3);
563+
EXPECT_EQ(result, ResultType::ABORTED);
564+
// and the results returned should not include failed insert
565+
TestingSQLUtil::ExecuteSQLQueryAndCheckResult(result_query,
566+
ref_result,
567+
false);
568+
569+
// free the database just created
570+
txn = txn_manager.BeginTransaction();
571+
catalog::Catalog::GetInstance()->DropDatabaseWithName(DEFAULT_DB_NAME, txn);
572+
txn_manager.CommitTransaction(txn);
573+
}
574+
526575
} // namespace test
527576
} // namespace peloton

0 commit comments

Comments
 (0)