-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreddit_agent_test.cc
82 lines (67 loc) · 2.14 KB
/
reddit_agent_test.cc
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
#include "json_utils.h"
#include "file_utils.h"
#include "reddit_types.pb.h"
#include "reddit_agent.h"
#include "reddit_constants.h"
using namespace reddit;
#include <gtest/gtest.h>
#include <iostream>
using std::cout;
#include <jsoncpp/json/json.h>
#include <memory>
using std::unique_ptr;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <chrono>
#include <thread>
const char kTestUserAgent[] = "test-user-agent";
const char kCaptchaPngFile[] = "testing/captcha.png";
class RedditAgentTest : public ::testing::Test {
protected:
virtual void SetUp() {
agent_.reset(RedditAgent::ConstructNew(kTestUserAgent));
ASSERT_TRUE(agent_.get());
}
void SleepForMillis(int millis) {
std::this_thread::sleep_for(std::chrono::milliseconds(millis));
}
unique_ptr<RedditAgent> agent_;
};
TEST_F(RedditAgentTest, TestUserAgent) {
EXPECT_EQ(agent_->user_agent(), string(kTestUserAgent));
}
TEST_F(RedditAgentTest, TestSubredditListing) {
Listing uiuc_listing = agent_->SubredditHot("uiuc");
EXPECT_GT(uiuc_listing.children_size(), 0);
for (const Thing& child : uiuc_listing.children()) {
EXPECT_TRUE(child.has_link_data());
}
SleepForMillis(1000);
}
TEST_F(RedditAgentTest, TestSubredditNew) {
Listing uiuc_listing = agent_->SubredditNew("uiuc", "", "", 0, 100, false);
EXPECT_EQ(uiuc_listing.children_size(), 100);
for (const Thing& child : uiuc_listing.children()) {
EXPECT_TRUE(child.has_link_data());
}
SleepForMillis(1000);
}
TEST_F(RedditAgentTest, TestRandom) {
Listing random = agent_->SubredditRandom("uiuc");
ASSERT_EQ(random.children_size(), 1);
ASSERT_TRUE(random.children(0).has_link_data());
Link random_link = random.children(0).link_data();
EXPECT_TRUE(random_link.has_id());
SleepForMillis(1000);
}
TEST_F(RedditAgentTest, TestCaptcha) {
EXPECT_TRUE(agent_->NeedsCaptcha());
string iden = agent_->NewCaptchaIden();
EXPECT_FALSE(iden.empty());
string png_data = agent_->GetCaptcha(iden);
EXPECT_FALSE(png_data.empty());
string fname = string("testing/") + iden + string(".png");
WriteStringToFile(fname, png_data);
}