Advertisement
Guest User

Untitled

a guest
Feb 10th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. require 'active_record'
  2. require 'active_support/hash_with_indifferent_access'
  3. require 'rspec'
  4. require 'mysql2'
  5.  
  6. DATABASE_NAME = :sql_playground_database.to_s
  7. DATABASE_CONFIGURATION_BASE = {
  8. adapter: :mysql2,
  9. host: :localhost,
  10. username: ENV['MYSQL_USER_NAME'],
  11. password: ENV['MYSQL_USER_PASSWORD'],
  12. }
  13.  
  14. class DummyCreator < ActiveRecord::Migration
  15. class << self
  16. def up
  17. create_table(:sql_playground_student) do |t|
  18. t.string :name
  19. end
  20.  
  21. create_table(:sql_playground_classroom) do |t|
  22. t.string :name
  23. end
  24.  
  25. create_table(:sql_playground_student_classroom) do |t|
  26. t.integer :student_id
  27. t.integer :classroom_id
  28. end
  29. rescue
  30. nil
  31. end
  32.  
  33. def down
  34. drop_table(:sql_playground_student)
  35. drop_table(:sql_playground_classroom)
  36. drop_table(:sql_playground_student_classroom)
  37. rescue
  38. nil
  39. end
  40. end
  41. end
  42.  
  43. RSpec.configure do |config|
  44. config.before :suite do
  45. ActiveRecord::Base.establish_connection(DATABASE_CONFIGURATION_BASE)
  46. begin
  47. ActiveRecord::Base.connection.create_database(DATABASE_NAME)
  48. rescue ActiveRecord::StatementInvalid => e
  49. pp e
  50. end
  51.  
  52. ActiveRecord::Base.establish_connection(DATABASE_CONFIGURATION_BASE.merge(
  53. database: DATABASE_NAME,
  54. ))
  55.  
  56. DummyCreator.up rescue nil
  57. end
  58.  
  59. config.after :suite do
  60. DummyCreator.down
  61. end
  62. end
  63.  
  64. class StudentClassroom < ActiveRecord::Base
  65. self.table_name = :sql_playground_student_classroom
  66.  
  67. belongs_to :student
  68. belongs_to :classroom
  69. end
  70.  
  71. class Student < ActiveRecord::Base
  72. self.table_name = :sql_playground_student
  73.  
  74. has_one :student_classrooms, class_name: StudentClassroom
  75. has_many :classrooms, through: :student_classrooms
  76.  
  77. scope :each_classroom_student, -> { Student.left_outer_joins(:classrooms) }
  78. end
  79.  
  80. class Classroom < ActiveRecord::Base
  81. self.table_name = :sql_playground_classroom
  82.  
  83. has_one :student_classrooms, class_name: StudentClassroom
  84. has_many :students, through: :student_classrooms
  85. end
  86.  
  87.  
  88. describe 'sql' do
  89. before :all do
  90. students = (0..3).map do |n|
  91. Student.create(name: "Student #{n}")
  92. end
  93.  
  94. classes = (0..2).map do |n|
  95. Classroom.create(name: "Classroom #{n}")
  96. end
  97.  
  98. StudentClassroom.create(student: students[0], classroom: classes[0])
  99. StudentClassroom.create(student: students[0], classroom: classes[1])
  100. StudentClassroom.create(student: students[0], classroom: classes[2])
  101.  
  102. StudentClassroom.create(student: students[1], classroom: classes[0])
  103. StudentClassroom.create(student: students[1], classroom: classes[1])
  104.  
  105. StudentClassroom.create(student: students[2], classroom: classes[1])
  106. StudentClassroom.create(student: students[2], classroom: classes[2])
  107.  
  108. StudentClassroom.create(student: students[3], classroom: classes[2])
  109.  
  110. @students = students
  111. @classes = classes
  112. end
  113.  
  114. let(:students) { @students }
  115. let(:classes) { @classes }
  116.  
  117. it 'premise' do
  118. expect(students[0].classrooms).to match_array([classes[0], classes[1], classes[2]])
  119. expect(students[1].classrooms).to match_array([classes[0], classes[1]])
  120. expect(students[2].classrooms).to match_array([classes[1], classes[2]])
  121. expect(students[3].classrooms).to match_array([classes[2]])
  122.  
  123. expect(classes[0].students).to match_array([students[0], students[1]])
  124. expect(classes[1].students).to match_array([students[0], students[1], students[2]])
  125. expect(classes[2].students).to match_array([students[0], students[2], students[3]])
  126. end
  127.  
  128. it do
  129. expect(
  130. Student
  131. .left_outer_joins(:classrooms)
  132. .select(
  133. :id,
  134. :name,
  135. Classroom.arel_table[:id].as('as_classroom_id'),
  136. Classroom.arel_table[:name].as('as_classroom_name'),
  137. )
  138. .as_json
  139. .map(&:symbolize_keys)
  140. ).to eq([
  141. { id: 1, name: 'Student 0', as_classroom_id: 1, as_classroom_name: 'Classroom 0' },
  142. { id: 1, name: 'Student 0', as_classroom_id: 2, as_classroom_name: 'Classroom 1' },
  143. { id: 1, name: 'Student 0', as_classroom_id: 3, as_classroom_name: 'Classroom 2' },
  144.  
  145. { id: 2, name: 'Student 1', as_classroom_id: 1, as_classroom_name: 'Classroom 0' },
  146. { id: 2, name: 'Student 1', as_classroom_id: 2, as_classroom_name: 'Classroom 1' },
  147.  
  148. { id: 3, name: 'Student 2', as_classroom_id: 2, as_classroom_name: 'Classroom 1' },
  149. { id: 3, name: 'Student 2', as_classroom_id: 3, as_classroom_name: 'Classroom 2' },
  150.  
  151. { id: 4, name: 'Student 3', as_classroom_id: 3, as_classroom_name: 'Classroom 2' },
  152. ])
  153. end
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement