117 lines
4.7 KiB
C++
117 lines
4.7 KiB
C++
// Opt-in live checks (MOLO_LIVE_NET=1, GOOGLE_ACCESS_TOKEN, GOOGLE_CLOUD_PROJECT).
|
|
// Read-only: lists and gets only. A disabled API is reported loudly as
|
|
// "API NOT ENABLED" and is not a failure. The single paid request (one short
|
|
// translation) additionally needs MOLO_LIVE_PAID=1. Nothing is created,
|
|
// changed or deleted.
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
|
|
#include "framework.h"
|
|
#include "google/cloudai/document_ai.h"
|
|
#include "google/cloudai/long_audio.h"
|
|
#include "google/cloudai/speech_v2.h"
|
|
#include "google/cloudai/translation.h"
|
|
#include "google/cloudai/video_intelligence.h"
|
|
#include "google/cloudai/vision.h"
|
|
#include "google/credentials.h"
|
|
|
|
using namespace molo::google;
|
|
using namespace molo::google::cloudai;
|
|
|
|
namespace {
|
|
|
|
bool ready() {
|
|
return std::getenv("MOLO_LIVE_NET") && !Credentials::env("GOOGLE_ACCESS_TOKEN").empty() &&
|
|
!Credentials::env("GOOGLE_CLOUD_PROJECT").empty();
|
|
}
|
|
|
|
CloudContext context(const char* location) {
|
|
CloudContext c = CloudContext::from_env();
|
|
c.location = location;
|
|
return c;
|
|
}
|
|
|
|
// Runs a read; prints the outcome. Returns false for a disabled API.
|
|
template <typename F>
|
|
void probe(const char* what, F&& f) {
|
|
try {
|
|
std::size_t n = f();
|
|
std::cout << " " << what << ": 200, " << n << " item(s)\n";
|
|
} catch (const GoogleError& e) {
|
|
std::string msg = e.message();
|
|
bool disabled = e.http_status() == 403 && msg.find("permission") == std::string::npos &&
|
|
(msg.find("has not been used") != std::string::npos || msg.find("is disabled") != std::string::npos ||
|
|
e.status() == "SERVICE_DISABLED");
|
|
if (disabled) {
|
|
std::cout << " API NOT ENABLED: " << what << " (" << e.http_status() << " " << e.status() << ": " << msg.substr(0, 90) << ")\n";
|
|
return;
|
|
}
|
|
std::cout << " " << what << ": " << e.http_status() << " " << e.status() << "\n";
|
|
FAIL(std::string(what) + " failed: " + e.status());
|
|
}
|
|
}
|
|
|
|
std::size_t count(const molo::clients::body::Json& j) { return j.is_array() ? j.as_array().size() : 0; }
|
|
|
|
#define LIVE_GUARD(name) \
|
|
if (!ready()) { \
|
|
std::cout << "SKIP " name " (set MOLO_LIVE_NET=1, GOOGLE_ACCESS_TOKEN and GOOGLE_CLOUD_PROJECT)\n"; \
|
|
return; \
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(cloudai_live, translation_reads) {
|
|
LIVE_GUARD("cloudai_live.translation_reads");
|
|
auto client = GoogleClient::from_env();
|
|
Translation t(client, context("global"));
|
|
probe("translation supportedLanguages", [&] {
|
|
auto j = t.supported_languages("en");
|
|
const auto* langs = j.find("languages");
|
|
return langs && langs->is_array() ? langs->as_array().size() : 0u;
|
|
});
|
|
probe("translation glossaries.list", [&] { return count(t.list_glossaries()); });
|
|
probe("translation adaptiveMtDatasets.list", [&] { return count(t.list_adaptive_datasets()); });
|
|
probe("translation models.list", [&] { return count(t.list_models()); });
|
|
}
|
|
|
|
TEST(cloudai_live, translation_one_paid_request) {
|
|
LIVE_GUARD("cloudai_live.translation_one_paid_request");
|
|
if (!std::getenv("MOLO_LIVE_PAID")) {
|
|
std::cout << "SKIP cloudai_live.translation_one_paid_request (set MOLO_LIVE_PAID=1: one short translateText)\n";
|
|
return;
|
|
}
|
|
auto client = GoogleClient::from_env();
|
|
Translation t(client, context("global"));
|
|
TranslateRequest r;
|
|
r.texts = {"hello"};
|
|
r.target = "nl";
|
|
r.source = "en";
|
|
probe("translation translateText (paid, 5 characters)", [&] {
|
|
auto out = t.translate(r);
|
|
std::cout << " -> " << (out.empty() ? "" : out[0].text) << "\n";
|
|
ASSERT_EQ(out.size(), 1u);
|
|
return out.size();
|
|
});
|
|
}
|
|
|
|
TEST(cloudai_live, other_apis_reads) {
|
|
LIVE_GUARD("cloudai_live.other_apis_reads");
|
|
auto client = GoogleClient::from_env();
|
|
Vision v(client, context("us-west1"));
|
|
probe("vision productSets.list", [&] { return count(v.list_product_sets()); });
|
|
DocumentAi d(client, context("us"));
|
|
probe("documentai processors.list", [&] { return count(d.list_processors()); });
|
|
probe("documentai processorTypes.list", [&] { return count(d.list_processor_types()); });
|
|
SpeechV2 s(client, context("us-central1"));
|
|
probe("speech recognizers.list", [&] { return count(s.list_recognizers()); });
|
|
LongAudio la(client, context("global"));
|
|
probe("texttospeech voices.list", [&] {
|
|
auto j = la.voices("nl-NL");
|
|
const auto* voices = j.find("voices");
|
|
return voices && voices->is_array() ? voices->as_array().size() : 0u;
|
|
});
|
|
VideoIntelligence vi(client, context("us-east1"));
|
|
probe("videointelligence operations.list", [&] { return count(vi.list_operations()); });
|
|
}
|