axle_attributes 1.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.travis.yml +16 -0
  4. data/Gemfile +15 -0
  5. data/README.md +10 -0
  6. data/Rakefile +18 -0
  7. data/app/models/attribute_customization.rb +7 -0
  8. data/app/models/attribute_customization/local_cache.rb +28 -0
  9. data/app/models/attribute_mapping.rb +15 -0
  10. data/app/models/attribute_mapping/local_cache.rb +40 -0
  11. data/axle_attributes.gemspec +22 -0
  12. data/bin/rails +8 -0
  13. data/db/migrate/20131118042651_create_attribute_customizations.rb +16 -0
  14. data/db/migrate/20131121013237_create_attribute_mappings.rb +17 -0
  15. data/db/migrate/20151120160522_add_editable_to_attribute_customizations.rb +7 -0
  16. data/lib/axle_attributes.rb +30 -0
  17. data/lib/axle_attributes/child_definition.rb +42 -0
  18. data/lib/axle_attributes/conversions.rb +36 -0
  19. data/lib/axle_attributes/definition.rb +118 -0
  20. data/lib/axle_attributes/definition/callbacks.rb +34 -0
  21. data/lib/axle_attributes/definition/customization.rb +51 -0
  22. data/lib/axle_attributes/definition/formatted.rb +57 -0
  23. data/lib/axle_attributes/definition/indexed.rb +41 -0
  24. data/lib/axle_attributes/definition/mappings.rb +48 -0
  25. data/lib/axle_attributes/dumper.rb +54 -0
  26. data/lib/axle_attributes/engine.rb +4 -0
  27. data/lib/axle_attributes/format.rb +74 -0
  28. data/lib/axle_attributes/has_attributes.rb +104 -0
  29. data/lib/axle_attributes/has_attributes/json_reader.rb +87 -0
  30. data/lib/axle_attributes/model.rb +34 -0
  31. data/lib/axle_attributes/null_definition.rb +11 -0
  32. data/lib/axle_attributes/parent_definition.rb +37 -0
  33. data/lib/axle_attributes/provided.rb +67 -0
  34. data/lib/axle_attributes/regex.rb +4 -0
  35. data/lib/axle_attributes/segmented.rb +30 -0
  36. data/lib/axle_attributes/serializations.rb +147 -0
  37. data/lib/axle_attributes/serializations/builder/serialization.rb +83 -0
  38. data/lib/axle_attributes/serializations/builder/serialize_many.rb +17 -0
  39. data/lib/axle_attributes/serializations/builder/serialize_one.rb +7 -0
  40. data/lib/axle_attributes/serializations/reflection.rb +40 -0
  41. data/lib/axle_attributes/serializations/serialization.rb +88 -0
  42. data/lib/axle_attributes/serializations/serialize_many.rb +88 -0
  43. data/lib/axle_attributes/serializations/serialize_one.rb +34 -0
  44. data/lib/axle_attributes/serialized_child.rb +58 -0
  45. data/lib/axle_attributes/validations.rb +29 -0
  46. data/lib/axle_attributes/versioned.rb +21 -0
  47. data/test/dummy/README.rdoc +6 -0
  48. data/test/dummy/Rakefile +6 -0
  49. data/test/dummy/app/assets/images/.keep +0 -0
  50. data/test/dummy/app/assets/javascripts/application.js +13 -0
  51. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  52. data/test/dummy/app/controllers/application_controller.rb +5 -0
  53. data/test/dummy/app/controllers/concerns/.keep +0 -0
  54. data/test/dummy/app/helpers/application_helper.rb +2 -0
  55. data/test/dummy/app/mailers/.keep +0 -0
  56. data/test/dummy/app/models/.keep +0 -0
  57. data/test/dummy/app/models/concerns/.keep +0 -0
  58. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  59. data/test/dummy/bin/bundle +3 -0
  60. data/test/dummy/bin/rails +4 -0
  61. data/test/dummy/bin/rake +4 -0
  62. data/test/dummy/config.ru +4 -0
  63. data/test/dummy/config/application.rb +12 -0
  64. data/test/dummy/config/boot.rb +5 -0
  65. data/test/dummy/config/database.yml +13 -0
  66. data/test/dummy/config/environment.rb +5 -0
  67. data/test/dummy/config/environments/development.rb +29 -0
  68. data/test/dummy/config/environments/production.rb +80 -0
  69. data/test/dummy/config/environments/test.rb +38 -0
  70. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  71. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  72. data/test/dummy/config/initializers/inflections.rb +16 -0
  73. data/test/dummy/config/initializers/mime_types.rb +5 -0
  74. data/test/dummy/config/initializers/secret_token.rb +12 -0
  75. data/test/dummy/config/initializers/session_store.rb +3 -0
  76. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  77. data/test/dummy/config/locales/en.yml +23 -0
  78. data/test/dummy/config/routes.rb +56 -0
  79. data/test/dummy/db/schema.rb +50 -0
  80. data/test/dummy/lib/assets/.keep +0 -0
  81. data/test/dummy/log/.keep +0 -0
  82. data/test/dummy/public/404.html +58 -0
  83. data/test/dummy/public/422.html +58 -0
  84. data/test/dummy/public/500.html +57 -0
  85. data/test/dummy/public/favicon.ico +0 -0
  86. data/test/factories/all.rb +14 -0
  87. data/test/helper.rb +25 -0
  88. data/test/lib/child_definition_test.rb +43 -0
  89. data/test/lib/conversions_test.rb +39 -0
  90. data/test/lib/definition/customization_test.rb +36 -0
  91. data/test/lib/definition/formatted_test.rb +75 -0
  92. data/test/lib/definition/indexed_test.rb +57 -0
  93. data/test/lib/definition/mappings_test.rb +26 -0
  94. data/test/lib/definition_test.rb +84 -0
  95. data/test/lib/dumper_test.rb +38 -0
  96. data/test/lib/format_test.rb +64 -0
  97. data/test/lib/has_attributes/json_reader_test.rb +46 -0
  98. data/test/lib/has_attributes_test.rb +69 -0
  99. data/test/lib/model_test.rb +44 -0
  100. data/test/lib/null_definition_test.rb +27 -0
  101. data/test/lib/parent_definition_test.rb +75 -0
  102. data/test/lib/provided_test.rb +46 -0
  103. data/test/lib/segmented_test.rb +27 -0
  104. data/test/lib/serializations/reflection_test.rb +24 -0
  105. data/test/lib/serializations/serialize_many_test.rb +194 -0
  106. data/test/lib/serializations/serialize_one_test.rb +122 -0
  107. data/test/lib/serializations_test.rb +24 -0
  108. data/test/lib/serialized_child_test.rb +91 -0
  109. data/test/lib/timestamp_attributes_test.rb +14 -0
  110. data/test/lib/validations_test.rb +8 -0
  111. data/test/lib/versioned_test.rb +35 -0
  112. data/test/models/attribute_customization/local_cache_test.rb +16 -0
  113. data/test/models/attribute_customization_test.rb +8 -0
  114. data/test/models/attribute_mapping/local_cache_test.rb +31 -0
  115. data/test/models/attribute_mapping_test.rb +20 -0
  116. data/test/support/business.rb +22 -0
  117. data/test/support/pg.rb +9 -0
  118. data/test/support/vegetable.rb +8 -0
  119. metadata +215 -0
@@ -0,0 +1,26 @@
1
+ require 'helper'
2
+
3
+ class AxleAttributes::Definition::MappingsTest < ActiveSupport::TestCase
4
+ test 'to_options' do
5
+ definition = AxleAttributes::Definition.new Business, :repair_services
6
+ definition.mappings = {'bar' => 'Foo'}
7
+
8
+ assert_equal([['Foo', 'bar']], definition.to_options)
9
+ end
10
+
11
+ test 'attribute_mapping' do
12
+ attribute_mapping = create :attribute_mapping, namespace: 'Business', attribute_name: 'repair_services', label: 'Foo', value: 'bar'
13
+
14
+ definition = AxleAttributes::Definition.new Business, :repair_services
15
+
16
+ assert_equal({'bar' => 'Foo'}, definition.mappings)
17
+ assert_equal([attribute_mapping], definition.mapping_records)
18
+ end
19
+
20
+ test 'mapping_model' do
21
+ foo = AxleAttributes::Definition.new Business, :foo, mapping_model: 'Vegetable'
22
+
23
+ assert_equal Vegetable, foo.mapping_model
24
+ assert_equal(Vegetable.cached_mapping, foo.mappings)
25
+ end
26
+ end
@@ -0,0 +1,84 @@
1
+ require 'helper'
2
+
3
+ class AxleAttributes::DefinitionTest < ActiveSupport::TestCase
4
+ class TestModel
5
+ class << self
6
+ def name
7
+ 'TestModel'
8
+ end
9
+ end
10
+ end
11
+
12
+ test 'filter_control' do
13
+ assert_equal 'text', AxleAttributes::Definition.new(TestModel, :foo, type: :string).filter_control
14
+ assert_equal 'range', AxleAttributes::Definition.new(TestModel, :foo, type: :integer).filter_control
15
+ assert_equal 'text', AxleAttributes::Definition.new(TestModel, :wtf, type: nil).filter_control
16
+ assert_equal 'combo', AxleAttributes::Definition.new(TestModel, :mapperoo, type: :string, mapping_model: 'Business').filter_control
17
+ assert_equal 'hierarchy', AxleAttributes::Definition.new(TestModel, :mapperoo, type: :string, mapping_model: 'SicCode').filter_control
18
+ end
19
+
20
+ test 'edit_control' do
21
+ assert_equal 'combo', AxleAttributes::Definition.new(TestModel, :bar, type: :boolean).edit_control
22
+ assert_equal 'textarea', AxleAttributes::Definition.new(TestModel, :bar, type: :text).edit_control
23
+ assert_equal 'filter', AxleAttributes::Definition.new(TestModel, :bar, type: :time).edit_control
24
+ assert_equal 'text', AxleAttributes::Definition.new(TestModel, :bar, type: :float).edit_control
25
+ assert_equal 'text', AxleAttributes::Definition.new(TestModel, :bar, type: :integer).edit_control
26
+ assert_equal 'text', AxleAttributes::Definition.new(TestModel, :bar, type: :string).edit_control
27
+
28
+ AxleAttributes::Definition.new(TestModel, :bar, type: :string).tap do |definition|
29
+ definition.mappings = {'foo' => 'bar'}
30
+ assert_equal 'combo', definition.edit_control
31
+ end
32
+ end
33
+
34
+ test 'aliases' do
35
+ foo = AxleAttributes::Definition.new TestModel, :foo, mmdb_name: 'hola'
36
+
37
+ assert_equal ['foo', 'hola', 'Foo'].to_set, foo.aliases.to_set
38
+ end
39
+
40
+ test 'to_param' do
41
+ assert_equal 'foo', AxleAttributes::Definition.new(TestModel, :foo, mmdb_name: 'hola').to_param
42
+ end
43
+
44
+ test 'namespace' do
45
+ assert_equal 'TestModel', AxleAttributes::Definition.new(TestModel, :foo).namespace
46
+ end
47
+
48
+ test 'type' do
49
+ foo = AxleAttributes::Definition.new TestModel, :foo, type: :string
50
+ assert_equal :string, foo.type
51
+ end
52
+
53
+ test 'format' do
54
+ foo = AxleAttributes::Definition.new TestModel, :foo, type: :string, format: :zip
55
+ assert_equal :zip, foo.format
56
+ end
57
+
58
+ test 'indexed' do
59
+ assert AxleAttributes::Definition.new(TestModel, :foo, index: true).indexed?
60
+ refute AxleAttributes::Definition.new(TestModel, :foo, index: false).indexed?
61
+ end
62
+
63
+ test 'versioned' do
64
+ assert AxleAttributes::Definition.new(TestModel, :foo, version: true).versioned?
65
+ refute AxleAttributes::Definition.new(TestModel, :foo).versioned?
66
+ end
67
+
68
+ test 'readonly' do
69
+ refute AxleAttributes::Definition.new(TestModel, :foo, type: :string).readonly?
70
+ refute AxleAttributes::Definition.new(TestModel, :foo, type: :string, editable: true).readonly?
71
+ assert AxleAttributes::Definition.new(TestModel, :foo, type: :string, editable: false).readonly?
72
+ end
73
+
74
+ test 'readonly when editable' do
75
+ attribute_customization = create :attribute_customization, name: 'foo', namespace: 'Business', editable: false
76
+ assert AxleAttributes::Definition.new(Business, :foo, type: :string).readonly?
77
+ refute AxleAttributes::Definition.new(Business, :bar, type: :string).readonly?
78
+ end
79
+
80
+ test 'readonly when deprecated' do
81
+ attribute_customization = create :attribute_customization, name: 'foo', namespace: 'Business', deprecated_on: Date.new
82
+ assert AxleAttributes::Definition.new(Business, :foo, type: :string, editable: true).readonly?
83
+ end
84
+ end
@@ -0,0 +1,38 @@
1
+ require 'helper'
2
+
3
+ class AxleAttributes::DumperTest < ActiveSupport::TestCase
4
+ class TestModel
5
+ def self.attributes
6
+ {
7
+ 'foo' => AxleAttributes::ParentDefinition.new(self, 'foo', type: :string, version: true),
8
+ 'momma' => AxleAttributes::ParentDefinition.new(self, 'momma', type: :integer, version: false),
9
+ }
10
+ end
11
+
12
+ def self.attribute_set
13
+ attributes.keys.to_set
14
+ end
15
+ end
16
+
17
+ test 'dump_internal' do
18
+ result = dumper.dump_internal
19
+
20
+ assert_match /Attribute,Display Name,Type,Deprecation/, result
21
+ assert_match Regexp.new('foo,Foo'), result
22
+ assert_match Regexp.new('momma,Momma'), result
23
+ end
24
+
25
+ test 'dump_public' do
26
+ result = dumper.dump_public(['foo'])
27
+
28
+ assert_match /Attribute,Display Name,Type,Description/, result
29
+ assert_match Regexp.new('foo,Foo'), result
30
+ refute_match Regexp.new('momma,Momma'), result
31
+ end
32
+
33
+ private
34
+
35
+ def dumper
36
+ @dumper ||= AxleAttributes::Dumper.new(TestModel)
37
+ end
38
+ end
@@ -0,0 +1,64 @@
1
+ require 'helper'
2
+
3
+ class AxleAttributes::FormatTest < ActiveSupport::TestCase
4
+ test 'standardize_phone' do
5
+ assert_equal nil, AxleAttributes::Format.standardize_phone(nil)
6
+ assert_equal 'too4few', AxleAttributes::Format.standardize_phone('too4few')
7
+ assert_equal '(222) 456-7890', AxleAttributes::Format.standardize_phone("2#2*2 \t4\n\n\n5()()()6.78====\\/===90!!!")
8
+ assert_equal '(123) 456-7890', AxleAttributes::Format.standardize_phone('1234567890')
9
+ assert_equal '(123) 456-7890', AxleAttributes::Format.standardize_phone(' (123) 456-7890 ')
10
+ assert_equal '(123) 456-7890', AxleAttributes::Format.standardize_phone('1-123-456-7890 ')
11
+ assert_equal '(800) 356-9377', AxleAttributes::Format.standardize_phone('1-800-FLOWERS')
12
+ assert_equal '(800) 356-9377', AxleAttributes::Format.standardize_phone('1-800-flowers')
13
+ assert_equal 'some string that is not a phone number', AxleAttributes::Format.standardize_phone('some string that is not a phone number')
14
+ end
15
+
16
+ test 'standardize_postal_code' do
17
+ assert_equal nil, AxleAttributes::Format.standardize_postal_code(nil)
18
+ assert_equal '', AxleAttributes::Format.standardize_postal_code('')
19
+ assert_equal '12345', AxleAttributes::Format.standardize_postal_code('12345')
20
+ assert_equal '12345', AxleAttributes::Format.standardize_postal_code('123 45')
21
+ assert_equal '12345-6789', AxleAttributes::Format.standardize_postal_code('12345-6789')
22
+ assert_equal '12345-6789', AxleAttributes::Format.standardize_postal_code('123456789')
23
+ assert_equal 'H0H0H0', AxleAttributes::Format.standardize_postal_code('H0H0H0')
24
+ assert_equal 'H0H0H0', AxleAttributes::Format.standardize_postal_code('h0h0h0')
25
+ assert_equal 'H0H0H0', AxleAttributes::Format.standardize_postal_code('H0H 0H0')
26
+ assert_equal 'Bogus Value', AxleAttributes::Format.standardize_postal_code('Bogus Value')
27
+ end
28
+
29
+ test "standardize_url" do
30
+ assert_equal nil, AxleAttributes::Format.standardize_url(nil)
31
+ assert_equal '', AxleAttributes::Format.standardize_url('')
32
+ assert_equal 'website', AxleAttributes::Format.standardize_url('website')
33
+ assert_equal '@twitter', AxleAttributes::Format.standardize_url('@twitter')
34
+ assert_equal 'http://@twitter', AxleAttributes::Format.standardize_url('http://@twitter')
35
+ assert_equal 'http://@twitter.com', AxleAttributes::Format.standardize_url('http://@twitter.com')
36
+ assert_equal 'httpssss://www.twitter.com', AxleAttributes::Format.standardize_url('httpssss://www.twitter.com')
37
+ assert_equal 'ftp://mywebsite.com', AxleAttributes::Format.standardize_url('ftp://mywebsite.com')
38
+ assert_equal 'http:www.infogroup.com', AxleAttributes::Format.standardize_url('http:www.infogroup.com')
39
+ assert_equal 'htp://www.infogroup.com', AxleAttributes::Format.standardize_url('htp://www.infogroup.com')
40
+ assert_equal 'http::///www.infogroup.com', AxleAttributes::Format.standardize_url('http::///www.infogroup.com')
41
+ assert_equal 'bad[twitter.com', AxleAttributes::Format.standardize_url('bad[twitter.com')
42
+
43
+ assert_equal "http://www.infogroup.co.uk", AxleAttributes::Format.standardize_url('http://www.infogroup.co.uk')
44
+ assert_equal "http://www.infogroup.co.uk", AxleAttributes::Format.standardize_url('Http://www.infogroup.co.uk')
45
+ assert_equal "https://www.infogroup.co.uk", AxleAttributes::Format.standardize_url('https://www.infogroup.co.uk')
46
+ assert_equal "https://foo.com/xyz/%20yzz", AxleAttributes::Format.standardize_url('https://foo.com/xyz/ yzz')
47
+ assert_equal "http://infogroup.co.uk", AxleAttributes::Format.standardize_url('infogroup.co.uk')
48
+ assert_equal "http://infogroup.com", AxleAttributes::Format.standardize_url('INFOGROUP.COM')
49
+ assert_equal "http://infogroup.com", AxleAttributes::Format.standardize_url('http://infogroup.com ')
50
+ assert_equal "http://infogroup.com/places/AbC123.html", AxleAttributes::Format.standardize_url('infogroup.com/places/AbC123.html')
51
+ assert_equal "http://infogroup.com/places/AbC%20123.html", AxleAttributes::Format.standardize_url('infogroup.com/places/AbC 123.html')
52
+ assert_equal "http://infogroup.com/places/AbC123.html", AxleAttributes::Format.standardize_url('infogroup.com/places/AbC123.html ')
53
+ assert_equal "http://infogroup.com/places/AbC123.html?ids=47,99#GREATNESS", AxleAttributes::Format.standardize_url('infogroup.com/places/AbC123.html?ids=47,99#GREATNESS')
54
+ assert_equal "http://infogroup.com:8080/my/photo.png", AxleAttributes::Format.standardize_url('http://infogroup.com:8080/my/photo.png')
55
+ assert_equal "http://infogroup.com:65535", AxleAttributes::Format.standardize_url('infogroup.com:65535')
56
+ assert_equal "http://infogroup.com", AxleAttributes::Format.standardize_url('http://infogroup.com/')
57
+ assert_equal "http://infogroup.com/fun/time/", AxleAttributes::Format.standardize_url('http://infogroup.com/fun/time/')
58
+ end
59
+
60
+ test "url_regex" do
61
+ assert "http://infogroup.com".match AxleAttributes::Format.url_regex
62
+ assert "http://infogroup.com:65535".match AxleAttributes::Format.url_regex
63
+ end
64
+ end
@@ -0,0 +1,46 @@
1
+ require 'helper'
2
+
3
+ class AxleAttributes::HasAttributes::JsonReaderTest < ActiveSupport::TestCase
4
+ class Pet < Superstore::Base
5
+ include AxleAttributes::SerializedChild
6
+ end
7
+
8
+ class TestModel < Superstore::Base
9
+ include AxleAttributes::Model
10
+
11
+ class << self
12
+ def name
13
+ 'TestModel'
14
+ end
15
+ end
16
+
17
+ json = {
18
+ 'location' => {
19
+ 'street' => {'type' => 'string', 'format' => 'phone'}
20
+ },
21
+ 'pets' => {
22
+ 'class_name' => 'AxleAttributes::HasAttributes::JsonReaderTest::Pet',
23
+ 'type' => 'serialize_many',
24
+ 'attributes' => {
25
+ 'name' => {'type' => 'string'},
26
+ 'disposition' => {'type' => 'string'}
27
+ }
28
+ }
29
+ }
30
+
31
+ AxleAttributes::HasAttributes::JsonReader.read_definitions(TestModel, json, defaults: {index: true, version: false})
32
+ end
33
+
34
+ test 'simple definition' do
35
+ refute_nil TestModel.attributes['street']
36
+ assert_equal :string, TestModel.attributes['street'].options[:type]
37
+ assert_equal :phone, TestModel.attributes['street'].options[:format]
38
+ assert_equal true, TestModel.attributes['street'].options[:index]
39
+ assert_equal false, TestModel.attributes['street'].options[:version]
40
+ end
41
+
42
+ test 'serialization definition' do
43
+ refute_nil TestModel.serialization_reflections[:pets]
44
+ assert_equal :string, Pet.attributes['disposition'].options[:type]
45
+ end
46
+ end
@@ -0,0 +1,69 @@
1
+ require 'helper'
2
+
3
+ class AxleAttributes::HasAttributesTest < ActiveSupport::TestCase
4
+ class TestModel
5
+ include AxleAttributes::HasAttributes
6
+
7
+ class << self
8
+ def name
9
+ 'TestModel'
10
+ end
11
+
12
+ def reset!
13
+ attributes.clear
14
+ attribute_set.clear
15
+ end
16
+ end
17
+ end
18
+
19
+ setup do
20
+ TestModel.reset!
21
+ end
22
+
23
+ test 'has_attributes' do
24
+ TestModel.has_attributes foo: {}
25
+ assert_kind_of AxleAttributes::Definition, TestModel.attributes['foo']
26
+
27
+ TestModel.has_attributes({ bar: {} }, { editable: false })
28
+ assert TestModel.attributes['bar'].readonly?
29
+
30
+ TestModel.has_attributes({ baz: { editable: true } }, { editable: false })
31
+ refute TestModel.attributes['baz'].readonly?
32
+ end
33
+
34
+ test 'has_attribute' do
35
+ TestModel.has_attribute :foo
36
+ assert_kind_of AxleAttributes::Definition, TestModel.attributes['foo']
37
+ assert_equal ['foo'].to_set, TestModel.attribute_set
38
+ end
39
+
40
+ test 'has_mapping' do
41
+ TestModel.has_attribute :foo
42
+ TestModel.has_mapping :foo, %w(foo bar)
43
+ expected = {'foo' => 'Foo', 'bar' => 'Bar'}
44
+ assert_equal expected, TestModel.attributes['foo'].mappings
45
+ end
46
+
47
+ test 'has_attributes_file' do
48
+ attributes_file = Tempfile.new(['attributes', '.json'])
49
+ attributes_file.write({"speshal" => {"colors" => {"type" => "array"}}}.to_json)
50
+ attributes_file.rewind
51
+ TestModel.attributes_file_root = File.dirname(attributes_file.path)
52
+
53
+ TestModel.has_attributes_file File.basename(attributes_file.path, '.json')
54
+
55
+ assert_equal(:array, TestModel.attributes['colors'].type)
56
+ assert_equal File.basename(attributes_file.path, '.json'), TestModel.attributes['colors'].options[:filename]
57
+ end
58
+
59
+ test 'attribute inheritance' do
60
+ child_model = Class.new(TestModel)
61
+ child_model.has_attribute :foo, {}
62
+
63
+ assert child_model.attributes.key?('foo')
64
+ assert child_model.attribute_set.include?('foo')
65
+
66
+ assert !TestModel.attributes.key?('foo')
67
+ assert !TestModel.attribute_set.include?('foo')
68
+ end
69
+ end
@@ -0,0 +1,44 @@
1
+ require 'helper'
2
+
3
+ class AxleAttributes::ModelTest < ActiveSupport::TestCase
4
+ class Person < Superstore::Base
5
+ include AxleAttributes::Model
6
+ end
7
+
8
+ test 'definition_class' do
9
+ assert_equal '::AxleAttributes::ParentDefinition', Person.has_attribute_definition_class
10
+ end
11
+
12
+ test 'core attributes' do
13
+ refute_nil Person.attributes['created_at']
14
+ refute_nil Person.attributes['updated_at']
15
+ refute_nil Person.attributes['segment_id']
16
+
17
+ assert Person.attributes['created_at'].readonly?
18
+ assert Person.attributes['updated_at'].readonly?
19
+ assert Person.attributes['segment_id'].readonly?
20
+
21
+ assert_equal({type: "date", format: "dateOptionalTime", doc_values: true}, Person.elastic_index.mapping[:properties]['created_at'])
22
+ assert_equal({type: "date", format: "dateOptionalTime", doc_values: true}, Person.elastic_index.mapping[:properties]['updated_at'])
23
+ assert_equal({type: "long", doc_values: true}, Person.elastic_index.mapping[:properties]['segment_id'])
24
+ end
25
+
26
+ module Framework
27
+ class Base < Superstore::Base
28
+ include AxleAttributes::Model
29
+ end
30
+ end
31
+
32
+ class InheritedModel < Framework::Base
33
+ end
34
+
35
+ test 'core attribute inheritence' do
36
+ assert_nil Framework::Base.attributes['created_at']
37
+ assert_nil Framework::Base.attributes['updated_at']
38
+ assert_nil Framework::Base.attributes['segment_id']
39
+
40
+ refute_nil Person.attributes['created_at']
41
+ refute_nil Person.attributes['updated_at']
42
+ refute_nil Person.attributes['segment_id']
43
+ end
44
+ end
@@ -0,0 +1,27 @@
1
+ require 'helper'
2
+
3
+ class AxleAttributes::NullDefinitionTest < ActiveSupport::TestCase
4
+
5
+ test "reports the given name" do
6
+ assert_equal 'somename', AxleAttributes::NullDefinition.new(Business, 'somename').name
7
+ end
8
+
9
+ test "has a null type" do
10
+ assert_equal :null, AxleAttributes::NullDefinition.new(Business, 'whatzit').type
11
+ end
12
+
13
+ test "equality" do
14
+ definition1 = AxleAttributes::NullDefinition.new(Business, 'attr')
15
+ definition2 = AxleAttributes::NullDefinition.new(Business, 'attr')
16
+ assert_equal definition1, definition2
17
+
18
+ # definition1 = AxleAttributes::NullDefinition.new(Business, 'attr')
19
+ # definition2 = AxleAttributes::NullDefinition.new(Contact, 'attr')
20
+ # assert_not_equal definition1, definition2
21
+
22
+ definition1 = AxleAttributes::NullDefinition.new(Business, 'attr')
23
+ definition2 = AxleAttributes::NullDefinition.new(Business, 'different')
24
+ assert_not_equal definition1, definition2
25
+ end
26
+
27
+ end
@@ -0,0 +1,75 @@
1
+ require 'helper'
2
+
3
+ class AxleAttributes::ParentDefinitionTest < ActiveSupport::TestCase
4
+ class TestModel < Superstore::Base
5
+ include AxleAttributes::Model
6
+
7
+ class << self
8
+ def reset!
9
+ elastic_index.mapping[:properties].clear
10
+ speshal_attributes.clear
11
+ versioned_attributes.clear
12
+ end
13
+
14
+ def base_class
15
+ TestModel
16
+ end
17
+
18
+ def attribute(name, options = {})
19
+ speshal_attributes[name] = options
20
+ end
21
+
22
+ def speshal_attributes
23
+ @@speshal_attributes ||= {}
24
+ end
25
+ end
26
+ end
27
+
28
+ setup do
29
+ TestModel.reset!
30
+ end
31
+
32
+ test 'attribute with blank' do
33
+ AxleAttributes::ParentDefinition.new(TestModel, :foo).setup!
34
+
35
+ assert_equal({}, TestModel.speshal_attributes)
36
+ end
37
+
38
+ test 'attribute with default' do
39
+ AxleAttributes::ParentDefinition.new(TestModel, :foo, type: :integer, default: 42).setup!
40
+
41
+ assert_equal({'foo' => {type: :integer, default: 42}}, TestModel.speshal_attributes)
42
+ end
43
+
44
+ test 'attribute with type integer' do
45
+ AxleAttributes::ParentDefinition.new(TestModel, :foo, type: :integer).setup!
46
+
47
+ assert_equal({'foo' => {type: :integer}}, TestModel.speshal_attributes)
48
+ end
49
+
50
+ test 'attribute with type text' do
51
+ AxleAttributes::ParentDefinition.new(TestModel, :foo, type: :text).setup!
52
+
53
+ assert_equal({'foo' => {type: :string}}, TestModel.speshal_attributes)
54
+ end
55
+
56
+ test 'setup index' do
57
+ definition = AxleAttributes::ParentDefinition.new(TestModel, :foo, type: :string, index: :snowball)
58
+
59
+ definition.setup!
60
+
61
+ assert_equal definition.index_mapping, TestModel.elastic_index.mapping[:properties]['foo']
62
+ end
63
+
64
+ test 'version with false' do
65
+ AxleAttributes::ParentDefinition.new(TestModel, :foo, version: false).setup!
66
+
67
+ assert_equal [].to_set, TestModel.versioned_attributes
68
+ end
69
+
70
+ test 'version with true' do
71
+ AxleAttributes::ParentDefinition.new(TestModel, :foo, version: true).setup!
72
+
73
+ assert_equal ['foo'].to_set, TestModel.versioned_attributes
74
+ end
75
+ end