Guest User

Untitled

a guest
Oct 15th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. require 'spec_helper'
  2. require 'active_record'
  3. require 'order_query'
  4. require 'pry-byebug'
  5.  
  6. RSpec.describe OrderQuery do
  7.  
  8. class Employee < ActiveRecord::Base
  9. include OrderQuery
  10. has_one :contract
  11. end
  12.  
  13. class Contract < ActiveRecord::Base
  14. belongs_to :employee
  15. end
  16.  
  17. def seed_data
  18. updated_at = Time.now
  19.  
  20. employees = [{
  21. name: "John",
  22. email: "john@foo.com",
  23. ctype: "seasonal"
  24. },
  25. {
  26. name: "Fred",
  27. email: "fred@foo.com",
  28. ctype: "annual"
  29. },
  30.  
  31. ].map do |info|
  32. contract_type = info.delete(:ctype)
  33. emp = Employee.create!(info)
  34. emp.create_contract!(contract_type: contract_type, start_at: updated_at)
  35. updated_at += 60*60*24
  36. binding.pry
  37. emp
  38. end
  39.  
  40. end
  41.  
  42. def db_config
  43. @db_config ||= {
  44. adapter: 'mysql2',
  45. host: ENV.fetch('DB_HOST'),
  46. port: ENV.fetch('DB_PORT', 3306),
  47. username: ENV.fetch('DB_USERNAME'),
  48. password: ENV.fetch('DB_PASSWORD'),
  49. database: ENV.fetch('DB_NAME')
  50. }
  51. @db_config.dup
  52. end
  53.  
  54. def establish_connection(db_name=nil)
  55. config = db_config
  56. if db_name
  57. config[:database] = db_name
  58. end
  59. ActiveRecord::Base.establish_connection(config)
  60. ActiveRecord::Base.connection
  61. end
  62.  
  63. def create_tables
  64. establish_connection
  65. ActiveRecord::Schema.define do
  66. self.verbose = false
  67.  
  68. create_table :employees do |t|
  69. t.column :name, :string
  70. t.column :email, :string
  71. end
  72.  
  73. create_table :contracts do |t|
  74. t.column :employee_id, :bigint, null:false
  75. t.column :contract_type, :string
  76. t.column :start_at, :datetime
  77. t.index ["employee_id"], name: "index_contracts_on_employee_id", using: :btree
  78. end
  79.  
  80. add_foreign_key "contracts", "employees"
  81. end
  82.  
  83. Employee.reset_column_information
  84. end
  85.  
  86. def drop_tables
  87. [:contracts, :employees].each do |t|
  88. begin
  89. establish_connection.drop_table t
  90. rescue ActiveRecord::StatementInvalid
  91. end
  92. end
  93. end
  94.  
  95. def create_db
  96. establish_connection(:mysql).execute("CREATE DATABASE IF NOT EXISTS #{db_config[:database]}")
  97. end
  98.  
  99. def drop_db
  100. establish_connection(:mysql).execute("DROP DATABASE IF EXISTS #{db_config[:database]}")
  101. end
  102.  
  103. describe "search" do
  104.  
  105. before :all do
  106. create_db
  107. drop_tables
  108. create_tables
  109. seed_data
  110. end
  111.  
  112. after :all do
  113. drop_tables
  114. drop_db
  115. end
  116.  
  117. describe "sort on secondary join table" do
  118. it "can fetch 'next' on a Employee point when sorting by contract.contract_type" do
  119. space = Employee.joins(:contract).seek([:contract_type, :desc, "contract.contract_type"])
  120. expect(space.class).to be(OrderQuery::Space)
  121. record = space.at(Employee.first).after(false).limit(1)
  122. end
  123. end
  124.  
  125. end
  126. end
Add Comment
Please, Sign In to add comment