<commit_before># encoding: utf-8
require File.dirname(__FILE__) + '/../spec_helper'

describe 'SendGrid4r::REST::Templates::Versions' do
  before :all do
    Dotenv.load
    @client = SendGrid4r::Client.new(
      ENV['SENDGRID_USERNAME'], ENV['SENDGRID_PASSWORD']
    )
    @template_edit = 'version_test'
    @version1_name = 'version1_test'
    @version2_name = 'version2_test'
  end

  context 'always' do
    it 'is normal' do
      begin
        # celan up test env
        tmps = @client.get_templates
        tmps.templates.each do |tmp|
          next if tmp.name != @template_edit
          tmp.versions.each do |ver|
            @client.delete_version(tmp.id, ver.id)
          end
          @client.delete_template(tmp.id)
        end
        # post a template
        new_template = @client.post_template(@template_edit)
        expect(@template_edit).to eq(new_template.name)
        # post a version
        factory = SendGrid4r::Factory::VersionFactory.new
        ver1 = factory.create(name: @version1_name)
        ver1 = @client.post_version(new_template.id, ver1)
        # get the version
        actual = @client.get_version(new_template.id, ver1.id)
        expect(ver1.template_id).to eq(actual.template_id)
        expect(ver1.active).to eq(actual.active)
        expect(ver1.name).to eq(actual.name)
        expect(ver1.html_content).to eq(actual.html_content)
        expect(ver1.plain_content).to eq(actual.plain_content)
        expect(ver1.subject).to eq(actual.subject)
        # edit the version
        edit_ver1 = actual.dup
        edit_ver1.name = 'edit_version'
        edit_ver1.subject = 'edit<%subject%>edit'
        edit_ver1.html_content = 'edit<%body%>edit'
        edit_ver1.plain_content = 'edit<%body%>edit'
        edit_ver1.active = 0
        @client.patch_version(new_template.id, ver1.id, edit_ver1)
        # get the version
        actual = @client.get_version(new_template.id, ver1.id)
        expect(new_template.id).to eq(actual.template_id)
        expect(edit_ver1.active).to eq(actual.active)
        expect(edit_ver1.name).to eq(actual.name)
        expect(edit_ver1.html_content).to eq(actual.html_content)
        expect(edit_ver1.plain_content).to eq(actual.plain_content)
        expect(edit_ver1.subject).to eq(actual.subject)
        # post a version 2
        ver2 = factory.create(
          name: @version2_name,
          subject: '<%subject%>',
          html_content: '<%body%>',
          plain_content: '<%body%>'
        )
        ver2 = @client.post_version(new_template.id, ver2)
        # activate version 2
        @client.activate_version(new_template.id, ver2.id)
        actual_ver1 = @client.get_version(new_template.id, ver1.id)
        actual_ver2 = @client.get_version(new_template.id, ver2.id)
        expect(0).to eq(actual_ver1.active)
        expect(1).to eq(actual_ver2.active)
        # delete the version
        @client.delete_version(new_template.id, actual_ver1.id)
        @client.delete_version(new_template.id, actual_ver2.id)
        expect do
          @client.get_version(new_template.id, actual_ver1.id)
        end.to raise_error(RestClient::ResourceNotFound)
        expect do
          @client.get_version(new_template.id, actual_ver2.id)
        end.to raise_error(RestClient::ResourceNotFound)
        # delete the template
        @client.delete_template(new_template.id)
      rescue => e
        puts e.inspect
        raise e
      end
    end
  end
end<commit_msg>Update version_spec<commit_after># encoding: utf-8
require File.dirname(__FILE__) + '/../spec_helper'

describe 'SendGrid4r::REST::Templates::Versions' do
  before :all do
    Dotenv.load
    @client = SendGrid4r::Client.new(
      ENV['SENDGRID_USERNAME'], ENV['SENDGRID_PASSWORD']
    )
    @template_name = 'version_test'
    @version1_name = 'version_name1'
    @version2_name = 'version_name2'
    @factory = SendGrid4r::Factory::VersionFactory.new
  end

  context 'without block call' do
    before :all do
      begin
        # celan up test env
        tmps = @client.get_templates
        tmps.templates.each do |tmp|
          next if tmp.name != @template_name
          tmp.versions.each do |ver|
            @client.delete_version(tmp.id, ver.id)
          end
          @client.delete_template(tmp.id)
        end
        # post a template
        @template = @client.post_template(@template_name)
        # post a version
        ver1 = @factory.create(name: @version1_name)
        @version1 = @client.post_version(@template.id, ver1)
      rescue => e
        puts e.inspect
        raise e
      end
    end

    it '#post_version' do
      begin
        ver2 = @factory.create(name: @version2_name)
        version2 = @client.post_version(@template.id, ver2)
        expect(version2.name).to eq(@version2_name)
      rescue => e
        puts e.inspect
        raise e
      end
    end

    it '#get_version' do
      begin
        actual = @client.get_version(@template.id, @version1.id)
        expect(actual.template_id).to eq(@version1.template_id)
        expect(actual.active).to be_a(Fixnum)
        expect(actual.name).to eq(@version1.name)
        expect(actual.html_content).to eq(@version1.html_content)
        expect(actual.plain_content).to eq(@version1.plain_content)
        expect(actual.subject).to eq(@version1.subject)
      rescue => e
        puts e.inspect
        raise e
      end
    end

    it '#patch_version' do
      begin
        edit_ver1 = @version1.dup
        edit_ver1.name = 'edit_version'
        edit_ver1.subject = 'edit<%subject%>edit'
        edit_ver1.html_content = 'edit<%body%>edit'
        edit_ver1.plain_content = 'edit<%body%>edit'
        edit_ver1.active = 0
        @client.patch_version(@template.id, @version1.id, edit_ver1)
      rescue => e
        puts e.inspect
        raise e
      end
    end

    it '#delete_version' do
      begin
        @client.delete_version(@template.id, @version1.id)
      rescue => e
        puts e.inspect
        raise e
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10917826375082837, 'fraction_numerical': 0.0145791915175613, 'mean_word_length': 2.44774414620217, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 18, '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': '2952783', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1948, 'n_words': 358}