aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/attribute_methods_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-131-1/+0
| | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* Give GeneratedRelationMethods module a nameRyuta Kamizono2019-04-191-2/+2
| | | | This follows #35595.
* Give GeneratedAttributeMethods module a nameChris Salzberg2019-03-131-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently GeneratedAttributeMethods is a module builder class, an instance of which is included in every AR class. OTOH, GeneratedAssociatedMethods is assigned to a constant under the model namespace. This is inconsistent and looks strange in the list of ancestors. There is no particular reason *not* to assign a constant for this (very important) module under the model namespace, so that's what this commit does. Previous to this change, ancestors for an AR class looked like this: ``` => [User (call 'User.connection' to establish a connection), User::GeneratedAssociationMethods, #<ActiveRecord::AttributeMethods::GeneratedAttributeMethods:0x000055ace0f05b08>, ApplicationRecord(abstract), ApplicationRecord::GeneratedAssociationMethods, #<ActiveRecord::AttributeMethods::GeneratedAttributeMethods:0x000055ace093c460>, ActiveRecord::Base, ... ``` With this change, they look like this: ``` => [User (call 'User.connection' to establish a connection), User::GeneratedAssociationMethods, User::GeneratedAttributeMethods, ApplicationRecord(abstract), ApplicationRecord::GeneratedAssociationMethods, ApplicationRecord::GeneratedAttributeMethods, ActiveRecord::Base, ... ``` The previously named `GeneratedAttributeMethods` module builder class is renamed `GeneratedAttributeMethodsBuilder` to emphasize that this is not a module but a class.
* Merge pull request #35320 from ↵Ryuta Kamizono2019-03-111-0/+63
|\ | | | | | | | | | | kamille-gz/fix_query_method_when_given_Date_data_type Fix ActiveRecord query attribute method when given value does't respond to to_i method
| * Fix query attribute method on user-defined attribute to be aware of ↵kamille-3212019-03-111-0/+63
|/ | | | | | | | | | typecasted value change the line to check an attribute has user-defined type ref: https://github.com/rails/rails/pull/35320#discussion_r257924552 check query attribute method is working when given value does not respond to to_i method
* More exercise string attribute predicate tests for falsy stringsRyuta Kamizono2019-02-241-0/+4
|
* Reset column info after making Topic tz-awareGannon McGibbon2019-02-181-0/+4
| | | | | | | In AttributeMethodsTest, we make the global Topic class time zone-aware which changes instance date time attribute casting behaviour. We need to reset column info after the test because future tests don't expect Topic date time columns to be time zone-aware.
* Allow aliased attributes in updateGannon McGibbon2018-11-291-0/+6
| | | | Allow aliased attributes to be used in `#update_columns` and `#update`.
* Use squiggly heredoc to strip odd indentation in the executed SQLRyuta Kamizono2018-11-221-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before: ``` LOG: execute <unnamed>: SELECT t.oid, t.typname FROM pg_type as t WHERE t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'bool') LOG: execute <unnamed>: SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype FROM pg_type as t LEFT JOIN pg_range as r ON oid = rngtypid WHERE t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'text', 'varchar', 'char', 'name', 'bpchar', 'bool', 'bit', 'varbit', 'timestamptz', 'date', 'money', 'bytea', 'point', 'hstore', 'json', 'jsonb', 'cidr', 'inet', 'uuid', 'xml', 'tsvector', 'macaddr', 'citext', 'ltree', 'interval', 'path', 'line', 'polygon', 'circle', 'lseg', 'box', 'time', 'timestamp', 'numeric') OR t.typtype IN ('r', 'e', 'd') OR t.typinput::varchar = 'array_in' OR t.typelem != 0 LOG: statement: SHOW TIME ZONE LOG: statement: SELECT 1 LOG: execute <unnamed>: SELECT COUNT(*) FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view AND c.relname = 'accounts' AND n.nspname = ANY (current_schemas(false)) ``` After: ``` LOG: execute <unnamed>: SELECT t.oid, t.typname FROM pg_type as t WHERE t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'bool') LOG: execute <unnamed>: SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype FROM pg_type as t LEFT JOIN pg_range as r ON oid = rngtypid WHERE t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'text', 'varchar', 'char', 'name', 'bpchar', 'bool', 'bit', 'varbit', 'timestamptz', 'date', 'money', 'bytea', 'point', 'hstore', 'json', 'jsonb', 'cidr', 'inet', 'uuid', 'xml', 'tsvector', 'macaddr', 'citext', 'ltree', 'interval', 'path', 'line', 'polygon', 'circle', 'lseg', 'box', 'time', 'timestamp', 'numeric') OR t.typtype IN ('r', 'e', 'd') OR t.typinput::varchar = 'array_in' OR t.typelem != 0 LOG: statement: SHOW TIME ZONE LOG: statement: SELECT 1 LOG: execute <unnamed>: SELECT COUNT(*) FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view AND c.relname = 'accounts' AND n.nspname = ANY (current_schemas(false)) ```
* Fix inspect with non-primary key id attributeEugene Kenny2018-11-061-0/+7
| | | | | | | | | | | The `read_attribute` method always returns the primary key when asked to read the `id` attribute, even if the primary key isn't named `id`, and even if another attribute named `id` exists. For the `inspect`, `attribute_for_inspect` and `pretty_print` methods, this behaviour is undesirable, as they're used to examine the internal state of the record. By using `_read_attribute` instead, we'll get the real value of the `id` attribute.
* `update_columns` raises if the column is unknownSean Griffin2018-10-301-0/+6
| | | | | | | | | Previosly, `update_columns` would just take whatever keys you gave it and tried to run the update query. Most likely this would result in an error from the database. However, if the column actually did exist, but was in `ignored_columns`, this would result in the method returning successfully when it should have raised, and an attribute that should not exist written to `@attributes`.
* Call initialize after allocateAaron Patterson2018-06-261-13/+0
| | | | | | | | If someone calls allocate on the object, they'd better also call an initialization routine too (you can't expect allocate to do any initialization work). Before this commit, AR objects that are instantiated from the database would call `define_attribute_methods` twice.
* Fix user_input_in_time_zone to coerce non valid string into nilAnnie-Claude Côté2018-05-161-0/+10
| | | | Before it was coercing an invalid string into "2000-01-01 00:00:00".
* Fix `CustomCops/AssertNot` to allow it to have failure messageRyuta Kamizono2018-05-131-4/+4
| | | | Follow up of #32605.
* Replace `assert !` with `assert_not`Daniel Colson2018-04-191-6/+6
| | | | | This autocorrects the violations after adding a custom cop in 3305c78dcd.
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-251-11/+11
|
* Use respond_to test helpersDaniel Colson2018-01-251-15/+15
|
* No difference between JRuby and CRuby at ↵Yasuo Honda2017-11-211-6/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | test_read_attributes_before_type_cast_on_a_boolean https://github.com/jruby/activerecord-jdbc-adapter ActiveRecord JDBC Adapter is actively developed and it supports Rails 5.1 now. This pull request addresses one of the failure when running ActiveRecord unit tests with ActiveRecord JDBC Adapter. As of right now, ActiveRecord JDBC Adapter supports Rails 5.1, not master branch then this test only can run on `5-1-stable` branch. But I have opened this pull request to `master` branch since this type cast should be going to work in the future versions of ActiveRecord JDBC Adapter . ```ruby $ ARCONN=jdbcmysql bin/test test/cases/attribute_methods_test.rb:203 Using jdbcmysql Run options: --seed 8874 F Finished in 0.709120s, 1.4102 runs/s, 1.4102 assertions/s. 1) Failure: AttributeMethodsTest#test_read_attributes_before_type_cast_on_a_boolean [/home/yahonda/git/rails/activerecord/test/cases/attribute_methods_test.rb:203]: Expected: "0" Actual: 0 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips $ ```
* Add test for class of GeneratedAttributeMethods instance in ancestorsChris Salzberg2017-10-091-0/+5
|
* Remove unused `cached_columns` and `time_related_columns_on_topic` in ↵Ryuta Kamizono2017-09-271-8/+0
| | | | | | `AttributeMethodsTest` These are no longer used since 66736c8e.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Make `generated_attribute_methods` to privateRyuta Kamizono2017-07-141-1/+1
| | | | Because `generated_attribute_methods` is an internal API.
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Fix `define_attribute_method` with Symbol in ARPrem Sichanugrist2017-02-211-0/+7
| | | | | | | | | | | | | | | | | | | This issue is only appear when you try to call `define_attribute_method` and passing a symbol in Active Record. It does not appear in isolation in Active Model itself. Before this patch, when you run `User.define_attribute_method :foo`, you will get: NoMethodError: undefined method `unpack' for :foo:Symbol from activerecord/lib/active_record/attribute_methods/read.rb:28:in `define_method_attribute' from activerecord/lib/active_record/attribute_methods/primary_key.rb:61:in `define_method_attribute' from activemodel/lib/active_model/attribute_methods.rb:292:in `block in define_attribute_method' from activemodel/lib/active_model/attribute_methods.rb:285:in `each' from activemodel/lib/active_model/attribute_methods.rb:285:in `define_attribute_method' This patch contains both a fix in Active Model and a test in Active Record for this error.
* "Use assert_nil if expecting nil. This will fail in minitest 6."Akira Matsuda2016-12-251-4/+4
|
* Check whether the current attribute being write is aliased or not before writingPrathamesh Sonpatki2016-12-091-0/+7
| | | | - If aliased, then use the aliased attribute name.
* Check whether the current attribute being read is aliased or not before readingPrathamesh Sonpatki2016-12-081-0/+10
| | | | | - If aliased, then use the aliased attribute name. - Fixes #26417.
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-6/+6
|
* improve error message when include assertions failMichael Grosser2016-09-161-3/+3
| | | | | | assert [1, 3].includes?(2) fails with unhelpful "Asserting failed" message assert_includes [1, 3], 2 fails with "Expected [1, 3] to include 2" which makes it easier to debug and more obvious what went wrong
* Fix broken heredoc indentation caused by rubocop auto-correctRyuta Kamizono2016-09-031-5/+5
| | | | | | All indentation was normalized by rubocop auto-correct at 80e66cc4d90bf8c15d1a5f6e3152e90147f00772. But heredocs was still kept absolute position. This commit aligns heredocs indentation for consistency.
* normalizes indentation and whitespace across the projectXavier Noria2016-08-061-24/+24
|
* remove redundant curlies from hash argumentsXavier Noria2016-08-061-1/+1
|
* applies new string literal convention in activerecord/testXavier Noria2016-08-061-186/+186
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* removes spurious lineXavier Noria2016-07-241-1/+0
|
* give some love to this test fileXavier Noria2016-07-231-151/+155
| | | | | | | | | It all started noticing some foo.method({ ... }) method calls in passing. This is a whole pass modernizing this file. While some string literals are edited where I touched code, this pass does not uniformizes quotes. A ton are left untouched on purposes. We have no defined style.
* systematic revision of =~ usage in ARXavier Noria2016-07-231-1/+1
| | | | | Where appropriatei, prefer the more concise Regexp#match?, String#include?, String#start_with?, or String#end_with?
* Do not specal case inspecting associated arrays of over 10 elements, ↵Kevin McPhillips2016-06-291-2/+21
| | | | preventing infinite looping in some cases.
* Typos in AR testsAkira Matsuda2016-02-041-1/+1
|
* Avoid infinite recursion when bad values are passed to tz aware fieldsSean Griffin2016-02-021-0/+7
| | | | | | | | | | | | | | | We had previously updated this to attempt to map over whatever was passed in, so that additional types like range and array could benefit from this behavior without the time zone converter having to deal with every known type. However, the default behavior of a type is to just yield the given value to `map`, which means that if we don't actually know how to handle a value, we'll just recurse infinitely. Since both uses of `map` in this case occur in cases where we know receiving the same object will recurse, we can just break on reference equality. Fixes #23241.
* Remove legacy mysql adapterAbdelkader Boudih2015-12-171-1/+1
|
* 💣Sean Griffin2015-10-021-1/+1
| | | | | I misread this test in https://github.com/rails/rails/commit/1a693c79c32cba070256fdb7bd1990c3d07d554f
* Fix test failures on MySQLSean Griffin2015-10-021-1/+1
| | | | | There were a few places where I missed a `create` vs `new` before_type_cast check, and the semantics of `reload` became wrong.
* Further encapsulate dirty checking on `Attribute`Sean Griffin2015-10-021-3/+0
| | | | | | | | | | | | | | | | | | | We can skip the allocation of a full `AttributeSet` by changing the semantics of how we structure things. Instead of comparing two separate `AttributeSet` objects, and `Attribute` is now a singly linked list of every change that has happened to it. Since the attribute objects are immutable, to apply the changes we simply need to copy the head of the list. It's worth noting that this causes one subtle change in the behavior of AR. When a record is saved successfully, the `before_type_cast` version of everything will be what was sent to the database. I honestly think these semantics make more sense, as we could have just as easily had the DB do `RETURNING *` and updated the record with those if we had things like timestamps implemented at the DB layer. This brings our performance closer to 4.2, but we're still not quite there.
* Should test both mysql adaptersRyuta Kamizono2015-09-201-1/+1
| | | | | Some test cases are testing only mysql adapter. We should test mysql2 adapter also.
* Removed duplicate require ‘models/computer’Ronak Jangir2015-08-261-1/+0
|
* Removed mocha from Active Record Part 1Ronak Jangir2015-08-251-2/+3
|
* Follow-up to #10776Robin Dupret2015-02-261-3/+3
| | | | | | | | | | The name `ActiveModel::AttributeAssignment::UnknownAttributeError` is too implementation specific so let's move the constant directly under the ActiveModel namespace. Also since this constant used to be under the ActiveRecord namespace, to make the upgrade path easier, let's avoid raising the former constant when we deal with this error on the Active Record side.
* Extracted `ActiveRecord::AttributeAssignment` to ↵Bogdan Gusiev2015-01-231-3/+3
| | | | | | `ActiveModel::AttributesAssignment` Allows to use it for any object as an includable module.
* Introduce `ActiveRecord::Base#accessed_fields`Sean Griffin2015-01-201-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This method can be used to see all of the fields on a model which have been read. This can be useful during development mode to quickly find out which fields need to be selected. For performance critical pages, if you are not using all of the fields of a database, an easy performance win is only selecting the fields which you need. By calling this method at the end of a controller action, it's easy to determine which fields need to be selected. While writing this, I also noticed a place for an easy performance win internally which I had been wanting to introduce. You cannot mutate a field which you have not read. Therefore, we can skip the calculation of in place changes if we have never read from the field. This can significantly speed up methods like `#changed?` if any of the fields have an expensive mutable type (like `serialize`) ``` Calculating ------------------------------------- #changed? with serialized column (before) 391.000 i/100ms #changed? with serialized column (after) 1.514k i/100ms ------------------------------------------------- #changed? with serialized column (before) 4.243k (± 3.7%) i/s - 21.505k #changed? with serialized column (after) 16.789k (± 3.2%) i/s - 84.784k ```