<commit_before>require "spec_helper"

describe DashboardSynchronizer do
  let(:fixtures) { File.join(File.dirname(__FILE__), "fixtures") }
  let(:logger) { Logger.new("/dev/null") }
  let(:synchronizer) { DashboardSynchronizer.new(File.join(fixtures, "config.yml"), "prod", logger) }
  let(:template) { File.join(fixtures, "dashboard_templates", "some_dashboard.json.erb") }

  describe "#new" do
    it "uses the right credentials" do
      allow(Dogapi::Client).to receive(:new).with("my_api_key", "my_app_key")

      synchronizer
    end
  end

  describe "#fetch_from_datadog" do
    let(:dashboards) do
      ["200",
       {"dashes" =>
        [{"title" => "My Dashboard",
          "resource" => "/api/v1/dash/2473",
          "id" => "2473",
          "description" => "An informative dashboard."},
          {"title" => "My First Metrics",
           "resource" => "/api/v1/dash/2552",
           "id" => "2552",
           "description" => "And they are marvelous."}
        ]}]
    end

    it "returns dashboards from datadog" do
      allow_any_instance_of(Dogapi::Client).to receive(:get_dashboards).and_return(dashboards)
      expect(synchronizer.fetch_from_datadog).to eq({"My Dashboard" => "2473", "My First Metrics" => "2552"})
    end

    it "retries in times of trouble" do
      allow_any_instance_of(Dogapi::Client).to receive(:get_dashboards).and_return([-1, {}], dashboards)

      expect(synchronizer.fetch_from_datadog).to eq({"My Dashboard" => "2473", "My First Metrics" => "2552"})
    end
  end

  describe "#process_template" do
    it "processes a template" do
      dash = synchronizer.process_template(template)
      expect(dash['title']).to eq("prod Some Dashboard")
      expect(dash['description']).to eq("Some description")
      expect(dash['graphs']).to eq([{
        "deployment" => "some-deployment",
        "services_deployment" => "services-deployment",
        "bosh_deployment" => "some-bosh-deployment"
      }])
      expect(dash['template_variables']).to match_array([{
        "default" => "*",
        "name" => "somename",
        "prefix" => "someprefix",
      }])
      expect(dash['gobbledygook_events'][0]["q"]).to eq("tags:deployment:prod start_deploy")
    end
  end

  describe "unknown_dashboard" do
    context "when there are no unknown dashboards" do
      it "returns an empty list of names" do
        allow(synchronizer).to receive(:fetch_from_datadog).and_return({"prod Some Dashboard" => 123})

        expect(synchronizer.unknown_dashboard_names([template])).to be_empty
      end

      it "returns an empty list of ids" do
        allow(synchronizer).to receive(:fetch_from_datadog).and_return({"prod Some Dashboard" => 123})

        expect(synchronizer.unknown_dashboard_ids([template])).to be_empty
      end
<commit_msg>Fix broken test.<commit_after>require "spec_helper"

describe DashboardSynchronizer do
  let(:fixtures) { File.join(File.dirname(__FILE__), "fixtures") }
  let(:logger) { Logger.new("/dev/null") }
  let(:synchronizer) { DashboardSynchronizer.new(File.join(fixtures, "config.yml"), "prod", logger) }
  let(:template) { File.join(fixtures, "dashboard_templates", "some_dashboard.json.erb") }

  describe "#new" do
    it "uses the right credentials" do
      allow(Dogapi::Client).to receive(:new).with("my_api_key", "my_app_key")

      synchronizer
    end
  end

  describe "#fetch_from_datadog" do
    let(:dashboards) do
      ["200",
       {"dashes" =>
        [{"title" => "My Dashboard",
          "resource" => "/api/v1/dash/2473",
          "id" => "2473",
          "description" => "An informative dashboard."},
          {"title" => "My First Metrics",
           "resource" => "/api/v1/dash/2552",
           "id" => "2552",
           "description" => "And they are marvelous."}
        ]}]
    end

    it "returns dashboards from datadog" do
      allow_any_instance_of(Dogapi::Client).to receive(:get_dashboards).and_return(dashboards)
      expect(synchronizer.fetch_from_datadog).to eq({"My Dashboard" => "2473", "My First Metrics" => "2552"})
    end

    it "retries in times of trouble" do
      allow_any_instance_of(Dogapi::Client).to receive(:get_dashboards).and_return([-1, {}], dashboards)

      expect(synchronizer.fetch_from_datadog).to eq({"My Dashboard" => "2473", "My First Metrics" => "2552"})
    end
  end

  describe "#process_template" do
    it "processes a template" do
      dash = synchronizer.process_template(template)
      expect(dash['title']).to eq("prod Some Dashboard")
      expect(dash['description']).to eq("Some description")
      expect(dash['graphs']).to eq([{
        "deployment" => "some-deployment",
        "services_deployment" => "services-deployment",
        "bosh_deployment" => "some-bosh-deployment"
      }])
      expect(dash['template_variables']).to match_array([{
        "default" => "*",
        "name" => "somename",
        "prefix" => "someprefix",
      }])
      expect(dash['gobbledygook_events'][0]["q"]).to eq("tags:deployment:prod start_deploy OR tags:deployment:prod end_deploy")
    end
  end

  describe "unknown_dashboard" do
    context "when there are no unknown dashboards" do
      it "returns an empty list of names" do
        allow(synchronizer).to receive(:fetch_from_datadog).and_return({"prod Some Dashboard" => 123})

        expect(synchronizer.unknown_dashboard_names([template])).to be_empty
      end

      it "returns an empty list of ids" do
        allow(synchronizer).to receive(:fetch_from_datadog).and_return({"prod Some Dashboard" => 123})

        expect(synchronizer.unknown_dashboard_ids([template])).to be_empty
      end

{'fraction_non_alphanumeric': 0.15164952110677546, 'fraction_numerical': 0.01596310748492373, 'mean_word_length': 3.4089132134480065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, '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': '6868663', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1792, 'n_words': 371}