repository_test.rb revision 5422b48ab5841a2909cd9e53905dae4cd8418f0a
165N/Arequire 'test_helper'
165N/A
165N/Aclass RepositoryTest < ActiveSupport::TestCase
165N/A
165N/A should have_many :ontologies
165N/A should have_many :permissions
165N/A
165N/A context "a repository" do
165N/A setup do
165N/A @user = FactoryGirl.create :user
165N/A @repository = FactoryGirl.create :repository, user: @user
165N/A end
165N/A
165N/A teardown do
165N/A @repository.destroy
165N/A end
165N/A
165N/A context 'with a reserved name' do
165N/A should 'not be valid' do
165N/A repository = FactoryGirl.build :repository, user: @user, name: 'repositories'
165N/A assert repository.invalid?
5294N/A assert repository.errors[:name].any?
165N/A end
165N/A end
165N/A
165N/A context 'creating a permission' do
5294N/A setup do
5294N/A assert_not_nil @permission = @repository.permissions.first
5294N/A end
165N/A
165N/A should 'with subject' do
5294N/A assert_equal @user, @permission.subject
5294N/A end
1273N/A
165N/A should 'with role owner' do
3935N/A assert_equal 'owner', @permission.role
3935N/A end
3935N/A end
3935N/A
3935N/A context 'saving a file' do
165N/A setup do
2577N/A @file_path = '/tmp/ontohub/test/git_repository/save_file.txt'
2577N/A @target_path = 'save_file.clif'
2577N/A @message = 'test message'
165N/A @content = "(Cat x)\n"
165N/A
1756N/A # create directory if it doesn't exist
165N/A dir = File.dirname(@file_path)
165N/A unless File.directory?(dir)
165N/A FileUtils.mkdir_p(dir)
3935N/A end
3935N/A
3935N/A # create file
3935N/A tmpfile = File.open(@file_path, 'w')
165N/A tmpfile.puts(@content)
165N/A tmpfile.close
165N/A end
3935N/A
3935N/A should 'not have an ontology yet' do
3935N/A assert_equal @repository.ontologies.count, 0
165N/A end
165N/A
165N/A context "that doesn't exist" do
165N/A setup do
165N/A @version = @repository.save_file(@file_path, @target_path, @message, @user)
165N/A assert_queued OntologyVersion
165N/A end
165N/A
165N/A should 'create the file in the git repository' do
165N/A assert @repository.git.path_exists?(@target_path)
165N/A end
165N/A
4346N/A should 'create the file with correct contents in the git repository' do
4346N/A assert_equal @content, @repository.git.get_file(@target_path)[:content]
4346N/A end
4346N/A
4346N/A should 'create a new ontology with a default name' do
4346N/A assert_equal @repository.ontologies.count, 1
165N/A assert_equal @repository.ontologies.first.name, 'Save_file'
165N/A end
4346N/A
4346N/A should 'create a new ontology with only one version pointing to the commit' do
4346N/A o = @repository.ontologies.first
4346N/A assert_equal o.versions.count, 1
4346N/A assert_equal o.versions.first[:commit_oid], @version.commit_oid
4346N/A end
4346N/A
165N/A should 'create a new ontology with only one version belonging to the right user' do
165N/A v = @repository.ontologies.first.versions.first
165N/A assert_equal v.user, @user
165N/A end
165N/A end
165N/A
165N/A context 'that exists' do
165N/A setup do
165N/A @repository.save_file(@file_path, @target_path, @message, @user)
165N/A assert_queued OntologyVersion
165N/A
165N/A file = File.open(@file_path, 'w')
165N/A file.puts("#{@content}\n#{@content}")
3935N/A file.close
165N/A
165N/A @repository.save_file(@file_path, @target_path, @message, @user)
165N/A end
165N/A
165N/A teardown do
165N/A @repository.destroy
165N/A end
165N/A
3935N/A should 'create a new ontology version' do
3935N/A ontology = @repository.ontologies.where(basepath: File.real_basepath(@target_path)).first!
3935N/A assert_equal 2, ontology.versions.count
3935N/A end
3935N/A end
3935N/A end
3935N/A
3935N/A context 'browsing the repository' do
3935N/A setup do
3935N/A @files = {
3935N/A 'inroot1.clif' => "(In1 Root)\n",
3935N/A 'inroot2.clf' => "(In2 Root Too)\n",
3935N/A 'inroot2.clif' => "(In2 Root Too2)\n",
3935N/A 'folder1/file1.clif' => "(In Folder)\n",
3935N/A 'folder1/file2.clf' => "(In2 Folder Too)\n",
3935N/A 'folder2/file3.clf' => "(In2 Folder Again)\n"
3935N/A }
3935N/A @message = 'test message'
3935N/A @file_path_prefix = '/tmp/ontohub/test/git_repository/'
3935N/A @iri_prefix = 'http://localhost/repo_name/'
3935N/A @files.each do | path, content |
3935N/A file_path = "#{@file_path_prefix}#{path}"
3935N/A
3935N/A # create directory if it doesn't exist
3935N/A dir = File.dirname(file_path)
165N/A unless File.directory?(dir)
165N/A FileUtils.mkdir_p(dir)
181N/A end
165N/A
3996N/A # create file
3996N/A tmpfile = File.open(file_path, 'w')
3996N/A tmpfile.puts(content)
3996N/A tmpfile.close
3996N/A
3996N/A @repository.save_file(file_path, path, @message, @user, "#{@iri_prefix}#{file_path}")
3996N/A assert_queued OntologyVersion
3996N/A end
3996N/A end
3996N/A
3996N/A teardown do
@repository.destroy
end
should 'list files and directories in the directories' do
assert_equal @repository.path_info, @repository.path_info('/')
%w{/ folder1 folder2}.each do |folder|
assert_equal :dir, @repository.path_info(folder)[:type]
end
assert_equal 4, @repository.path_info('/')[:entries].size
assert_equal 2, @repository.path_info('folder1')[:entries].size
assert_equal 1, @repository.path_info('folder2')[:entries].size
path_infos_entries = @repository.path_info('/')[:entries]
assert_equal 4, path_infos_entries.size
selected_entry = path_infos_entries["folder1"]
assert_equal 1, selected_entry.size
selected_entry[0][:ontology] = !selected_entry[0][:ontology].nil?
assert_equal [{:type=>:dir, :name=>"folder1", :path=>"folder1", :index=>0, :ontology => false}], selected_entry
selected_entry = path_infos_entries["inroot2"]
assert_equal 2, selected_entry.size
selected_entry[0][:ontology] = !selected_entry[0][:ontology].nil?
selected_entry[1][:ontology] = !selected_entry[1][:ontology].nil?
assert_equal [
{:type=>:file, :name=>"inroot2.clf", :path=>"inroot2.clf", :index=>3, :ontology => true},
{:type=>:file, :name=>"inroot2.clif", :path=>"inroot2.clif", :index=>4, :ontology => true}
], selected_entry
path_infos_entries = @repository.path_info('folder2')[:entries]
assert_equal 1, path_infos_entries.size
selected_entry = path_infos_entries["file3"]
selected_entry[0][:ontology] = !selected_entry[0][:ontology].nil?
assert_equal([{:type=>:file, :name=>"file3.clf", :path=>"folder2/file3.clf", :index=>0, :ontology => true}], selected_entry)
end
should 'list files with given basename' do
expected = {
type: :file_base,
entry: {:type=>:file, :name=>"inroot1.clif", :path=>"inroot1.clif"}
}
assert_equal expected, @repository.path_info('inroot1')
expected = {
type: :file_base_ambiguous,
entries: [
{:type=>:file, :name=>"inroot2.clf", :path=>"inroot2.clf"},
{:type=>:file, :name=>"inroot2.clif", :path=>"inroot2.clif"}
]
}
assert_equal expected, @repository.path_info('inroot2')
end
should 'have type file on exact filename' do
@files.each do |path, content|
assert_equal :file, @repository.path_info(path)[:type]
assert_equal path.split('/')[-1], @repository.path_info(path)[:file][:name]
assert_equal content.size, @repository.path_info(path)[:file][:size]
assert_equal content, @repository.path_info(path)[:file][:content]
end
end
should 'be nil on non-existent file/folder' do
assert_nil @repository.path_info('dolfer1')
end
end
end
end