<commit_before>require 'rails_helper'

RSpec.describe Group, :type => :model do
  it { should have_many(:leaders) }

  it "should list orphaned groups as departments" do
    parent = create(:group, parent: nil)
    child = create(:group, parent: parent)
    expect(Group.departments.to_a).to eql([parent])
  end

  it "should calculate its level" do
    parent = Group.new
    child = Group.new(parent: parent)
    expect(parent.level).to eql(0)
    expect(child.level).to eql(1)
  end

  it "should know about its ancestors" do
    grandparent = create(:group, parent: nil)
    parent = create(:group, parent: grandparent)
    child = create(:group, parent: parent)
    grandchild = create(:group, parent: child)

    expect(grandchild.hierarchy).to eql([grandparent, parent, child, grandchild])
  end

  it "should know when it has no ancestors" do
    department = create(:group, parent: nil)

    expect(department.hierarchy).to eql([department])
  end

  describe '.leaf_node?' do
    let(:group) { create(:group) }

    it "should know when it is a leaf_node (no children)" do
      expect(group).to be_leaf_node
    end

    it "should know when it is not a leaf_node (no children)" do
      expect(group).to be_leaf_node
    end

  end

  describe '.assignable_people' do
    let(:group) { build(:group) }

    before do
      ['alice', 'bob', 'charlie'].each do |name|
         create(:person, surname: name)
      end
    end

    it 'should contain all people when a group has no memberships' do
      expect(group.assignable_people.length).to eql(3)
    end

    it 'should contain alice when bob and charlie are group members' do
      group.save!
      group.memberships.create(person: Person.find_by_surname('bob'))
      group.memberships.create(person:  Person.find_by_surname('charlie'))
      expect(group.assignable_people.first.surname).to eql('alice')
    end
  end

  describe '.deletable?' do
    let(:group) { create(:group) }

    it 'should only be detable when there are no memberships' do
      group.memberships.create(person: create(:person))
      expect(group).not_to be_deletable
    end

    it 'should not be detable when there are memberships' do
      group.memberships.create(person: create(:person))
      expect(group).not_to be_deletable
    end

    it 'should not be detable when there are children' do
      create(:group, parent: group)
      expect(group.reload).not_to be_deletable
    end

    it 'should be detable when there are unsaved memberships (e.g. in the groups#form)' do
      group.memberships.build
      expect(group).to be_deletable
    end
  end

  describe '.destroy' do
    let(:group) { create(:group) }

    it 'should persist the record and add an error message when it is not deletable' do
      allow(group).to receive(:deletable?).once.and_return(false)
      group.destroy
      expect(group.errors[:base].to_s).to include('cannot be deleted')
    end

    it 'should delete the record when it is deletable' do
      allow(group).to receive(:deletable?).once.and_return(true)
      group.destroy
      expect { Group.find(group) }.to raise_error(ActiveRecord::RecordNotFound)
    end
  end

  describe '.all_people' do
    let(:team) { create(:group) }
    let(:subteam) { create(:group, parent: team) }

    let(:alice) { create(:person, surname: 'alice') }
    let(:bob) { create(:person, surname: 'bob') }

    subject { team.all_people }

    context 'with bob in the team' do
      before { team.people << bob }

      it 'should have 1 membership' do
        expect(subject.length).to eq(1)
      end

      context 'and alice in the subteam' do
        before { subteam.people << alice }

        it 'should have 2 membership' do
          expect(subject.length).to eq(2)
        end

        context 'and bob also in the subteam' do
          before { subteam.people << alice }

          it 'should still have 2 memberships' do
            expect(subject.length).to eq(2)
          end

          it 'should return alice and bob in alphabetical order' do
            expect(subject.map(&:name)).to eql(['alice', 'bob'])
          end
        end
      end
    end
  end
end<commit_msg>Testing group#leadership gives 100% coverage<commit_after>require 'rails_helper'

RSpec.describe Group, :type => :model do
  it { should have_many(:leaders) }

  it "should list orphaned groups as departments" do
    parent = create(:group, parent: nil)
    child = create(:group, parent: parent)
    expect(Group.departments.to_a).to eql([parent])
  end

  it "should calculate its level" do
    parent = Group.new
    child = Group.new(parent: parent)
    expect(parent.level).to eql(0)
    expect(child.level).to eql(1)
  end

  it "should know about its ancestors" do
    grandparent = create(:group, parent: nil)
    parent = create(:group, parent: grandparent)
    child = create(:group, parent: parent)
    grandchild = create(:group, parent: child)

    expect(grandchild.hierarchy).to eql([grandparent, parent, child, grandchild])
  end

  it "should know when it has no ancestors" do
    department = create(:group, parent: nil)

    expect(department.hierarchy).to eql([department])
  end

  describe '.leaf_node?' do
    let(:group) { create(:group) }

    it "should know when it is a leaf_node (no children)" do
      expect(group).to be_leaf_node
    end

    it "should know when it is not a leaf_node (no children)" do
      expect(group).to be_leaf_node
    end

  end

  describe '.assignable_people' do
    let(:group) { build(:group) }

    before do
      ['alice', 'bob', 'charlie'].each do |name|
         create(:person, surname: name)
      end
    end

    it 'should contain all people when a group has no memberships' do
      expect(group.assignable_people.length).to eql(3)
    end

    it 'should contain alice when bob and charlie are group members' do
      group.save!
      group.memberships.create(person: Person.find_by_surname('bob'))
      group.memberships.create(person:  Person.find_by_surname('charlie'))
      expect(group.assignable_people.first.surname).to eql('alice')
    end
  end

  describe '.deletable?' do
    let(:group) { create(:group) }

    it 'should only be detable when there are no memberships' do
      group.memberships.create(person: create(:person))
      expect(group).not_to be_deletable
    end

    it 'should not be detable when there are memberships' do
      group.memberships.create(person: create(:person))
      expect(group).not_to be_deletable
    end

    it 'should not be detable when there are children' do
      create(:group, parent: group)
      expect(group.reload).not_to be_deletable
    end

    it 'should be detable when there are unsaved memberships (e.g. in the groups#form)' do
      group.memberships.build
      expect(group).to be_deletable
    end
  end

  describe '.destroy' do
    let(:group) { create(:group) }

    it 'should persist the record and add an error message when it is not deletable' do
      allow(group).to receive(:deletable?).once.and_return(false)
      group.destroy
      expect(group.errors[:base].to_s).to include('cannot be deleted')
    end

    it 'should delete the record when it is deletable' do
      allow(group).to receive(:deletable?).once.and_return(true)
      group.destroy
      expect { Group.find(group) }.to raise_error(ActiveRecord::RecordNotFound)
    end
  end

  describe '.all_people' do
    let(:team) { create(:group) }
    let(:subteam) { create(:group, parent: team) }

    let(:alice) { create(:person, surname: 'alice') }
    let(:bob) { create(:person, surname: 'bob') }

    subject { team.all_people }

    context 'with bob in the team' do
      before { team.people << bob }

      it 'should have 1 membership' do
        expect(subject.length).to eq(1)
      end

      context 'and alice in the subteam' do
        before { subteam.people << alice }

        it 'should have 2 membership' do
          expect(subject.length).to eq(2)
        end

        context 'and bob also in the subteam' do
          before { subteam.people << alice }

          it 'should still have 2 memberships' do
            expect(subject.length).to eq(2)
          end

          it 'should return alice and bob in alphabetical order' do
            expect(subject.map(&:name)).to eql(['alice', 'bob'])
          end
        end
      end
    end
  end

  describe '.leadership' do
    it 'should get the first leader' do
      group = create(:group)
      leaderships = [ create(:membership, group: group, leader: true) ]
      expect(group.leadership).to eql(leaderships.first)
    end
  end
end
{'fraction_non_alphanumeric': 0.10666976528003719, 'fraction_numerical': 0.0024401580292818963, 'mean_word_length': 2.880522993688007, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, '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': '891539', 'n_tokens_mistral': 2715, 'n_tokens_neox': 2581, 'n_words': 813}