<commit_before>require 'spec_helper'

describe DatabasesController do
  login_user

  
  # This should return the minimal set of attributes required to create a valid
  # Database. As you add validations to Database, be sure to
  # update the return value of this method accordingly.
  def valid_attributes
    {name: "db-01", type: "postgres"}
  end

  before do
    @database = FactoryGirl.create(:database)
  end

  describe "GET index" do
    it "assigns all databases as @databases" do
      db = Database.create! valid_attributes
      get :index
      assert_template 'index'
      assigns(:databases).should include db
    end
  end
  
  def test_show
    get :show, id: @database
    assert_template 'show'
  end
  
  def test_new
    get :new
    assert_template 'new'
  end
  
### TODO: understand why it fails
### test_create_invalid(DatabasesControllerTest) [test/functional/databases_controller_test.rb:26]:
###   Expected block to return true value.
###
###  def test_create_invalid
###    Database.any_instance.stubs(:valid?).returns(false)
###    post :create
###    assert_response :success
###    assert_template 'new'
###  end

  def test_create_valid
    Database.any_instance.stubs(:valid?).returns(true)
    post :create, database: { name: "database", type: "postgres", server_ids: [] }
    assert_redirected_to databases_url #database_url(assigns(:database))
  end
  
  def test_edit
    get :edit, id: @database
    assert_template 'edit'
  end
  
### TODO: understand why it fails
### test_create_invalid(DatabasesControllerTest) [test/functional/databases_controller_test.rb:26]:
###   Expected block to return true value.
###
###  def test_update_invalid
###    Database.any_instance.stubs(:valid?).returns(false)
###    put :update, id: @database
###    assert_template 'edit'
###  end

  def test_update_valid
    Database.any_instance.stubs(:valid?).returns(true)
    put :update, id: @database
    assert_redirected_to databases_url #database_url(assigns(:database))
  end
  
  def test_destroy
    database = Database.first
    delete :destroy, id: database
    assert_redirected_to databases_url
    assert !Database.exists?(database.id)
  end
end<commit_msg>Rewrite DatabaseController spec with a more rspec-ish style<commit_after>require 'spec_helper'

describe DatabasesController do
  login_user
  
  # This should return the minimal set of attributes required to create a valid
  # Database. As you add validations to Database, be sure to
  # update the return value of this method accordingly.
  def valid_attributes
    {name: "db-01", type: "postgres"}
  end

  before do
    @database = FactoryGirl.create(:database)
  end

  describe "GET :index" do
    it "assigns all databases as @databases" do
      db = Database.create! valid_attributes
      get :index
      assert_template 'index'
      assigns(:databases).should include db
    end
  end
  
  describe "GET :show" do
    it "uses the right template" do
      get :show, id: @database
      assert_template 'show'
    end
  end
  
  describe "GET :new" do
    it "uses the right template" do
      get :new
      assert_template 'new'
    end
  end
  
### TODO: understand why it fails
### test_create_invalid(DatabasesControllerTest) [test/functional/databases_controller_test.rb:26]:
###   Expected block to return true value.
###
###  def test_create_invalid
###    Database.any_instance.stubs(:valid?).returns(false)
###    post :create
###    assert_response :success
###    assert_template 'new'
###  end

  describe "POST :create" do
    it "works with valid data" do
      Database.any_instance.stub(:valid?).and_return(true)
      post :create, database: { name: "database", type: "postgres", server_ids: [] }
      assert_redirected_to databases_url #database_url(assigns(:database))
    end
  end

  describe "GET :edit" do
    it "uses the right template" do
      get :edit, id: @database
      assert_template 'edit'
    end
  end
  
### TODO: understand why it fails
### test_create_invalid(DatabasesControllerTest) [test/functional/databases_controller_test.rb:26]:
###   Expected block to return true value.
###
###  def test_update_invalid
###    Database.any_instance.stubs(:valid?).returns(false)
###    put :update, id: @database
###    assert_template 'edit'
###  end

  describe "PUT :update" do
    it "works with valid data" do
      Database.any_instance.stub(:valid?).and_return(true)
      put :update, id: @database
      assert_redirected_to databases_url #database_url(assigns(:database))
    end
  end

  describe "DELETE :destroy" do
    it "destroys database" do
      database = Database.first
      delete :destroy, id: database
      assert_redirected_to databases_url
      Database.where(_id: database.id).count.should == 0
    end
  end
end
{'fraction_non_alphanumeric': 0.1014007944804516, 'fraction_numerical': 0.002717959439682208, 'mean_word_length': 3.662768031189084, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4432063', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1418, 'n_words': 425}