Skip to content

Commit f648733

Browse files
comparing json in a test
1 parent c09ba03 commit f648733

File tree

4 files changed

+110
-82
lines changed

4 files changed

+110
-82
lines changed

examples/basic_example/basic_example.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,7 @@ void SelectSimple(TTableClient client, const std::string& path) {
442442
ThrowOnError(client.RetryOperationSync([path, &resultSet](TSession session) {
443443
return SelectSimpleTransaction(session, path, resultSet);
444444
}));
445-
446-
if (resultSet.has_value()) {
447-
std::cout << FormatResultSetJson(resultSet.value(), EBinaryStringEncoding::Base64);
448-
}
445+
449446
TResultSetParser parser(*resultSet);
450447
if (parser.TryNextRow()) {
451448
std::cout << "> SelectSimple:" << std::endl << "Series"
@@ -482,7 +479,7 @@ void PreparedSelect(TTableClient client, const std::string& path, uint32_t serie
482479
ThrowOnError(client.RetryOperationSync([path, seriesId, seasonId, episodeId, &resultSet](TSession session) {
483480
return PreparedSelectTransaction(session, path, seriesId, seasonId, episodeId, resultSet);
484481
}));
485-
482+
486483
TResultSetParser parser(*resultSet);
487484
if (parser.TryNextRow()) {
488485
auto airDate = TInstant::Days(*parser.ColumnParser("air_date").GetOptionalUint64());
@@ -501,6 +498,7 @@ void MultiStep(TTableClient client, const std::string& path) {
501498
}));
502499

503500
TResultSetParser parser(*resultSet);
501+
504502
std::cout << "> MultiStep:" << std::endl;
505503
while (parser.TryNextRow()) {
506504
auto airDate = TInstant::Days(*parser.ColumnParser("air_date").GetOptionalUint64());
@@ -563,7 +561,6 @@ void ScanQuerySelect(TTableClient client, const std::string& path) {
563561
if (streamPart.HasResultSet()) {
564562
auto rs = streamPart.ExtractResultSet();
565563
auto columns = rs.GetColumnsMeta();
566-
567564
TResultSetParser parser(rs);
568565
while (parser.TryNextRow()) {
569566
std::cout << "Season"

tests/integration/basic_example_it/basic_example.cpp

Lines changed: 20 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,13 @@
88
using namespace NYdb;
99
using namespace NYdb::NTable;
1010

11-
class TYdbErrorException : public yexception {
12-
public:
13-
TYdbErrorException(const TStatus& status)
14-
: Status(status) {}
15-
16-
TStatus Status;
17-
};
18-
19-
static void ThrowOnError(const TStatus& status) {
11+
void ThrowOnError(const TStatus& status) {
2012
if (!status.IsSuccess()) {
2113
throw TYdbErrorException(status) << status;
2214
}
2315
}
2416

25-
static void PrintStatus(const TStatus& status) {
17+
void PrintStatus(const TStatus& status) {
2618
std::cerr << "Status: " << ToString(status.GetStatus()) << std::endl;
2719
status.GetIssues().PrintTo(std::cerr);
2820
}
@@ -41,7 +33,7 @@ static std::string JoinPath(const std::string& basePath, const std::string& path
4133
///////////////////////////////////////////////////////////////////////////////
4234

4335
//! Creates sample tables with CrateTable API.
44-
static void CreateTables(TTableClient client, const std::string& path) {
36+
void CreateTables(TTableClient client, const std::string& path) {
4537
ThrowOnError(client.RetryOperationSync([path](TSession session) {
4638
auto seriesDesc = TTableBuilder()
4739
.AddNullableColumn("series_id", EPrimitiveType::Uint64)
@@ -83,7 +75,7 @@ static void CreateTables(TTableClient client, const std::string& path) {
8375
}
8476

8577
//! Describe existing table.
86-
static void DescribeTable(TTableClient client, const std::string& path, const std::string& name) {
78+
void DescribeTable(TTableClient client, const std::string& path, const std::string& name) {
8779
std::optional<TTableDescription> desc;
8880
std::string result;
8981
ThrowOnError(client.RetryOperationSync([path, name, &desc](TSession session) {
@@ -104,7 +96,7 @@ static void DescribeTable(TTableClient client, const std::string& path, const st
10496
///////////////////////////////////////////////////////////////////////////////
10597

10698
//! Fills sample tables with data in single parameterized data query.
107-
static TStatus FillTableDataTransaction(TSession session, const std::string& path) {
99+
TStatus FillTableDataTransaction(TSession session, const std::string& path) {
108100
auto query = std::format(R"(
109101
PRAGMA TablePathPrefix("{}");
110102
@@ -443,10 +435,9 @@ std::string SelectSimple(TTableClient client, const std::string& path) {
443435
std::cout << std::format("> SelectSimple:\nSeries, Id: {}, Title: {}, Release date: {}\n"
444436
, ToString(parser.ColumnParser("series_id").GetOptionalUint64())
445437
, ToString(parser.ColumnParser("title").GetOptionalUtf8())
446-
, ToString(parser.ColumnParser("release_date").GetOptionalString()));
447-
return FormatResultSetJson(resultSet.value(), EBinaryStringEncoding::Unicode);
438+
, ToString(parser.ColumnParser("release_date").GetOptionalString()));
448439
}
449-
return "";
440+
return FormatResultSetJson(resultSet.value(), EBinaryStringEncoding::Unicode);
450441
}
451442

452443
void UpsertSimple(TTableClient client, const std::string& path) {
@@ -464,50 +455,47 @@ std::string SelectWithParams(TTableClient client, const std::string& path) {
464455

465456
TResultSetParser parser(*resultSet);
466457
if (parser.TryNextRow()) {
467-
return std::format("> SelectWithParams:\nSeason, Title: {}, Series title: {}\n"
458+
std::cout << std::format("> SelectWithParams:\nSeason, Title: {}, Series title: {}\n"
468459
, ToString(parser.ColumnParser("season_title").GetOptionalUtf8())
469-
, ToString(parser.ColumnParser("series_title").GetOptionalUtf8()));
460+
, ToString(parser.ColumnParser("series_title").GetOptionalUtf8()));
470461
}
471-
return "";
462+
return FormatResultSetJson(resultSet.value(), EBinaryStringEncoding::Unicode);
472463
}
473464

474465
std::string PreparedSelect(TTableClient client, const std::string& path, ui32 seriesId, ui32 seasonId, ui32 episodeId) {
475466
std::optional<TResultSet> resultSet;
476-
std::string result;
477467
ThrowOnError(client.RetryOperationSync([path, seriesId, seasonId, episodeId, &resultSet](TSession session) {
478468
return PreparedSelectTransaction(session, path, seriesId, seasonId, episodeId, resultSet);
479469
}));
480470

481471
TResultSetParser parser(*resultSet);
482472
if (parser.TryNextRow()) {
483473
auto airDate = TInstant::Days(*parser.ColumnParser("air_date").GetOptionalUint64());
484-
485-
return std::format("> PreparedSelect:\nEpisode {}, Title: {}, Air date: {}\n"
474+
std::cout << std::format("> PreparedSelect:\nEpisode {}, Title: {}, Air date: {}\n"
486475
, ToString(parser.ColumnParser("episode_id").GetOptionalUint64())
487476
, ToString(parser.ColumnParser("title").GetOptionalUtf8())
488-
, airDate.FormatLocalTime("%a %b %d, %Y"));
477+
, airDate.FormatLocalTime("%a %b %d, %Y"));
489478
}
490-
return "";
479+
return FormatResultSetJson(resultSet.value(), EBinaryStringEncoding::Unicode);
491480
}
492481

493482
std::string MultiStep(TTableClient client, const std::string& path) {
494483
std::optional<TResultSet> resultSet;
495-
std::string result;
496484
ThrowOnError(client.RetryOperationSync([path, &resultSet](TSession session) {
497485
return MultiStepTransaction(session, path, 2, 5, resultSet);
498486
}));
499487

500488
TResultSetParser parser(*resultSet);
501-
result = "> MultiStep:\n";
489+
std::cout << "> MultiStep:\n";
502490
while (parser.TryNextRow()) {
503491
auto airDate = TInstant::Days(*parser.ColumnParser("air_date").GetOptionalUint64());
504-
result += std::format("Episode {}, Season: {}, Title: {}, Air date: {}\n"
492+
std::cout << std::format("Episode {}, Season: {}, Title: {}, Air date: {}\n"
505493
, ToString(parser.ColumnParser("episode_id").GetOptionalUint64())
506494
, ToString(parser.ColumnParser("season_id").GetOptionalUint64())
507495
, ToString(parser.ColumnParser("title").GetOptionalUtf8())
508496
, airDate.FormatLocalTime("%a %b %d, %Y"));
509497
}
510-
return result;
498+
return FormatResultSetJson(resultSet.value(), EBinaryStringEncoding::Unicode);
511499
}
512500

513501
void ExplicitTcl(TTableClient client, const std::string& path) {
@@ -517,7 +505,6 @@ void ExplicitTcl(TTableClient client, const std::string& path) {
517505
}
518506

519507
std::string ScanQuerySelect(TTableClient client, const std::string& path) {
520-
std::string result;
521508
auto query = std::format(R"(
522509
--!syntax_v1
523510
PRAGMA TablePathPrefix("{}");
@@ -546,7 +533,7 @@ std::string ScanQuerySelect(TTableClient client, const std::string& path) {
546533
}
547534

548535
bool eos = false;
549-
result = "> ScanQuerySelect:\n";
536+
std::cout << "> ScanQuerySelect:\n";
550537

551538
while (!eos) {
552539
auto streamPart = resultScanQuery.ReadNext().ExtractValueSync();
@@ -565,51 +552,14 @@ std::string ScanQuerySelect(TTableClient client, const std::string& path) {
565552

566553
TResultSetParser parser(rs);
567554
while (parser.TryNextRow()) {
568-
result += std::format("Season, SeriesId: {}, SeasonId: {}, Title: {}, Air date: {}\n"
555+
std::cout << std::format("Season, SeriesId: {}, SeasonId: {}, Title: {}, Air date: {}\n"
569556
, ToString(parser.ColumnParser("series_id").GetOptionalUint64())
570557
, ToString(parser.ColumnParser("season_id").GetOptionalUint64())
571558
, ToString(parser.ColumnParser("title").GetOptionalUtf8())
572559
, ToString(parser.ColumnParser("first_aired").GetOptionalString()));
573560
}
561+
return FormatResultSetJson(rs, EBinaryStringEncoding::Unicode);
574562
}
575563
}
576-
return result;
577-
}
578-
579-
///////////////////////////////////////////////////////////////////////////////
580-
581-
bool Run(const TDriver& driver, const std::string& path) {
582-
TTableClient client(driver);
583-
try {
584-
CreateTables(client, path);
585-
586-
DescribeTable(client, path, "series");
587-
588-
ThrowOnError(client.RetryOperationSync([path](TSession session) {
589-
return FillTableDataTransaction(session, path);
590-
}));
591-
592-
std::string expectedResultSelectSimple = "{\"series_id\":1,\"title\":\"IT Crowd\",\"release_date\":\"2006-02-03\"}";
593-
std::string resultSelectSimple = SelectSimple(client, path);
594-
UpsertSimple(client, path);
595-
596-
SelectWithParams(client, path);
597-
598-
PreparedSelect(client, path, 2, 3, 7);
599-
PreparedSelect(client, path, 2, 3, 8);
600-
601-
MultiStep(client, path);
602-
603-
ExplicitTcl(client, path);
604-
605-
PreparedSelect(client, path, 2, 6, 1);
606-
607-
ScanQuerySelect(client, path);
608-
}
609-
catch (const TYdbErrorException& e) {
610-
std::cerr << "Execution failed due to fatal error:" << std::endl;
611-
PrintStatus(e.Status);
612-
return false;
613-
}
614-
return true;
564+
return "";
615565
}

tests/integration/basic_example_it/basic_example.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@
33
#include <ydb-cpp-sdk/client/driver/driver.h>
44
#include <ydb-cpp-sdk/client/table/table.h>
55

6+
#include <gtest/gtest.h>
7+
8+
using namespace NYdb;
9+
using namespace NYdb::NTable;
10+
11+
struct RunArgs {
12+
TDriver driver;
13+
std::string path;
14+
};
15+
class TYdbErrorException : public yexception {
16+
public:
17+
TYdbErrorException(const TStatus& status)
18+
: Status(status) {}
19+
20+
TStatus Status;
21+
};
22+
623
NYdb::TParams GetTablesDataParams();
724

8-
bool Run(const NYdb::TDriver& driver, const std::string& path);
25+
void CreateTables(TTableClient client, const std::string& path);
26+
void DescribeTable(TTableClient client, const std::string& path, const std::string& name);
27+
void ThrowOnError(const TStatus& status);
28+
void PrintStatus(const TStatus& status);
29+
TStatus FillTableDataTransaction(TSession session, const std::string& path);
30+
std::string SelectSimple(TTableClient client, const std::string& path);
31+
void UpsertSimple(TTableClient client, const std::string& path);
32+
std::string SelectWithParams(TTableClient client, const std::string& path);
33+
std::string PreparedSelect(TTableClient client, const std::string& path, ui32 seriesId, ui32 seasonId, ui32 episodeId);
34+
std::string MultiStep(TTableClient client, const std::string& path);
35+
void ExplicitTcl(TTableClient client, const std::string& path);
36+
std::string PreparedSelect(TTableClient client, const std::string& path, ui32 seriesId, ui32 seasonId, ui32 episodeId);
37+
std::string ScanQuerySelect(TTableClient client, const std::string& path);

tests/integration/basic_example_it/main.cpp

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@
1010
#include <gtest/gtest.h>
1111

1212
using namespace NLastGetopt;
13-
using namespace NYdb;
1413

1514
void StopHandler(int) {
1615
exit(1);
1716
}
1817

19-
TEST(Integration, BasicExample) {
18+
RunArgs getRunArgs() {
2019
TOpts opts = TOpts::Default();
2120

2221
std::string database = std::getenv( "DATABASE" );
23-
std::string endpoint = std::getenv( "ENDPOINT" );;
22+
std::string endpoint = std::getenv( "ENDPOINT" );
2423
std::string path;
2524
std::string certPath;
2625

@@ -39,10 +38,63 @@ TEST(Integration, BasicExample) {
3938
}
4039

4140
TDriver driver(driverConfig);
41+
return {driver, path};
42+
}
4243

43-
if (!::Run(driver, path)) {
44-
FAIL();
44+
45+
TEST(Integration, BasicExample) {
46+
47+
auto [driver, path] = getRunArgs();
48+
49+
TTableClient client(driver);
50+
51+
try {
52+
CreateTables(client, path);
53+
54+
DescribeTable(client, path, "series");
55+
56+
ThrowOnError(client.RetryOperationSync([path](TSession session) {
57+
return FillTableDataTransaction(session, path);
58+
}));
59+
60+
std::string expectedResultSelectSimple = "{\"series_id\":1,\"title\":\"IT Crowd\",\"release_date\":\"2006-02-03\"}\n";
61+
std::string resultSelectSimple = SelectSimple(client, path);
62+
ASSERT_EQ(resultSelectSimple, expectedResultSelectSimple);
63+
64+
UpsertSimple(client, path);
65+
66+
67+
std::string expectedResultSelectWithParams = "{\"season_title\":\"Season 3\",\"series_title\":\"Silicon Valley\"}\n";
68+
std::string resultSelectWithParms = SelectWithParams(client, path);
69+
ASSERT_EQ(resultSelectWithParms, expectedResultSelectWithParams);
70+
71+
std::string expectedResultPreparedSelect1 = "{\"air_date\":16957,\"episode_id\":7,\"season_id\":3,\"series_id\":2,\"title\":\"To Build a Better Beta\"}\n";
72+
std::string resultPreparedSelect1 = PreparedSelect(client, path, 2, 3, 7);
73+
ASSERT_EQ(resultPreparedSelect1, expectedResultPreparedSelect1);
74+
75+
std::string expectedResultPreparedSelect2 = "{\"air_date\":16964,\"episode_id\":8,\"season_id\":3,\"series_id\":2,\"title\":\"Bachman's Earnings Over-Ride\"}\n";
76+
std::string resultPreparedSelect2 = PreparedSelect(client, path, 2, 3, 8);
77+
ASSERT_EQ(resultPreparedSelect2, expectedResultPreparedSelect2);
78+
79+
std::string expectedResultMultiStep = "{\"season_id\":5,\"episode_id\":1,\"title\":\"Grow Fast or Die Slow\",\"air_date\":17615}\n{\"season_id\":5,\"episode_id\":2,\"title\":\"Reorientation\",\"air_date\":17622}\n{\"season_id\":5,\"episode_id\":3,\"title\":\"Chief Operating Officer\",\"air_date\":17629}\n";
80+
std::string resultMultiStep = MultiStep(client, path);
81+
ASSERT_EQ(resultMultiStep, expectedResultMultiStep);
82+
83+
ExplicitTcl(client, path);
84+
85+
std::string expectedResultPreparedSelect3 = "{\"air_date\":0,\"episode_id\":1,\"season_id\":6,\"series_id\":2,\"title\":\"TBD\"}\n";
86+
std::string resultPreparedSelect3 = PreparedSelect(client, path, 2, 6, 1);
87+
ASSERT_EQ(resultPreparedSelect3, expectedResultPreparedSelect3);
88+
89+
std::string expectedResultScanQuerySelect = "{\"series_id\":1,\"season_id\":1,\"title\":\"Season 1\",\"first_aired\":\"2006-02-03\"}\n{\"series_id\":1,\"season_id\":2,\"title\":\"Season 2\",\"first_aired\":\"2007-08-24\"}\n{\"series_id\":1,\"season_id\":3,\"title\":\"Season 3\",\"first_aired\":\"2008-11-21\"}\n{\"series_id\":1,\"season_id\":4,\"title\":\"Season 4\",\"first_aired\":\"2010-06-25\"}\n";
90+
std::string resultScanQuerySelect = ScanQuerySelect(client, path);
91+
ASSERT_EQ(resultScanQuerySelect, expectedResultScanQuerySelect);
92+
}
93+
catch (const TYdbErrorException& e) {
94+
std::cerr << "Execution failed due to fatal error:" << std::endl;
95+
PrintStatus(e.Status);
4596
driver.Stop(true);
97+
FAIL();
4698
}
4799

48100
driver.Stop(true);

0 commit comments

Comments
 (0)