<commit_before>require 'rails_helper'
require "#{Rails.root}/lib/importers/course_importer"
require "#{Rails.root}/lib/importers/revision_importer"

describe Revision do
  describe '#update' do
    it 'should update a revision with new data' do
      revision = build(:revision,
                       id: 1,
                       article_id: 1,
                       views: 1000
      )
      revision.update(
        user_id: 1,
        views: 9000
      )
      expect(revision.views).to eq(9000)
      expect(revision.user_id).to eq(1)
    end
  end

  describe '.update_all_revisions' do
    it 'should fetch revisions for existing courses' do
      VCR.use_cassette 'revisions/update_all_revisions' do
        # Try it with no courses.
        RevisionImporter.update_all_revisions
        expect(Revision.all.count).to eq(0)

        # Now add a course with users
        VCR.use_cassette 'wiki/course_data' do
          CourseImporter.update_all_courses(false, hash: '351')
        end

        RevisionImporter.update_all_revisions nil, true
        # When the non-students are included, Revisions count is 1919.
        expect(Revision.all.count).to eq(433)
        # When the non-students are included, ArticlesCourses count is 224.
        expect(ArticlesCourses.all.count).to eq(10)

        # Update the revision counts, then try update_all_revisions again
        # to test how it handles old users.
        User.update_all_caches(Course.find(351).users)
        RevisionImporter.update_all_revisions nil, true
        expect(Revision.all.count).to eq(433)
        expect(ArticlesCourses.all.count).to eq(10)
      end
    end

    it 'should not fail with returning students' do
      VCR.use_cassette 'revisions/returning_students' do
        # Create a user who has a revision from long ago
        create(:trained) # This is user 319203, with edits since 2015. 
        create(:revision,
               user_id: 319203,
               date: '2003-03-01'.to_date)
        # Create a recent course and add the user to it.
        create(:course,
               id: 1,
               start: '2015-01-01'.to_date,
               end: '2030-01-01'.to_date)
        create(:courses_user,
               course_id: 1,
               user_id: 319203,
               role: 0)
        User.update_all_caches(Course.find(1).users)
        RevisionImporter.update_all_revisions nil, true
        expect(Revision.all.count > 1).to be true        
      end
    end
  end
end<commit_msg>Update Revision spec to account for additional namespaces<commit_after>require 'rails_helper'
require "#{Rails.root}/lib/importers/course_importer"
require "#{Rails.root}/lib/importers/revision_importer"

describe Revision do
  describe '#update' do
    it 'should update a revision with new data' do
      revision = build(:revision,
                       id: 1,
                       article_id: 1,
                       views: 1000
      )
      revision.update(
        user_id: 1,
        views: 9000
      )
      expect(revision.views).to eq(9000)
      expect(revision.user_id).to eq(1)
    end
  end

  describe '.update_all_revisions' do
    it 'should fetch revisions for existing courses' do
      VCR.use_cassette 'revisions/update_all_revisions' do
        # Try it with no courses.
        RevisionImporter.update_all_revisions
        expect(Revision.all.count).to eq(0)

        # Now add a course with users
        VCR.use_cassette 'wiki/course_data' do
          CourseImporter.update_all_courses(false, hash: '351')
        end

        RevisionImporter.update_all_revisions nil, true
        # When the non-students are included, Revisions count is 1919.
        expect(Revision.all.count).to eq(519)
        # When the non-students are included, ArticlesCourses count is 224.
        expect(ArticlesCourses.all.count).to eq(10)

        # Update the revision counts, then try update_all_revisions again
        # to test how it handles old users.
        User.update_all_caches(Course.find(351).users)
        RevisionImporter.update_all_revisions nil, true
        expect(Revision.all.count).to eq(519)
        expect(ArticlesCourses.all.count).to eq(10)
      end
    end

    it 'should not fail with returning students' do
      VCR.use_cassette 'revisions/returning_students' do
        # Create a user who has a revision from long ago
        create(:trained) # This is user 319203, with edits since 2015. 
        create(:revision,
               user_id: 319203,
               date: '2003-03-01'.to_date)
        # Create a recent course and add the user to it.
        create(:course,
               id: 1,
               start: '2015-01-01'.to_date,
               end: '2030-01-01'.to_date)
        create(:courses_user,
               course_id: 1,
               user_id: 319203,
               role: 0)
        User.update_all_caches(Course.find(1).users)
        RevisionImporter.update_all_revisions nil, true
        expect(Revision.all.count > 1).to be true        
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08267090620031796, 'fraction_numerical': 0.03616852146263911, 'mean_word_length': 2.114480198019802, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4642625', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1481, 'n_words': 398}