<commit_before>require 'test_helper'

class RepositoryTest < ActiveSupport::TestCase
  
  should have_many :ontologies
  should have_many :permissions

  context "a repository" do
    setup do
      @user       = FactoryGirl.create :user
      @repository = FactoryGirl.create :repository, user: @user
    end

    teardown do
      @repository.destroy
    end

    context 'with a reserved name' do
      should 'not be valid' do
        repository = FactoryGirl.build :repository, user: @user, name: 'repositories'
        assert repository.invalid?
        assert repository.errors[:name].any?
      end
    end

    context 'creating a permission' do
      setup do
        assert_not_nil @permission = @repository.permissions.first
      end
      
      should 'with subject' do
        assert_equal @user, @permission.subject
      end
      
      should 'with role owner' do
        assert_equal 'owner', @permission.role
      end
    end

    context 'made private' do
      setup do
        @repository.is_private = true
        @repository.save

        editor = FactoryGirl.create :user
        readers = [FactoryGirl.create(:user), FactoryGirl.create(:user), FactoryGirl.create(:user)]

        FactoryGirl.create(:permission, subject: editor, role: 'editor', item: @repository)
        readers.each { |r| FactoryGirl.create(:permission, subject: r, role: 'reader', item: @repository) }
      end

      should 'not clear reader premissions when saved, but not set public' do
        assert_equal 3, @repository.permissions.where(role: 'reader').count
        @repository.name += "_foo"
        @repository.save
        assert_equal 3, @repository.permissions.where(role: 'reader').count
      end

      should 'clear reader premissions when set public' do
        assert_equal 3, @repository.permissions.where(role: 'reader').count
        @repository.is_private = false
        @repository.save
        assert_equal 0, @repository.permissions.where(role: 'reader').count
      end
    end

    context 'saving a file' do
      setup do 
        @file_path = '/tmp/ontohub/test/git_repository/save_file.txt'
        @target_path = 'save_file.clif'
        @message = 'test message'
        @content = "(Cat x)\n"

        # create directory if it doesn't exist
        dir = File.dirname(@file_path)
        unless File.directory?(dir)
          FileUtils.mkdir_p(dir)<commit_msg>Quick fix for repository test for now.<commit_after>require 'test_helper'

class RepositoryTest < ActiveSupport::TestCase
  
  should have_many :ontologies
  should have_many :permissions

  context "a repository" do
    setup do
      @user       = FactoryGirl.create :user
      @repository = FactoryGirl.create :repository, user: @user
    end

    teardown do
      @repository.destroy
    end

    context 'with a reserved name' do
      should 'not be valid' do
        repository = FactoryGirl.build :repository, user: @user, name: 'repositories'
        assert repository.invalid?
        assert repository.errors[:name].any?
      end
    end

    context 'creating a permission' do
      setup do
        assert_not_nil @permission = @repository.permissions.first
      end
      
      should 'with subject' do
        assert_equal @user, @permission.subject
      end
      
      should 'with role owner' do
        assert_equal 'owner', @permission.role
      end
    end

    context 'made private' do
      setup do
        @repository.access = 'private'
        @repository.save

        editor = FactoryGirl.create :user
        readers = [FactoryGirl.create(:user), FactoryGirl.create(:user), FactoryGirl.create(:user)]

        FactoryGirl.create(:permission, subject: editor, role: 'editor', item: @repository)
        readers.each { |r| FactoryGirl.create(:permission, subject: r, role: 'reader', item: @repository) }
      end

      should 'not clear reader premissions when saved, but not set public' do
        assert_equal 3, @repository.permissions.where(role: 'reader').count
        @repository.name += "_foo"
        @repository.save
        assert_equal 3, @repository.permissions.where(role: 'reader').count
      end

      should 'clear reader premissions when set public' do
        assert_equal 3, @repository.permissions.where(role: 'reader').count
        @repository.access = 'public_r'
        @repository.save
        assert_equal 0, @repository.permissions.where(role: 'reader').count
      end
    end

    context 'saving a file' do
      setup do 
        @file_path = '/tmp/ontohub/test/git_repository/save_file.txt'
        @target_path = 'save_file.clif'
        @message = 'test message'
        @content = "(Cat x)\n"

        # create directory if it doesn't exist
        dir = File.dirname(@file_path)
        unless File.directory?(dir)
          FileUtils.mkdir_p(dir)
{'fraction_non_alphanumeric': 0.09437875959344534, 'fraction_numerical': 0.0016594067620825555, 'mean_word_length': 2.5612998522895127, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2825344', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1311, 'n_words': 383}