Skip to content

Commit d4a7e2d

Browse files
basic_example integration test
1 parent 992bd46 commit d4a7e2d

File tree

5 files changed

+81
-50
lines changed

5 files changed

+81
-50
lines changed

tests/integration/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
add_subdirectory(basic_example_it)
1+
set(ENDPOINT "localhost:2136")
2+
set(DATABASE "/Root/test")
3+
configure_file(config_ydb.h.in ${CMAKE_CURRENT_SOURCE_DIR}/config_ydb.h @ONLY)
4+
5+
add_subdirectory(basic_example_it)

tests/integration/basic_example_it/basic_example.cpp

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ static std::string DescribeTable(TTableClient client, const std::string& path, c
9595
return result;
9696
}));
9797

98-
result += std::format("> Describe table: {}", name);
98+
result += std::format("> Describe table: {}\n", name);
9999
for (auto& column : desc->GetColumns()) {
100-
result += std::format("Column, name: {}, type: {}", column.Name, FormatType(column.Type));
100+
result += std::format("Column, name: {}, type: {}\n", column.Name, FormatType(column.Type));
101101
}
102102
return result;
103103
}
@@ -434,20 +434,20 @@ static TStatus ExplicitTclTransaction(TSession session, const std::string& path,
434434

435435
///////////////////////////////////////////////////////////////////////////////
436436

437-
void SelectSimple(TTableClient client, const std::string& path) {
437+
std::string SelectSimple(TTableClient client, const std::string& path) {
438438
std::optional<TResultSet> resultSet;
439439
ThrowOnError(client.RetryOperationSync([path, &resultSet](TSession session) {
440440
return SelectSimpleTransaction(session, path, resultSet);
441441
}));
442442

443443
TResultSetParser parser(*resultSet);
444444
if (parser.TryNextRow()) {
445-
std::cout << "> SelectSimple:" << std::endl << "Series"
446-
<< ", Id: " << ToString(parser.ColumnParser("series_id").GetOptionalUint64())
447-
<< ", Title: " << ToString(parser.ColumnParser("title").GetOptionalUtf8())
448-
<< ", Release date: " << ToString(parser.ColumnParser("release_date").GetOptionalString())
449-
<< std::endl;
445+
return std::format("> SelectSimple:\nSeries, Id: {}, Title: {}, Release date: {}\n"
446+
, ToString(parser.ColumnParser("series_id").GetOptionalUint64())
447+
, ToString(parser.ColumnParser("title").GetOptionalUtf8())
448+
, ToString(parser.ColumnParser("release_date").GetOptionalString()));
450449
}
450+
return "";
451451
}
452452

453453
void UpsertSimple(TTableClient client, const std::string& path) {
@@ -456,23 +456,25 @@ void UpsertSimple(TTableClient client, const std::string& path) {
456456
}));
457457
}
458458

459-
void SelectWithParams(TTableClient client, const std::string& path) {
459+
std::string SelectWithParams(TTableClient client, const std::string& path) {
460460
std::optional<TResultSet> resultSet;
461+
std::string result;
461462
ThrowOnError(client.RetryOperationSync([path, &resultSet](TSession session) {
462463
return SelectWithParamsTransaction(session, path, 2, 3, resultSet);
463464
}));
464465

465466
TResultSetParser parser(*resultSet);
466467
if (parser.TryNextRow()) {
467-
std::cout << "> SelectWithParams:" << std::endl << "Season"
468-
<< ", Title: " << ToString(parser.ColumnParser("season_title").GetOptionalUtf8())
469-
<< ", Series title: " << ToString(parser.ColumnParser("series_title").GetOptionalUtf8())
470-
<< std::endl;
468+
return std::format("> SelectWithParams:\nSeason, Title: {}, Series title: {}\n"
469+
, ToString(parser.ColumnParser("season_title").GetOptionalUtf8())
470+
, ToString(parser.ColumnParser("series_title").GetOptionalUtf8()));
471471
}
472+
return "";
472473
}
473474

474-
void PreparedSelect(TTableClient client, const std::string& path, ui32 seriesId, ui32 seasonId, ui32 episodeId) {
475+
std::string PreparedSelect(TTableClient client, const std::string& path, ui32 seriesId, ui32 seasonId, ui32 episodeId) {
475476
std::optional<TResultSet> resultSet;
477+
std::string result;
476478
ThrowOnError(client.RetryOperationSync([path, seriesId, seasonId, episodeId, &resultSet](TSession session) {
477479
return PreparedSelectTransaction(session, path, seriesId, seasonId, episodeId, resultSet);
478480
}));
@@ -481,30 +483,32 @@ void PreparedSelect(TTableClient client, const std::string& path, ui32 seriesId,
481483
if (parser.TryNextRow()) {
482484
auto airDate = TInstant::Days(*parser.ColumnParser("air_date").GetOptionalUint64());
483485

484-
std::cout << "> PreparedSelect:" << std::endl << "Episode " << ToString(parser.ColumnParser("episode_id").GetOptionalUint64())
485-
<< ", Title: " << ToString(parser.ColumnParser("title").GetOptionalUtf8())
486-
<< ", Air date: " << airDate.FormatLocalTime("%a %b %d, %Y")
487-
<< std::endl;
486+
return std::format("> PreparedSelect:\nEpisode {}, Title: {}, Air date: {}\n"
487+
, ToString(parser.ColumnParser("episode_id").GetOptionalUint64())
488+
, ToString(parser.ColumnParser("title").GetOptionalUtf8())
489+
, airDate.FormatLocalTime("%a %b %d, %Y"));
488490
}
491+
return "";
489492
}
490493

491-
void MultiStep(TTableClient client, const std::string& path) {
494+
std::string MultiStep(TTableClient client, const std::string& path) {
492495
std::optional<TResultSet> resultSet;
496+
std::string result;
493497
ThrowOnError(client.RetryOperationSync([path, &resultSet](TSession session) {
494498
return MultiStepTransaction(session, path, 2, 5, resultSet);
495499
}));
496500

497501
TResultSetParser parser(*resultSet);
498-
std::cout << "> MultiStep:" << std::endl;
502+
result = "> MultiStep:\n";
499503
while (parser.TryNextRow()) {
500504
auto airDate = TInstant::Days(*parser.ColumnParser("air_date").GetOptionalUint64());
501-
502-
std::cout << "Episode " << ToString(parser.ColumnParser("episode_id").GetOptionalUint64())
503-
<< ", Season: " << ToString(parser.ColumnParser("season_id").GetOptionalUint64())
504-
<< ", Title: " << ToString(parser.ColumnParser("title").GetOptionalUtf8())
505-
<< ", Air date: " << airDate.FormatLocalTime("%a %b %d, %Y")
506-
<< std::endl;
505+
result += std::format("Episode {}, Season: {}, Title: {}, Air date: {}\n"
506+
, ToString(parser.ColumnParser("episode_id").GetOptionalUint64())
507+
, ToString(parser.ColumnParser("season_id").GetOptionalUint64())
508+
, ToString(parser.ColumnParser("title").GetOptionalUtf8())
509+
, airDate.FormatLocalTime("%a %b %d, %Y"));
507510
}
511+
return result;
508512
}
509513

510514
void ExplicitTcl(TTableClient client, const std::string& path) {
@@ -513,7 +517,8 @@ void ExplicitTcl(TTableClient client, const std::string& path) {
513517
}));
514518
}
515519

516-
void ScanQuerySelect(TTableClient client, const std::string& path) {
520+
std::string ScanQuerySelect(TTableClient client, const std::string& path) {
521+
std::string result;
517522
auto query = std::format(R"(
518523
--!syntax_v1
519524
PRAGMA TablePathPrefix("{}");
@@ -534,17 +539,18 @@ void ScanQuerySelect(TTableClient client, const std::string& path) {
534539
.Build();
535540

536541
// Executes scan query
537-
auto result = client.StreamExecuteScanQuery(query, parameters).GetValueSync();
542+
auto resultScanQuery = client.StreamExecuteScanQuery(query, parameters).GetValueSync();
538543

539-
if (!result.IsSuccess()) {
540-
std::cerr << "ScanQuery execution failure: " << result.GetIssues().ToString() << std::endl;
541-
return;
544+
if (!resultScanQuery.IsSuccess()) {
545+
std::cerr << "ScanQuery execution failure: " << resultScanQuery.GetIssues().ToString() << std::endl;
546+
return "";
542547
}
543548

544549
bool eos = false;
545-
std::cout << "> ScanQuerySelect:" << std::endl;
550+
result = "> ScanQuerySelect:\n";
551+
546552
while (!eos) {
547-
auto streamPart = result.ReadNext().ExtractValueSync();
553+
auto streamPart = resultScanQuery.ReadNext().ExtractValueSync();
548554

549555
if (!streamPart.IsSuccess()) {
550556
eos = true;
@@ -560,15 +566,15 @@ void ScanQuerySelect(TTableClient client, const std::string& path) {
560566

561567
TResultSetParser parser(rs);
562568
while (parser.TryNextRow()) {
563-
std::cout << "Season"
564-
<< ", SeriesId: " << ToString(parser.ColumnParser("series_id").GetOptionalUint64())
565-
<< ", SeasonId: " << ToString(parser.ColumnParser("season_id").GetOptionalUint64())
566-
<< ", Title: " << ToString(parser.ColumnParser("title").GetOptionalUtf8())
567-
<< ", Air date: " << ToString(parser.ColumnParser("first_aired").GetOptionalString())
568-
<< std::endl;
569+
result += std::format("Season, SeriesId: {}, SeasonId: {}, Title: {}, Air date: {}\n"
570+
, ToString(parser.ColumnParser("series_id").GetOptionalUint64())
571+
, ToString(parser.ColumnParser("season_id").GetOptionalUint64())
572+
, ToString(parser.ColumnParser("title").GetOptionalUtf8())
573+
, ToString(parser.ColumnParser("first_aired").GetOptionalString()));
569574
}
570575
}
571576
}
577+
return result;
572578
}
573579

574580
///////////////////////////////////////////////////////////////////////////////
@@ -585,21 +591,21 @@ std::unique_ptr<Response> Run(const TDriver& driver, const std::string& path) {
585591
return FillTableDataTransaction(session, path);
586592
}));
587593

588-
SelectSimple(client, path);
594+
response->result += SelectSimple(client, path);
589595
UpsertSimple(client, path);
590596

591-
SelectWithParams(client, path);
597+
response->result += SelectWithParams(client, path);
592598

593-
PreparedSelect(client, path, 2, 3, 7);
594-
PreparedSelect(client, path, 2, 3, 8);
599+
response->result += PreparedSelect(client, path, 2, 3, 7);
600+
response->result += PreparedSelect(client, path, 2, 3, 8);
595601

596-
MultiStep(client, path);
602+
response->result += MultiStep(client, path);
597603

598604
ExplicitTcl(client, path);
599605

600-
PreparedSelect(client, path, 2, 6, 1);
606+
response->result += PreparedSelect(client, path, 2, 6, 1);
601607

602-
ScanQuerySelect(client, path);
608+
response->result += ScanQuerySelect(client, path);
603609
}
604610
catch (const TYdbErrorException& e) {
605611
response->success = false;

tests/integration/basic_example_it/main.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "basic_example.h"
2+
#include "../config_ydb.h"
23

34
#include <src/library/getopt/last_getopt.h>
45

@@ -19,8 +20,6 @@ void StopHandler(int) {
1920
TEST(Integration, BasicExample) {
2021
TOpts opts = TOpts::Default();
2122

22-
std::string endpoint = "localhost:2136";
23-
std::string database = "/Root/test";
2423
std::string path;
2524
std::string certPath;
2625

@@ -39,12 +38,16 @@ TEST(Integration, BasicExample) {
3938
}
4039

4140
TDriver driver(driverConfig);
41+
std::unique_ptr<Response> response = ::Run(driver, path);
4242

43-
if (auto response = ::Run(driver, path); !response->success) {
43+
if (!response->success) {
4444
std::cerr << std::format("The driver could not be started (path = {})", path) << std::endl;
4545
FAIL();
4646
driver.Stop(true);
4747
}
4848

49+
std::string correct_output = "> Describe table: series\nColumn, name: series_id, type: Uint64?\nColumn, name: title, type: Utf8?\nColumn, name: series_info, type: Utf8?\nColumn, name: release_date, type: Uint64?\n> SelectSimple:\nSeries, Id: 1, Title: IT Crowd, Release date: 2006-02-03\n> SelectWithParams:\nSeason, Title: Season 3, Series title: Silicon Valley\n> PreparedSelect:\nEpisode 7, Title: To Build a Better Beta, Air date: Sun Jun 05, 2016\n> PreparedSelect:\nEpisode 8, Title: Bachman's Earnings Over-Ride, Air date: Sun Jun 12, 2016\n> MultiStep:\nEpisode 1, Season: 5, Title: Grow Fast or Die Slow, Air date: Sun Mar 25, 2018\nEpisode 2, Season: 5, Title: Reorientation, Air date: Sun Apr 01, 2018\nEpisode 3, Season: 5, Title: Chief Operating Officer, Air date: Sun Apr 08, 2018\n> PreparedSelect:\nEpisode 1, Title: TBD, Air date: Fri Jul 19, 2024\n> ScanQuerySelect:\nSeason, SeriesId: 1, SeasonId: 1, Title: Season 1, Air date: 2006-02-03\nSeason, SeriesId: 1, SeasonId: 2, Title: Season 2, Air date: 2007-08-24\nSeason, SeriesId: 1, SeasonId: 3, Title: Season 3, Air date: 2008-11-21\nSeason, SeriesId: 1, SeasonId: 4, Title: Season 4, Air date: 2010-06-25\n";
50+
ASSERT_EQ(response->result, correct_output);
51+
4952
driver.Stop(true);
5053
}

tests/integration/config_ydb.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef CONFIG_YDB_H_IN
2+
#define CONFIG_YDB_H_IN
3+
4+
#include <string>
5+
6+
const std::string endpoint = "localhost:2136";
7+
const std::string database = "/Root/test";
8+
9+
#endif // CONFIG_YDB_H_IN

tests/integration/config_ydb.h.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef CONFIG_YDB_H_IN
2+
#define CONFIG_YDB_H_IN
3+
4+
#include <string>
5+
6+
const std::string endpoint = "@ENDPOINT@";
7+
const std::string database = "@DATABASE@";
8+
9+
#endif // CONFIG_YDB_H_IN

0 commit comments

Comments
 (0)