-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_c_api.cxx
149 lines (121 loc) · 4.13 KB
/
test_c_api.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef TESTDIR
#define TESTDIR ""
#endif
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include "fdp/fdp.h"
#include "fdp/objects/metadata.hxx" // read_token
#include "gtest/gtest.h"
#include <ghc/filesystem.hpp>
namespace fs = ghc::filesystem;
namespace fdp = FairDataPipeline;
std::string home_dir() {
std::string home;
#ifdef _WIN32
home = getenv("HOMEDRIVE");
home += getenv("HOMEPATH");
#else
home = getenv("HOME");
#endif
return home;
}
#define BUFFER_SIZE 512
TEST(CTest, link_read_write) {
fdp_set_log_level(FDP_LOG_DEBUG);
char buf[BUFFER_SIZE];
// Initialise
FdpDataPipeline *pipeline;
fs::path config = fs::path(TESTDIR) / "data" / "write_csv.yaml";
fs::path script = fs::path(TESTDIR) / "test_script.sh";
fs::path token_path = fs::path(home_dir()) / ".fair" / "registry" / "token";
char token[BUFFER_SIZE];
ASSERT_EQ(fdp_read_token(token_path.c_str(), token, BUFFER_SIZE),
FDP_ERR_NONE);
ASSERT_EQ(fdp_init(&pipeline, config.string().c_str(),
script.string().c_str(), token),
FDP_ERR_NONE);
// Test link write
buf[0] = '\0'; // Ensure strlen of output buffer is 0
EXPECT_EQ(fdp_link_write(pipeline, "test/csv/c", buf, BUFFER_SIZE),
FDP_ERR_NONE);
EXPECT_GT(strlen(buf), 1);
// Write to new path
std::ofstream fstream(buf);
fstream << "Test";
fstream.close();
// Finalise and re-initialise
ASSERT_EQ(fdp_finalise(&pipeline), FDP_ERR_NONE);
config = fs::path(TESTDIR) / "data" / "read_csv.yaml";
ASSERT_EQ(fdp_init(&pipeline, config.string().c_str(),
script.string().c_str(), token),
FDP_ERR_NONE);
// Test link read
buf[0] = '\0'; // Ensure strlen of output buffer is 0
EXPECT_EQ(fdp_link_read(pipeline, "test/csv/c", buf, BUFFER_SIZE),
FDP_ERR_NONE);
EXPECT_GT(strlen(buf), 1);
// Finalise again
EXPECT_EQ(fdp_finalise(&pipeline), FDP_ERR_NONE);
}
TEST(CTest, cpp_to_c) {
fdp_set_log_level(FDP_LOG_DEBUG);
char buf[512];
// Initialise using C++
fs::path config = fs::path(TESTDIR) / "data" / "write_csv.yaml";
fs::path script = fs::path(TESTDIR) / "test_script.sh";
std::string token =
fdp::read_token(fs::path(home_dir()) / ".fair" / "registry" / "token");
auto cpp_pipeline =
fdp::DataPipeline::construct(config.string(), script.string(), token);
// Switch to C API, use temporary FdpDataPipeline
FdpDataPipeline *c_pipeline = fdp::to_c_struct(cpp_pipeline);
// Test link write
buf[0] = '\0'; // Ensure strlen of output buffer is 0
EXPECT_EQ(fdp_link_write(c_pipeline, "test/csv", buf, BUFFER_SIZE),
FDP_ERR_NONE);
EXPECT_GT(strlen(buf), 1);
// Write to new path
std::ofstream fstream(buf);
fstream << "Test";
fstream.close();
// Finish working in C, delete FdpDataPipeline
fdp::delete_c_struct(c_pipeline);
// Finalise in C++
cpp_pipeline->finalise();
}
TEST(CTest, c_to_cpp) {
fdp_set_log_level(FDP_LOG_DEBUG);
// Initialise using C
FdpDataPipeline *c_pipeline;
fs::path config = fs::path(TESTDIR) / "data" / "write_csv.yaml";
fs::path script = fs::path(TESTDIR) / "test_script.sh";
fs::path token_path = fs::path(home_dir()) / ".fair" / "registry" / "token";
char token[BUFFER_SIZE];
ASSERT_EQ(fdp_read_token(token_path.c_str(), token, BUFFER_SIZE),
FDP_ERR_NONE);
ASSERT_EQ(fdp_init(&c_pipeline, config.string().c_str(),
script.string().c_str(), token),
FDP_ERR_NONE);
// Switch to C++ API
auto cpp_pipeline = fdp::from_c_struct(c_pipeline);
// Test link write
std::string data_product = "test/csv";
fs::path currentLink = fs::path(cpp_pipeline->link_write(data_product));
EXPECT_GT(currentLink.string().size(), 1);
// Write to new path
std::ofstream fstream(currentLink);
fstream << "Test";
fstream.close();
// Finish working in C++
cpp_pipeline = nullptr;
// Finalise in C
EXPECT_EQ(fdp_finalise(&c_pipeline), FDP_ERR_NONE);
}
TEST(CTest, log_levels) {
fdp_set_log_level(FDP_LOG_INFO);
EXPECT_EQ(fdp_get_log_level(), FDP_LOG_INFO);
fdp_set_log_level(FDP_LOG_DEBUG);
EXPECT_EQ(fdp_get_log_level(), FDP_LOG_DEBUG);
}