@@ -523,5 +523,54 @@ TEST_F(InsertSQLTests, InsertIntoSelectColumn) {
523
523
txn_manager.CommitTransaction (txn);
524
524
}
525
525
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
+
526
575
} // namespace test
527
576
} // namespace peloton
0 commit comments