<commit_before>require "test_helper"

require "app"

class UpdateTest < Test::Unit::TestCase
  include Rack::Test::Methods

  ENDPOINT = "http://solr-test-server:9999/solr/rummager/update"
  SUCCESS_RESPONSE = <<-END
    <response>
      <lst name="responseHeader">
        <int name="status">0</int>
        <int name="QTime">9</int>
      </lst>
    </response>
  END

  def app
    Sinatra::Application
  end

  def test_should_send_a_document_to_solr_when_a_json_document_is_posted
    json = JSON.dump(
      "title" => "TITLE",
      "description" => "DESCRIPTION",
      "format" => "answer",
      "link" => "/an-example-answer",
      "indexable_content" => "HERE IS SOME CONTENT"
    )

    stub_request(:post, ENDPOINT).
      to_return(body: SUCCESS_RESPONSE)

    post "/documents", json

    assert last_response.ok?
    assert_requested :post, ENDPOINT, body: %r{HERE IS SOME CONTENT}
  end

  def test_should_send_documents_to_solr_when_a_json_array_of_documents_is_posted
    json = JSON.dump([{
      "title" => "TITLE1",
      "description" => "DESCRIPTION",
      "format" => "answer",
      "link" => "/an-example-answer",
      "indexable_content" => "HERE IS SOME CONTENT"
    }, {
      "title" => "TITLE2",
      "description" => "DESCRIPTION",
      "format" => "answer",
      "link" => "/an-example-answer",
      "indexable_content" => "HERE IS SOME CONTENT"
    }])

    stub_request(:post, "http://solr-test-server:9999/solr/rummager/update").
      to_return(body: SUCCESS_RESPONSE)

    post "/documents", json

    assert last_response.ok?
    assert_requested :post, ENDPOINT, body: %r{TITLE1}
    assert_requested :post, ENDPOINT, body: %r{TITLE2}
  end

  def test_should_post_delete_by_query_to_solr
    stub_request(:post, "http://solr-test-server:9999/solr/rummager/update").
      to_return(body: SUCCESS_RESPONSE)

    delete "/documents/http%3A%2F%2Fexample.com%2Flink-name"

    assert last_response.ok?
    assert_requested :post, ENDPOINT,
      body: Regexp.new(Regexp.escape("link:http\\://example.com/link\\-name"))
  end

  def test_should_post_delete_using_link_parameter_by_query_to_solr
    stub_request(:post, "http://solr-test-server:9999/solr/rummager/update").
      to_return(body: SUCCESS_RESPONSE)

    delete "/documents", "link=http%3A%2F%2Fexample.com%2Flink-name"

    assert last_response.ok?
    assert_requested :post, ENDPOINT,
      body: Regexp.new(Regexp.escape("link:http\\://example.com/link\\-name"))
  end

  def test_should_post_delete_all_to_solr
    stub_request(:post, "http://solr-test-server:9999/solr/rummager/update").
      to_return(body: SUCCESS_RESPONSE)

    delete "/documents", "delete_all=true"

    assert last_response.ok?
    assert_requested :post, ENDPOINT,
      body: Regexp.new(Regexp.escape("link:[* TO *]"))
  end
end<commit_msg>Stub integration tests.<commit_after>require "test_helper"

require "app"

class UpdateTest < Test::Unit::TestCase
  include Rack::Test::Methods

  ENDPOINT = "http://solr-test-server:9999/solr/rummager/update"
  SUCCESS_RESPONSE = <<-END
    <response>
      <lst name="responseHeader">
        <int name="status">0</int>
        <int name="QTime">9</int>
      </lst>
    </response>
  END

  def app
    Sinatra::Application
  end

  def test_should_send_a_document_to_solr_when_a_json_document_is_posted
    json = JSON.dump(
      "title" => "TITLE",
      "description" => "DESCRIPTION",
      "format" => "answer",
      "link" => "/an-example-answer",
      "indexable_content" => "HERE IS SOME CONTENT"
    )

    stub_request(:post, ENDPOINT).
      to_return(body: SUCCESS_RESPONSE)

    post "/documents", json

    assert last_response.ok?
    assert_requested :post, ENDPOINT, body: %r{HERE IS SOME CONTENT}
  end

  def test_should_send_documents_to_solr_when_a_json_array_of_documents_is_posted
    json = JSON.dump([{
      "title" => "TITLE1",
      "description" => "DESCRIPTION",
      "format" => "answer",
      "link" => "/an-example-answer",
      "indexable_content" => "HERE IS SOME CONTENT"
    }, {
      "title" => "TITLE2",
      "description" => "DESCRIPTION",
      "format" => "answer",
      "link" => "/an-example-answer",
      "indexable_content" => "HERE IS SOME CONTENT"
    }])

    stub_request(:post, "http://solr-test-server:9999/solr/rummager/update").
      to_return(body: SUCCESS_RESPONSE)

    post "/documents", json

    assert last_response.ok?
    assert_requested :post, ENDPOINT, body: %r{TITLE1}
    assert_requested :post, ENDPOINT, body: %r{TITLE2}
  end

  def test_should_post_delete_by_query_to_solr
    stub_request(:post, "http://solr-test-server:9999/solr/rummager/update").
      to_return(body: SUCCESS_RESPONSE)

    delete "/documents/http%3A%2F%2Fexample.com%2Flink-name"

    assert last_response.ok?
    assert_requested :post, ENDPOINT,
      body: Regexp.new(Regexp.escape("link:http\\://example.com/link\\-name"))
  end

  def test_should_post_delete_using_link_parameter_by_query_to_solr
    stub_request(:post, "http://solr-test-server:9999/solr/rummager/update").
      to_return(body: SUCCESS_RESPONSE)

    delete "/documents", "link=http%3A%2F%2Fexample.com%2Flink-name"

    assert last_response.ok?
    assert_requested :post, ENDPOINT,
      body: Regexp.new(Regexp.escape("link:http\\://example.com/link\\-name"))
  end

  def test_should_post_delete_all_to_solr
    stub_request(:post, "http://solr-test-server:9999/solr/rummager/update").
      to_return(body: SUCCESS_RESPONSE)

    delete "/documents", "delete_all=true"

    assert last_response.ok?
    assert_requested :post, ENDPOINT,
      body: Regexp.new(Regexp.escape("link:[* TO *]"))
  end

  def test_should_amend_existing_document
    # TODO: stub
  end

  def test_should_fail_to_amend_missing_document
    # TODO: stub
  end
end
{'fraction_non_alphanumeric': 0.1402995352039938, 'fraction_numerical': 0.01170597348941298, 'mean_word_length': 3.574803149606299, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 49, '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': '498915', 'n_tokens_mistral': 2169, 'n_tokens_neox': 2069, 'n_words': 339}