Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.63 KB | None | 0 0
  1. require_relative 'spec_helper'
  2.  
  3. describe Student do
  4.   before :each do
  5.     DB[:conn].execute("DROP TABLE IF EXISTS students")
  6.  
  7.     sql = <<-SQL
  8.       CREATE TABLE IF NOT EXISTS students (
  9.       id INTEGER PRIMARY KEY,
  10.       name TEXT,
  11.       grade INTEGER
  12.       )
  13.     SQL
  14.  
  15.     DB[:conn].execute(sql)
  16.     DB[:conn].results_as_hash = true
  17.   end
  18.  
  19.   let(:attributes) {
  20.     {
  21.       id: nil,
  22.       name: "Sam",
  23.       grade: 11
  24.     }
  25.   }
  26.  
  27.   let(:new_student) {Student.new(attributes)}
  28.  
  29.   describe 'inheritance' do
  30.     it 'inherits from InteractiveRecord class' do
  31.       expect(Student).to be < InteractiveRecord
  32.     end
  33.   end
  34.  
  35.   describe '.table_name' do
  36.     it 'creates a downcased, plural table name based on the Class name' do
  37.       expect(Student.table_name).to eq('students')
  38.     end
  39.   end
  40.  
  41.   describe '.column_names' do
  42.     it 'returns an array of SQL column names' do
  43.       expect(Student.column_names).to eq(["id", "name", "grade"])
  44.     end
  45.   end
  46.  
  47.   describe 'initialize' do
  48.     it 'creates an new instance of a student' do
  49.       expect(Student.new).to be_a Student
  50.     end
  51.  
  52.     it 'creates a new student with attributes' do
  53.       expect(new_student.name).to eq("Sam")
  54.     end
  55.   end
  56.  
  57.   describe 'attr_accessor' do
  58.     it 'creates attr_accessors for each column name' do
  59.       old_name = new_student.name
  60.       new_name = new_student.name = "Jo"
  61.       old_grade = new_student.grade
  62.       new_grade = new_student.grade = 12
  63.       expect(old_name).to eq("Sam")
  64.       expect(new_name).to eq("Jo")
  65.       expect(old_grade).to eq(11)
  66.       expect(new_grade).to eq(12)
  67.     end
  68.   end
  69.  
  70.   context 'has instance methods to insert data into db' do
  71.     describe '#table_name_for_insert' do
  72.       it 'return the table name when called on an instance of Student' do
  73.         expect(new_student.table_name_for_insert).to eq("students")
  74.       end
  75.     end
  76.  
  77.     describe '#col_names_for_insert' do
  78.       it 'return the column names when called on an instance of Student' do
  79.         expect(new_student.col_names_for_insert).to include("name, grade")
  80.       end
  81.  
  82.       it 'does not include an id column' do
  83.         expect(new_student.col_names_for_insert).not_to include("id")
  84.       end
  85.     end
  86.  
  87.     describe '#values_for_insert' do
  88.       it 'formats the column names to be used in a SQL statement' do
  89.         expect(new_student.values_for_insert).to eq("'Sam', '11'")
  90.       end
  91.     end
  92.  
  93.     describe '#save' do
  94.       it 'saves the student to the db' do
  95.         new_student.save
  96.         expect(DB[:conn].execute("SELECT * FROM students WHERE name = 'Sam'")).to eq([{"id"=>1, "name"=>"Sam", "grade"=>11, 0=>1, 1=>"Sam", 2=>11}])
  97.       end
  98.  
  99.       it 'sets the student\'s id' do
  100.         new_student.save
  101.         expect(new_student.id).to eq(1)
  102.       end
  103.     end
  104.   end
  105.  
  106.   describe '.find_by_name' do
  107.     it 'executes the SQL to find a row by name' do
  108.       Student.new({name: "Jan", grade: 10}).save
  109.       expect(Student.find_by_name("Jan")).to eq([{"id"=>1, "name"=>"Jan", "grade"=>10, 0=>1, 1=>"Jan", 2=>10}])
  110.     end
  111.   end
  112.  
  113.   describe '.find_by' do
  114.     it 'executes the SQL to find a row by the attribute passed into the method' do
  115.       Student.new({name: "Susan", grade: 10}).save
  116.       expect(Student.find_by({name: "Susan"})).to eq([{"id"=>1, "name"=>"Susan", "grade"=>10, 0=>1, 1=>"Susan", 2=>10}])
  117.     end
  118.  
  119.     it 'accounts for when an attribute value is an integer' do
  120.       Student.new({name: "Susan", grade: 10}).save
  121.       Student.new({name: "Geraldine", grade: 9}).save
  122.       expect(Student.find_by({grade: 10})).to eq([{"id"=>1, "name"=>"Susan", "grade"=>10, 0=>1, 1=>"Susan", 2=>10}])
  123.     end
  124.   end
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement