Browse Source

test torrent get method

master
Jørgen Lien Sellæg 9 years ago
parent
commit
43359b33c5
  1. 3
      toREST/tests/include/SessionContext.hpp
  2. 6
      toREST/tests/include/TorrentContext.hpp
  3. 4
      toREST/tests/stubs/SessionContext.cpp
  4. 4
      toREST/tests/stubs/TorrentContext.cpp
  5. 29
      toREST/tests/torrents_test.cpp

3
toREST/tests/include/SessionContext.hpp

@ -28,6 +28,7 @@ class TestSession {
public:
bool valid = false;
bool paused = false;
std::vector<TestTorrent> torrents;
TestSessionSettings settings_;
bool is_valid();
TestSessionSettings get_settings();
@ -36,7 +37,7 @@ public:
void resume();
int listen_port();
bool is_dht_running() const;
std::vector<TestTorrent> get_torrents() const;
std::vector<TestTorrent>& get_torrents();
void apply_settings(TestSessionSettings settings);
};

6
toREST/tests/include/TorrentContext.hpp

@ -4,7 +4,7 @@
class InfoHash {
public:
std::string to_string() { return "banana"; }
std::string to_string() { return "12a"; }
};
class TorrentStatus {
@ -13,7 +13,7 @@ public:
bool is_seeding = false;
int state = 0;
int priority = 0;
std::string name = "N/A";
std::string name = "Arch";
};
class TestTorrent {
@ -24,6 +24,8 @@ public:
int query_name = 1;
int query_save_path = 2;
bool valid = true;
InfoHash hash;
TorrentStatus status_;
};
#endif

4
toREST/tests/stubs/SessionContext.cpp

@ -60,8 +60,8 @@ int TestSession::listen_port() {
bool TestSession::is_dht_running() const {
return settings_.enable_dht_;
}
std::vector<TestTorrent> TestSession::get_torrents() const {
return std::vector<TestTorrent>();
std::vector<TestTorrent>& TestSession::get_torrents() {
return torrents;
}
void TestSession::apply_settings(TestSessionSettings settings) {
settings_ = settings;

4
toREST/tests/stubs/TorrentContext.cpp

@ -1,11 +1,11 @@
#include <TorrentContext.hpp>
InfoHash TestTorrent::info_hash() {
return InfoHash();
return hash;
}
TorrentStatus TestTorrent::status(int type) {
return TorrentStatus();
return status_;
}
bool TestTorrent::is_valid() {

29
toREST/tests/torrents_test.cpp

@ -5,6 +5,15 @@
using namespace std;
const nlohmann::json torrent = {
{"hash", "12a"},
{"paused", false},
{"seeding", false},
{"state", 0},
{"priority", 0},
{"name", "Arch"}
};
SCENARIO("We are running a GET /session/torrents resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
@ -19,12 +28,22 @@ SCENARIO("We are running a GET /session/torrents resource") {
}
GIVEN("the server is working properly") {
torrent_session.valid = true;
AND_WHEN("the session the session has no torrents") {
tr::session::torrents::get(torrent_session, response, request);
THEN("the server should reply with an empty array") {
CommonResponse::ok(response, {{"torrents", nlohmann::json::array()}});
WHEN("we recive a valid request") {
GIVEN("we have no torrents") {
THEN("the server should reply with an empty array") {
tr::session::torrents::get(torrent_session,response,request);
CommonResponse::ok(response, {{"torrents", nlohmann::json::array()}});
}
}
GIVEN("we have at least one torrent"){
auto t_torrent=TestTorrent();
torrent_session.get_torrents().emplace_back(t_torrent);
THEN("the server should reply with an array of torrents"){
tr::session::torrents::get(torrent_session,response,request);
CommonResponse::ok(response, {{"torrents", {torrent}}});
}
}
}
}
}
Loading…
Cancel
Save