validity 0.0.1 → 0.0.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.
- checksums.yaml +4 -4
- data/lib/validity/active_record.rb +26 -8
- data/lib/validity.rb +9 -0
- metadata +19 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: beb78593ced7359983b6ec67d25feb60671d5284
|
4
|
+
data.tar.gz: 21d5a1f8687b5963666978399acc9bc42b98b54c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd8ee1805308de021d90009e9562872101ffb32b83ffb884670b73dfd35b30b2b60e3ce63cefd0a5ff81e1bb593d6d3cfcab1b621962d0942f13f1652ad4a9b1
|
7
|
+
data.tar.gz: 1e4b4acf97af072ff04d6a9818088dc85a019aec59e67c1f7385725a4e80b078fd94e1958101e7e74068065551d1b4c3527600a1aa7f733441e2ab076361d78b
|
@@ -1,62 +1,80 @@
|
|
1
|
+
# Author:: Matt Fornaciari (mailto:mattforni@gmail.com)
|
2
|
+
# License:: MIT
|
3
|
+
|
1
4
|
module Validity
|
5
|
+
# The Validity::ActiveRecord module contains testing logic specific to ActiveRecord
|
2
6
|
module ActiveRecord
|
7
|
+
# Creates an ActiveRecord::Wrapper to test against
|
3
8
|
def validates(record)
|
4
9
|
Wrapper.new(record)
|
5
10
|
end
|
6
11
|
|
7
12
|
private
|
8
13
|
|
14
|
+
# The Wrapper class is just a thin wrapper around the ActiveRecord model
|
15
|
+
# which defines several methods to validate a model behaves as it should.
|
16
|
+
# Example Usage:
|
17
|
+
# validates(@model).field_presence(:required)
|
9
18
|
class Wrapper
|
10
19
|
include Test::Unit::Assertions
|
11
20
|
|
21
|
+
# Creates a new instance of Validity::ActiveRecord::Wrapper to test against
|
12
22
|
def initialize(record)
|
13
23
|
@record = record
|
14
24
|
end
|
15
25
|
|
16
|
-
|
26
|
+
# Asserts that the record has a belongs_to association and that the
|
27
|
+
# associated record equals the +target+ record, if provided.
|
28
|
+
def belongs_to(field, target = nil)
|
17
29
|
clazz = @record.class
|
18
30
|
assert_respond_to @record, field, "#{clazz} cannot find associated #{field}"
|
19
31
|
|
20
32
|
one = @record.send(field)
|
21
33
|
assert_not_nil one, "#{clazz} does not have associated #{field}"
|
22
|
-
|
34
|
+
if target
|
35
|
+
assert_equal target, one, "#{field.to_s.capitalize} associated with this #{clazz.to_s.downcase} is not the target #{field}"
|
36
|
+
end
|
23
37
|
end
|
24
38
|
|
39
|
+
# Asserts that the record responds to the +delegated+ method and that
|
40
|
+
# the returned object is equal to the object referenced by +delegated_to+
|
25
41
|
def delegates(delegated, delegated_to)
|
26
42
|
clazz = @record.class
|
27
43
|
assert_respond_to @record, delegated, "#{clazz} does not respond to #{delegated}"
|
28
44
|
assert_equal delegated_to.send(delegated), @record.send(delegated), "Delegated objects do not match"
|
29
45
|
end
|
30
46
|
|
47
|
+
# Asserts that the +field+ field must be present for the record to be valid
|
31
48
|
def field_presence(field)
|
32
|
-
# Null out the field to test
|
33
49
|
@record.send("#{field}=", nil)
|
34
50
|
|
35
|
-
# Assert the record is invalid and will not save
|
36
51
|
clazz = @record.class
|
37
52
|
assert !@record.valid?, "#{clazz} is considered valid with nil #{field}"
|
38
53
|
assert !@record.save, "#{clazz} saved without #{field} field"
|
39
54
|
assert @record.errors[field].any?, "#{clazz} does not have an error on #{field}"
|
40
55
|
end
|
41
56
|
|
57
|
+
# Asserts that the +field+ field must be unique for the record to be valid
|
42
58
|
def field_uniqueness(field)
|
43
|
-
# Create a duplicate of the record
|
44
59
|
dup = @record.dup
|
45
60
|
|
46
|
-
# Assert the duplicate is invalid based on the field
|
47
61
|
clazz = dup.class
|
48
62
|
assert !dup.valid?, "#{clazz} is considered valid with duplicate #{field}"
|
49
63
|
assert !dup.save, "#{clazz} saved with a duplicate #{field}"
|
50
64
|
assert dup.errors[field].any?, "#{clazz} does not have an error on #{field}"
|
51
65
|
end
|
52
66
|
|
53
|
-
|
67
|
+
# Asserts that the record has a has_many association and that the
|
68
|
+
# associated records equal the +targets+ records, if provided.
|
69
|
+
def has_many(field, targets = nil)
|
54
70
|
clazz = @record.class
|
55
71
|
assert_respond_to @record, field, "#{clazz} cannot find associated #{field}"
|
56
72
|
|
57
73
|
many = @record.send(field)
|
58
74
|
assert !(many.nil? || many.empty?), "#{clazz} does not have associated #{field}"
|
59
|
-
|
75
|
+
if targets
|
76
|
+
assert_equal targets.size, many.size, "#{clazz} does not have #{targets.size} associated #{field}"
|
77
|
+
end
|
60
78
|
end
|
61
79
|
|
62
80
|
private
|
data/lib/validity.rb
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
# This gem attempts to provide a library to easily test validations on
|
2
|
+
# ActiveRecord models. The ultimate goal is to make spinning up and testing a
|
3
|
+
# data model for a Ruby on Rails stack both quick and painless. The
|
4
|
+
# syntactical structure of this library is based losely upon rspec's
|
5
|
+
# expect/match syntax as it seems to lend itself well to testing readability.
|
6
|
+
#
|
7
|
+
# Author:: Matt Fornaciari (mailto:mattforni@gmail.com)
|
8
|
+
# License:: MIT
|
9
|
+
|
1
10
|
require 'test/unit/assertions'
|
2
11
|
require 'validity/active_record'
|
3
12
|
|
metadata
CHANGED
@@ -1,16 +1,30 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Fornaciari
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
12
|
-
dependencies:
|
13
|
-
|
11
|
+
date: 2014-05-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: test-unit
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2'
|
27
|
+
description: The beginning of a validation library
|
14
28
|
email: mattforni@gmail.com
|
15
29
|
executables: []
|
16
30
|
extensions: []
|
@@ -22,7 +36,7 @@ homepage: http://rubygems.org/gems/validity
|
|
22
36
|
licenses:
|
23
37
|
- MIT
|
24
38
|
metadata: {}
|
25
|
-
post_install_message:
|
39
|
+
post_install_message: Happy testing!
|
26
40
|
rdoc_options: []
|
27
41
|
require_paths:
|
28
42
|
- lib
|