nice_hash 1.12.14 → 1.13.0
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.
- checksums.yaml +4 -4
- data/README.md +16 -0
- data/lib/nice_hash.rb +44 -32
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c67eecf89013b5b500ad264434c9dd8a08c64f75817e753533029d5a96c7584
|
4
|
+
data.tar.gz: 7a2d166ff3256b3f08068995720e9f2cac083be298ddd7ba8d1462b728c8b2d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82647a827b924e6f3d7a470b63a88eeb9c2be871c38014ef6726632e2eb7c6f97a00541e5d4df083faa99cfb20e96bc17f1ba96e7b3ed52340e16290a9d7f91c
|
7
|
+
data.tar.gz: 610ecaa35046b9107b8551cdf6fae2c0547092cb8ffb5a1cd44359320149718d0eb5ff5e651173979484931e0465d1b583a22660b7cece3673a95cb973fa12c4
|
data/README.md
CHANGED
@@ -736,6 +736,22 @@ Another example that will return false since customer key is missing on first va
|
|
736
736
|
|
737
737
|
Also you can use a third parameter, compare_only_if_exist_key (Boolean), by default false. If true, in case an element exist on structure but doesn't exist on replica won't be verified.
|
738
738
|
|
739
|
+
|
740
|
+
|
741
|
+
The last parameter (patterns) allow you to add verification of data values following the patterns supplied on a one level hash.
|
742
|
+
|
743
|
+
Valid patterns:
|
744
|
+
- a regular expression
|
745
|
+
- any string_pattern, more info: string_pattern project: https://github.com/MarioRuiz/string_pattern
|
746
|
+
- Boolean: specifying Boolean will check if the value is TrueClass or FalseClass
|
747
|
+
- ranges: Any kind of numeric ranges, for example:
|
748
|
+
- 10..400
|
749
|
+
- 20..50
|
750
|
+
- 60.0..500.0
|
751
|
+
- 10.. (from 10 to infinite) Only from Ruby 2.6
|
752
|
+
- DateTime: it will verify if the value is following Time stamp string '2019-06-20T12:01:09.971Z' or if the object is a Time, Date or DateTime class
|
753
|
+
- selectors, one of the values. Example: "uno|dos|tres"
|
754
|
+
|
739
755
|
### Other useful methods
|
740
756
|
|
741
757
|
In case you need the time stamp, we added the method `stamp` to the `Time` class
|
data/lib/nice_hash.rb
CHANGED
@@ -411,29 +411,29 @@ class NiceHash
|
|
411
411
|
if v.is_a?(Hash)
|
412
412
|
ret = NiceHash.generate(v, select_hash_key, expected_errors: expected_errors)
|
413
413
|
else
|
414
|
-
ret = NiceHash.generate({doit: v}, select_hash_key, expected_errors: expected_errors)
|
414
|
+
ret = NiceHash.generate({ doit: v }, select_hash_key, expected_errors: expected_errors)
|
415
415
|
ret = ret[:doit] if ret.is_a?(Hash) and ret.key?(:doit)
|
416
416
|
end
|
417
|
-
ret = v if ret.kind_of?(Hash) and ret.size == 0
|
417
|
+
ret = v if ret.kind_of?(Hash) and ret.size == 0
|
418
418
|
value_ret << ret
|
419
419
|
}
|
420
420
|
hashv[key] = value_ret
|
421
421
|
end
|
422
422
|
elsif value.kind_of?(Range)
|
423
423
|
if expected_errors.empty?
|
424
|
-
hashv[key] = rand(value)
|
424
|
+
hashv[key] = rand(value)
|
425
425
|
else
|
426
426
|
hashv[key] = rand(value)
|
427
427
|
expected_errors.each do |er|
|
428
428
|
if er == :min_length
|
429
|
-
hashv[key] = rand((value.first-value.last)..value.first-1)
|
429
|
+
hashv[key] = rand((value.first - value.last)..value.first - 1)
|
430
430
|
elsif er == :max_length
|
431
|
-
hashv[key] = rand((value.last+1)..(value.last*2))
|
431
|
+
hashv[key] = rand((value.last + 1)..(value.last * 2))
|
432
432
|
elsif er == :length
|
433
|
-
if rand.round==1
|
434
|
-
hashv[key] = rand((value.first-value.last)..value.first-1)
|
433
|
+
if rand.round == 1
|
434
|
+
hashv[key] = rand((value.first - value.last)..value.first - 1)
|
435
435
|
else
|
436
|
-
hashv[key] = rand((value.last+1)..(value.last*2))
|
436
|
+
hashv[key] = rand((value.last + 1)..(value.last * 2))
|
437
437
|
end
|
438
438
|
elsif er == :value
|
439
439
|
hashv[key] = :"1-10:N/L/".gen
|
@@ -451,13 +451,13 @@ class NiceHash
|
|
451
451
|
elsif er == :max_length
|
452
452
|
hashv[key] = hashv[key] + "Z"
|
453
453
|
elsif er == :length
|
454
|
-
if rand.round==1
|
454
|
+
if rand.round == 1
|
455
455
|
hashv[key] = hashv[key].chop
|
456
456
|
else
|
457
457
|
hashv[key] = hashv[key] + "Z"
|
458
458
|
end
|
459
459
|
elsif er == :value
|
460
|
-
hashv[key][rand(hashv[key].size-1)] =
|
460
|
+
hashv[key][rand(hashv[key].size - 1)] = "1:L".gen
|
461
461
|
end
|
462
462
|
end
|
463
463
|
end
|
@@ -468,7 +468,7 @@ class NiceHash
|
|
468
468
|
hashv[key] = (rand.round == 0)
|
469
469
|
expected_errors.each do |er|
|
470
470
|
if er == :value
|
471
|
-
hashv[key] =
|
471
|
+
hashv[key] = "1-10:L".gen
|
472
472
|
end
|
473
473
|
end
|
474
474
|
end
|
@@ -591,8 +591,10 @@ class NiceHash
|
|
591
591
|
results[key] = false
|
592
592
|
end
|
593
593
|
elsif value.kind_of?(Class) and value == DateTime
|
594
|
-
if values[key].size == 24
|
595
|
-
d = Date.strptime(values[key],
|
594
|
+
if values[key].is_a?(String) and values[key].size == 24
|
595
|
+
d = Date.strptime(values[key], "%Y-%m-%dT%H:%M:%S.%LZ") rescue results[key] = false
|
596
|
+
elsif values[key].is_a?(Time) or values[key].is_a?(Date) or values[key].is_a?(DateTime)
|
597
|
+
# correct
|
596
598
|
else
|
597
599
|
results[key] = false
|
598
600
|
end
|
@@ -620,14 +622,14 @@ class NiceHash
|
|
620
622
|
unless array_pattern or results.include?(key)
|
621
623
|
if value.size == 1 and values[key].size > 1
|
622
624
|
# for the case value == ['Ford|Newton|Seat'] and values == ['Ford', 'Newton', 'Ford']
|
623
|
-
i= 0
|
625
|
+
i = 0
|
624
626
|
if values[key].class == value.class
|
625
627
|
values[key].each do |v|
|
626
628
|
if value[0].is_a?(Hash)
|
627
629
|
res = NiceHash.validate([value[0], select_hash_key], v, only_patterns: only_patterns)
|
628
630
|
else
|
629
631
|
# for the case {cars: ['Ford|Newton|Seat']}
|
630
|
-
res = NiceHash.validate([{key => value[0]}, select_hash_key], {key => v}, only_patterns: only_patterns)
|
632
|
+
res = NiceHash.validate([{ key => value[0] }, select_hash_key], { key => v }, only_patterns: only_patterns)
|
631
633
|
#res = {key => res[:doit]} if res.is_a?(Hash) and res.key?(:doit)
|
632
634
|
array_pattern = true
|
633
635
|
end
|
@@ -647,7 +649,7 @@ class NiceHash
|
|
647
649
|
res = NiceHash.validate([v, select_hash_key], values[key][i], only_patterns: only_patterns)
|
648
650
|
else
|
649
651
|
# for the case {cars: ['Ford|Newton|Seat']}
|
650
|
-
res = NiceHash.validate([{key => v}, select_hash_key], {key => values[key][i]}, only_patterns: only_patterns)
|
652
|
+
res = NiceHash.validate([{ key => v }, select_hash_key], { key => values[key][i] }, only_patterns: only_patterns)
|
651
653
|
array_pattern = true
|
652
654
|
end
|
653
655
|
if res.size > 0
|
@@ -658,7 +660,7 @@ class NiceHash
|
|
658
660
|
}
|
659
661
|
end
|
660
662
|
end
|
661
|
-
unless array_pattern or only_patterns or results.include?(key) or complex_data
|
663
|
+
unless array_pattern or only_patterns or results.include?(key) or complex_data
|
662
664
|
results[key] = false unless value == values[key]
|
663
665
|
end
|
664
666
|
else
|
@@ -863,6 +865,7 @@ class NiceHash
|
|
863
865
|
# @param structure [Array] [Hash] Contains the structure that should follow the replica. It can be a nested combination of arrays and hashes.
|
864
866
|
# @param replica [Array] [Hash] Contains the element to be verified on following the supplied structure. It can be a nested combination of arrays and hashes.
|
865
867
|
# @param compare_only_if_exist_key [Boolean] (Default false) If true, in case an element exist on structure but doesn't exist on replica won't be verified.
|
868
|
+
# @param patterns [Hash] add verification of data values following the patterns supplied on a one level hash
|
866
869
|
#
|
867
870
|
# @return [Boolean] true in case replica follows the structure supplied
|
868
871
|
#
|
@@ -879,39 +882,49 @@ class NiceHash
|
|
879
882
|
# NiceHash.compare_structure(my_structure, my_replica)
|
880
883
|
# #>true
|
881
884
|
##################################################
|
882
|
-
def NiceHash.compare_structure(structure, replica, compare_only_if_exist_key = false)
|
885
|
+
def NiceHash.compare_structure(structure, replica, compare_only_if_exist_key = false, patterns = {})
|
883
886
|
unless structure.class == replica.class or
|
884
|
-
|
887
|
+
((structure.is_a?(TrueClass) or structure.is_a?(FalseClass)) and (replica.is_a?(TrueClass) or replica.is_a?(FalseClass)))
|
885
888
|
puts "NiceHash.compare_structure: different object type #{structure.class} is not #{replica.class}. expected: #{structure.inspect}. found: #{replica.inspect}."
|
886
889
|
return false
|
887
890
|
end
|
891
|
+
success = true
|
888
892
|
if structure.is_a?(Hash)
|
889
893
|
structure.each do |key, value|
|
894
|
+
if patterns.key?(key) and replica.key?(key)
|
895
|
+
unless (patterns[key].is_a?(Array) and replica[key].is_a?(Array) and replica[key].empty?) or
|
896
|
+
{key => patterns[key]}.validate({key => replica[key]}).empty?
|
897
|
+
puts "NiceHash.compare_structure: key :#{key} not following pattern #{patterns[key]}. value: #{replica[key]}"
|
898
|
+
success = false
|
899
|
+
end
|
900
|
+
end
|
901
|
+
|
890
902
|
if compare_only_if_exist_key and replica.key?(key)
|
891
|
-
unless compare_structure(value, replica[key], compare_only_if_exist_key)
|
903
|
+
unless compare_structure(value, replica[key], compare_only_if_exist_key, patterns)
|
892
904
|
puts "NiceHash.compare_structure: key :#{key} different."
|
893
|
-
|
905
|
+
success = false
|
894
906
|
end
|
895
907
|
elsif compare_only_if_exist_key == false
|
896
908
|
unless replica.key?(key)
|
897
|
-
puts "NiceHash.compare_structure: key :#{key}
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
909
|
+
puts "NiceHash.compare_structure: key :#{key} missing."
|
910
|
+
success = false
|
911
|
+
else
|
912
|
+
unless compare_structure(value, replica[key], compare_only_if_exist_key, patterns)
|
913
|
+
puts "NiceHash.compare_structure: key :#{key} different."
|
914
|
+
success = false
|
915
|
+
end
|
903
916
|
end
|
904
917
|
end
|
905
918
|
end
|
906
919
|
elsif structure.is_a?(Array)
|
907
920
|
# compare all elements of replica with the structure of the first element on structure
|
908
921
|
replica.each do |elem|
|
909
|
-
unless compare_structure(structure[0], elem, compare_only_if_exist_key)
|
910
|
-
|
922
|
+
unless compare_structure(structure[0], elem, compare_only_if_exist_key, patterns)
|
923
|
+
success = false
|
911
924
|
end
|
912
925
|
end
|
913
926
|
end
|
914
|
-
return
|
927
|
+
return success
|
915
928
|
end
|
916
929
|
|
917
930
|
##################################################
|
@@ -957,7 +970,7 @@ class NiceHash
|
|
957
970
|
# #>[:uno, :dos, :tres]
|
958
971
|
##################################################
|
959
972
|
def self.get_all_keys(h)
|
960
|
-
h.each_with_object([]) do |(k,v),keys|
|
973
|
+
h.each_with_object([]) do |(k, v), keys|
|
961
974
|
keys << k
|
962
975
|
keys.concat(get_all_keys(v)) if v.is_a? Hash
|
963
976
|
if v.is_a?(Array)
|
@@ -967,7 +980,6 @@ class NiceHash
|
|
967
980
|
end
|
968
981
|
end
|
969
982
|
end
|
970
|
-
|
971
983
|
|
972
984
|
##################################################
|
973
985
|
# Deletes the supplied key
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nice_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mario Ruiz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06
|
11
|
+
date: 2019-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: string_pattern
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '2.1'
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 2.1.
|
22
|
+
version: 2.1.4
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '2.1'
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: 2.1.
|
32
|
+
version: 2.1.4
|
33
33
|
description: 'NiceHash creates hashes following certain patterns so your testing will
|
34
34
|
be much easier. Parse and filter JSON. You can easily generate all the hashes you
|
35
35
|
want following the criteria you specify. Many other features coming to Hash class
|