2017-07-09 13:41:28 -04:00
# frozen_string_literal: true
2016-08-06 12:26:20 -04:00
require " cases/helper "
2018-07-18 22:25:08 -04:00
require " support/connection_helper "
2016-08-06 12:26:20 -04:00
require " models/admin "
require " models/admin/account "
require " models/admin/randomly_named_c1 "
require " models/admin/user "
require " models/binary "
require " models/book "
require " models/bulb "
require " models/category "
2017-01-04 15:27:55 -05:00
require " models/post "
2016-08-06 12:26:20 -04:00
require " models/comment "
require " models/company "
require " models/computer "
require " models/course "
require " models/developer "
2016-12-14 06:14:47 -05:00
require " models/dog "
2016-08-06 12:26:20 -04:00
require " models/doubloon "
require " models/joke "
require " models/matey "
2016-12-14 06:14:47 -05:00
require " models/other_dog "
2016-08-06 12:26:20 -04:00
require " models/parrot "
require " models/pirate "
require " models/randomly_named_c1 "
require " models/reply "
require " models/ship "
require " models/task "
require " models/topic "
require " models/traffic_light "
require " models/treasure "
require " tempfile "
2004-11-23 20:04:44 -05:00
2008-01-21 12:20:51 -05:00
class FixturesTest < ActiveRecord :: TestCase
2018-07-18 22:25:08 -04:00
include ConnectionHelper
2005-06-10 10:58:02 -04:00
self . use_instantiated_fixtures = true
2015-03-10 22:21:19 -04:00
self . use_transactional_tests = false
2005-06-10 10:58:02 -04:00
2011-12-19 10:07:02 -05:00
# other_topics fixture should not be included here
2010-12-29 11:15:45 -05:00
fixtures :topics , :developers , :accounts , :tasks , :categories , :funny_jokes , :binaries , :traffic_lights
2004-12-09 10:52:54 -05:00
2007-05-21 14:54:51 -04:00
FIXTURES = %w( accounts binaries companies customers
2004-11-23 20:04:44 -05:00
developers developers_projects entrants
2005-04-07 02:54:25 -04:00
movies projects subscribers topics tasks )
2010-04-16 11:53:36 -04:00
MATCH_ATTRIBUTE_NAME = / [a-zA-Z][- \ w]* /
2004-11-23 20:04:44 -05:00
def test_clean_fixtures
FIXTURES . each do | name |
fixtures = nil
2011-02-11 19:09:34 -05:00
assert_nothing_raised { fixtures = create_fixtures ( name ) . first }
2012-05-12 07:17:03 -04:00
assert_kind_of ( ActiveRecord :: FixtureSet , fixtures )
2010-07-14 15:43:59 -04:00
fixtures . each { | _name , fixture |
2004-11-23 20:04:44 -05:00
fixture . each { | key , value |
assert_match ( MATCH_ATTRIBUTE_NAME , key )
}
}
end
end
2017-06-18 11:14:52 -04:00
class InsertQuerySubscriber
attr_reader :events
def initialize
@events = [ ]
end
def call ( _ , _ , _ , _ , values )
@events << values [ :sql ] if values [ :sql ] =~ / INSERT /
end
end
if current_adapter? ( :Mysql2Adapter , :PostgreSQLAdapter )
def test_bulk_insert
2018-12-20 12:44:01 -05:00
subscriber = InsertQuerySubscriber . new
subscription = ActiveSupport :: Notifications . subscribe ( " sql.active_record " , subscriber )
create_fixtures ( " bulbs " )
assert_equal 1 , subscriber . events . size , " It takes one INSERT query to insert two fixtures "
ensure
ActiveSupport :: Notifications . unsubscribe ( subscription )
2017-06-18 11:14:52 -04:00
end
Build a multi-statement query when inserting fixtures:
- The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table
- The previous code was bulk inserting fixtures for a single table, making X query for X fixture files
- This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers):
```ruby
# before
%w(authors dogs computers).each do |table|
sql = build_sql(table)
connection.query(sql)
end
# after
sql = build_sql(authors, dogs, computers)
connection.query(sql)
```
- `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement
- My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds
- In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats:
1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)...
- In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db
- As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later
2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value
- Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction
3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html)
- Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets)
- Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one
- On mysql the `max_allowed_packet` is looked up:
1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised
2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
2017-12-12 15:38:10 -05:00
def test_bulk_insert_multiple_table_with_a_multi_statement_query
subscriber = InsertQuerySubscriber . new
subscription = ActiveSupport :: Notifications . subscribe ( " sql.active_record " , subscriber )
create_fixtures ( " bulbs " , " authors " , " computers " )
2018-02-16 22:51:19 -05:00
expected_sql = << ~ EOS . chop
Build a multi-statement query when inserting fixtures:
- The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table
- The previous code was bulk inserting fixtures for a single table, making X query for X fixture files
- This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers):
```ruby
# before
%w(authors dogs computers).each do |table|
sql = build_sql(table)
connection.query(sql)
end
# after
sql = build_sql(authors, dogs, computers)
connection.query(sql)
```
- `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement
- My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds
- In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats:
1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)...
- In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db
- As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later
2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value
- Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction
3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html)
- Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets)
- Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one
- On mysql the `max_allowed_packet` is looked up:
1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised
2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
2017-12-12 15:38:10 -05:00
INSERT INTO #{ActiveRecord::Base.connection.quote_table_name("bulbs")} .*
INSERT INTO #{ActiveRecord::Base.connection.quote_table_name("authors")} .*
INSERT INTO #{ActiveRecord::Base.connection.quote_table_name("computers")} .*
EOS
assert_equal 1 , subscriber . events . size
assert_match ( / #{ expected_sql } / , subscriber . events . first )
ensure
ActiveSupport :: Notifications . unsubscribe ( subscription )
end
def test_bulk_insert_with_a_multi_statement_query_raises_an_exception_when_any_insert_fails
require " models/aircraft "
assert_equal false , Aircraft . columns_hash [ " wheels_count " ] . null
fixtures = {
" aircraft " = > [
{ " name " = > " working_aircrafts " , " wheels_count " = > 2 } ,
{ " name " = > " broken_aircrafts " , " wheels_count " = > nil } ,
]
}
assert_no_difference " Aircraft.count " do
assert_raises ( ActiveRecord :: NotNullViolation ) do
ActiveRecord :: Base . connection . insert_fixtures_set ( fixtures )
end
end
end
2018-07-14 23:11:16 -04:00
def test_bulk_insert_with_a_multi_statement_query_in_a_nested_transaction
fixtures = {
" traffic_lights " = > [
{ " location " = > " US " , " state " = > [ " NY " ] , " long_state " = > [ " a " ] } ,
]
}
2018-07-18 22:25:08 -04:00
assert_difference " TrafficLight.count " do
ActiveRecord :: Base . transaction do
conn = ActiveRecord :: Base . connection
assert_equal 1 , conn . open_transactions
conn . insert_fixtures_set ( fixtures )
assert_equal 1 , conn . open_transactions
end
2018-07-14 23:11:16 -04:00
end
end
Build a multi-statement query when inserting fixtures:
- The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table
- The previous code was bulk inserting fixtures for a single table, making X query for X fixture files
- This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers):
```ruby
# before
%w(authors dogs computers).each do |table|
sql = build_sql(table)
connection.query(sql)
end
# after
sql = build_sql(authors, dogs, computers)
connection.query(sql)
```
- `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement
- My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds
- In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats:
1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)...
- In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db
- As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later
2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value
- Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction
3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html)
- Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets)
- Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one
- On mysql the `max_allowed_packet` is looked up:
1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised
2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
2017-12-12 15:38:10 -05:00
end
if current_adapter? ( :Mysql2Adapter )
2018-07-18 22:25:08 -04:00
def test_bulk_insert_with_multi_statements_enabled
run_without_connection do | orig_connection |
ActiveRecord :: Base . establish_connection (
orig_connection . merge ( flags : %w[ MULTI_STATEMENTS ] )
)
fixtures = {
" traffic_lights " = > [
{ " location " = > " US " , " state " = > [ " NY " ] , " long_state " = > [ " a " ] } ,
]
}
ActiveRecord :: Base . connection . stub ( :supports_set_server_option? , false ) do
assert_nothing_raised do
conn = ActiveRecord :: Base . connection
conn . execute ( " SELECT 1; SELECT 2; " )
conn . raw_connection . abandon_results!
end
assert_difference " TrafficLight.count " do
ActiveRecord :: Base . transaction do
conn = ActiveRecord :: Base . connection
assert_equal 1 , conn . open_transactions
conn . insert_fixtures_set ( fixtures )
assert_equal 1 , conn . open_transactions
end
end
assert_nothing_raised do
conn = ActiveRecord :: Base . connection
conn . execute ( " SELECT 1; SELECT 2; " )
conn . raw_connection . abandon_results!
end
end
end
end
def test_bulk_insert_with_multi_statements_disabled
run_without_connection do | orig_connection |
ActiveRecord :: Base . establish_connection (
orig_connection . merge ( flags : [ ] )
)
fixtures = {
" traffic_lights " = > [
{ " location " = > " US " , " state " = > [ " NY " ] , " long_state " = > [ " a " ] } ,
]
}
ActiveRecord :: Base . connection . stub ( :supports_set_server_option? , false ) do
assert_raises ( ActiveRecord :: StatementInvalid ) do
conn = ActiveRecord :: Base . connection
conn . execute ( " SELECT 1; SELECT 2; " )
conn . raw_connection . abandon_results!
end
assert_difference " TrafficLight.count " do
conn = ActiveRecord :: Base . connection
conn . insert_fixtures_set ( fixtures )
end
assert_raises ( ActiveRecord :: StatementInvalid ) do
conn = ActiveRecord :: Base . connection
conn . execute ( " SELECT 1; SELECT 2; " )
conn . raw_connection . abandon_results!
end
end
end
end
Build a multi-statement query when inserting fixtures:
- The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table
- The previous code was bulk inserting fixtures for a single table, making X query for X fixture files
- This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers):
```ruby
# before
%w(authors dogs computers).each do |table|
sql = build_sql(table)
connection.query(sql)
end
# after
sql = build_sql(authors, dogs, computers)
connection.query(sql)
```
- `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement
- My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds
- In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats:
1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)...
- In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db
- As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later
2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value
- Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction
3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html)
- Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets)
- Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one
- On mysql the `max_allowed_packet` is looked up:
1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised
2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
2017-12-12 15:38:10 -05:00
def test_insert_fixtures_set_raises_an_error_when_max_allowed_packet_is_smaller_than_fixtures_set_size
conn = ActiveRecord :: Base . connection
2018-01-23 14:21:58 -05:00
mysql_margin = 2
Build a multi-statement query when inserting fixtures:
- The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table
- The previous code was bulk inserting fixtures for a single table, making X query for X fixture files
- This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers):
```ruby
# before
%w(authors dogs computers).each do |table|
sql = build_sql(table)
connection.query(sql)
end
# after
sql = build_sql(authors, dogs, computers)
connection.query(sql)
```
- `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement
- My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds
- In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats:
1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)...
- In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db
- As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later
2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value
- Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction
3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html)
- Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets)
- Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one
- On mysql the `max_allowed_packet` is looked up:
1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised
2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
2017-12-12 15:38:10 -05:00
packet_size = 1024
2018-01-25 22:42:28 -05:00
bytes_needed_to_have_a_1024_bytes_fixture = 858
Build a multi-statement query when inserting fixtures:
- The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table
- The previous code was bulk inserting fixtures for a single table, making X query for X fixture files
- This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers):
```ruby
# before
%w(authors dogs computers).each do |table|
sql = build_sql(table)
connection.query(sql)
end
# after
sql = build_sql(authors, dogs, computers)
connection.query(sql)
```
- `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement
- My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds
- In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats:
1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)...
- In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db
- As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later
2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value
- Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction
3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html)
- Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets)
- Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one
- On mysql the `max_allowed_packet` is looked up:
1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised
2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
2017-12-12 15:38:10 -05:00
fixtures = {
" traffic_lights " = > [
2018-01-23 14:21:58 -05:00
{ " location " = > " US " , " state " = > [ " NY " ] , " long_state " = > [ " a " * bytes_needed_to_have_a_1024_bytes_fixture ] } ,
Build a multi-statement query when inserting fixtures:
- The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table
- The previous code was bulk inserting fixtures for a single table, making X query for X fixture files
- This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers):
```ruby
# before
%w(authors dogs computers).each do |table|
sql = build_sql(table)
connection.query(sql)
end
# after
sql = build_sql(authors, dogs, computers)
connection.query(sql)
```
- `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement
- My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds
- In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats:
1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)...
- In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db
- As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later
2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value
- Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction
3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html)
- Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets)
- Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one
- On mysql the `max_allowed_packet` is looked up:
1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised
2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
2017-12-12 15:38:10 -05:00
]
}
2018-07-10 09:21:08 -04:00
conn . stub ( :max_allowed_packet , packet_size - mysql_margin ) do
error = assert_raises ( ActiveRecord :: ActiveRecordError ) { conn . insert_fixtures_set ( fixtures ) }
assert_match ( / Fixtures set is too large #{ packet_size } \ . / , error . message )
end
Build a multi-statement query when inserting fixtures:
- The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table
- The previous code was bulk inserting fixtures for a single table, making X query for X fixture files
- This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers):
```ruby
# before
%w(authors dogs computers).each do |table|
sql = build_sql(table)
connection.query(sql)
end
# after
sql = build_sql(authors, dogs, computers)
connection.query(sql)
```
- `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement
- My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds
- In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats:
1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)...
- In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db
- As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later
2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value
- Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction
3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html)
- Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets)
- Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one
- On mysql the `max_allowed_packet` is looked up:
1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised
2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
2017-12-12 15:38:10 -05:00
end
def test_insert_fixture_set_when_max_allowed_packet_is_bigger_than_fixtures_set_size
conn = ActiveRecord :: Base . connection
packet_size = 1024
fixtures = {
" traffic_lights " = > [
{ " location " = > " US " , " state " = > [ " NY " ] , " long_state " = > [ " a " * 51 ] } ,
]
}
2018-07-10 09:21:08 -04:00
conn . stub ( :max_allowed_packet , packet_size ) do
assert_difference " TrafficLight.count " do
conn . insert_fixtures_set ( fixtures )
end
Build a multi-statement query when inserting fixtures:
- The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table
- The previous code was bulk inserting fixtures for a single table, making X query for X fixture files
- This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers):
```ruby
# before
%w(authors dogs computers).each do |table|
sql = build_sql(table)
connection.query(sql)
end
# after
sql = build_sql(authors, dogs, computers)
connection.query(sql)
```
- `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement
- My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds
- In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats:
1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)...
- In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db
- As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later
2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value
- Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction
3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html)
- Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets)
- Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one
- On mysql the `max_allowed_packet` is looked up:
1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised
2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
2017-12-12 15:38:10 -05:00
end
end
def test_insert_fixtures_set_split_the_total_sql_into_two_chunks_smaller_than_max_allowed_packet
subscriber = InsertQuerySubscriber . new
subscription = ActiveSupport :: Notifications . subscribe ( " sql.active_record " , subscriber )
conn = ActiveRecord :: Base . connection
packet_size = 1024
fixtures = {
" traffic_lights " = > [
{ " location " = > " US " , " state " = > [ " NY " ] , " long_state " = > [ " a " * 450 ] } ,
] ,
" comments " = > [
{ " post_id " = > 1 , " body " = > " a " * 450 } ,
]
}
2018-07-10 09:21:08 -04:00
conn . stub ( :max_allowed_packet , packet_size ) do
conn . insert_fixtures_set ( fixtures )
Build a multi-statement query when inserting fixtures:
- The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table
- The previous code was bulk inserting fixtures for a single table, making X query for X fixture files
- This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers):
```ruby
# before
%w(authors dogs computers).each do |table|
sql = build_sql(table)
connection.query(sql)
end
# after
sql = build_sql(authors, dogs, computers)
connection.query(sql)
```
- `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement
- My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds
- In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats:
1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)...
- In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db
- As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later
2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value
- Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction
3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html)
- Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets)
- Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one
- On mysql the `max_allowed_packet` is looked up:
1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised
2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
2017-12-12 15:38:10 -05:00
2018-07-10 09:21:08 -04:00
assert_equal 2 , subscriber . events . size
assert_operator subscriber . events . first . bytesize , :< , packet_size
assert_operator subscriber . events . second . bytesize , :< , packet_size
end
Build a multi-statement query when inserting fixtures:
- The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table
- The previous code was bulk inserting fixtures for a single table, making X query for X fixture files
- This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers):
```ruby
# before
%w(authors dogs computers).each do |table|
sql = build_sql(table)
connection.query(sql)
end
# after
sql = build_sql(authors, dogs, computers)
connection.query(sql)
```
- `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement
- My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds
- In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats:
1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)...
- In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db
- As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later
2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value
- Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction
3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html)
- Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets)
- Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one
- On mysql the `max_allowed_packet` is looked up:
1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised
2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
2017-12-12 15:38:10 -05:00
ensure
ActiveSupport :: Notifications . unsubscribe ( subscription )
end
def test_insert_fixtures_set_concat_total_sql_into_a_single_packet_smaller_than_max_allowed_packet
subscriber = InsertQuerySubscriber . new
subscription = ActiveSupport :: Notifications . subscribe ( " sql.active_record " , subscriber )
conn = ActiveRecord :: Base . connection
packet_size = 1024
fixtures = {
" traffic_lights " = > [
{ " location " = > " US " , " state " = > [ " NY " ] , " long_state " = > [ " a " * 200 ] } ,
] ,
" comments " = > [
{ " post_id " = > 1 , " body " = > " a " * 200 } ,
]
}
2018-07-10 09:21:08 -04:00
conn . stub ( :max_allowed_packet , packet_size ) do
assert_difference [ " TrafficLight.count " , " Comment.count " ] , + 1 do
conn . insert_fixtures_set ( fixtures )
end
Build a multi-statement query when inserting fixtures:
- The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table
- The previous code was bulk inserting fixtures for a single table, making X query for X fixture files
- This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers):
```ruby
# before
%w(authors dogs computers).each do |table|
sql = build_sql(table)
connection.query(sql)
end
# after
sql = build_sql(authors, dogs, computers)
connection.query(sql)
```
- `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement
- My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds
- In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats:
1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)...
- In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db
- As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later
2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value
- Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction
3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html)
- Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets)
- Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one
- On mysql the `max_allowed_packet` is looked up:
1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised
2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
2017-12-12 15:38:10 -05:00
end
assert_equal 1 , subscriber . events . size
ensure
ActiveSupport :: Notifications . unsubscribe ( subscription )
end
2017-06-18 11:14:52 -04:00
end
2018-01-25 22:42:28 -05:00
def test_auto_value_on_primary_key
fixtures = [
{ " name " = > " first " , " wheels_count " = > 2 } ,
{ " name " = > " second " , " wheels_count " = > 3 }
]
conn = ActiveRecord :: Base . connection
assert_nothing_raised do
conn . insert_fixtures_set ( { " aircraft " = > fixtures } , [ " aircraft " ] )
2018-01-16 15:45:18 -05:00
end
2018-01-25 22:42:28 -05:00
result = conn . select_all ( " SELECT name, wheels_count FROM aircraft ORDER BY id " )
assert_equal fixtures , result . to_a
2018-01-16 15:45:18 -05:00
end
2018-01-25 15:47:35 -05:00
def test_deprecated_insert_fixtures
fixtures = [
{ " name " = > " first " , " wheels_count " = > 2 } ,
{ " name " = > " second " , " wheels_count " = > 3 }
]
conn = ActiveRecord :: Base . connection
conn . delete ( " DELETE FROM aircraft " )
assert_deprecated do
conn . insert_fixtures ( fixtures , " aircraft " )
end
result = conn . select_all ( " SELECT name, wheels_count FROM aircraft ORDER BY id " )
assert_equal fixtures , result . to_a
end
2011-09-01 18:00:31 -04:00
def test_broken_yaml_exception
2016-08-06 12:26:20 -04:00
badyaml = Tempfile . new [ " foo " , " .yml " ]
badyaml . write " a: : "
2011-09-01 18:00:31 -04:00
badyaml . flush
dir = File . dirname badyaml . path
2016-08-06 12:26:20 -04:00
name = File . basename badyaml . path , " .yml "
2011-09-01 18:00:31 -04:00
assert_raises ( ActiveRecord :: Fixture :: FormatError ) do
2012-05-12 07:17:03 -04:00
ActiveRecord :: FixtureSet . create_fixtures ( dir , name )
2011-09-01 18:00:31 -04:00
end
ensure
badyaml . close
badyaml . unlink
end
2011-04-29 21:21:07 -04:00
def test_create_fixtures
2012-05-12 07:17:03 -04:00
fixtures = ActiveRecord :: FixtureSet . create_fixtures ( FIXTURES_ROOT , " parrots " )
2016-08-06 12:26:20 -04:00
assert Parrot . find_by_name ( " Curious George " ) , " George is not in the database "
assert fixtures . detect { | f | f . name == " parrots " } , " no fixtures named 'parrots' in #{ fixtures . map ( & :name ) . inspect } "
2011-04-29 21:21:07 -04:00
end
2004-11-23 20:04:44 -05:00
def test_multiple_clean_fixtures
fixtures_array = nil
assert_nothing_raised { fixtures_array = create_fixtures ( * FIXTURES ) }
assert_kind_of ( Array , fixtures_array )
2012-05-12 07:17:03 -04:00
fixtures_array . each { | fixtures | assert_kind_of ( ActiveRecord :: FixtureSet , fixtures ) }
2004-11-23 20:04:44 -05:00
end
2012-01-04 00:49:10 -05:00
def test_create_symbol_fixtures
2016-08-06 13:37:57 -04:00
fixtures = ActiveRecord :: FixtureSet . create_fixtures ( FIXTURES_ROOT , :collections , collections : Course ) { Course . connection }
2012-01-04 00:49:10 -05:00
2016-08-06 12:26:20 -04:00
assert Course . find_by_name ( " Collection " ) , " course is not in the database "
assert fixtures . detect { | f | f . name == " collections " } , " no fixtures named 'collections' in #{ fixtures . map ( & :name ) . inspect } "
2012-01-04 00:49:10 -05:00
end
2004-11-23 20:04:44 -05:00
def test_attributes
2011-02-11 19:09:34 -05:00
topics = create_fixtures ( " topics " ) . first
2004-11-23 20:04:44 -05:00
assert_equal ( " The First Topic " , topics [ " first " ] [ " title " ] )
assert_nil ( topics [ " second " ] [ " author_email_address " ] )
end
2017-04-06 21:43:58 -04:00
def test_no_args_returns_all
all_topics = topics
assert_equal 5 , all_topics . length
assert_equal " The First Topic " , all_topics . first [ " title " ]
assert_equal 5 , all_topics . last . id
end
def test_no_args_record_returns_all_without_array
all_binaries = binaries
assert_kind_of ( Array , all_binaries )
2017-08-04 05:58:16 -04:00
assert_equal 2 , binaries . length
2017-04-06 21:43:58 -04:00
end
def test_nil_raises
assert_raise ( StandardError ) { topics ( nil ) }
assert_raise ( StandardError ) { topics ( [ nil ] ) }
end
2004-11-23 20:04:44 -05:00
def test_inserts
2010-11-16 20:06:50 -05:00
create_fixtures ( " topics " )
2007-09-28 10:56:07 -04:00
first_row = ActiveRecord :: Base . connection . select_one ( " SELECT * FROM topics WHERE author_name = 'David' " )
assert_equal ( " The First Topic " , first_row [ " title " ] )
2004-11-23 20:04:44 -05:00
2007-09-28 10:56:07 -04:00
second_row = ActiveRecord :: Base . connection . select_one ( " SELECT * FROM topics WHERE author_name = 'Mary' " )
assert_nil ( second_row [ " author_email_address " ] )
2004-11-23 20:04:44 -05:00
end
2017-02-25 11:45:04 -05:00
def test_inserts_with_pre_and_suffix
# Reset cache to make finds on the new table work
ActiveRecord :: FixtureSet . reset_cache
ActiveRecord :: Base . connection . create_table :prefix_other_topics_suffix do | t |
t . column :title , :string
t . column :author_name , :string
t . column :author_email_address , :string
t . column :written_on , :datetime
t . column :bonus_time , :time
t . column :last_read , :date
t . column :content , :string
t . column :approved , :boolean , default : true
t . column :replies_count , :integer , default : 0
t . column :parent_id , :integer
t . column :type , :string , limit : 50
end
2005-11-16 03:16:54 -05:00
2017-02-25 11:45:04 -05:00
# Store existing prefix/suffix
old_prefix = ActiveRecord :: Base . table_name_prefix
old_suffix = ActiveRecord :: Base . table_name_suffix
2005-11-16 03:16:54 -05:00
2017-02-25 11:45:04 -05:00
# Set a prefix/suffix we can test against
ActiveRecord :: Base . table_name_prefix = " prefix_ "
ActiveRecord :: Base . table_name_suffix = " _suffix "
2005-11-16 03:16:54 -05:00
2017-02-25 11:45:04 -05:00
other_topic_klass = Class . new ( ActiveRecord :: Base ) do
def self . name
" OtherTopic "
2011-12-19 10:07:02 -05:00
end
2017-02-25 11:45:04 -05:00
end
2011-12-19 10:07:02 -05:00
2017-02-25 11:45:04 -05:00
topics = [ create_fixtures ( " other_topics " ) ] . flatten . first
2011-12-19 10:07:02 -05:00
2017-02-25 11:45:04 -05:00
# This checks for a caching problem which causes a bug in the fixtures
# class-level configuration helper.
assert_not_nil topics , " Fixture data inserted, but fixture objects not returned from create "
2011-12-19 10:07:02 -05:00
2017-02-25 11:45:04 -05:00
first_row = ActiveRecord :: Base . connection . select_one ( " SELECT * FROM prefix_other_topics_suffix WHERE author_name = 'David' " )
assert_not_nil first_row , " The prefix_other_topics_suffix table appears to be empty despite create_fixtures: the row with author_name = 'David' was not found "
assert_equal ( " The First Topic " , first_row [ " title " ] )
2011-12-19 10:07:02 -05:00
2017-02-25 11:45:04 -05:00
second_row = ActiveRecord :: Base . connection . select_one ( " SELECT * FROM prefix_other_topics_suffix WHERE author_name = 'Mary' " )
assert_nil ( second_row [ " author_email_address " ] )
2011-12-19 10:07:02 -05:00
2017-02-25 11:45:04 -05:00
assert_equal :prefix_other_topics_suffix , topics . table_name . to_sym
# This assertion should preferably be the last in the list, because calling
# other_topic_klass.table_name sets a class-level instance variable
assert_equal :prefix_other_topics_suffix , other_topic_klass . table_name . to_sym
2011-12-19 10:07:02 -05:00
2017-02-25 11:45:04 -05:00
ensure
# Restore prefix/suffix to its previous values
ActiveRecord :: Base . table_name_prefix = old_prefix
ActiveRecord :: Base . table_name_suffix = old_suffix
2006-03-01 11:01:53 -05:00
2017-02-25 11:45:04 -05:00
ActiveRecord :: Base . connection . drop_table :prefix_other_topics_suffix rescue nil
2005-09-02 06:51:23 -04:00
end
2005-04-07 02:54:25 -04:00
def test_insert_with_datetime
2010-11-16 20:06:50 -05:00
create_fixtures ( " tasks " )
2005-04-07 02:54:25 -04:00
first = Task . find ( 1 )
assert first
end
2004-11-23 20:04:44 -05:00
def test_logger_level_invariant
level = ActiveRecord :: Base . logger . level
2016-08-06 12:26:20 -04:00
create_fixtures ( " topics " )
2004-11-23 20:04:44 -05:00
assert_equal level , ActiveRecord :: Base . logger . level
end
2004-12-09 10:52:54 -05:00
2004-11-23 20:04:44 -05:00
def test_instantiation
2011-02-11 19:09:34 -05:00
topics = create_fixtures ( " topics " ) . first
2004-11-23 20:04:44 -05:00
assert_kind_of Topic , topics [ " first " ] . find
end
2004-12-09 10:52:54 -05:00
2004-11-23 20:04:44 -05:00
def test_complete_instantiation
assert_equal " The First Topic " , @first . title
end
2004-12-09 10:52:54 -05:00
2004-11-23 20:04:44 -05:00
def test_fixtures_from_root_yml_with_instantiation
assert_equal 50 , @unknown . credit_limit
end
2004-12-09 10:52:54 -05:00
2004-11-23 20:04:44 -05:00
def test_erb_in_fixtures
assert_equal " fixture_5 " , @dev_5 . name
end
2004-12-14 19:46:26 -05:00
def test_empty_yaml_fixture
2018-11-03 08:38:19 -04:00
assert_not_nil ActiveRecord :: FixtureSet . new ( nil , " accounts " , Account , FIXTURES_ROOT + " /naked/yml/accounts " )
2004-12-14 19:46:26 -05:00
end
def test_empty_yaml_fixture_with_a_comment_in_it
2018-11-03 08:38:19 -04:00
assert_not_nil ActiveRecord :: FixtureSet . new ( nil , " companies " , Company , FIXTURES_ROOT + " /naked/yml/companies " )
2004-12-14 19:46:26 -05:00
end
2010-08-10 22:09:24 -04:00
def test_nonexistent_fixture_file
nonexistent_fixture_path = FIXTURES_ROOT + " /imnothere "
2017-12-14 03:30:54 -05:00
# sanity check to make sure that this file never exists
2018-01-25 18:16:57 -05:00
assert_empty Dir [ nonexistent_fixture_path + " * " ]
2010-08-10 22:09:24 -04:00
2011-12-10 18:32:14 -05:00
assert_raise ( Errno :: ENOENT ) do
2018-11-03 08:38:19 -04:00
ActiveRecord :: FixtureSet . new ( nil , " companies " , Company , nonexistent_fixture_path )
2010-08-10 22:09:24 -04:00
end
end
2004-12-14 19:46:26 -05:00
def test_dirty_dirty_yaml_file
2016-12-15 15:19:34 -05:00
fixture_path = FIXTURES_ROOT + " /naked/yml/courses "
error = assert_raise ( ActiveRecord :: Fixture :: FormatError ) do
2018-11-03 08:38:19 -04:00
ActiveRecord :: FixtureSet . new ( nil , " courses " , Course , fixture_path )
2004-12-14 19:46:26 -05:00
end
2016-12-15 15:19:34 -05:00
assert_equal " fixture is not a hash: #{ fixture_path } .yml " , error . to_s
end
def test_yaml_file_with_one_invalid_fixture
fixture_path = FIXTURES_ROOT + " /naked/yml/courses_with_invalid_key "
error = assert_raise ( ActiveRecord :: Fixture :: FormatError ) do
2018-11-03 08:38:19 -04:00
ActiveRecord :: FixtureSet . new ( nil , " courses " , Course , fixture_path )
2016-12-15 15:19:34 -05:00
end
assert_equal " fixture key is not a hash: #{ fixture_path } .yml, keys: [ \" two \" ] " , error . to_s
2004-12-14 19:46:26 -05:00
end
2015-08-13 10:35:09 -04:00
def test_yaml_file_with_invalid_column
e = assert_raise ( ActiveRecord :: Fixture :: FixtureError ) do
ActiveRecord :: FixtureSet . create_fixtures ( FIXTURES_ROOT + " /naked/yml " , " parrots " )
end
2017-06-18 11:14:52 -04:00
if current_adapter? ( :SQLite3Adapter )
assert_equal ( %( table "parrots" has no column named "arrr". ) , e . message )
else
assert_equal ( %( table "parrots" has no columns named "arrr", "foobar". ) , e . message )
end
2015-08-13 10:35:09 -04:00
end
2016-01-13 04:11:54 -05:00
def test_yaml_file_with_symbol_columns
ActiveRecord :: FixtureSet . create_fixtures ( FIXTURES_ROOT + " /naked/yml " , " trees " )
end
2005-10-14 22:01:38 -04:00
def test_omap_fixtures
assert_nothing_raised do
2018-11-03 08:38:19 -04:00
fixtures = ActiveRecord :: FixtureSet . new ( nil , " categories " , Category , FIXTURES_ROOT + " /categories_ordered " )
2005-10-14 22:01:38 -04:00
2011-07-08 08:45:41 -04:00
fixtures . each . with_index do | ( name , fixture ) , i |
2005-10-14 22:01:38 -04:00
assert_equal " fixture_no_ #{ i } " , name
2016-08-06 12:26:20 -04:00
assert_equal " Category #{ i } " , fixture [ " name " ]
2005-10-14 22:01:38 -04:00
end
end
2005-10-15 23:45:39 -04:00
end
2006-02-27 00:14:57 -05:00
def test_yml_file_in_subdirectory
2006-02-28 15:39:21 -05:00
assert_equal ( categories ( :sub_special_1 ) . name , " A special category in a subdir file " )
assert_equal ( categories ( :sub_special_1 ) . class , SpecialCategory )
2006-02-27 00:14:57 -05:00
end
def test_subsubdir_file_with_arbitrary_name
2006-02-28 15:39:21 -05:00
assert_equal ( categories ( :sub_special_3 ) . name , " A special category in an arbitrarily named subsubdir file " )
assert_equal ( categories ( :sub_special_3 ) . class , SpecialCategory )
2006-02-27 00:14:57 -05:00
end
2007-05-21 14:54:51 -04:00
def test_binary_in_fixtures
2016-08-06 12:26:20 -04:00
data = File . open ( ASSETS_ROOT + " /flowers.jpg " , " rb " ) { | f | f . read }
data . force_encoding ( " ASCII-8BIT " )
2008-04-01 02:58:52 -04:00
data . freeze
2007-05-21 14:54:51 -04:00
assert_equal data , @flowers . data
2017-08-04 05:58:16 -04:00
assert_equal data , @binary_helper . data
2007-05-21 14:54:51 -04:00
end
2010-12-29 11:15:45 -05:00
def test_serialized_fixtures
assert_equal [ " Green " , " Red " , " Orange " ] , traffic_lights ( :uk ) . state
end
2013-06-10 07:52:22 -04:00
def test_fixtures_are_set_up_with_database_env_variable
2016-08-06 12:26:20 -04:00
db_url_tmp = ENV [ " DATABASE_URL " ]
ENV [ " DATABASE_URL " ] = " sqlite3::memory: "
2015-08-15 15:35:42 -04:00
ActiveRecord :: Base . stub ( :configurations , { } ) do
test_case = Class . new ( ActiveRecord :: TestCase ) do
fixtures :accounts
2013-06-10 07:52:22 -04:00
2015-08-15 15:35:42 -04:00
def test_fixtures
assert accounts ( :signals37 )
end
2013-06-10 07:52:22 -04:00
end
2015-08-15 15:35:42 -04:00
result = test_case . new ( :test_fixtures ) . run
2013-06-10 07:52:22 -04:00
2015-08-15 15:35:42 -04:00
assert result . passed? , " Expected #{ result . name } to pass: \n #{ result } "
end
2013-10-07 04:15:23 -04:00
ensure
2016-08-06 12:26:20 -04:00
ENV [ " DATABASE_URL " ] = db_url_tmp
2013-06-10 07:52:22 -04:00
end
2005-10-18 19:52:07 -04:00
end
2016-08-11 00:16:07 -04:00
class HasManyThroughFixture < ActiveRecord :: TestCase
2013-09-06 14:29:40 -04:00
def make_model ( name )
Class . new ( ActiveRecord :: Base ) { define_singleton_method ( :name ) { name } }
end
2014-10-29 08:48:25 -04:00
def test_has_many_through_with_default_table_name
2013-09-06 14:29:40 -04:00
pt = make_model " ParrotTreasure "
parrot = make_model " Parrot "
treasure = make_model " Treasure "
pt . table_name = " parrots_treasures "
2016-08-06 13:37:57 -04:00
pt . belongs_to :parrot , anonymous_class : parrot
pt . belongs_to :treasure , anonymous_class : treasure
2013-09-06 14:29:40 -04:00
2016-08-06 13:37:57 -04:00
parrot . has_many :parrot_treasures , anonymous_class : pt
parrot . has_many :treasures , through : :parrot_treasures
2013-09-06 14:29:40 -04:00
2016-08-06 12:26:20 -04:00
parrots = File . join FIXTURES_ROOT , " parrots "
2013-09-06 14:29:40 -04:00
2018-11-03 08:38:19 -04:00
fs = ActiveRecord :: FixtureSet . new ( nil , " parrots " , parrot , parrots )
2013-09-06 14:29:40 -04:00
rows = fs . table_rows
2016-08-06 12:26:20 -04:00
assert_equal load_has_and_belongs_to_many [ " parrots_treasures " ] , rows [ " parrots_treasures " ]
2013-09-06 14:29:40 -04:00
end
2014-10-29 08:48:25 -04:00
def test_has_many_through_with_renamed_table
pt = make_model " ParrotTreasure "
parrot = make_model " Parrot "
treasure = make_model " Treasure "
2016-08-06 13:37:57 -04:00
pt . belongs_to :parrot , anonymous_class : parrot
pt . belongs_to :treasure , anonymous_class : treasure
2014-10-29 08:48:25 -04:00
2016-08-06 13:37:57 -04:00
parrot . has_many :parrot_treasures , anonymous_class : pt
parrot . has_many :treasures , through : :parrot_treasures
2014-10-29 08:48:25 -04:00
2016-08-06 12:26:20 -04:00
parrots = File . join FIXTURES_ROOT , " parrots "
2014-10-29 08:48:25 -04:00
2018-11-03 08:38:19 -04:00
fs = ActiveRecord :: FixtureSet . new ( nil , " parrots " , parrot , parrots )
2014-10-29 08:48:25 -04:00
rows = fs . table_rows
2016-08-06 12:26:20 -04:00
assert_equal load_has_and_belongs_to_many [ " parrots_treasures " ] , rows [ " parrot_treasures " ]
2014-10-29 08:48:25 -04:00
end
2015-03-18 11:15:05 -04:00
def test_has_and_belongs_to_many_order
assert_equal [ " parrots " , " parrots_treasures " ] , load_has_and_belongs_to_many . keys
end
2013-09-06 14:29:40 -04:00
def load_has_and_belongs_to_many
parrot = make_model " Parrot "
parrot . has_and_belongs_to_many :treasures
2016-08-06 12:26:20 -04:00
parrots = File . join FIXTURES_ROOT , " parrots "
2013-09-06 14:29:40 -04:00
2018-11-03 08:38:19 -04:00
fs = ActiveRecord :: FixtureSet . new ( nil , " parrots " , parrot , parrots )
2013-09-06 14:29:40 -04:00
fs . table_rows
end
end
2005-10-18 19:52:07 -04:00
if Account . connection . respond_to? ( :reset_pk_sequence! )
2008-01-21 12:20:51 -05:00
class FixturesResetPkSequenceTest < ActiveRecord :: TestCase
2005-10-18 19:52:07 -04:00
fixtures :accounts
2005-11-12 06:59:54 -05:00
fixtures :companies
2017-05-30 10:04:36 -04:00
self . use_transactional_tests = false
2005-10-15 23:45:39 -04:00
2005-11-12 06:59:54 -05:00
def setup
2016-08-06 13:37:57 -04:00
@instances = [ Account . new ( credit_limit : 50 ) , Company . new ( name : " RoR Consulting " ) , Course . new ( name : " Test " ) ]
2012-05-12 07:17:03 -04:00
ActiveRecord :: FixtureSet . reset_cache # make sure tables get reinitialized
2005-11-12 06:59:54 -05:00
end
def test_resets_to_min_pk_with_specified_pk_and_sequence
@instances . each do | instance |
model = instance . class
model . delete_all
model . connection . reset_pk_sequence! ( model . table_name , model . primary_key , model . sequence_name )
2005-10-15 23:45:39 -04:00
2005-11-12 06:59:54 -05:00
instance . save!
assert_equal 1 , instance . id , " Sequence reset for #{ model . table_name } failed. "
end
2005-10-15 23:45:39 -04:00
end
2005-11-12 06:59:54 -05:00
def test_resets_to_min_pk_with_default_pk_and_sequence
@instances . each do | instance |
model = instance . class
model . delete_all
model . connection . reset_pk_sequence! ( model . table_name )
instance . save!
assert_equal 1 , instance . id , " Sequence reset for #{ model . table_name } failed. "
2005-10-15 23:45:39 -04:00
end
2005-11-12 06:59:54 -05:00
end
2005-10-15 23:45:39 -04:00
2007-09-28 10:56:07 -04:00
def test_create_fixtures_resets_sequences_when_not_cached
2005-11-12 06:59:54 -05:00
@instances . each do | instance |
2011-02-11 19:09:34 -05:00
max_id = create_fixtures ( instance . class . table_name ) . first . fixtures . inject ( 0 ) do | _max_id , ( _ , fixture ) |
2016-08-06 12:26:20 -04:00
fixture_id = fixture [ " id " ] . to_i
2010-07-14 15:43:59 -04:00
fixture_id > _max_id ? fixture_id : _max_id
2005-11-12 06:59:54 -05:00
end
# Clone the last fixture to check that it gets the next greatest id.
instance . save!
assert_equal max_id + 1 , instance . id , " Sequence reset for #{ instance . class . table_name } failed. "
end
2005-10-15 23:45:39 -04:00
end
2005-10-14 22:01:38 -04:00
end
2005-03-06 08:51:55 -05:00
end
2008-01-21 12:20:51 -05:00
class FixturesWithoutInstantiationTest < ActiveRecord :: TestCase
2005-03-06 08:51:55 -05:00
self . use_instantiated_fixtures = false
fixtures :topics , :developers , :accounts
def test_without_complete_instantiation
2018-04-17 18:21:34 -04:00
assert_not defined? ( @first )
assert_not defined? ( @topics )
assert_not defined? ( @developers )
assert_not defined? ( @accounts )
2005-03-06 08:51:55 -05:00
end
def test_fixtures_from_root_yml_without_instantiation
2018-05-12 22:26:10 -04:00
assert_not defined? ( @unknown ) , " @unknown is not defined "
2005-03-06 08:51:55 -05:00
end
2010-08-14 01:13:00 -04:00
2010-05-16 10:12:13 -04:00
def test_visibility_of_accessor_method
assert_equal false , respond_to? ( :topics , false ) , " should be private method "
assert_equal true , respond_to? ( :topics , true ) , " confirm to respond surely "
end
2005-06-03 07:49:34 -04:00
def test_accessor_methods
assert_equal " The First Topic " , topics ( :first ) . title
assert_equal " Jamis " , developers ( :jamis ) . name
assert_equal 50 , accounts ( :signals37 ) . credit_limit
end
2007-06-04 23:47:02 -04:00
def test_accessor_methods_with_multiple_args
assert_equal 2 , topics ( :first , :second ) . size
assert_raise ( StandardError ) { topics ( [ :first , :second ] ) }
end
2009-02-03 21:25:37 -05:00
def test_reloading_fixtures_through_accessor_methods
2015-09-09 15:00:40 -04:00
topic = Struct . new ( :title )
2009-02-03 21:25:37 -05:00
assert_equal " The First Topic " , topics ( :first ) . title
2016-08-06 12:26:20 -04:00
assert_called ( @loaded_fixtures [ " topics " ] [ " first " ] , :find , returns : topic . new ( " Fresh Topic! " ) ) do
2015-09-09 15:00:40 -04:00
assert_equal " Fresh Topic! " , topics ( :first , true ) . title
end
2007-06-04 23:47:02 -04:00
end
2005-03-06 08:51:55 -05:00
end
2008-01-21 12:20:51 -05:00
class FixturesWithoutInstanceInstantiationTest < ActiveRecord :: TestCase
2005-06-10 10:58:02 -04:00
self . use_instantiated_fixtures = true
2005-03-20 08:42:35 -05:00
self . use_instantiated_fixtures = :no_instances
2005-06-10 10:58:02 -04:00
2005-03-20 08:42:35 -05:00
fixtures :topics , :developers , :accounts
def test_without_instance_instantiation
2018-05-12 22:26:10 -04:00
assert_not defined? ( @first ) , " @first is not defined "
2005-03-20 08:42:35 -05:00
end
end
2008-01-21 12:20:51 -05:00
class TransactionalFixturesTest < ActiveRecord :: TestCase
2005-06-10 10:58:02 -04:00
self . use_instantiated_fixtures = true
2015-03-10 22:21:19 -04:00
self . use_transactional_tests = true
2005-06-10 10:58:02 -04:00
2005-03-06 08:51:55 -05:00
fixtures :topics
def test_destroy
assert_not_nil @first
@first . destroy
end
def test_destroy_just_kidding
assert_not_nil @first
end
2004-12-01 07:25:04 -05:00
end
2005-03-17 14:36:44 -05:00
2008-01-21 12:20:51 -05:00
class MultipleFixturesTest < ActiveRecord :: TestCase
2005-03-17 14:36:44 -05:00
fixtures :topics
fixtures :developers , :accounts
def test_fixture_table_names
2005-10-23 15:02:38 -04:00
assert_equal %w( topics developers accounts ) , fixture_table_names
2005-03-17 14:36:44 -05:00
end
end
2008-01-21 12:20:51 -05:00
class SetupTest < ActiveRecord :: TestCase
2007-12-19 17:49:27 -05:00
# fixtures :topics
2008-01-18 02:30:42 -05:00
2007-12-19 17:49:27 -05:00
def setup
@first = true
end
2008-01-18 02:30:42 -05:00
2007-12-19 17:49:27 -05:00
def test_nothing
end
end
class SetupSubclassTest < SetupTest
def setup
super
@second = true
end
2008-01-18 02:30:42 -05:00
2007-12-19 17:49:27 -05:00
def test_subclassing_should_preserve_setups
assert @first
assert @second
end
end
2008-01-21 12:20:51 -05:00
class OverlappingFixturesTest < ActiveRecord :: TestCase
2005-03-17 14:36:44 -05:00
fixtures :topics , :developers
fixtures :developers , :accounts
def test_fixture_table_names
2005-10-23 15:02:38 -04:00
assert_equal %w( topics developers accounts ) , fixture_table_names
2005-03-17 14:36:44 -05:00
end
end
2005-04-18 03:52:58 -04:00
2008-01-21 12:20:51 -05:00
class ForeignKeyFixturesTest < ActiveRecord :: TestCase
2005-04-18 03:52:58 -04:00
fixtures :fk_test_has_pk , :fk_test_has_fk
# if foreign keys are implemented and fixtures
# are not deleted in reverse order then this test
# case will raise StatementInvalid
def test_number1
assert true
end
def test_number2
assert true
end
end
2006-02-27 15:29:28 -05:00
2011-02-15 18:52:27 -05:00
class OverRideFixtureMethodTest < ActiveRecord :: TestCase
fixtures :topics
def topics ( name )
topic = super
2016-08-06 12:26:20 -04:00
topic . title = " omg "
2011-02-15 18:52:27 -05:00
topic
end
def test_fixture_methods_can_be_overridden
x = topics :first
2016-08-06 12:26:20 -04:00
assert_equal " omg " , x . title
2011-02-15 18:52:27 -05:00
end
end
2015-06-15 23:16:39 -04:00
class FixtureWithSetModelClassTest < ActiveRecord :: TestCase
fixtures :other_posts , :other_comments
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
# and thus takes into account the +set_model_class+.
self . use_transactional_tests = false
2015-09-30 05:06:41 -04:00
def test_uses_fixture_class_defined_in_yaml
2015-06-15 23:16:39 -04:00
assert_kind_of Post , other_posts ( :second_welcome )
end
def test_loads_the_associations_to_fixtures_with_set_model_class
post = other_posts ( :second_welcome )
comment = other_comments ( :second_greetings )
assert_equal [ comment ] , post . comments
assert_equal post , comment . post
end
end
class SetFixtureClassPrevailsTest < ActiveRecord :: TestCase
set_fixture_class bad_posts : Post
fixtures :bad_posts
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
# and thus takes into account the +set_model_class+.
self . use_transactional_tests = false
2015-09-30 05:06:41 -04:00
def test_uses_set_fixture_class
2015-06-15 23:16:39 -04:00
assert_kind_of Post , bad_posts ( :bad_welcome )
end
end
2008-03-28 16:21:20 -04:00
class CheckSetTableNameFixturesTest < ActiveRecord :: TestCase
2016-08-06 13:37:57 -04:00
set_fixture_class funny_jokes : Joke
2006-02-27 15:29:28 -05:00
fixtures :funny_jokes
2010-08-14 01:13:00 -04:00
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
2008-03-28 07:55:01 -04:00
# and thus takes into account our set_fixture_class
2015-03-10 22:21:19 -04:00
self . use_transactional_tests = false
2007-09-28 10:56:07 -04:00
2006-02-27 15:29:28 -05:00
def test_table_method
assert_kind_of Joke , funny_jokes ( :a_joke )
end
end
2006-03-06 18:03:35 -05:00
2008-07-13 21:01:52 -04:00
class FixtureNameIsNotTableNameFixturesTest < ActiveRecord :: TestCase
2016-08-06 13:37:57 -04:00
set_fixture_class items : Book
2008-07-13 21:01:52 -04:00
fixtures :items
2010-08-14 01:13:00 -04:00
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
2008-07-13 21:01:52 -04:00
# and thus takes into account our set_fixture_class
2015-03-10 22:21:19 -04:00
self . use_transactional_tests = false
2008-07-13 21:01:52 -04:00
def test_named_accessor
assert_kind_of Book , items ( :dvd )
end
end
class FixtureNameIsNotTableNameMultipleFixturesTest < ActiveRecord :: TestCase
2016-08-06 13:37:57 -04:00
set_fixture_class items : Book , funny_jokes : Joke
2008-07-13 21:01:52 -04:00
fixtures :items , :funny_jokes
2010-08-14 01:13:00 -04:00
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
2008-07-13 21:01:52 -04:00
# and thus takes into account our set_fixture_class
2015-03-10 22:21:19 -04:00
self . use_transactional_tests = false
2008-07-13 21:01:52 -04:00
def test_named_accessor_of_differently_named_fixture
assert_kind_of Book , items ( :dvd )
end
def test_named_accessor_of_same_named_fixture
assert_kind_of Joke , funny_jokes ( :a_joke )
end
end
2008-01-21 12:20:51 -05:00
class CustomConnectionFixturesTest < ActiveRecord :: TestCase
2016-08-06 13:37:57 -04:00
set_fixture_class courses : Course
2007-01-23 20:29:49 -05:00
fixtures :courses
2015-03-10 22:21:19 -04:00
self . use_transactional_tests = false
2007-09-28 10:56:07 -04:00
2011-10-05 20:21:43 -04:00
def test_leaky_destroy
assert_nothing_raised { courses ( :ruby ) }
courses ( :ruby ) . destroy
end
def test_it_twice_in_whatever_order_to_check_for_fixture_leakage
test_leaky_destroy
end
end
class TransactionalFixturesOnCustomConnectionTest < ActiveRecord :: TestCase
2016-08-06 13:37:57 -04:00
set_fixture_class courses : Course
2011-10-05 20:21:43 -04:00
fixtures :courses
2015-03-10 22:21:19 -04:00
self . use_transactional_tests = true
2011-10-05 20:21:43 -04:00
def test_leaky_destroy
assert_nothing_raised { courses ( :ruby ) }
courses ( :ruby ) . destroy
end
def test_it_twice_in_whatever_order_to_check_for_fixture_leakage
test_leaky_destroy
end
2007-01-23 20:29:49 -05:00
end
2015-06-28 18:11:34 -04:00
class TransactionalFixturesOnConnectionNotification < ActiveRecord :: TestCase
self . use_transactional_tests = true
self . use_instantiated_fixtures = false
def test_transaction_created_on_connection_notification
2018-07-10 00:48:37 -04:00
connection = Class . new do
attr_accessor :pool
def transaction_open? ; end
def begin_transaction ( * args ) ; end
def rollback_transaction ( * args ) ; end
end . new
connection . pool = Class . new do
2018-07-10 16:31:37 -04:00
def lock_thread = ( lock_thread ) ; end
2018-07-10 00:48:37 -04:00
end . new
2018-07-18 18:25:25 -04:00
assert_called_with ( connection , :begin_transaction , [ joinable : false ] ) do
fire_connection_notification ( connection )
end
2015-06-28 18:11:34 -04:00
end
def test_notification_established_transactions_are_rolled_back
connection = Class . new do
attr_accessor :rollback_transaction_called
2017-02-20 13:35:19 -05:00
attr_accessor :pool
2018-07-10 00:48:37 -04:00
2015-06-28 18:11:34 -04:00
def transaction_open? ; true ; end
def begin_transaction ( * args ) ; end
def rollback_transaction ( * args )
@rollback_transaction_called = true
end
end . new
2018-07-10 00:48:37 -04:00
2017-02-20 13:35:19 -05:00
connection . pool = Class . new do
2018-07-10 16:31:37 -04:00
def lock_thread = ( lock_thread ) ; end
2017-02-20 13:35:19 -05:00
end . new
2018-07-10 00:48:37 -04:00
2015-06-28 18:11:34 -04:00
fire_connection_notification ( connection )
teardown_fixtures
2018-07-10 00:48:37 -04:00
2015-06-28 18:11:34 -04:00
assert ( connection . rollback_transaction_called , " Expected <mock connection> # rollback_transaction to be called but was not " )
end
private
def fire_connection_notification ( connection )
2018-07-18 18:25:25 -04:00
assert_called_with ( ActiveRecord :: Base . connection_handler , :retrieve_connection , [ " book " ] , returns : connection ) do
2018-07-16 15:28:34 -04:00
message_bus = ActiveSupport :: Notifications . instrumenter
payload = {
spec_name : " book " ,
config : nil ,
connection_id : connection . object_id
}
2015-06-28 18:11:34 -04:00
2018-09-25 13:18:20 -04:00
message_bus . instrument ( " !connection.active_record " , payload ) { }
2018-07-16 15:28:34 -04:00
end
2015-06-28 18:11:34 -04:00
end
end
2008-01-21 12:20:51 -05:00
class InvalidTableNameFixturesTest < ActiveRecord :: TestCase
2006-03-06 18:03:35 -05:00
fixtures :funny_jokes
2010-08-14 01:13:00 -04:00
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
2008-03-28 07:55:01 -04:00
# and thus takes into account our lack of set_fixture_class
2015-03-10 22:21:19 -04:00
self . use_transactional_tests = false
2006-03-06 18:03:35 -05:00
def test_raises_error
2012-03-22 19:25:48 -04:00
assert_raise ActiveRecord :: FixtureClassNotFound do
2006-03-06 18:03:35 -05:00
funny_jokes ( :a_joke )
end
end
end
2006-08-11 19:38:46 -04:00
2008-01-21 12:20:51 -05:00
class CheckEscapedYamlFixturesTest < ActiveRecord :: TestCase
2016-08-06 13:37:57 -04:00
set_fixture_class funny_jokes : Joke
2006-08-23 21:50:24 -04:00
fixtures :funny_jokes
2010-08-14 01:13:00 -04:00
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
2008-03-28 07:55:01 -04:00
# and thus takes into account our set_fixture_class
2015-03-10 22:21:19 -04:00
self . use_transactional_tests = false
2006-08-23 21:50:24 -04:00
def test_proper_escaped_fixture
assert_equal " The \\ n Aristocrats \n Ate the candy \n " , funny_jokes ( :another_joke ) . name
end
end
2007-09-28 10:56:07 -04:00
class DevelopersProject ; end
2008-01-21 12:20:51 -05:00
class ManyToManyFixturesWithClassDefined < ActiveRecord :: TestCase
2006-08-11 19:38:46 -04:00
fixtures :developers_projects
2007-09-28 10:56:07 -04:00
2006-08-11 19:38:46 -04:00
def test_this_should_run_cleanly
assert true
end
2006-10-09 03:48:27 -04:00
end
2008-01-21 12:20:51 -05:00
class FixturesBrokenRollbackTest < ActiveRecord :: TestCase
2011-10-05 20:21:43 -04:00
def blank_setup
@fixture_connections = [ ActiveRecord :: Base . connection ]
end
2008-01-05 08:34:15 -05:00
alias_method :ar_setup_fixtures , :setup_fixtures
alias_method :setup_fixtures , :blank_setup
2006-10-09 03:48:27 -04:00
alias_method :setup , :blank_setup
def blank_teardown ; end
2008-01-05 08:34:15 -05:00
alias_method :ar_teardown_fixtures , :teardown_fixtures
alias_method :teardown_fixtures , :blank_teardown
2006-10-09 03:48:27 -04:00
alias_method :teardown , :blank_teardown
def test_no_rollback_in_teardown_unless_transaction_active
2008-07-02 00:01:26 -04:00
assert_equal 0 , ActiveRecord :: Base . connection . open_transactions
2008-01-05 08:34:15 -05:00
assert_raise ( RuntimeError ) { ar_setup_fixtures }
2008-07-02 00:01:26 -04:00
assert_equal 0 , ActiveRecord :: Base . connection . open_transactions
2008-01-05 08:34:15 -05:00
assert_nothing_raised { ar_teardown_fixtures }
2008-07-02 00:01:26 -04:00
assert_equal 0 , ActiveRecord :: Base . connection . open_transactions
2006-10-09 03:48:27 -04:00
end
private
2013-08-26 18:24:23 -04:00
def load_fixtures ( config )
2016-08-06 12:26:20 -04:00
raise " argh "
2006-10-09 03:48:27 -04:00
end
end
2007-02-25 12:31:43 -05:00
2008-01-21 12:20:51 -05:00
class LoadAllFixturesTest < ActiveRecord :: TestCase
2007-02-25 12:31:43 -05:00
def test_all_there
2013-11-16 03:22:05 -05:00
self . class . fixture_path = FIXTURES_ROOT + " /all "
self . class . fixtures :all
2014-02-08 19:52:32 -05:00
if File . symlink? FIXTURES_ROOT + " /all/admin "
2016-09-11 08:43:03 -04:00
assert_equal %w( admin/accounts admin/users developers namespaced/accounts people tasks ) , fixture_table_names . sort
2014-02-08 19:52:32 -05:00
end
2013-11-16 03:22:05 -05:00
ensure
ActiveRecord :: FixtureSet . reset_cache
2007-02-25 12:31:43 -05:00
end
end
2007-09-28 10:05:03 -04:00
2013-03-21 17:10:44 -04:00
class LoadAllFixturesWithPathnameTest < ActiveRecord :: TestCase
def test_all_there
2016-08-06 12:26:20 -04:00
self . class . fixture_path = Pathname . new ( FIXTURES_ROOT ) . join ( " all " )
2013-11-16 03:22:05 -05:00
self . class . fixtures :all
2014-02-08 19:52:32 -05:00
if File . symlink? FIXTURES_ROOT + " /all/admin "
2016-09-11 08:43:03 -04:00
assert_equal %w( admin/accounts admin/users developers namespaced/accounts people tasks ) , fixture_table_names . sort
2014-02-08 19:52:32 -05:00
end
2013-11-16 03:22:05 -05:00
ensure
ActiveRecord :: FixtureSet . reset_cache
2013-03-21 17:10:44 -04:00
end
end
2008-01-21 12:20:51 -05:00
class FasterFixturesTest < ActiveRecord :: TestCase
2015-03-10 22:21:19 -04:00
self . use_transactional_tests = false
2017-04-27 02:44:25 -04:00
fixtures :categories , :authors , :author_addresses
2007-09-28 10:56:07 -04:00
2007-09-28 10:05:03 -04:00
def load_extra_fixture ( name )
2011-02-11 19:09:34 -05:00
fixture = create_fixtures ( name ) . first
2012-05-12 07:17:03 -04:00
assert fixture . is_a? ( ActiveRecord :: FixtureSet )
2007-09-28 10:05:03 -04:00
@loaded_fixtures [ fixture . table_name ] = fixture
end
2007-09-28 10:56:07 -04:00
2007-09-28 10:05:03 -04:00
def test_cache
2016-08-06 12:26:20 -04:00
assert ActiveRecord :: FixtureSet . fixture_is_cached? ( ActiveRecord :: Base . connection , " categories " )
assert ActiveRecord :: FixtureSet . fixture_is_cached? ( ActiveRecord :: Base . connection , " authors " )
2007-09-28 10:56:07 -04:00
2007-09-28 10:05:03 -04:00
assert_no_queries do
2016-08-06 12:26:20 -04:00
create_fixtures ( " categories " )
create_fixtures ( " authors " )
2007-09-28 10:05:03 -04:00
end
2007-09-28 10:56:07 -04:00
2016-08-06 12:26:20 -04:00
load_extra_fixture ( " posts " )
assert ActiveRecord :: FixtureSet . fixture_is_cached? ( ActiveRecord :: Base . connection , " posts " )
2012-01-03 17:22:03 -05:00
self . class . setup_fixture_accessors :posts
2016-08-06 12:26:20 -04:00
assert_equal " Welcome to the weblog " , posts ( :welcome ) . title
2007-09-28 10:05:03 -04:00
end
end
2007-10-26 01:56:46 -04:00
2008-01-21 12:20:51 -05:00
class FoxyFixturesTest < ActiveRecord :: TestCase
2017-05-31 12:12:58 -04:00
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
self . use_transactional_tests = false
2015-01-14 08:01:30 -05:00
fixtures :parrots , :parrots_pirates , :pirates , :treasures , :mateys , :ships , :computers ,
2015-05-15 23:27:41 -04:00
:developers , :" admin/accounts " , :" admin/users " , :live_parrots , :dead_parrots , :books
2007-10-26 01:56:46 -04:00
2016-08-06 12:26:20 -04:00
if ActiveRecord :: Base . connection . adapter_name == " PostgreSQL "
require " models/uuid_parent "
require " models/uuid_child "
2014-09-22 13:12:19 -04:00
fixtures :uuid_parents , :uuid_children
2013-08-25 05:22:36 -04:00
end
2007-10-26 01:56:46 -04:00
def test_identifies_strings
2012-05-12 07:17:03 -04:00
assert_equal ( ActiveRecord :: FixtureSet . identify ( " foo " ) , ActiveRecord :: FixtureSet . identify ( " foo " ) )
assert_not_equal ( ActiveRecord :: FixtureSet . identify ( " foo " ) , ActiveRecord :: FixtureSet . identify ( " FOO " ) )
2007-10-26 01:56:46 -04:00
end
def test_identifies_symbols
2012-05-12 07:17:03 -04:00
assert_equal ( ActiveRecord :: FixtureSet . identify ( :foo ) , ActiveRecord :: FixtureSet . identify ( :foo ) )
2007-10-26 01:56:46 -04:00
end
2009-05-11 23:22:20 -04:00
def test_identifies_consistently
2012-05-12 07:17:03 -04:00
assert_equal 207281424 , ActiveRecord :: FixtureSet . identify ( :ruby )
assert_equal 1066363776 , ActiveRecord :: FixtureSet . identify ( :sapphire_2 )
2013-08-25 05:22:36 -04:00
2016-08-06 12:26:20 -04:00
assert_equal " f92b6bda-0d0d-5fe1-9124-502b18badded " , ActiveRecord :: FixtureSet . identify ( :daddy , :uuid )
assert_equal " b4b10018-ad47-595d-b42f-d8bdaa6d01bf " , ActiveRecord :: FixtureSet . identify ( :sonny , :uuid )
2009-05-11 23:22:20 -04:00
end
2007-10-26 01:56:46 -04:00
TIMESTAMP_COLUMNS = %w( created_at created_on updated_at updated_on )
def test_populates_timestamp_columns
TIMESTAMP_COLUMNS . each do | property |
assert_not_nil ( parrots ( :george ) . send ( property ) , " should set #{ property } " )
end
end
2007-11-26 17:45:43 -05:00
def test_does_not_populate_timestamp_columns_if_model_has_set_record_timestamps_to_false
TIMESTAMP_COLUMNS . each do | property |
assert_nil ( ships ( :black_pearl ) . send ( property ) , " should not set #{ property } " )
end
end
2007-10-26 01:56:46 -04:00
def test_populates_all_columns_with_the_same_time
last = nil
TIMESTAMP_COLUMNS . each do | property |
current = parrots ( :george ) . send ( property )
last || = current
assert_equal ( last , current )
last = current
end
end
def test_only_populates_columns_that_exist
assert_not_nil ( pirates ( :blackbeard ) . created_on )
assert_not_nil ( pirates ( :blackbeard ) . updated_on )
end
def test_preserves_existing_fixture_data
2011-08-03 19:23:58 -04:00
assert_equal ( 2 . weeks . ago . to_date , pirates ( :redbeard ) . created_on . to_date )
assert_equal ( 2 . weeks . ago . to_date , pirates ( :redbeard ) . updated_on . to_date )
2007-10-26 01:56:46 -04:00
end
def test_generates_unique_ids
assert_not_nil ( parrots ( :george ) . id )
assert_not_equal ( parrots ( :george ) . id , parrots ( :louis ) . id )
end
2007-11-26 17:45:43 -05:00
def test_automatically_sets_primary_key
assert_not_nil ( ships ( :black_pearl ) )
end
def test_preserves_existing_primary_key
assert_equal ( 2 , ships ( :interceptor ) . id )
end
2007-10-26 01:56:46 -04:00
def test_resolves_belongs_to_symbols
assert_equal ( parrots ( :george ) , pirates ( :blackbeard ) . parrot )
end
2007-11-26 17:45:43 -05:00
def test_ignores_belongs_to_symbols_if_association_and_foreign_key_are_named_the_same
assert_equal ( developers ( :david ) , computers ( :workstation ) . developer )
end
2007-10-26 01:56:46 -04:00
def test_supports_join_tables
assert ( pirates ( :blackbeard ) . parrots . include? ( parrots ( :george ) ) )
assert ( pirates ( :blackbeard ) . parrots . include? ( parrots ( :louis ) ) )
assert ( parrots ( :george ) . pirates . include? ( pirates ( :blackbeard ) ) )
end
def test_supports_inline_habtm
assert ( parrots ( :george ) . treasures . include? ( treasures ( :diamond ) ) )
assert ( parrots ( :george ) . treasures . include? ( treasures ( :sapphire ) ) )
2018-04-17 18:21:34 -04:00
assert_not ( parrots ( :george ) . treasures . include? ( treasures ( :ruby ) ) )
2007-10-26 01:56:46 -04:00
end
2007-11-26 17:45:43 -05:00
def test_supports_inline_habtm_with_specified_id
assert ( parrots ( :polly ) . treasures . include? ( treasures ( :ruby ) ) )
assert ( parrots ( :polly ) . treasures . include? ( treasures ( :sapphire ) ) )
2018-04-17 18:21:34 -04:00
assert_not ( parrots ( :polly ) . treasures . include? ( treasures ( :diamond ) ) )
2007-11-26 17:45:43 -05:00
end
2007-10-26 01:56:46 -04:00
def test_supports_yaml_arrays
assert ( parrots ( :louis ) . treasures . include? ( treasures ( :diamond ) ) )
assert ( parrots ( :louis ) . treasures . include? ( treasures ( :sapphire ) ) )
end
def test_strips_DEFAULTS_key
assert_raise ( StandardError ) { parrots ( :DEFAULTS ) }
# this lets us do YAML defaults and not have an extra fixture entry
%w( sapphire ruby ) . each { | t | assert ( parrots ( :davey ) . treasures . include? ( treasures ( t ) ) ) }
end
def test_supports_label_interpolation
assert_equal ( " frederick " , parrots ( :frederick ) . name )
end
2007-11-20 16:53:22 -05:00
2014-03-15 21:49:04 -04:00
def test_supports_label_string_interpolation
assert_equal ( " X marks the spot! " , pirates ( :mark ) . catchphrase )
end
2016-05-17 10:56:08 -04:00
def test_supports_label_interpolation_for_integer_label
2015-01-06 04:24:41 -05:00
assert_equal ( " # 1 pirate! " , pirates ( 1 ) . catchphrase )
end
2007-11-20 16:53:22 -05:00
def test_supports_polymorphic_belongs_to
assert_equal ( pirates ( :redbeard ) , treasures ( :sapphire ) . looter )
assert_equal ( parrots ( :louis ) , treasures ( :ruby ) . looter )
end
2007-11-26 17:46:11 -05:00
2007-11-25 17:08:38 -05:00
def test_only_generates_a_pk_if_necessary
2012-04-26 13:32:55 -04:00
m = Matey . first
2007-11-25 17:08:38 -05:00
m . pirate = pirates ( :blackbeard )
m . target = pirates ( :redbeard )
end
2007-11-26 17:46:11 -05:00
def test_supports_sti
assert_kind_of DeadParrot , parrots ( :polly )
assert_equal pirates ( :blackbeard ) , parrots ( :polly ) . killer
end
2010-04-14 13:15:27 -04:00
2015-01-14 08:01:30 -05:00
def test_supports_sti_with_respective_files
assert_kind_of LiveParrot , live_parrots ( :dusty )
assert_kind_of DeadParrot , dead_parrots ( :deadbird )
assert_equal pirates ( :blackbeard ) , dead_parrots ( :deadbird ) . killer
end
2010-04-14 13:15:27 -04:00
def test_namespaced_models
2016-09-16 12:44:05 -04:00
assert_includes admin_accounts ( :signals37 ) . users , admin_users ( :david )
2010-04-14 13:15:27 -04:00
assert_equal 2 , admin_accounts ( :signals37 ) . users . size
end
2015-05-15 23:27:41 -04:00
def test_resolves_enums
2018-01-25 18:14:09 -05:00
assert_predicate books ( :awdr ) , :published?
assert_predicate books ( :awdr ) , :read?
assert_predicate books ( :rfr ) , :proposed?
assert_predicate books ( :ddd ) , :published?
2015-05-15 23:27:41 -04:00
end
2007-10-26 01:56:46 -04:00
end
2007-10-31 01:43:52 -04:00
2008-01-21 12:20:51 -05:00
class ActiveSupportSubclassWithFixturesTest < ActiveRecord :: TestCase
2007-10-31 01:43:52 -04:00
fixtures :parrots
# This seemingly useless assertion catches a bug that caused the fixtures
# setup code call nil[]
def test_foo
2013-01-18 09:15:19 -05:00
assert_equal parrots ( :louis ) , Parrot . find_by_name ( " King Louis " )
2007-10-31 01:43:52 -04:00
end
2007-11-26 17:45:43 -05:00
end
2008-03-17 17:48:28 -04:00
2011-12-19 10:00:24 -05:00
class CustomNameForFixtureOrModelTest < ActiveRecord :: TestCase
2012-05-12 07:17:03 -04:00
ActiveRecord :: FixtureSet . reset_cache
2011-12-19 10:00:24 -05:00
2016-10-28 23:05:58 -04:00
set_fixture_class :randomly_named_a9 = >
2011-12-19 10:00:24 -05:00
ClassNameThatDoesNotFollowCONVENTIONS ,
:'admin/randomly_named_a9' = >
2015-01-14 08:01:30 -05:00
Admin :: ClassNameThatDoesNotFollowCONVENTIONS1 ,
2016-08-06 12:26:20 -04:00
" admin/randomly_named_b0 " = >
2015-01-14 08:01:30 -05:00
Admin :: ClassNameThatDoesNotFollowCONVENTIONS2
2011-12-19 10:00:24 -05:00
2016-08-06 12:26:20 -04:00
fixtures :randomly_named_a9 , " admin/randomly_named_a9 " ,
2011-12-19 10:00:24 -05:00
:'admin/randomly_named_b0'
def test_named_accessor_for_randomly_named_fixture_and_class
assert_kind_of ClassNameThatDoesNotFollowCONVENTIONS ,
randomly_named_a9 ( :first_instance )
end
def test_named_accessor_for_randomly_named_namespaced_fixture_and_class
2015-01-14 08:01:30 -05:00
assert_kind_of Admin :: ClassNameThatDoesNotFollowCONVENTIONS1 ,
2011-12-19 10:00:24 -05:00
admin_randomly_named_a9 ( :first_instance )
2015-01-14 08:01:30 -05:00
assert_kind_of Admin :: ClassNameThatDoesNotFollowCONVENTIONS2 ,
2011-12-19 10:00:24 -05:00
admin_randomly_named_b0 ( :second_instance )
end
2011-12-20 15:32:39 -05:00
def test_table_name_is_defined_in_the_model
2018-02-22 01:26:48 -05:00
assert_equal " randomly_named_table2 " , ActiveRecord :: FixtureSet . all_loaded_fixtures [ " admin/randomly_named_a9 " ] . table_name
2016-08-06 12:26:20 -04:00
assert_equal " randomly_named_table2 " , Admin :: ClassNameThatDoesNotFollowCONVENTIONS1 . table_name
2011-12-20 15:32:39 -05:00
end
2011-12-19 10:00:24 -05:00
end
2014-11-21 08:57:25 -05:00
class FixturesWithDefaultScopeTest < ActiveRecord :: TestCase
fixtures :bulbs
test " inserts fixtures excluded by a default scope " do
assert_equal 1 , Bulb . count
assert_equal 2 , Bulb . unscoped . count
end
test " allows access to fixtures excluded by a default scope " do
assert_equal " special " , bulbs ( :special ) . name
end
end
2015-06-04 12:27:45 -04:00
class FixturesWithAbstractBelongsTo < ActiveRecord :: TestCase
fixtures :pirates , :doubloons
test " creates fixtures with belongs_to associations defined in abstract base classes " do
assert_not_nil doubloons ( :blackbeards_doubloon )
assert_equal pirates ( :blackbeard ) , doubloons ( :blackbeards_doubloon ) . pirate
end
end
FixtureSet.fixture_class_names should have no default value
Look at `TestFixtures.set_fixture_class`. As documented, it
accepts a mapping of fixture identifiers (string or symbol) to Classes
(the model classes that implement the named fixture).
Look now at the initialization of `TestFixtures.fixture_class_names`.
It defines a Hash, which will return a string by default (where the
string is the estimated class name of the given fixture identifier).
Now look at TestFixtures.load_fixtures. It calls `FixtureSet.create_fixtures`,
passing in the mapping of `fixture_class_names`.
Following this on to `FixtureSet.create_fixtures`, this instantiates a
`FixtureSet::ClassCache`, passing in the map of class names.
`ClassCache`, in turn, calls `insert_class` for each value in the cache.
(Recall that `set_fixture_class` puts Class objects in there, while the
default proc for the mapping puts String objects.)
Look finally at `insert_class`. If the value is present, it checks to
see if the value is a subclass of `AR::Base`. Fair enough...but wait!
What if the value is a String? You get an exception, because a String
instance cannot be compared with a Class.
Judging from the implementation, it seems like the expected behavior
here is for `fixture_class_names` to have no default proc. Look-ups are
supposed to happen via `ClassCache`, with `fixture_class_names` existing
solely as a repository for explicitly-registered class mappings.
That is what this change does.
2015-10-28 15:14:33 -04:00
class FixtureClassNamesTest < ActiveRecord :: TestCase
def setup
2017-01-05 03:20:57 -05:00
@saved_cache = fixture_class_names . dup
FixtureSet.fixture_class_names should have no default value
Look at `TestFixtures.set_fixture_class`. As documented, it
accepts a mapping of fixture identifiers (string or symbol) to Classes
(the model classes that implement the named fixture).
Look now at the initialization of `TestFixtures.fixture_class_names`.
It defines a Hash, which will return a string by default (where the
string is the estimated class name of the given fixture identifier).
Now look at TestFixtures.load_fixtures. It calls `FixtureSet.create_fixtures`,
passing in the mapping of `fixture_class_names`.
Following this on to `FixtureSet.create_fixtures`, this instantiates a
`FixtureSet::ClassCache`, passing in the map of class names.
`ClassCache`, in turn, calls `insert_class` for each value in the cache.
(Recall that `set_fixture_class` puts Class objects in there, while the
default proc for the mapping puts String objects.)
Look finally at `insert_class`. If the value is present, it checks to
see if the value is a subclass of `AR::Base`. Fair enough...but wait!
What if the value is a String? You get an exception, because a String
instance cannot be compared with a Class.
Judging from the implementation, it seems like the expected behavior
here is for `fixture_class_names` to have no default proc. Look-ups are
supposed to happen via `ClassCache`, with `fixture_class_names` existing
solely as a repository for explicitly-registered class mappings.
That is what this change does.
2015-10-28 15:14:33 -04:00
end
def teardown
2016-08-07 19:05:28 -04:00
fixture_class_names . replace ( @saved_cache )
FixtureSet.fixture_class_names should have no default value
Look at `TestFixtures.set_fixture_class`. As documented, it
accepts a mapping of fixture identifiers (string or symbol) to Classes
(the model classes that implement the named fixture).
Look now at the initialization of `TestFixtures.fixture_class_names`.
It defines a Hash, which will return a string by default (where the
string is the estimated class name of the given fixture identifier).
Now look at TestFixtures.load_fixtures. It calls `FixtureSet.create_fixtures`,
passing in the mapping of `fixture_class_names`.
Following this on to `FixtureSet.create_fixtures`, this instantiates a
`FixtureSet::ClassCache`, passing in the map of class names.
`ClassCache`, in turn, calls `insert_class` for each value in the cache.
(Recall that `set_fixture_class` puts Class objects in there, while the
default proc for the mapping puts String objects.)
Look finally at `insert_class`. If the value is present, it checks to
see if the value is a subclass of `AR::Base`. Fair enough...but wait!
What if the value is a String? You get an exception, because a String
instance cannot be compared with a Class.
Judging from the implementation, it seems like the expected behavior
here is for `fixture_class_names` to have no default proc. Look-ups are
supposed to happen via `ClassCache`, with `fixture_class_names` existing
solely as a repository for explicitly-registered class mappings.
That is what this change does.
2015-10-28 15:14:33 -04:00
end
test " fixture_class_names returns nil for unregistered identifier " do
2016-08-07 19:05:28 -04:00
assert_nil fixture_class_names [ " unregistered_identifier " ]
FixtureSet.fixture_class_names should have no default value
Look at `TestFixtures.set_fixture_class`. As documented, it
accepts a mapping of fixture identifiers (string or symbol) to Classes
(the model classes that implement the named fixture).
Look now at the initialization of `TestFixtures.fixture_class_names`.
It defines a Hash, which will return a string by default (where the
string is the estimated class name of the given fixture identifier).
Now look at TestFixtures.load_fixtures. It calls `FixtureSet.create_fixtures`,
passing in the mapping of `fixture_class_names`.
Following this on to `FixtureSet.create_fixtures`, this instantiates a
`FixtureSet::ClassCache`, passing in the map of class names.
`ClassCache`, in turn, calls `insert_class` for each value in the cache.
(Recall that `set_fixture_class` puts Class objects in there, while the
default proc for the mapping puts String objects.)
Look finally at `insert_class`. If the value is present, it checks to
see if the value is a subclass of `AR::Base`. Fair enough...but wait!
What if the value is a String? You get an exception, because a String
instance cannot be compared with a Class.
Judging from the implementation, it seems like the expected behavior
here is for `fixture_class_names` to have no default proc. Look-ups are
supposed to happen via `ClassCache`, with `fixture_class_names` existing
solely as a repository for explicitly-registered class mappings.
That is what this change does.
2015-10-28 15:14:33 -04:00
end
end
2016-12-14 06:14:47 -05:00
class SameNameDifferentDatabaseFixturesTest < ActiveRecord :: TestCase
fixtures :dogs , :other_dogs
test " fixtures are properly loaded " do
# Force loading the fixtures again to reproduce issue
ActiveRecord :: FixtureSet . reset_cache
create_fixtures ( " dogs " , " other_dogs " )
assert_kind_of Dog , dogs ( :sophie )
assert_kind_of OtherDog , other_dogs ( :lassie )
end
end
2018-09-25 18:52:45 -04:00
class NilFixturePathTest < ActiveRecord :: TestCase
test " raises an error when all fixtures loaded " do
error = assert_raises ( StandardError ) do
TestCase = Class . new ( ActiveRecord :: TestCase )
TestCase . class_eval do
self . fixture_path = nil
fixtures :all
end
end
assert_equal << ~ MSG . squish , error . message
No fixture path found .
Please set ` NilFixturePathTest::TestCase.fixture_path ` .
MSG
end
end