<commit_before># encoding: utf-8

#
# Copyright (C) 2014 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

require_relative '../../../../../spec/spec_helper'
require_relative '../spec_helper'

describe Analytics::Department do

  before :each do
    @account = Account.default
    @account.sub_accounts.create!(name: "Some department")
    @account.sub_accounts.create!(name: "Some other department")
    account_admin_user

    @acct_statistics = Analytics::Department.new(@user, @account, @account.default_enrollment_term, "current")
  end

  describe "account level statistics" do
    it "should return number of subaccounts" do
      expect(@acct_statistics.statistics[:subaccounts]).to eq 2
      expect(@acct_statistics.statistics_by_subaccount.size).to eq 3
    end

    it "should return the number of courses, across all subaccounts" do
      course_shim(account: @account, active_course: true)
      course_shim(account: @account.sub_accounts.first, active_course: true)
      expect(@acct_statistics.statistics[:courses]).to eq 2
    end

    it "should not count courses that are crosslisted" do
      c1 = course_shim(account: @account.sub_accounts.first, active_all: true)
      c2 = course_shim(account: @account.sub_accounts.second, active_all: true)
      c2.course_sections.create!({ :name => "section 2" })
      c2.course_sections.first.crosslist_to_course(c1)
      lst = @acct_statistics.statistics_by_subaccount
      expect(lst[0][:courses]).to eq 0
      expect(lst[1][:courses]).to eq 1
      expect(lst[2][:courses]).to eq 1
    end

    it "should return the number of courses, grouped by subaccount" do
      course_shim(account: @account, active_course: true)
      course_shim(account: @account.sub_accounts.first, active_course: true)
      course_shim(account: @account.sub_accounts.second, active_course: true)
      course_shim(account: @account.sub_accounts.second, active_course: true)
      lst = @acct_statistics.statistics_by_subaccount
      expect(lst[0][:courses]).to eq 1
      expect(lst[1][:courses]).to eq 1
      expect(lst[2][:courses]).to eq 2
    end

    it "should return the number of teachers and students, across all subaccounts" do
      c1 = course_shim(account: @account, active_all: true)
      c2 = course_shim(account: @account.sub_accounts.first, active_all: true)
      s1 = student_in_course(course: c1, active_all: true).user
      student_in_course(course: c2, active_all: true)
      student_in_course(course: c2, user: s1, active_all: true) # enroll student in both courses
      hsh = @acct_statistics.statistics
      expect(hsh[:teachers]).to eq 2
      expect(hsh[:students]).to eq 2
    end

    it "should return the number of teachers and students, grouped by subaccount" do
      c1 = course_shim(account: @account, active_all: true)
      c2 = course_shim(account: @account.sub_accounts.first, active_all: true)
      c3 = course_shim(account: @account.sub_accounts.second, active_all: true)
      c4 = course_shim(account: @account.sub_accounts.second, active_all: true)
      student_in_course(course: c1, active_all: true)
      student_in_course(course: c2, active_all: true)
      student_in_course(course: c3, active_all: true)
      3.times do
        student_in_course(course: c4, active_all: true)
      end
      lst = @acct_statistics.statistics_by_subaccount
      expect(lst[0][:teachers]).to eq 1
      expect(lst[1][:teachers]).to eq 1
      expect(lst[2][:teachers]).to eq 2
      expect(lst[0][:students]).to eq 1
      expect(lst[1][:students]).to eq 1<commit_msg>spec: fix flaky order comparison<commit_after># encoding: utf-8

#
# Copyright (C) 2014 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

require_relative '../../../../../spec/spec_helper'
require_relative '../spec_helper'

describe Analytics::Department do

  before :each do
    @account = Account.default
    @account.sub_accounts.create!(name: "Some department")
    @account.sub_accounts.create!(name: "Some other department")
    account_admin_user

    @acct_statistics = Analytics::Department.new(@user, @account, @account.default_enrollment_term, "current")
  end

  describe "account level statistics" do
    it "should return number of subaccounts" do
      expect(@acct_statistics.statistics[:subaccounts]).to eq 2
      expect(@acct_statistics.statistics_by_subaccount.size).to eq 3
    end

    it "should return the number of courses, across all subaccounts" do
      course_shim(account: @account, active_course: true)
      course_shim(account: @account.sub_accounts.first, active_course: true)
      expect(@acct_statistics.statistics[:courses]).to eq 2
    end

    it "should not count courses that are crosslisted" do
      c1 = course_shim(account: @account.sub_accounts.first, active_all: true)
      c2 = course_shim(account: @account.sub_accounts.second, active_all: true)
      c2.course_sections.create!({ :name => "section 2" })
      c2.course_sections.first.crosslist_to_course(c1)
      lst = @acct_statistics.statistics_by_subaccount.sort_by{|x| x[:id]}
      expect(lst[0][:courses]).to eq 0
      expect(lst[1][:courses]).to eq 1
      expect(lst[2][:courses]).to eq 1
    end

    it "should return the number of courses, grouped by subaccount" do
      course_shim(account: @account, active_course: true)
      course_shim(account: @account.sub_accounts.first, active_course: true)
      course_shim(account: @account.sub_accounts.second, active_course: true)
      course_shim(account: @account.sub_accounts.second, active_course: true)
      lst = @acct_statistics.statistics_by_subaccount.sort_by{|x| x[:id]}
      expect(lst[0][:courses]).to eq 1
      expect(lst[1][:courses]).to eq 1
      expect(lst[2][:courses]).to eq 2
    end

    it "should return the number of teachers and students, across all subaccounts" do
      c1 = course_shim(account: @account, active_all: true)
      c2 = course_shim(account: @account.sub_accounts.first, active_all: true)
      s1 = student_in_course(course: c1, active_all: true).user
      student_in_course(course: c2, active_all: true)
      student_in_course(course: c2, user: s1, active_all: true) # enroll student in both courses
      hsh = @acct_statistics.statistics
      expect(hsh[:teachers]).to eq 2
      expect(hsh[:students]).to eq 2
    end

    it "should return the number of teachers and students, grouped by subaccount" do
      c1 = course_shim(account: @account, active_all: true)
      c2 = course_shim(account: @account.sub_accounts.first, active_all: true)
      c3 = course_shim(account: @account.sub_accounts.second, active_all: true)
      c4 = course_shim(account: @account.sub_accounts.second, active_all: true)
      student_in_course(course: c1, active_all: true)
      student_in_course(course: c2, active_all: true)
      student_in_course(course: c3, active_all: true)
      3.times do
        student_in_course(course: c4, active_all: true)
      end
      lst = @acct_statistics.statistics_by_subaccount.sort_by{|x| x[:id]}
      expect(lst[0][:teachers]).to eq 1
      expect(lst[1][:teachers]).to eq 1
      expect(lst[2][:teachers]).to eq 2
      expect(lst[0][:students]).to eq 1
      expect(lst[1][:students]).to eq 1
{'fraction_non_alphanumeric': 0.10610932475884244, 'fraction_numerical': 0.013099916636894129, 'mean_word_length': 3.91398478642481, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2357564', 'n_tokens_mistral': 2725, 'n_tokens_neox': 2641, 'n_words': 746}