<commit_before>require 'bazil'
require 'net/http'
require 'rspec'

class FakeResponse
  attr_reader :code, :message, :body
  def initialize(code,message,body)
    @code = code
    @message = message
    @body = body
  end
end

class FakeClient
  attr_accessor :http_client
  def initialize
    @http_client = Object.new
  end
  def api_version
    'v2'
  end
end

describe Bazil::Client  do
  let(:client) { FakeClient.new }
  let(:model_id) { 42 }
  let(:config_id) { 184 }

  let(:status_path) { "/#{client.api_version}/models/#{model_id}/configs/#{config_id}/status" }
  let(:status_result) {
    {
      "num_features" => 0,
      "num_train_queries" => 0,
      "num_labels" => 0,
      "num_queries" => 0,
    }
  }

  let(:retrain_path) { "/#{client.api_version}/models/#{model_id}/configs/#{config_id}/retrain" }
  let(:retrain_result) {
    {
      "total" => 1000,
      "elapsed_time" => 0.5,
    }
  }

  let(:query_path) { "/#{client.api_version}/models/#{model_id}/configs/#{config_id}/query" }
  let(:query_result) {
    {
      "score" => {
        "Label1" => 0.5,
        "Label2" => -0.5,
      },
      "classifier_result" => "Label1",
    }
  }

  let(:trace_path) { "/#{client.api_version}/models/#{model_id}/configs/#{config_id}/trace" }
  let(:trace_result) {
    {
      "result" => {
        "Label1" => 0.5,
        "Label2" => -0.5,
        "feature_weights" => {
          "Label1" => {
            "key" => {
              "string_weights" => [ [ 0.5, 0, 2 ] ]
            }
          },
          "Label2" => {
            "key" => {
              "string_weights" => [ [ -0.5, 0, 2 ] ]
            }
          }
        }
      },
      "data" => {
        "key" => "value"
      }
    }
  }

  let(:evaluate_path) { "/#{client.api_version}/models/#{model_id}/configs/#{config_id}/evaluate" }
  let(:evaluate_result) {
    # evaluate result is too much complex and different result among multi_class and multi_label
    {
      "folds" => [{},{}],
      "result" => {},
    }
  }

  def header_for_json(js)
    {
      "Content-Type" => "application/json; charset=UTF-8",
      "Content-Length" => js.to_json.length.to_s
    }
  end

  it "check status at initialize" do
    client.http_client.should_receive(:get).with(status_path).and_return(FakeResponse.new("200","OK",status_result.to_json))
    model = Bazil::Model.new(client, model_id, config_id)
  end

  let(:model) {
    client.http_client.should_receive(:get).with(status_path).and_return(FakeResponse.new("200","OK",status_result.to_json))
    Bazil::Model.new(client, model_id, config_id)
  }

  describe "model" do
    it "send get request at status" do
      client.http_client.should_receive(:get).with(status_path).and_return(FakeResponse.new("200","OK",status_result.to_json))
      expect(model.status).to eq(status_result)
    end

    it "send post request at retrain without argument" do
      arg = {}
      client.http_client.should_receive(:post).with(retrain_path, arg.to_json, header_for_json(arg)).and_return(FakeResponse.new("200","OK",retrain_result.to_json))
      expect(model.retrain).to eq(retrain_result)
    end

    it "send post request at retrain with times argument" do<commit_msg>Fix test for pull request #17<commit_after>require 'bazil'
require 'net/http'
require 'rspec'

class FakeResponse
  attr_reader :code, :message, :body
  def initialize(code,message,body)
    @code = code
    @message = message
    @body = body
  end
end

class FakeClient
  attr_accessor :http_client
  def initialize
    @http_client = Object.new
  end
  def api_version
    'v2'
  end
end

describe Bazil::Client  do
  let(:client) { FakeClient.new }
  let(:model_id) { 42 }
  let(:config_id) { 184 }

  let(:status_path) { "/#{client.api_version}/models/#{model_id}/configs/#{config_id}/status" }
  let(:status_result) {
    {
      "num_features" => 0,
      "num_train_queries" => 0,
      "num_labels" => 0,
      "num_queries" => 0,
    }
  }

  let(:retrain_path) { "/#{client.api_version}/models/#{model_id}/configs/#{config_id}/retrain" }
  let(:retrain_result) {
    {
      "total" => 1000,
      "elapsed_time" => 0.5,
    }
  }

  let(:query_path) { "/#{client.api_version}/models/#{model_id}/configs/#{config_id}/query" }
  let(:query_result) {
    {
      "score" => {
        "Label1" => 0.5,
        "Label2" => -0.5,
      },
      "classifier_result" => "Label1",
    }
  }

  let(:trace_path) { "/#{client.api_version}/models/#{model_id}/configs/#{config_id}/trace" }
  let(:trace_result) {
    {
      "result" => {
        "Label1" => 0.5,
        "Label2" => -0.5,
        "feature_weights" => {
          "Label1" => {
            "key" => {
              "string_weights" => [ [ 0.5, 0, 2 ] ]
            }
          },
          "Label2" => {
            "key" => {
              "string_weights" => [ [ -0.5, 0, 2 ] ]
            }
          }
        }
      },
      "data" => {
        "key" => "value"
      }
    }
  }

  let(:evaluate_path) { "/#{client.api_version}/models/#{model_id}/configs/#{config_id}/evaluate" }
  let(:evaluate_result) {
    # evaluate result is too much complex and different result among multi_class and multi_label
    {
      "folds" => [{},{}],
      "result" => {},
    }
  }

  def header_for_json(js)
    {
      "Content-Type" => "application/json; charset=UTF-8",
      "Content-Length" => js.to_json.length.to_s
    }
  end

  let(:model) {
    Bazil::Model.new(client, model_id, config_id)
  }

  describe "model" do
    it "send get request at status" do
      client.http_client.should_receive(:get).with(status_path).and_return(FakeResponse.new("200","OK",status_result.to_json))
      expect(model.status).to eq(status_result)
    end

    it "send post request at retrain without argument" do
      arg = {}
      client.http_client.should_receive(:post).with(retrain_path, arg.to_json, header_for_json(arg)).and_return(FakeResponse.new("200","OK",retrain_result.to_json))
      expect(model.retrain).to eq(retrain_result)
    end

    it "send post request at retrain with times argument" do
{'fraction_non_alphanumeric': 0.16326194398682042, 'fraction_numerical': 0.016474464579901153, 'mean_word_length': 2.6179976162097733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, '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': '7187342', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2157, 'n_words': 351}