validity 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9612ae7e91b7132c10b0f0113b3b1bc46aaeb080
4
+ data.tar.gz: 36b3d82c42fa44f90f9430b807d7199015afc15b
5
+ SHA512:
6
+ metadata.gz: 87a3799c447d7d030eabfb317384e414a84fa0730a4a96865aa49f5fdb03d69b62fc59f74e07be943ff207bdddd4d85e5344aa8a8ba04a18d29731cfdcf56228
7
+ data.tar.gz: 6d84b1bacb1b381d4c9eaa0c6fb205c86deb85cb6faee825d633b4601174b9e589a57961ccf119a2c7a6c65e644a122cec208a303bcf0e6c28b2ca4f82911405
@@ -0,0 +1,68 @@
1
+ module Validity
2
+ module ActiveRecord
3
+ def validates(record)
4
+ Wrapper.new(record)
5
+ end
6
+
7
+ private
8
+
9
+ class Wrapper
10
+ include Test::Unit::Assertions
11
+
12
+ def initialize(record)
13
+ @record = record
14
+ end
15
+
16
+ def belongs_to(field, target)
17
+ clazz = @record.class
18
+ assert_respond_to @record, field, "#{clazz} cannot find associated #{field}"
19
+
20
+ one = @record.send(field)
21
+ assert_not_nil one, "#{clazz} does not have associated #{field}"
22
+ assert_equal target, one, "#{field.to_s.capitalize} associated with this #{clazz.to_s.downcase} is not the target #{field}"
23
+ end
24
+
25
+ def delegates(delegated, delegated_to)
26
+ clazz = @record.class
27
+ assert_respond_to @record, delegated, "#{clazz} does not respond to #{delegated}"
28
+ assert_equal delegated_to.send(delegated), @record.send(delegated), "Delegated objects do not match"
29
+ end
30
+
31
+ def field_presence(field)
32
+ # Null out the field to test
33
+ @record.send("#{field}=", nil)
34
+
35
+ # Assert the record is invalid and will not save
36
+ clazz = @record.class
37
+ assert !@record.valid?, "#{clazz} is considered valid with nil #{field}"
38
+ assert !@record.save, "#{clazz} saved without #{field} field"
39
+ assert @record.errors[field].any?, "#{clazz} does not have an error on #{field}"
40
+ end
41
+
42
+ def field_uniqueness(field)
43
+ # Create a duplicate of the record
44
+ dup = @record.dup
45
+
46
+ # Assert the duplicate is invalid based on the field
47
+ clazz = dup.class
48
+ assert !dup.valid?, "#{clazz} is considered valid with duplicate #{field}"
49
+ assert !dup.save, "#{clazz} saved with a duplicate #{field}"
50
+ assert dup.errors[field].any?, "#{clazz} does not have an error on #{field}"
51
+ end
52
+
53
+ def has_many(field, targets)
54
+ clazz = @record.class
55
+ assert_respond_to @record, field, "#{clazz} cannot find associated #{field}"
56
+
57
+ many = @record.send(field)
58
+ assert !(many.nil? || many.empty?), "#{clazz} does not have associated #{field}"
59
+ assert_equal targets.size, many.size, "#{clazz} does not have #{targets.size} associated #{field}"
60
+ end
61
+
62
+ private
63
+
64
+ attr_reader :record
65
+ end
66
+ end
67
+ end
68
+
data/lib/validity.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'test/unit/assertions'
2
+ require 'validity/active_record'
3
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Fornaciari
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Just the beginning of a validation library
14
+ email: mattforni@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/validity.rb
20
+ - lib/validity/active_record.rb
21
+ homepage: http://rubygems.org/gems/validity
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Validates ActiveRecord models
45
+ test_files: []