jat 0.0.3 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jat/attribute.rb +26 -5
  3. data/lib/jat/config.rb +4 -7
  4. data/lib/jat/plugins/activerecord/activerecord.rb +23 -0
  5. data/lib/jat/plugins/base/base_activerecord_preloads/base_activerecord_preloads.rb +38 -0
  6. data/lib/jat/plugins/{_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +22 -16
  7. data/lib/jat/plugins/base/base_lower_camel_case/base_lower_camel_case.rb +36 -0
  8. data/lib/jat/plugins/{_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +14 -4
  9. data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/format_user_preloads.rb +1 -1
  10. data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/preloads_with_path.rb +1 -1
  11. data/lib/jat/plugins/cache/cache.rb +14 -6
  12. data/lib/jat/plugins/json_api/json_api.rb +150 -106
  13. data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +40 -0
  14. data/lib/jat/plugins/json_api/lib/include_param_parser.rb +84 -0
  15. data/lib/jat/plugins/json_api/lib/map.rb +92 -27
  16. data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +8 -5
  17. data/lib/jat/plugins/json_api/lib/response.rb +81 -197
  18. data/lib/jat/plugins/json_api/lib/response_piece.rb +175 -0
  19. data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +23 -0
  20. data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +34 -0
  21. data/lib/jat/plugins/json_api/plugins/json_api_maps_cache/json_api_maps_cache.rb +58 -0
  22. data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +38 -0
  23. data/lib/jat/plugins/{_json_api_activerecord → json_api/plugins/json_api_preloads}/lib/preloads.rb +17 -25
  24. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/json_api_validate_params.rb +61 -0
  25. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/params_error.rb +6 -0
  26. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
  27. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -0
  28. data/lib/jat/plugins/lower_camel_case/lower_camel_case.rb +23 -0
  29. data/lib/jat/plugins/maps_cache/maps_cache.rb +23 -0
  30. data/lib/jat/plugins/preloads/preloads.rb +23 -0
  31. data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +97 -0
  32. data/lib/jat/plugins/simple_api/lib/map.rb +80 -10
  33. data/lib/jat/plugins/simple_api/lib/response.rb +75 -90
  34. data/lib/jat/plugins/simple_api/lib/response_piece.rb +80 -0
  35. data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +23 -0
  36. data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +34 -0
  37. data/lib/jat/plugins/simple_api/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +50 -0
  38. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/lib/preloads.rb +55 -0
  39. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +38 -0
  40. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
  41. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
  42. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/simple_api_validate_params.rb +49 -0
  43. data/lib/jat/plugins/simple_api/simple_api.rb +87 -27
  44. data/lib/jat/plugins/to_str/to_str.rb +15 -5
  45. data/lib/jat/plugins/types/types.rb +54 -0
  46. data/lib/jat/plugins/validate_params/validate_params.rb +23 -0
  47. data/lib/jat/plugins.rb +42 -30
  48. data/lib/jat.rb +40 -35
  49. data/test/lib/jat/attribute_test.rb +22 -4
  50. data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
  51. data/test/lib/jat/plugins/base_activerecord_preloads/base_activerecord_preloads_test.rb +59 -0
  52. data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +11 -25
  53. data/test/lib/jat/plugins/base_lower_camel_case/base_lower_camel_case_test.rb +26 -0
  54. data/test/lib/jat/plugins/{_preloads/_preloads_test.rb → base_preloads/base_preloads_test.rb} +2 -2
  55. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/format_user_preloads_test.rb +3 -3
  56. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/preloads_with_path_test.rb +2 -2
  57. data/test/lib/jat/plugins/cache/cache_test.rb +11 -11
  58. data/test/lib/jat/plugins/json_api/json_api_test.rb +57 -49
  59. data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +46 -0
  60. data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +57 -0
  61. data/test/lib/jat/plugins/json_api/lib/map_test.rb +171 -92
  62. data/test/lib/jat/plugins/json_api/lib/response_piece_test.rb +13 -0
  63. data/test/lib/jat/plugins/json_api/lib/response_test.rb +38 -32
  64. data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +24 -0
  65. data/test/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case_test.rb +85 -0
  66. data/test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb +107 -0
  67. data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +51 -0
  68. data/test/lib/jat/plugins/{_json_api_activerecord → json_api_preloads}/lib/preloads_test.rb +19 -13
  69. data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +108 -0
  70. data/test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb +27 -0
  71. data/test/lib/jat/plugins/maps_cache/maps_cache_test.rb +27 -0
  72. data/test/lib/jat/plugins/preloads/preloads_test.rb +27 -0
  73. data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +87 -0
  74. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +118 -33
  75. data/test/lib/jat/plugins/simple_api/lib/response_piece_test.rb +13 -0
  76. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +90 -76
  77. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +118 -25
  78. data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +24 -0
  79. data/test/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb +54 -0
  80. data/test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb +95 -0
  81. data/test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb +140 -0
  82. data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +51 -0
  83. data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +101 -0
  84. data/test/lib/jat/plugins/to_str/to_str_test.rb +3 -3
  85. data/test/lib/jat/plugins/types/types_test.rb +84 -0
  86. data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
  87. data/test/lib/jat/plugins_test.rb +88 -0
  88. data/test/lib/jat_test.rb +47 -24
  89. data/test/lib/plugin_test.rb +3 -3
  90. data/test/test_helper.rb +7 -6
  91. data/test/test_plugin.rb +9 -12
  92. metadata +107 -88
  93. data/CHANGELOG.md +0 -7
  94. data/README.md +0 -21
  95. data/jat.gemspec +0 -37
  96. data/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads.rb +0 -29
  97. data/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord.rb +0 -22
  98. data/lib/jat/plugins/camel_lower/camel_lower.rb +0 -18
  99. data/lib/jat/plugins/json_api/lib/construct_traversal_map.rb +0 -91
  100. data/lib/jat/plugins/json_api/lib/presenters/document_links_presenter.rb +0 -48
  101. data/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter.rb +0 -48
  102. data/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter.rb +0 -48
  103. data/lib/jat/plugins/json_api/lib/presenters/links_presenter.rb +0 -48
  104. data/lib/jat/plugins/json_api/lib/presenters/meta_presenter.rb +0 -48
  105. data/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter.rb +0 -53
  106. data/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter.rb +0 -53
  107. data/lib/jat/plugins/json_api/lib/traversal_map.rb +0 -34
  108. data/lib/jat/plugins/simple_api/lib/construct_traversal_map.rb +0 -45
  109. data/lib/jat/plugins/simple_api/lib/params/parse.rb +0 -68
  110. data/lib/jat/presenter.rb +0 -51
  111. data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +0 -40
  112. data/test/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord_test.rb +0 -29
  113. data/test/lib/jat/plugins/camel_lower/camel_lower_test.rb +0 -78
  114. data/test/lib/jat/plugins/json_api/lib/construct_traversal_map_test.rb +0 -119
  115. data/test/lib/jat/plugins/json_api/lib/params/fields/parse_test.rb +0 -24
  116. data/test/lib/jat/plugins/json_api/lib/params/fields/validate_test.rb +0 -47
  117. data/test/lib/jat/plugins/json_api/lib/params/fields_test.rb +0 -37
  118. data/test/lib/jat/plugins/json_api/lib/params/include/parse_test.rb +0 -46
  119. data/test/lib/jat/plugins/json_api/lib/params/include/validate_test.rb +0 -51
  120. data/test/lib/jat/plugins/json_api/lib/params/include_test.rb +0 -41
  121. data/test/lib/jat/plugins/json_api/lib/presenters/document_links_presenter_test.rb +0 -69
  122. data/test/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter_test.rb +0 -69
  123. data/test/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter_test.rb +0 -69
  124. data/test/lib/jat/plugins/json_api/lib/presenters/links_presenter_test.rb +0 -69
  125. data/test/lib/jat/plugins/json_api/lib/presenters/meta_presenter_test.rb +0 -69
  126. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter_test.rb +0 -75
  127. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter_test.rb +0 -75
  128. data/test/lib/jat/plugins/json_api/lib/traversal_map_test.rb +0 -58
  129. data/test/lib/jat/plugins/simple_api/lib/construct_traversal_map_test.rb +0 -100
  130. data/test/lib/jat/plugins/simple_api/lib/params/parse_test.rb +0 -71
  131. data/test/lib/jat/presenter_test.rb +0 -61
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::SimpleApiMapsCache" do
6
+ before do
7
+ @plugin = Jat::Plugins.find_plugin(:simple_api_maps_cache)
8
+ end
9
+
10
+ it "checks simple_api plugin loaded before" do
11
+ jat_class = Class.new(Jat)
12
+ error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
13
+ assert_match(/simple_api/, error.message)
14
+ end
15
+
16
+ it "adds config variable how many maps to store per serializer" do
17
+ jat_class = Class.new(Jat)
18
+
19
+ @plugin.after_load(jat_class)
20
+ assert_equal(100, jat_class.config[:cached_maps_count]) # default 100
21
+
22
+ @plugin.after_load(jat_class, cached_maps_count: 10) # change value via opts
23
+ assert_equal(10, jat_class.config[:cached_maps_count])
24
+ end
25
+
26
+ describe "Test maps responses" do
27
+ let(:jat_class) do
28
+ Class.new(Jat) do
29
+ plugin(:simple_api)
30
+ plugin(:simple_api_maps_cache)
31
+
32
+ attribute(:attr1)
33
+ attribute(:attr2)
34
+ end
35
+ end
36
+
37
+ it "returns same maps when requested with same params" do
38
+ full_map1 = jat_class::Map.call(exposed: :all)
39
+ full_map2 = jat_class::Map.call(exposed: :all)
40
+
41
+ exposed_map1 = jat_class::Map.call(exposed: :default)
42
+ exposed_map2 = jat_class::Map.call(exposed: :default)
43
+
44
+ current_map1 = jat_class::Map.call(fields: "attr1")
45
+ current_map2 = jat_class::Map.call(fields: "attr1")
46
+ current_map3 = jat_class::Map.call(fields: "attr2")
47
+
48
+ assert_same(full_map1, full_map2)
49
+ assert_same(exposed_map1, exposed_map2)
50
+ assert_same(current_map1, current_map2)
51
+
52
+ # should not match, fields are not same
53
+ assert !current_map1.equal?(current_map3)
54
+ end
55
+
56
+ it "stores different maps keys" do
57
+ # key 1
58
+ jat_class::Map.call(exposed: :all)
59
+ jat_class::Map.call(exposed: :all)
60
+
61
+ # key 2
62
+ jat_class::Map.call(exposed: :default)
63
+ jat_class::Map.call(exposed: :default)
64
+
65
+ # key 3
66
+ jat_class::Map.call(fields: "attr1")
67
+ jat_class::Map.call(fields: "attr1")
68
+
69
+ # key 4
70
+ jat_class::Map.call(fields: "attr2")
71
+
72
+ # key 5
73
+ jat_class::Map.call(fields: "attr1,attr2")
74
+
75
+ assert_equal 5, jat_class::Map.maps_cache.keys.count
76
+ end
77
+
78
+ it "clears old results when there are too many cache keys" do
79
+ jat_class.config[:cached_maps_count] = 1
80
+
81
+ full_map1 = jat_class::Map.call(exposed: :all)
82
+ full_map2 = jat_class::Map.call(exposed: :all)
83
+
84
+ # ensure maps refer to same object
85
+ assert_same(full_map1, full_map2)
86
+
87
+ # replace single possible cache key with another `exposed` map
88
+ jat_class::Map.call(exposed: :default)
89
+
90
+ # calculate full map again, it should not match
91
+ full_map3 = jat_class::Map.call(exposed: :all)
92
+ assert !full_map1.equal?(full_map3)
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::SimpleApiPreloads::Preloads" do
6
+ let(:base) do
7
+ Class.new(Jat) do
8
+ plugin :simple_api
9
+ plugin :simple_api_preloads
10
+ end
11
+ end
12
+
13
+ let(:user_serializer) { Class.new(base) }
14
+ let(:profile_serializer) { Class.new(base) }
15
+ let(:email_serializer) { Class.new(base) }
16
+
17
+ let(:jat_user) { user_serializer.new }
18
+ let(:jat_profile) { profile_serializer.new }
19
+ let(:jat_email) { email_serializer.new }
20
+
21
+ let(:described_class) { Jat::Plugins::SimpleApiPreloads::Preloads }
22
+
23
+ def define_map(map)
24
+ user_serializer::Map.expects(:call).returns(map)
25
+ end
26
+
27
+ it "returns empty hash when no attributes requested" do
28
+ define_map({})
29
+
30
+ result = described_class.call(jat_user)
31
+ assert_equal({}, result)
32
+ end
33
+
34
+ it "returns empty hash when no attributes with preloads requested" do
35
+ user_serializer.attribute :name
36
+ define_map({name: {}})
37
+
38
+ result = described_class.call(jat_user)
39
+ assert_equal({}, result)
40
+ end
41
+
42
+ it "returns preloads for requested attributes" do
43
+ user_serializer.attribute :name, preload: :profile
44
+ define_map({name: {}})
45
+
46
+ result = described_class.call(jat_user)
47
+ assert_equal({profile: {}}, result)
48
+ end
49
+
50
+ it "returns merged preloads for requested attributes" do
51
+ user_serializer.attribute :first_name, preload: :profile
52
+ user_serializer.attribute :phone, preload: {profile: :phones}
53
+ user_serializer.attribute :email, preload: {profile: :emails}
54
+
55
+ define_map({first_name: {}, phone: {}, email: {}})
56
+
57
+ result = described_class.call(jat_user)
58
+ assert_equal({profile: {phones: {}, emails: {}}}, result)
59
+ end
60
+
61
+ it "returns no preloads and no nested preloads for relationships when specified preloads is nil" do
62
+ user_serializer.relationship :profile, serializer: profile_serializer, preload: nil
63
+ profile_serializer.attribute :email, preload: :email # should not be preloaded
64
+ define_map(profile: {email: {}})
65
+
66
+ result = described_class.call(jat_user)
67
+ assert_equal({}, result)
68
+ end
69
+
70
+ it "returns preloads for nested relationships joined to root when specified preloads is {}" do
71
+ user_serializer.relationship :profile, serializer: profile_serializer, preload: {}
72
+ profile_serializer.attribute :email, preload: :email # should be preloaded to root
73
+ define_map({profile: {email: {}}})
74
+
75
+ result = described_class.call(jat_user)
76
+ assert_equal({email: {}}, result)
77
+ end
78
+
79
+ it "returns preloads for nested relationships joined to root when specified preloads is []" do
80
+ user_serializer.relationship :profile, serializer: profile_serializer, preload: []
81
+ profile_serializer.attribute :email, preload: :email # should be preloaded to root
82
+ define_map({profile: {email: {}}})
83
+
84
+ result = described_class.call(jat_user)
85
+ assert_equal({email: {}}, result)
86
+ end
87
+
88
+ it "returns preloads for relationships" do
89
+ user_serializer.relationship :profile, serializer: profile_serializer
90
+ define_map(profile: {})
91
+
92
+ result = described_class.call(jat_user)
93
+ assert_equal({profile: {}}, result)
94
+ end
95
+
96
+ it "returns nested preloads for relationships" do
97
+ user_serializer.relationship :profile, serializer: profile_serializer
98
+ profile_serializer.attribute :email, preload: %i[confirmed_email unconfirmed_email]
99
+ define_map(profile: {email: {}})
100
+
101
+ result = described_class.call(jat_user)
102
+ assert_equal({profile: {confirmed_email: {}, unconfirmed_email: {}}}, result)
103
+ end
104
+
105
+ it "preloads nested relationships for nested relationship" do
106
+ user_serializer.relationship :profile, serializer: profile_serializer, preload: {company: :profile}
107
+ profile_serializer.attribute :email, preload: %i[confirmed_email unconfirmed_email]
108
+ define_map(profile: {email: {}})
109
+
110
+ result = described_class.call(jat_user)
111
+ assert_equal({company: {profile: {confirmed_email: {}, unconfirmed_email: {}}}}, result)
112
+ end
113
+
114
+ it "preloads nested relationships to main (!) resource" do
115
+ user_serializer.relationship :profile, serializer: profile_serializer, preload: {company!: :profile}
116
+ profile_serializer.attribute :email, preload: %i[confirmed_email unconfirmed_email]
117
+ define_map(profile: {email: {}})
118
+
119
+ result = described_class.call(jat_user)
120
+ assert_equal({company: {profile: {}, confirmed_email: {}, unconfirmed_email: {}}}, result)
121
+ end
122
+
123
+ it "merges preloads the same way regardless of order of preloads" do
124
+ a = Class.new(base)
125
+ a.attribute :a1, preload: {foo: {bar: {bazz1: {}, bazz: {}}}}
126
+ a.attribute :a2, preload: {foo: {bar: {bazz2: {}, bazz: {last: {}}}}}
127
+
128
+ jat_a1 = a.allocate
129
+ jat_a2 = a.allocate
130
+
131
+ jat_a1.expects(:map).returns({a1: {}, a2: {}})
132
+ jat_a2.expects(:map).returns({a2: {}, a1: {}})
133
+
134
+ result1 = described_class.call(jat_a1)
135
+ result2 = described_class.call(jat_a2)
136
+
137
+ assert_equal(result2, result1)
138
+ assert_equal({foo: {bar: {bazz: {last: {}}, bazz1: {}, bazz2: {}}}}, result1)
139
+ end
140
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::SimpleApiPreloads" do
6
+ before do
7
+ @plugin = Jat::Plugins.find_plugin(:simple_api_preloads)
8
+ end
9
+
10
+ it "checks simple_api plugin loaded before" do
11
+ jat_class = Class.new(Jat)
12
+ error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
13
+ assert_match(/simple_api/, error.message)
14
+ end
15
+
16
+ it "loads other plugins in before_load" do
17
+ jat_class = Class.new(Jat)
18
+ jat_class.plugin :simple_api
19
+
20
+ jat_class.expects(:plugin).with(:base_preloads, foo: :bar)
21
+
22
+ @plugin.before_load(jat_class, foo: :bar)
23
+ end
24
+
25
+ describe "InstanceMethods" do
26
+ it "adds #preloads method as a delegator to #{@plugin}::Preloads" do
27
+ jat_class = Class.new(Jat)
28
+ jat_class.plugin :simple_api
29
+ jat_class.plugin @plugin
30
+ jat = jat_class.allocate
31
+
32
+ @plugin::Preloads.expects(:call).with(jat).returns("RES")
33
+
34
+ assert_equal "RES", jat.preloads
35
+ end
36
+ end
37
+
38
+ describe "ClassMethods" do
39
+ it "adds .preloads method as a delegator to #{@plugin}::Preloads" do
40
+ jat_class = Class.new(Jat)
41
+ jat_class.plugin :simple_api
42
+ jat_class.plugin @plugin
43
+
44
+ jat = jat_class.allocate
45
+ jat_class.expects(:new).with("CONTEXT").returns(jat)
46
+ @plugin::Preloads.expects(:call).with(jat).returns("RES")
47
+
48
+ assert_equal "RES", jat_class.preloads("CONTEXT")
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::SimpleApiValidateParams" do
6
+ before do
7
+ @plugin = Jat::Plugins.find_plugin(:simple_api_validate_params)
8
+ end
9
+
10
+ it "checks simple_api plugin loaded before" do
11
+ jat_class = Class.new(Jat)
12
+ error = assert_raises(Jat::Error) { jat_class.plugin @plugin }
13
+ assert_match(/simple_api/, error.message)
14
+ end
15
+
16
+ let(:base_serializer) do
17
+ jat_class = Class.new(Jat)
18
+ jat_class.plugin :simple_api
19
+ jat_class.plugin @plugin
20
+ jat_class
21
+ end
22
+
23
+ let(:serializer) do
24
+ jat_class = Class.new(base_serializer)
25
+ jat_class.attribute :foo_bar
26
+ jat_class.relationship :foo_bazz, serializer: jat_class
27
+ jat_class
28
+ end
29
+
30
+ let(:serializer_lower_camel_case) do
31
+ jat_class = Class.new(base_serializer)
32
+ jat_class.plugin :lower_camel_case
33
+
34
+ jat_class.attribute :foo_bar
35
+ jat_class.relationship :foo_bazz, serializer: jat_class
36
+ jat_class
37
+ end
38
+
39
+ it "returns true when provided fields present" do
40
+ jat = serializer.new(fields: "foo_bar,foo_bazz(foo_bar)")
41
+ assert jat.validate
42
+ end
43
+
44
+ it "validates fields" do
45
+ jat = serializer.new(fields: "foo_bar,extra")
46
+ error = assert_raises(Jat::SimpleApiFieldsError) { jat.validate }
47
+ expected_message = "Field 'extra' not exists"
48
+ assert_equal(expected_message, error.message)
49
+ end
50
+
51
+ it "validates fields when using lower_camel_case plugin" do
52
+ jat = serializer_lower_camel_case.new(fields: "fooBar,extra")
53
+ error = assert_raises(Jat::SimpleApiFieldsError) { jat.validate }
54
+ expected_message = "Field 'extra' not exists"
55
+ assert_equal(expected_message, error.message)
56
+ end
57
+
58
+ it "validates deeply nested fields" do
59
+ c = Class.new(base_serializer)
60
+ c.attribute :c1
61
+ c.attribute :c2
62
+
63
+ b = Class.new(base_serializer)
64
+ b.attribute :b1
65
+ b.attribute :b2
66
+ b.relationship :c, serializer: c
67
+
68
+ a = Class.new(base_serializer)
69
+ a.attribute :a1
70
+ a.attribute :a2
71
+ a.relationship :b, serializer: b
72
+ a.relationship :c, serializer: c
73
+
74
+ jat = a.new(fields: "a1,a2,c(c1,c2),b(b1,b2,c(c1,c2,c3)")
75
+ error = assert_raises(Jat::SimpleApiFieldsError) { jat.validate }
76
+ expected_message = "Field 'c3' ('b.c.c3') not exists"
77
+ assert_equal(expected_message, error.message)
78
+ end
79
+
80
+ it "validates deeply nested fields about not existing relationship" do
81
+ c = Class.new(base_serializer)
82
+ c.attribute :c1
83
+ c.attribute :c2
84
+
85
+ b = Class.new(base_serializer)
86
+ b.attribute :b1
87
+ b.attribute :b2
88
+ b.relationship :c, serializer: c
89
+
90
+ a = Class.new(base_serializer)
91
+ a.attribute :a1
92
+ a.attribute :a2
93
+ a.relationship :b, serializer: b
94
+ a.relationship :c, serializer: c
95
+
96
+ jat = a.new(fields: "a1,a2,c(c1,c2),b(b1,b2,c(c1,c2(c3))")
97
+ error = assert_raises(Jat::SimpleApiFieldsError) { jat.validate }
98
+ expected_message = "Field 'c2' ('b.c.c2') is not a relationship to add 'c3' attribute"
99
+ assert_equal(expected_message, error.message)
100
+ end
101
+ end
@@ -7,7 +7,7 @@ describe "Jat::Plugins::ToStr" do
7
7
  new_class = Class.new(Jat)
8
8
  new_class.plugin(:to_str)
9
9
  new_class.class_exec do
10
- def to_h
10
+ def to_h(object)
11
11
  {object => context}
12
12
  end
13
13
  end
@@ -34,7 +34,7 @@ describe "Jat::Plugins::ToStr" do
34
34
  new_class = Class.new(Jat)
35
35
  new_class.plugin(:to_str, to_str: ->(data) { data.inspect })
36
36
  new_class.class_exec do
37
- def to_h
37
+ def to_h(object)
38
38
  {object => context}
39
39
  end
40
40
  end
@@ -45,7 +45,7 @@ describe "Jat::Plugins::ToStr" do
45
45
 
46
46
  describe "#to_str" do
47
47
  it "returns json string of to_h" do
48
- assert_equal '{"obj":"ctx"}', jat_class.new("obj", "ctx").to_str
48
+ assert_equal '{"obj":"ctx"}', jat_class.new("ctx").to_str("obj")
49
49
  end
50
50
  end
51
51
  end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::Types" do
6
+ let(:struct) { Struct.new(:attr) }
7
+ let(:obj) { struct.new(nil) }
8
+
9
+ let(:jat_class) do
10
+ new_class = Class.new(Jat)
11
+ new_class.plugin(:types)
12
+ new_class
13
+ end
14
+
15
+ def attr_value(original_value)
16
+ obj = struct.new(original_value)
17
+ jat_class.attributes[:attr].value(obj, nil)
18
+ end
19
+
20
+ it "has predefined :int type" do
21
+ jat_class.attribute(:attr, type: :int)
22
+
23
+ assert_equal(0, attr_value("0"))
24
+ assert_equal(8, attr_value("010")) # octal system
25
+ assert_equal(10, attr_value("0xa")) # hexadecimal system
26
+ end
27
+
28
+ it "has predefined :bool type" do
29
+ jat_class.attribute(:attr, type: :bool)
30
+
31
+ assert_equal(false, attr_value(nil))
32
+ assert_equal(false, attr_value(false))
33
+ assert_equal(true, attr_value(1))
34
+ end
35
+
36
+ it "has predefined :float type" do
37
+ jat_class.attribute(:attr, type: :float)
38
+
39
+ assert_same(0.0, attr_value("0"))
40
+ assert_same(0.001, attr_value("1e-3"))
41
+ assert_same(1.234, attr_value("1.234"))
42
+ end
43
+
44
+ it "has predefined :array type" do
45
+ jat_class.attribute(:attr, type: :array)
46
+
47
+ assert_equal([1], attr_value(1))
48
+ assert_equal([1], attr_value([1]))
49
+ end
50
+
51
+ it "has predefined :hash type" do
52
+ jat_class.attribute(:attr, type: :hash)
53
+
54
+ assert_equal({}, attr_value([]))
55
+ assert_equal({}, attr_value({}))
56
+ assert_equal({key: :value}, attr_value(key: :value))
57
+ end
58
+
59
+ it "has predefined :str type" do
60
+ jat_class.attribute(:attr, type: :str)
61
+
62
+ assert_equal("", attr_value(nil))
63
+ assert_equal("123", attr_value(123))
64
+ end
65
+
66
+ it "allows to use callable method as type" do
67
+ jat_class.attribute(:attr, type: ->(obj) { obj ? :yes : :no })
68
+
69
+ assert_equal(:no, attr_value(nil))
70
+ assert_equal(:yes, attr_value(1))
71
+ end
72
+
73
+ it "allows to configure custom type" do
74
+ jat_class.config[:types][:year] = ->(obj) { obj.strftime("%Y") }
75
+ jat_class.attribute(:attr, type: :year)
76
+
77
+ assert_equal("2020", attr_value(Time.new(2020, 10, 10)))
78
+ end
79
+
80
+ it "allows to skip type" do
81
+ jat_class.attribute(:attr)
82
+ assert_equal(333, attr_value(333))
83
+ end
84
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::ValidateParams" do
6
+ it "raises error if no response plugin is loaded" do
7
+ new_class = Class.new(Jat)
8
+ err = assert_raises(Jat::Error) { new_class.plugin :validate_params }
9
+ assert_equal "Please load :json_api or :simple_api plugin first", err.message
10
+ end
11
+
12
+ it "loads simple_api compatible plugin" do
13
+ new_class = Class.new(Jat)
14
+ new_class.plugin :simple_api
15
+ new_class.plugin :validate_params
16
+
17
+ assert new_class.plugin_used?(:simple_api_validate_params)
18
+ end
19
+
20
+ it "loads json_api compatible plugin" do
21
+ new_class = Class.new(Jat)
22
+ new_class.plugin :json_api
23
+ new_class.plugin :validate_params
24
+
25
+ assert new_class.plugin_used?(:json_api_validate_params)
26
+ end
27
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe Jat::Plugins do
6
+ let(:described_module) { Jat::Plugins }
7
+
8
+ describe ".register_plugin" do
9
+ it "adds plugin to the @plugins list" do
10
+ plugin = Module.new
11
+ plugin_name = :new_plugin
12
+ described_module.register_plugin(plugin_name, plugin)
13
+
14
+ assert_equal plugin, described_module.instance_variable_get(:@plugins).fetch(plugin_name)
15
+ end
16
+ end
17
+
18
+ describe ".find_plugin" do
19
+ it "returns module if module provided" do
20
+ plugin = Module.new
21
+ assert_equal plugin, described_module.find_plugin(plugin)
22
+ end
23
+
24
+ it "returns already registered plugin found by name" do
25
+ plugin = Module.new
26
+ plugin_name = :new_plugin
27
+ described_module.register_plugin(plugin_name, plugin)
28
+
29
+ assert_equal plugin, described_module.find_plugin(plugin_name)
30
+ end
31
+
32
+ it "returns global plugins found by name" do
33
+ assert_equal "Jat::Plugins::Activerecord", described_module.find_plugin(:activerecord).name
34
+ assert_equal "Jat::Plugins::Cache", described_module.find_plugin(:cache).name
35
+ assert_equal "Jat::Plugins::JsonApi", described_module.find_plugin(:json_api).name
36
+ assert_equal "Jat::Plugins::LowerCamelCase", described_module.find_plugin(:lower_camel_case).name
37
+ assert_equal "Jat::Plugins::MapsCache", described_module.find_plugin(:maps_cache).name
38
+ assert_equal "Jat::Plugins::Preloads", described_module.find_plugin(:preloads).name
39
+ assert_equal "Jat::Plugins::SimpleApi", described_module.find_plugin(:simple_api).name
40
+ assert_equal "Jat::Plugins::ToStr", described_module.find_plugin(:to_str).name
41
+ assert_equal "Jat::Plugins::Types", described_module.find_plugin(:types).name
42
+ assert_equal "Jat::Plugins::ValidateParams", described_module.find_plugin(:validate_params).name
43
+ end
44
+
45
+ it "returns json_api only plugins found by name (name has `json_api_` prefix)" do
46
+ assert_equal "Jat::Plugins::JsonApiActiverecord", described_module.find_plugin(:json_api_activerecord).name
47
+ assert_equal "Jat::Plugins::JsonApiLowerCamelCase", described_module.find_plugin(:json_api_lower_camel_case).name
48
+ assert_equal "Jat::Plugins::JsonApiMapsCache", described_module.find_plugin(:json_api_maps_cache).name
49
+ assert_equal "Jat::Plugins::JsonApiPreloads", described_module.find_plugin(:json_api_preloads).name
50
+ assert_equal "Jat::Plugins::JsonApiValidateParams", described_module.find_plugin(:json_api_validate_params).name
51
+ end
52
+
53
+ it "returns simple_api only plugins found by name (name has `simple_api_` prefix)" do
54
+ assert_equal "Jat::Plugins::SimpleApiActiverecord", described_module.find_plugin(:simple_api_activerecord).name
55
+ assert_equal "Jat::Plugins::SimpleApiLowerCamelCase", described_module.find_plugin(:simple_api_lower_camel_case).name
56
+ assert_equal "Jat::Plugins::SimpleApiMapsCache", described_module.find_plugin(:simple_api_maps_cache).name
57
+ assert_equal "Jat::Plugins::SimpleApiPreloads", described_module.find_plugin(:simple_api_preloads).name
58
+ assert_equal "Jat::Plugins::SimpleApiValidateParams", described_module.find_plugin(:simple_api_validate_params).name
59
+ end
60
+
61
+ it "returns base plugins found by name (name has `base_` prefix)" do
62
+ assert_equal "Jat::Plugins::BaseActiverecordPreloads", described_module.find_plugin(:base_activerecord_preloads).name
63
+ assert_equal "Jat::Plugins::BaseLowerCamelCase", described_module.find_plugin(:base_lower_camel_case).name
64
+ assert_equal "Jat::Plugins::BasePreloads", described_module.find_plugin(:base_preloads).name
65
+ end
66
+
67
+ it "raises specific error if plugin not found" do
68
+ err = assert_raises(Jat::PluginLoadError) { described_module.find_plugin(:foo) }
69
+ assert_equal "Plugin 'foo' does not exist", err.message
70
+ end
71
+
72
+ it "raises specific error if plugin was found by name but was not registered" do
73
+ plugin_name = "test_foo"
74
+
75
+ # Add plugin folder and file in plugins directory
76
+ plugin_dir = File.join(__dir__, "../../../lib/jat/plugins", plugin_name)
77
+ plugin_path = File.join(plugin_dir, "#{plugin_name}.rb")
78
+ Dir.mkdir(plugin_dir)
79
+ File.new(plugin_path, File::CREAT)
80
+
81
+ err = assert_raises(Jat::PluginLoadError) { described_module.find_plugin(plugin_name) }
82
+ assert_equal "Plugin '#{plugin_name}' did not register itself correctly", err.message
83
+ ensure
84
+ File.unlink(plugin_path)
85
+ Dir.unlink(plugin_dir)
86
+ end
87
+ end
88
+ end