repository_test.rb revision 7097237ffbd7227b30272b307b6e48c78b22ecb1
c4bdc37c9de27abfa1d5be18008953b32457e6c4Eugen Kuksarequire 'test_helper'
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksaclass RepositoryTest < ActiveSupport::TestCase
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa
c4bdc37c9de27abfa1d5be18008953b32457e6c4Eugen Kuksa should have_many :ontologies
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa should have_many :permissions
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa
f9328b75002f0a5584b4ec930536fc43e5b56fdcEugen Kuksa context "a repository" do
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa setup do
c4bdc37c9de27abfa1d5be18008953b32457e6c4Eugen Kuksa @user = FactoryGirl.create :user
c4bdc37c9de27abfa1d5be18008953b32457e6c4Eugen Kuksa @repository = FactoryGirl.create :repository, user: @user
c4bdc37c9de27abfa1d5be18008953b32457e6c4Eugen Kuksa end
c4bdc37c9de27abfa1d5be18008953b32457e6c4Eugen Kuksa
c4bdc37c9de27abfa1d5be18008953b32457e6c4Eugen Kuksa context 'creating a permission' do
c4bdc37c9de27abfa1d5be18008953b32457e6c4Eugen Kuksa setup do
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa assert_not_nil @permission = @repository.permissions.first
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa end
82b684a930d8914af8f829c4dc6687c698d82169Eugen Kuksa
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa should 'with subject' do
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa assert_equal @user, @permission.subject
f9328b75002f0a5584b4ec930536fc43e5b56fdcEugen Kuksa end
f9328b75002f0a5584b4ec930536fc43e5b56fdcEugen Kuksa
e0ae31a48b304a53c4280b5b3b50a6dfba6d448aEugen Kuksa should 'with role owner' do
e0ae31a48b304a53c4280b5b3b50a6dfba6d448aEugen Kuksa assert_equal 'owner', @permission.role
e0ae31a48b304a53c4280b5b3b50a6dfba6d448aEugen Kuksa end
e0ae31a48b304a53c4280b5b3b50a6dfba6d448aEugen Kuksa end
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa context 'saving a file' do
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa setup do
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa @file_path = '/tmp/ontohub/test/git_repository/save_file.txt'
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa @target_path = 'save_file.clif'
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa @message = 'test message'
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa @content = "(Cat x)\n"
44f41210d9e3ab001acbbe8df9b39445ac00b5ddEugen Kuksa
44f41210d9e3ab001acbbe8df9b39445ac00b5ddEugen Kuksa # create directory if it doesn't exist
44f41210d9e3ab001acbbe8df9b39445ac00b5ddEugen Kuksa dir = File.dirname(@file_path)
44f41210d9e3ab001acbbe8df9b39445ac00b5ddEugen Kuksa unless File.directory?(dir)
44f41210d9e3ab001acbbe8df9b39445ac00b5ddEugen Kuksa FileUtils.mkdir_p(dir)
44f41210d9e3ab001acbbe8df9b39445ac00b5ddEugen Kuksa end
8d4c6e7397e88fade190ad107583c30dc6b34afcEugen Kuksa
c4bdc37c9de27abfa1d5be18008953b32457e6c4Eugen Kuksa # create file
tmpfile = File.open(@file_path, 'w')
tmpfile.puts(@content)
tmpfile.close
end
should 'not have an ontology yet' do
assert_equal @repository.ontologies.count, 0
end
context 'that doesn\'t exist' do
setup do
OntologyVersion.any_instance.expects(:parse_async).once
@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
OntologyVersion.any_instance.expects(:parse_async).twice
@repository.save_file(@file_path, @target_path, @message, @user)
file = File.open(@file_path, 'w')
file.puts("#{@content}\n#{@content}")
file.close
@repository.save_file(@file_path, @target_path, @message, @user)
end
should 'create a new ontology version' do
ontology = @repository.ontologies.where(path: @target_path).first!
assert_equal 2, ontology.versions.count
end
end
end
end
end