repository_test.rb revision afd1d888784385307c9c0544597a513c2008d342
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainenrequire 'test_helper'
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainenclass RepositoryTest < ActiveSupport::TestCase
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen should have_many :ontologies
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen should have_many :permissions
1ed1ad066e4aa313e33dafedb892fb84946cacebTimo Sirainen
146240408e677e99e579d1feed92689585cc25d4Timo Sirainen context "a repository" do
b1dd6be436e887774b94965ebe9af6d04179c227Timo Sirainen setup do
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen @user = FactoryGirl.create :user
4073f0dbf3277f981a8fcee3b89ea15aaf380a7fTimo Sirainen @repository = FactoryGirl.create :repository, user: @user
b200bc3875fa06d42c8619865cc306c3297fcaccAki Tuomi end
b200bc3875fa06d42c8619865cc306c3297fcaccAki Tuomi
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen teardown do
b200bc3875fa06d42c8619865cc306c3297fcaccAki Tuomi @repository.destroy
b200bc3875fa06d42c8619865cc306c3297fcaccAki Tuomi end
0aac625db5e6e179c8ee7420a12ab300d6b178edTimo Sirainen
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen context 'saving a file' do
e5224c0589916fb22f95f959326cf4b6221715b0Timo Sirainen setup do
ca44a6ba994aaa3231a20ef6e046dfd97a8dcd2dTimo Sirainen @file_path = '/tmp/ontohub/test/git_repository/save_file.txt'
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen @target_path = 'save_file.clif'
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen @message = 'test message'
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen @content = "(Cat x)\n"
ca44a6ba994aaa3231a20ef6e046dfd97a8dcd2dTimo Sirainen
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen # create directory if it doesn't exist
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen dir = File.dirname(@file_path)
b200bc3875fa06d42c8619865cc306c3297fcaccAki Tuomi unless File.directory?(dir)
ca44a6ba994aaa3231a20ef6e046dfd97a8dcd2dTimo Sirainen FileUtils.mkdir_p(dir)
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen end
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen
1ed1ad066e4aa313e33dafedb892fb84946cacebTimo Sirainen # create file
1ed1ad066e4aa313e33dafedb892fb84946cacebTimo Sirainen tmpfile = File.open(@file_path, 'w')
146240408e677e99e579d1feed92689585cc25d4Timo Sirainen tmpfile.puts(@content)
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen tmpfile.close
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen end
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen
1ed1ad066e4aa313e33dafedb892fb84946cacebTimo Sirainen should 'not have an ontology yet' do
1ed1ad066e4aa313e33dafedb892fb84946cacebTimo Sirainen assert_equal @repository.ontologies.count, 0
cbe49ba128638e63395aedaa2144087c89835633Timo Sirainen end
cbe49ba128638e63395aedaa2144087c89835633Timo Sirainen
context "that doesn't exist" do
setup do
@version = @repository.save_file(@file_path, @target_path, @message, @user)
end
should 'create the file in the git repository' do
assert @repository.git.path_exists?(@target_path)
end
should 'create the file with correct contents in the git repository' do
assert_equal @content, @repository.git.get_file(@target_path).content
end
should 'create a new ontology with a default name' do
assert_equal @repository.ontologies.count, 1
assert_equal @repository.ontologies.first.name, 'Save_file'
end
should 'create a new ontology with only one version pointing to the commit' do
o = @repository.ontologies.first
assert_equal o.versions.count, 1
assert_equal o.versions.first[:commit_oid], @version.commit_oid
end
should 'create a new ontology with only one version belonging to the right user' do
v = @repository.ontologies.first.versions.first
assert_equal v.user, @user
end
end
context 'that exists' do
setup do
assert_difference 'Worker.jobs.count' do
@repository.save_file(@file_path, @target_path, @message, @user)
end
file = File.open(@file_path, 'w')
file.puts("#{@content}\n#{@content}")
file.close
@repository.save_file(@file_path, @target_path, @message, @user)
end
teardown do
@repository.destroy
end
should 'create a new ontology version' do
ontology = @repository.ontologies.where(basepath: File.basepath(@target_path)).first!
assert_equal 2, ontology.versions.count
end
end
end
context 'browsing the repository' do
setup do
@files = {
'inroot1.clif' => "(In1 Root)\n",
'inroot2.clf' => "(In2 Root Too)\n",
'inroot2.clif' => "(In2 Root Too2)\n",
'folder1/file1.clif' => "(In Folder)\n",
'folder1/file2.clf' => "(In2 Folder Too)\n",
'folder2/file3.clf' => "(In2 Folder Again)\n"
}
@message = 'test message'
@file_path_prefix = '/tmp/ontohub/test/git_repository/'
@iri_prefix = 'http://localhost/repo_name/'
@files.each do | path, content |
file_path = "#{@file_path_prefix}#{path}"
# create directory if it doesn't exist
dir = File.dirname(file_path)
unless File.directory?(dir)
FileUtils.mkdir_p(dir)
end
# create file
tmpfile = File.open(file_path, 'w')
tmpfile.puts(content)
tmpfile.close
if path == 'inroot2.clif'
assert_no_difference 'Worker.jobs.count' do
@repository.save_file(file_path, path, @message, @user, "#{@iri_prefix}#{file_path}")
end
else
assert_difference 'Worker.jobs.count' do
@repository.save_file(file_path, path, @message, @user, "#{@iri_prefix}#{file_path}")
end
end
end
end
teardown do
@repository.destroy
end
should 'paths_starting_with?' do
assert_equal(['inroot2.clf', 'inroot2.clif'], @repository.paths_starting_with('inroot2'))
assert_equal(['inroot1.clif', 'inroot2.clf', 'inroot2.clif'],
@repository.paths_starting_with('inroot'))
assert_equal(['folder1/file1.clif', 'folder1/file2.clf'],
@repository.paths_starting_with('folder1/file'))
end
should 'dir?' do
assert !@repository.dir?('non-existent')
assert !@repository.dir?('inroot1.clif')
assert @repository.dir?('folder1')
end
end
end
end