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

Commit f50bf18

Browse files
committed
Added SequenceCatalogTests. This is working.
1 parent 39f6832 commit f50bf18

File tree

1 file changed

+284
-0
lines changed

1 file changed

+284
-0
lines changed
Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// sequence_test.cpp
6+
//
7+
// Identification: test/sequence/sequence_test.cpp
8+
//
9+
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "catalog/catalog.h"
14+
#include "catalog/sequence_catalog.h"
15+
#include "storage/abstract_table.h"
16+
#include "common/harness.h"
17+
#include "common/exception.h"
18+
#include "executor/executors.h"
19+
#include "executor/executor_context.h"
20+
#include "parser/postgresparser.h"
21+
#include "planner/create_plan.h"
22+
#include "planner/insert_plan.h"
23+
#include "concurrency/transaction_manager_factory.h"
24+
25+
namespace peloton {
26+
namespace test {
27+
28+
class SequenceCatalogTests : public PelotonTest {
29+
protected:
30+
void CreateDatabaseHelper() {
31+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
32+
auto txn = txn_manager.BeginTransaction();
33+
catalog::Catalog::GetInstance()->Bootstrap();
34+
catalog::Catalog::GetInstance()->CreateDatabase(DEFAULT_DB_NAME, txn);
35+
txn_manager.CommitTransaction(txn);
36+
}
37+
38+
std::shared_ptr<catalog::SequenceCatalogObject> GetSequenceHelper(
39+
std::string sequence_name, concurrency::TransactionContext *txn) {
40+
// Check the effect of creation
41+
oid_t database_oid = catalog::Catalog::GetInstance()
42+
->GetDatabaseWithName(DEFAULT_DB_NAME, txn)
43+
->GetOid();
44+
std::shared_ptr<catalog::SequenceCatalogObject> new_sequence =
45+
catalog::Catalog::GetInstance()
46+
->GetSystemCatalogs(database_oid)
47+
->GetSequenceCatalog()
48+
->GetSequence(database_oid, sequence_name, txn);
49+
50+
return new_sequence;
51+
}
52+
53+
void CreateSequenceHelper(std::string query,
54+
concurrency::TransactionContext *txn) {
55+
auto parser = parser::PostgresParser::GetInstance();
56+
57+
std::unique_ptr<parser::SQLStatementList> stmt_list(
58+
parser.BuildParseTree(query).release());
59+
EXPECT_TRUE(stmt_list->is_valid);
60+
EXPECT_EQ(StatementType::CREATE, stmt_list->GetStatement(0)->GetType());
61+
auto create_sequence_stmt =
62+
static_cast<parser::CreateStatement *>(stmt_list->GetStatement(0));
63+
64+
create_sequence_stmt->TryBindDatabaseName(DEFAULT_DB_NAME);
65+
// Create plans
66+
planner::CreatePlan plan(create_sequence_stmt);
67+
68+
// plan type
69+
EXPECT_EQ(CreateType::SEQUENCE, plan.GetCreateType());
70+
71+
// Execute the create sequence
72+
std::unique_ptr<executor::ExecutorContext> context(
73+
new executor::ExecutorContext(txn, {}));
74+
executor::CreateExecutor createSequenceExecutor(&plan, context.get());
75+
createSequenceExecutor.Init();
76+
createSequenceExecutor.Execute();
77+
}
78+
79+
void DropSequenceHelper(std::string query,
80+
concurrency::TransactionContext *txn) {
81+
auto parser = parser::PostgresParser::GetInstance();
82+
83+
std::unique_ptr<parser::SQLStatementList> stmt_list(
84+
parser.BuildParseTree(query).release());
85+
EXPECT_TRUE(stmt_list->is_valid);
86+
EXPECT_EQ(StatementType::DROP, stmt_list->GetStatement(0)->GetType());
87+
auto drop_sequence_stmt =
88+
static_cast<parser::DropStatement *>(stmt_list->GetStatement(0));
89+
90+
drop_sequence_stmt->TryBindDatabaseName(DEFAULT_DB_NAME);
91+
// Drop plans
92+
planner::DropPlan plan(drop_sequence_stmt);
93+
94+
// Plan type
95+
EXPECT_EQ(DropType::SEQUENCE, plan.GetDropType());
96+
97+
// Execute the drop sequence
98+
std::unique_ptr<executor::ExecutorContext> context(
99+
new executor::ExecutorContext(txn));
100+
executor::DropExecutor dropSequenceExecutor(&plan, context.get());
101+
dropSequenceExecutor.Init();
102+
dropSequenceExecutor.Execute();
103+
}
104+
};
105+
106+
TEST_F(SequenceCatalogTests, BasicTest) {
107+
CreateDatabaseHelper();
108+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
109+
auto txn = txn_manager.BeginTransaction();
110+
111+
// Create statement
112+
std::string query =
113+
"CREATE SEQUENCE seq "
114+
"INCREMENT BY 2 "
115+
"MINVALUE 10 MAXVALUE 50 "
116+
"START 10 CYCLE;";
117+
std::string name = "seq";
118+
119+
CreateSequenceHelper(query, txn);
120+
std::shared_ptr<catalog::SequenceCatalogObject> new_sequence =
121+
GetSequenceHelper(name, txn);
122+
123+
EXPECT_EQ(name, new_sequence->seq_name);
124+
EXPECT_EQ(2, new_sequence->seq_increment);
125+
EXPECT_EQ(10, new_sequence->seq_min);
126+
EXPECT_EQ(50, new_sequence->seq_max);
127+
EXPECT_EQ(10, new_sequence->seq_start);
128+
EXPECT_EQ(true, new_sequence->seq_cycle);
129+
EXPECT_EQ(10, new_sequence->GetNextVal());
130+
EXPECT_EQ(10, new_sequence->GetCurrVal());
131+
132+
txn_manager.CommitTransaction(txn);
133+
}
134+
135+
TEST_F(SequenceCatalogTests, NoDuplicateTest) {
136+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
137+
auto txn = txn_manager.BeginTransaction();
138+
139+
// Create statement
140+
std::string query =
141+
"CREATE SEQUENCE seq "
142+
"INCREMENT BY 2 "
143+
"MINVALUE 10 MAXVALUE 50 "
144+
"START 10 CYCLE;";
145+
std::string name = "seq";
146+
147+
// Expect exception
148+
EXPECT_THROW(CreateSequenceHelper(query, txn), peloton::SequenceException);
149+
txn_manager.CommitTransaction(txn);
150+
}
151+
152+
TEST_F(SequenceCatalogTests, DropTest) {
153+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
154+
auto txn = txn_manager.BeginTransaction();
155+
156+
// Create statement
157+
std::string query =
158+
"CREATE SEQUENCE seq "
159+
"INCREMENT BY 2 "
160+
"MINVALUE 10 MAXVALUE 50 "
161+
"START 10 CYCLE;";
162+
163+
// Drop statement
164+
std::string dropQuery =
165+
"DROP SEQUENCE seq";
166+
167+
// Expect exception
168+
EXPECT_THROW(CreateSequenceHelper(query, txn), peloton::SequenceException);
169+
170+
// FIXME: The DROP SEQUENCE should fail here because the sequence shouldn't exist.
171+
DropSequenceHelper(dropQuery, txn);
172+
CreateSequenceHelper(query, txn);
173+
174+
txn_manager.CommitTransaction(txn);
175+
}
176+
177+
TEST_F(SequenceCatalogTests, NextValPosIncrementFunctionalityTest) {
178+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
179+
auto txn = txn_manager.BeginTransaction();
180+
181+
std::string query =
182+
"CREATE SEQUENCE seq1 "
183+
"INCREMENT BY 1 "
184+
"MINVALUE 10 MAXVALUE 50 "
185+
"START 10 CYCLE;";
186+
std::string name = "seq1";
187+
188+
CreateSequenceHelper(query, txn);
189+
std::shared_ptr<catalog::SequenceCatalogObject> new_sequence =
190+
GetSequenceHelper(name, txn);
191+
192+
int64_t nextVal = new_sequence->GetNextVal();
193+
EXPECT_EQ(10, nextVal);
194+
nextVal = new_sequence->GetNextVal();
195+
EXPECT_EQ(11, nextVal);
196+
197+
// test cycle
198+
new_sequence->SetCurrVal(50);
199+
nextVal = new_sequence->GetNextVal();
200+
nextVal = new_sequence->GetNextVal();
201+
EXPECT_EQ(10, nextVal);
202+
203+
// test no cycle
204+
new_sequence->SetCycle(false);
205+
new_sequence->SetCurrVal(50);
206+
207+
// Expect exception
208+
EXPECT_THROW(new_sequence->GetNextVal(), peloton::SequenceException);
209+
210+
txn_manager.CommitTransaction(txn);
211+
}
212+
213+
TEST_F(SequenceCatalogTests, NextValNegIncrementFunctionalityTest) {
214+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
215+
auto txn = txn_manager.BeginTransaction();
216+
217+
std::string query =
218+
"CREATE SEQUENCE seq2 "
219+
"INCREMENT BY -1 "
220+
"MINVALUE 10 MAXVALUE 50 "
221+
"START 10 CYCLE;";
222+
std::string name = "seq2";
223+
224+
CreateSequenceHelper(query, txn);
225+
std::shared_ptr<catalog::SequenceCatalogObject> new_sequence =
226+
GetSequenceHelper(name, txn);
227+
228+
// test cycle
229+
int64_t nextVal = new_sequence->GetNextVal();
230+
EXPECT_EQ(10, nextVal);
231+
nextVal = new_sequence->GetNextVal();
232+
EXPECT_EQ(50, nextVal);
233+
234+
new_sequence->SetCurrVal(49);
235+
nextVal = new_sequence->GetNextVal();
236+
nextVal = new_sequence->GetNextVal();
237+
EXPECT_EQ(48, nextVal);
238+
239+
// test no cycle
240+
new_sequence->SetCycle(false);
241+
new_sequence->SetCurrVal(10);
242+
243+
// Expect exception
244+
EXPECT_THROW(new_sequence->GetNextVal(), peloton::SequenceException);
245+
246+
txn_manager.CommitTransaction(txn);
247+
}
248+
249+
TEST_F(SequenceCatalogTests, InvalidArgumentTest) {
250+
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
251+
auto txn = txn_manager.BeginTransaction();
252+
253+
std::string query =
254+
"CREATE SEQUENCE seq3 "
255+
"INCREMENT BY -1 "
256+
"MINVALUE 50 MAXVALUE 10 "
257+
"START 10 CYCLE;";
258+
EXPECT_THROW(CreateSequenceHelper(query, txn), peloton::SequenceException);
259+
260+
query =
261+
"CREATE SEQUENCE seq3 "
262+
"INCREMENT BY 0 "
263+
"MINVALUE 10 MAXVALUE 50 "
264+
"START 10 CYCLE;";
265+
EXPECT_THROW(CreateSequenceHelper(query, txn), peloton::SequenceException);
266+
267+
query =
268+
"CREATE SEQUENCE seq3 "
269+
"INCREMENT BY 1 "
270+
"MINVALUE 10 MAXVALUE 50 "
271+
"START 8 CYCLE;";
272+
EXPECT_THROW(CreateSequenceHelper(query, txn), peloton::SequenceException);
273+
274+
query =
275+
"CREATE SEQUENCE seq3 "
276+
"INCREMENT BY -1 "
277+
"MINVALUE 10 MAXVALUE 50 "
278+
"START 60 CYCLE;";
279+
EXPECT_THROW(CreateSequenceHelper(query, txn), peloton::SequenceException);
280+
281+
txn_manager.CommitTransaction(txn);
282+
}
283+
}
284+
}

0 commit comments

Comments
 (0)