axle_attributes 1.13.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/.travis.yml +16 -0
- data/Gemfile +15 -0
- data/README.md +10 -0
- data/Rakefile +18 -0
- data/app/models/attribute_customization.rb +7 -0
- data/app/models/attribute_customization/local_cache.rb +28 -0
- data/app/models/attribute_mapping.rb +15 -0
- data/app/models/attribute_mapping/local_cache.rb +40 -0
- data/axle_attributes.gemspec +22 -0
- data/bin/rails +8 -0
- data/db/migrate/20131118042651_create_attribute_customizations.rb +16 -0
- data/db/migrate/20131121013237_create_attribute_mappings.rb +17 -0
- data/db/migrate/20151120160522_add_editable_to_attribute_customizations.rb +7 -0
- data/lib/axle_attributes.rb +30 -0
- data/lib/axle_attributes/child_definition.rb +42 -0
- data/lib/axle_attributes/conversions.rb +36 -0
- data/lib/axle_attributes/definition.rb +118 -0
- data/lib/axle_attributes/definition/callbacks.rb +34 -0
- data/lib/axle_attributes/definition/customization.rb +51 -0
- data/lib/axle_attributes/definition/formatted.rb +57 -0
- data/lib/axle_attributes/definition/indexed.rb +41 -0
- data/lib/axle_attributes/definition/mappings.rb +48 -0
- data/lib/axle_attributes/dumper.rb +54 -0
- data/lib/axle_attributes/engine.rb +4 -0
- data/lib/axle_attributes/format.rb +74 -0
- data/lib/axle_attributes/has_attributes.rb +104 -0
- data/lib/axle_attributes/has_attributes/json_reader.rb +87 -0
- data/lib/axle_attributes/model.rb +34 -0
- data/lib/axle_attributes/null_definition.rb +11 -0
- data/lib/axle_attributes/parent_definition.rb +37 -0
- data/lib/axle_attributes/provided.rb +67 -0
- data/lib/axle_attributes/regex.rb +4 -0
- data/lib/axle_attributes/segmented.rb +30 -0
- data/lib/axle_attributes/serializations.rb +147 -0
- data/lib/axle_attributes/serializations/builder/serialization.rb +83 -0
- data/lib/axle_attributes/serializations/builder/serialize_many.rb +17 -0
- data/lib/axle_attributes/serializations/builder/serialize_one.rb +7 -0
- data/lib/axle_attributes/serializations/reflection.rb +40 -0
- data/lib/axle_attributes/serializations/serialization.rb +88 -0
- data/lib/axle_attributes/serializations/serialize_many.rb +88 -0
- data/lib/axle_attributes/serializations/serialize_one.rb +34 -0
- data/lib/axle_attributes/serialized_child.rb +58 -0
- data/lib/axle_attributes/validations.rb +29 -0
- data/lib/axle_attributes/versioned.rb +21 -0
- data/test/dummy/README.rdoc +6 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/images/.keep +0 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/concerns/.keep +0 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/mailers/.keep +0 -0
- data/test/dummy/app/models/.keep +0 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +12 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +13 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +29 -0
- data/test/dummy/config/environments/production.rb +80 -0
- data/test/dummy/config/environments/test.rb +38 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +12 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/db/schema.rb +50 -0
- data/test/dummy/lib/assets/.keep +0 -0
- data/test/dummy/log/.keep +0 -0
- data/test/dummy/public/404.html +58 -0
- data/test/dummy/public/422.html +58 -0
- data/test/dummy/public/500.html +57 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/factories/all.rb +14 -0
- data/test/helper.rb +25 -0
- data/test/lib/child_definition_test.rb +43 -0
- data/test/lib/conversions_test.rb +39 -0
- data/test/lib/definition/customization_test.rb +36 -0
- data/test/lib/definition/formatted_test.rb +75 -0
- data/test/lib/definition/indexed_test.rb +57 -0
- data/test/lib/definition/mappings_test.rb +26 -0
- data/test/lib/definition_test.rb +84 -0
- data/test/lib/dumper_test.rb +38 -0
- data/test/lib/format_test.rb +64 -0
- data/test/lib/has_attributes/json_reader_test.rb +46 -0
- data/test/lib/has_attributes_test.rb +69 -0
- data/test/lib/model_test.rb +44 -0
- data/test/lib/null_definition_test.rb +27 -0
- data/test/lib/parent_definition_test.rb +75 -0
- data/test/lib/provided_test.rb +46 -0
- data/test/lib/segmented_test.rb +27 -0
- data/test/lib/serializations/reflection_test.rb +24 -0
- data/test/lib/serializations/serialize_many_test.rb +194 -0
- data/test/lib/serializations/serialize_one_test.rb +122 -0
- data/test/lib/serializations_test.rb +24 -0
- data/test/lib/serialized_child_test.rb +91 -0
- data/test/lib/timestamp_attributes_test.rb +14 -0
- data/test/lib/validations_test.rb +8 -0
- data/test/lib/versioned_test.rb +35 -0
- data/test/models/attribute_customization/local_cache_test.rb +16 -0
- data/test/models/attribute_customization_test.rb +8 -0
- data/test/models/attribute_mapping/local_cache_test.rb +31 -0
- data/test/models/attribute_mapping_test.rb +20 -0
- data/test/support/business.rb +22 -0
- data/test/support/pg.rb +9 -0
- data/test/support/vegetable.rb +8 -0
- 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
|