Skip to content

Commit

Permalink
Definition of the unit test for removing methods
Browse files Browse the repository at this point in the history
Creation of the unit test to ensure the functioning of removing files
locally and remotelly.

re #7031
  • Loading branch information
gesnerpassos committed May 28, 2013
1 parent f0f0901 commit f35c0b4
Showing 1 changed file with 136 additions and 4 deletions.
140 changes: 136 additions & 4 deletions Code/Mantid/Framework/ScriptRepository/test/ScriptRepositoryTestImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class ScriptRepositoryImplLocal : public ScriptRepositoryImpl{
repository_json_content = REPOSITORYJSON;
tofconv_readme_content = TOFCONV_README;
tofconv_tofconverter_content = TOFCONV_CONVERTER;
fail = false;
}
virtual ~ScriptRepositoryImplLocal()throw(){};

Expand Down Expand Up @@ -171,14 +172,42 @@ class ScriptRepositoryImplLocal : public ScriptRepositoryImpl{
std::stringstream ss;
ss << "Failed to download this file : " << url_file << " to " << local_file_path << std::ends;
throw ScriptRepoException(ss.str());
}

};
};

/** Override the ScriptRepositoryImpl::doDeleteRemoteFile in order
to mock its functioning avoiding the internet connection.
/** Protect the logic and behavior of ScriptRepositoryImpl without requiring internet connection.
Its answer depends on the public attribute fail. If fail is false (default)
then, it will return a json string that simulate the answer from the remote host
when it succeed in deleting one file. If fail is true, than, it will return
a json file that simulate the answer from the remote host when the operation fails.
@code
// delete success
std::string json_ans = doDeleteRemoteFile("<remote_url>/README.md","README.md",
"noone","noone@nowhere.com","Remove this useless file");
// delete failure
fail = true;
std::string json_fail_ans = doDeleteRemoteFile("<remote_url>/README.md","README.md",
"noone","noone@nowhere.com","Remove this useless file");
@endcode
It also make it public, in order to be able to test this method itself.
*/
bool fail;
std::string doDeleteRemoteFile(const std::string & /*url*/, const std::string & /*file_path*/,
const std::string & /*author*/, const std::string & /*email*/,
const std::string & /*comment*/){
if (fail)
return "{\n \"message\": \"Invalid author: \"\n}";
else
return "{\n \"message\": \"success\"\n}";
};

};

/** Protect the logic and behavior of ScriptRepositoryImpl without requiring internet connection.
These tests do no depend on the internet connection
ctest -j8 -R ScriptRepositoryTestImpl_ --verbose
Expand Down Expand Up @@ -783,6 +812,109 @@ void test_downloading_locally_modified_file(){

}

/**This test ensure that when you remove a file from the central repository,
the remove method,
the entry can not be listed anymore, internally or externally
*/
void test_delete_remove_valid_file_from_central_repository(){
TS_ASSERT_THROWS_NOTHING(repo->install(local_rep));
TS_ASSERT_THROWS_NOTHING(repo->listFiles());
std::string file_name = "TofConv/TofConverter.py";
// download the file
TS_ASSERT_THROWS_NOTHING(repo->download(file_name));

// it must be unchanged
TS_ASSERT(repo->fileStatus(file_name) == Mantid::API::BOTH_UNCHANGED) ;

// now, lets delete this file from the central repository
TS_ASSERT_THROWS_NOTHING(repo->remove(file_name, "please remove it","noauthor","noemail"));

// you should not find the file, so fileStatus should throw exception entry not inside repository
TS_ASSERT_THROWS(repo->fileStatus(file_name),ScriptRepoException);

// even if you re-read the repository listing the files
TS_ASSERT_THROWS_NOTHING(repo->listFiles());

// you should not find this file agin
TS_ASSERT_THROWS(repo->fileStatus(file_name),ScriptRepoException);

// assert file does not exists inside the local folder
Poco::File f(std::string(local_rep).append(file_name));
TS_ASSERT(!f.exists());
}


/** This test simulate the reaction when the delete from the central repository
fails.
*/
void test_delete_remove_valid_file_from_central_repository_simulate_server_rejection(){
TS_ASSERT_THROWS_NOTHING(repo->install(local_rep));
TS_ASSERT_THROWS_NOTHING(repo->listFiles());
std::string file_name = "TofConv/TofConverter.py";
// download
TS_ASSERT_THROWS_NOTHING(repo->download(file_name));

// it must be unchanged
TS_ASSERT(repo->fileStatus(file_name) == Mantid::API::BOTH_UNCHANGED) ;
repo->fail = true;
// now, lets delete this file from the repository
// it must throw exception describing the reason for failuring.
TS_ASSERT_THROWS(repo->remove(file_name, "please remove it","noauthor","noemail")
,ScriptRepoException);

// you should find the file internally and externally
TS_ASSERT(repo->fileStatus(file_name) == Mantid::API::BOTH_UNCHANGED) ;
// nothing should changing, re-reading the whole repository list
TS_ASSERT_THROWS_NOTHING(repo->listFiles());
// you should find the file
TS_ASSERT(repo->fileStatus(file_name) == Mantid::API::BOTH_UNCHANGED) ;
}

/**
Test what happens with a file is removed only locally (remove_local).
If the file existed in the central repository, after removing the local copy,
its state must be REMOVE_ONLY
*/
void test_delete_remove_file_locally(){
TS_ASSERT_THROWS_NOTHING(repo->install(local_rep));
TS_ASSERT_THROWS_NOTHING(repo->listFiles());
std::string file_name = "TofConv/TofConverter.py";
// download
TS_ASSERT_THROWS_NOTHING(repo->download(file_name));

// it must be unchanged
TS_ASSERT(repo->fileStatus(file_name) == Mantid::API::BOTH_UNCHANGED) ;

// now, lets delete this file locally
TS_ASSERT_THROWS_NOTHING(repo->remove_local(file_name));

// you should find the file but status REMOTE_ONLY
TS_ASSERT(repo->fileStatus(file_name) == Mantid::API::REMOTE_ONLY) ;
// nothing should change listing all the files again
TS_ASSERT_THROWS_NOTHING(repo->listFiles());
TS_ASSERT(repo->fileStatus(file_name) == Mantid::API::REMOTE_ONLY) ;
}

/** Test invalid entry for removing files, when they are not local (not downloaded)
Ensure that removing from the central repository is not allowed, if the file has
not been downloaded first.
*/
void test_delete_file_not_local(){
TS_ASSERT_THROWS_NOTHING(repo->install(local_rep));
TS_ASSERT_THROWS_NOTHING(repo->listFiles());
std::string file_name = "TofConv/TofConverter.py";


// attempt to remove file that is not local (no download was done)
// it must throw exception, to inform that it is not allowed to remove it.
TS_ASSERT_THROWS(repo->remove(file_name, "please remove it","noauthor","noemail")
,ScriptRepoException);
// the state is still remote-only
TS_ASSERT(repo->fileStatus(file_name) == Mantid::API::REMOTE_ONLY) ;
}


};

Expand Down

0 comments on commit f35c0b4

Please sign in to comment.