<commit_before>require File.expand_path(File.dirname(__FILE__) + '/../test_helper')

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

  def app
    Sinatra::Application
  end

  context "get /sources with 0 sources" do
    include SharedTests

    before :all do
      reset_sources_data
      get '/sources'
      @body = JSON.parse(last_response.body)
    end
  
    test "should return []" do
      assert_equal [], @body
    end
  end
  
  context "post /sources" do
    include SharedTests

    before :all do
      reset_sources_data
      post '/sources', :url => "http://new-source.gov"
      @body = JSON.parse(last_response.body)
    end
    
    test "body should have correct url value" do
      assert_equal "http://new-source.gov", @body["url"]
    end
    
    test "body should have created_at" do
      assert @body.keys.include?("created_at")
    end
    
    test "body should have updated_at" do
      assert @body.keys.include?("updated_at")
    end

    test "body should have _id" do
      assert @body.keys.include?("_id")
    end
  end
  
  context "get /sources with 1 source" do
    include SharedTests
  
    before :all do
      reset_sources_data
      post '/sources', :url => "http://source-1.org"
      get '/sources'
      @body = JSON.parse(last_response.body)
    end
    
    test "body should have one top level element" do
      assert @body.length, 1
    end
    
    test "body should have correct url value" do
      assert_equal "http://source-1.org", @body[0]["url"]
    end
  
    test "body should have created_at" do
      assert @body[0].keys.include?("created_at")
    end
    
    test "body should have updated_at" do
      assert @body[0].keys.include?("updated_at")
    end
  
    test "body should have _id" do
      assert @body[0].keys.include?("_id")
    end
  end
  
  context "put /sources/_id" do
    include SharedTests
    
    before :all do
      reset_sources_data
      post '/sources', :url => "http://modify-me.gov"
      @original = JSON.parse(last_response.body)
      id = @original["_id"]
      sleep(1)
      put "/sources/#{id}", :url => "http://modified.gov"
      @body = JSON.parse(last_response.body)
    end
    
    test "body should have correct url" do
      assert_equal "http://modified.gov", @body["url"]
    end
    
    test "body should have an unchanged created_at" do
      assert_equal @original["created_at"], @body["created_at"]
    end
    
    test "body should have an updated updated_at" do
      assert_not_equal @original["updated_at"], @body["updated_at"]
    end
  
    test "body should have an unchanged _id" do
      assert_equal @original["_id"], @body["_id"]
    end
  end
  
  context "delete /sources/_id" do
    include SharedTests

    before :all do
      reset_sources_data
      post '/sources', :url => "http://delete-me.com"
      @id = JSON.parse(last_response.body)["_id"]
      delete "/sources/#{@id}"
      @body = JSON.parse(last_response.body)
    end

    test "body should have updated_at" do
      assert_equal @id, @body["_id"]
    end
  end<commit_msg>Cosmetic changes to a controller test<commit_after>require File.expand_path(File.dirname(__FILE__) + '/../test_helper')

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

  def app
    Sinatra::Application
  end

  context "get /sources with 0 sources" do
    include SharedTests

    before :all do
      reset_sources_data
      get '/sources'
      @body = JSON.parse(last_response.body)
    end
  
    test "should return []" do
      assert_equal [], @body
    end
  end
  
  context "post /sources" do
    include SharedTests

    before :all do
      reset_sources_data
      post '/sources', :url => "http://new-source.gov"
      @body = JSON.parse(last_response.body)
    end
    
    test "body should have correct url value" do
      assert_equal "http://new-source.gov", @body["url"]
    end
    
    test "body should have created_at" do
      assert @body.keys.include?("created_at")
    end
    
    test "body should have updated_at" do
      assert @body.keys.include?("updated_at")
    end

    test "body should have _id" do
      assert @body.keys.include?("_id")
    end
  end
  
  context "get /sources with 1 source" do
    include SharedTests
  
    before :all do
      reset_sources_data
      post '/sources', :url => "http://source-1.org"
      get '/sources'
      @body = JSON.parse(last_response.body)
    end
    
    test "body should have one top level element" do
      assert @body.length, 1
    end
    
    test "body should have correct url value" do
      assert_equal "http://source-1.org", @body[0]["url"]
    end
  
    test "body should have created_at" do
      assert @body[0].keys.include?("created_at")
    end
    
    test "body should have updated_at" do
      assert @body[0].keys.include?("updated_at")
    end
  
    test "body should have _id" do
      assert @body[0].keys.include?("_id")
    end
  end
  
  context "put /sources/_id" do
    include SharedTests
    
    before :all do
      reset_sources_data
      post '/sources', :url => "http://original.gov"
      @original = JSON.parse(last_response.body)
      id = @original["_id"]
      sleep 1 # ensure different timestamp
      put "/sources/#{id}", :url => "http://updated.gov"
      @updated = JSON.parse(last_response.body)
    end
    
    test "body should have updated url" do
      assert_equal "http://updated.gov", @updated["url"]
    end
    
    test "body should have an unchanged created_at" do
      puts @original["created_at"].class
      puts @updated["created_at"].class
      assert_equal @original["created_at"], @updated["created_at"]
    end
    
    test "body should have an updated updated_at" do
      assert_not_equal @original["updated_at"], @updated["updated_at"]
    end
  
    test "body should have an unchanged _id" do
      assert_equal @original["_id"], @updated["_id"]
    end
  end
  
  context "delete /sources/_id" do
    include SharedTests

    before :all do
      reset_sources_data
      post '/sources', :url => "http://delete-me.com"
      @id = JSON.parse(last_response.body)["_id"]
      delete "/sources/#{@id}"
      @body = JSON.parse(last_response.body)
    end

    test "body should have updated_at" do
      assert_equal @id, @body["_id"]
    end
  end
{'fraction_non_alphanumeric': 0.11555974250274768, 'fraction_numerical': 0.0031402103940964044, 'mean_word_length': 2.5271317829457365, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '4988590', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1986, 'n_words': 564}