<commit_before>#encoding: utf-8

require 'spec_helper'

describe 'usermacro' do
  before :all do
    @hostgroup = gen_name 'hostgroup'
    @hostgroupid = zbx.hostgroups.create(:name => @hostgroup)
    @template = gen_name 'template'
    @templateid = zbx.templates.create(
      :host => @template,
      :groups => [:groupid => @hostgroupid]
    )
  end

  context 'when hostmacro not exists' do
    before do
      @hostmacro = gen_name 'hostmacro'
    end

    describe 'create' do
      it "should return integer id" do
        hostmacroid = zbx.usermacros.create(
          :macro => @hostmacro,
          :value => "public",
          :hostid => @templateid
        )
        expect(hostmacroid).to be_kind_of(Integer)
      end
    end

    describe 'get_id' do
      it "should return nil" do
        expect(zbx.usermacros.get_id(:macro => @hostmacro)).to be_kind_of(NilClass)
      end
    end
  end

  context 'when hostmacro exists' do
    before :all do
      @hostmacro = gen_name 'hostmacro'
      @hostmacroid = zbx.usermacros.create(
        :macro => @hostmacro,
        :value => "public",
        :hostid => @templateid
      )
    end

    describe 'get_or_create' do
      it "should return id of hostmacro" do
        expect(zbx.usermacros.get_or_create(
          :macro => @hostmacro,
          :value => "public",
          :hostid => @templateid
        )).to eq @hostmacroid
      end
    end

    describe 'get_full_data' do
      it "should contains created hostmacro" do
        expect(zbx.usermacros.get_full_data(:macro => @hostmacro)[0]).to include("macro" => @hostmacro)
      end
    end

    describe 'get_id' do
      it "should return id of hostmacro" do
        expect(zbx.usermacros.get_id(:macro => @hostmacro)).to eq @hostmacroid
      end
    end

    it "should raise error on no identity given" do
        expect { zbx.usermacros.get_id({}) }.to raise_error(ZabbixApi::ApiError)
    end

    describe 'update' do
      it "should return id" do
        expect(zbx.usermacros.update(
          :hostmacroid => zbx.usermacros.get_id(:macro => @hostmacro),
          :value => "private",
        )).to eq @hostmacroid
      end
    end

    describe 'create_or_update' do
      it "should update existing usermacro" do
        expect(zbx.usermacros.create_or_update(
          :macro => @hostmacro,
          :value => "public",
          :hostid => @templateid
        )).to eq @hostmacroid
      end

      it "should create usermacro" do
        new_hostmacro_id = zbx.usermacros.create_or_update(
          :macro => @hostmacro + "____1",
          :value => "public",
          :hostid => @templateid
        )

        expect(new_hostmacro_id).to be_kind_of(Integer)
        expect(new_hostmacro_id).to be > @hostmacroid
      end
    end

    describe 'delete' do
      before :all do
        @result = zbx.usermacros.delete(@hostmacroid)
      end

      it "should return deleted id" do
        expect(@result).to eq @hostmacroid
      end

      it "should delete item from zabbix" do
        expect(zbx.usermacros.get_id(:macro => @hostmacro)).to be_nil
      end
    end
  end
  
  context 'when globalmacro not exists' do
    before do
      @globalmacro = gen_name 'globalmacro'
    end

    describe 'create_global' do
      it "should return integer id" do
        globalmacroid = zbx.usermacros.create_global(
          :macro => @globalmacro,
          :value => "public",
          :hostid => @templateid
        )
        expect(globalmacroid).to be_kind_of(Integer)
      end
    end

    describe 'get_id' do
      it "should return nil" do
        expect(zbx.usermacros.get_id(:macro => @globalmacro)).to be_kind_of(NilClass)
      end
    end
  end

  context 'when globalmacro exists' do
    before :all do
      @globalmacro = gen_name 'globalmacro'
      @globalmacroid = zbx.usermacros.create_global(
        :macro => @globalmacro,
        :value => "public",
        :hostid => @templateid
      )
    end

    describe 'get_full_data' do
      it "should contains created globalmacro" do
        expect(zbx.usermacros.get_full_data(:macro => @globalmacro)[0]).to include("macro" => @globalmacro)<commit_msg>fix for invalid macro syntax in new usermacro spec<commit_after>#encoding: utf-8

require 'spec_helper'

describe 'usermacro' do
  before :all do
    @hostgroup = gen_name 'hostgroup'
    @hostgroupid = zbx.hostgroups.create(:name => @hostgroup)
    @template = gen_name 'template'
    @templateid = zbx.templates.create(
      :host => @template,
      :groups => [:groupid => @hostgroupid]
    )
  end

  context 'when hostmacro not exists' do
    before do
      @hostmacro = '{$' + gen_name 'hostmacro' + '}'
    end

    describe 'create' do
      it "should return integer id" do
        hostmacroid = zbx.usermacros.create(
          :macro => @hostmacro,
          :value => "public",
          :hostid => @templateid
        )
        expect(hostmacroid).to be_kind_of(Integer)
      end
    end

    describe 'get_id' do
      it "should return nil" do
        expect(zbx.usermacros.get_id(:macro => @hostmacro)).to be_kind_of(NilClass)
      end
    end
  end

  context 'when hostmacro exists' do
    before :all do
      @hostmacro = '{$' + gen_name 'hostmacro' + '}'
      @hostmacroid = zbx.usermacros.create(
        :macro => @hostmacro,
        :value => "public",
        :hostid => @templateid
      )
    end

    describe 'get_or_create' do
      it "should return id of hostmacro" do
        expect(zbx.usermacros.get_or_create(
          :macro => @hostmacro,
          :value => "public",
          :hostid => @templateid
        )).to eq @hostmacroid
      end
    end

    describe 'get_full_data' do
      it "should contains created hostmacro" do
        expect(zbx.usermacros.get_full_data(:macro => @hostmacro)[0]).to include("macro" => @hostmacro)
      end
    end

    describe 'get_id' do
      it "should return id of hostmacro" do
        expect(zbx.usermacros.get_id(:macro => @hostmacro)).to eq @hostmacroid
      end
    end

    it "should raise error on no identity given" do
        expect { zbx.usermacros.get_id({}) }.to raise_error(ZabbixApi::ApiError)
    end

    describe 'update' do
      it "should return id" do
        expect(zbx.usermacros.update(
          :hostmacroid => zbx.usermacros.get_id(:macro => @hostmacro),
          :value => "private",
        )).to eq @hostmacroid
      end
    end

    describe 'create_or_update' do
      it "should update existing usermacro" do
        expect(zbx.usermacros.create_or_update(
          :macro => @hostmacro,
          :value => "public",
          :hostid => @templateid
        )).to eq @hostmacroid
      end

      it "should create usermacro" do
        new_hostmacro_id = zbx.usermacros.create_or_update(
          :macro => @hostmacro + "____1",
          :value => "public",
          :hostid => @templateid
        )

        expect(new_hostmacro_id).to be_kind_of(Integer)
        expect(new_hostmacro_id).to be > @hostmacroid
      end
    end

    describe 'delete' do
      before :all do
        @result = zbx.usermacros.delete(@hostmacroid)
      end

      it "should return deleted id" do
        expect(@result).to eq @hostmacroid
      end

      it "should delete item from zabbix" do
        expect(zbx.usermacros.get_id(:macro => @hostmacro)).to be_nil
      end
    end
  end
  
  context 'when globalmacro not exists' do
    before do
      @globalmacro = '{$' + gen_name 'globalmacro' + '}'
    end

    describe 'create_global' do
      it "should return integer id" do
        globalmacroid = zbx.usermacros.create_global(
          :macro => @globalmacro,
          :value => "public",
          :hostid => @templateid
        )
        expect(globalmacroid).to be_kind_of(Integer)
      end
    end

    describe 'get_id' do
      it "should return nil" do
        expect(zbx.usermacros.get_id(:macro => @globalmacro)).to be_kind_of(NilClass)
      end
    end
  end

  context 'when globalmacro exists' do
    before :all do
      @globalmacro = '{$' + gen_name 'globalmacro' + '}'
      @globalmacroid = zbx.usermacros.create_global(
        :macro => @globalmacro,
        :value => "public",
        :hostid => @templateid
      )
    end

    describe 'get_full_data' do
      it "should contains created globalmacro" do
        expect(zbx.usermacros.get_full_data(:macro => @globalmacro)[0]).to include("macro" => @globalmacro)
{'fraction_non_alphanumeric': 0.10773055522670771, 'fraction_numerical': 0.000947081804190837, 'mean_word_length': 2.2910011686793923, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 77, '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': '3259352', 'n_tokens_mistral': 2851, 'n_tokens_neox': 2686, 'n_words': 673}