govuk-content-schema-test-helpers 1.0.0 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e5b0d8aa1f1b503bcaae77e1ff8cab0be160d7e
4
- data.tar.gz: 38e3efdeb49fc8d9c281ad083031de80a965603b
3
+ metadata.gz: 906bc8e45e2ba472d55fff389747c51b6fcd0681
4
+ data.tar.gz: a025e7bcb2d6a365dfd44326e0dfca7146f4d464
5
5
  SHA512:
6
- metadata.gz: b72867225ec2fb2ed2287abc1023639e22f372048a29699f7f073f9ece7c719850a8faee006a09d0f581438da0e27c34a526d0a5aa65fd3ffe7ca2831cc008e6
7
- data.tar.gz: f859e915528ba45b48c22fd2dabf2d78260ba09a18a498b102bacf89cefe13d31cefae5bcf42335c608dd90bf25775518280125ef76d27d2e1a59b443e907457
6
+ metadata.gz: 3b6b22ea8e2bfe16f49d993fa61469fc850e10f4fe1544003dcde64a35b455d8033d81766f9ff67342bd14ac49801a816411dc5d7ccbfd1ff6b958dfef3415e1
7
+ data.tar.gz: 8ea79a0196fa515f97abd91ffebe086df58d1823481c3ece47818da1317583295503c986ddc51e90cc4be326265a30198d171a53e025223d46faa1f83d5c58e0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Crown Copyright (Government Digital Service)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # govuk-content-schema-test-helpers
2
+
3
+ This app provides test helpers for working with [alphagov/govuk-content-schemas](https://github.com/alphagov/govuk-content-schemas). This consists of two things:
4
+
5
+ * code for validating a JSON document against a JSON schema in govuk-content-schemas
6
+ * code for fetching example JSON documents from govuk-content-schemas
7
+
8
+
9
+ ## Technical documentation
10
+
11
+ ### Dependencies
12
+
13
+ - [alphagov/govuk-content-schemas](https://github.com/alphagov/govuk-content-schemas) - contains the examples and schemas returned by this gem
14
+
15
+ ### Usage
16
+
17
+ #### Configuration and setup
18
+
19
+ Firstly, you will need a copy of [govuk-content-schemas](http://github.com/alphagov/govuk-content-schemas) on your file system. By default these should be in a sibling directory to your project. Alternatively, you can specify their location with the `GOVUK_CONTENT_SCHEMAS_PATH` environment variable. You should probably duplicate this paragraph (excluding this sentence) in your own `README`.
20
+
21
+ You will need to configure which type of schemas your app uses. A good place to do this is in `test_helper.rb` or `spec_helper.rb`:
22
+
23
+ ```ruby
24
+ require 'govuk-content-schema-test-helpers'
25
+
26
+ GovukContentSchemaTestHelpers.configure do |config|
27
+ config.schema_type = 'frontend' # or 'publisher'
28
+ config.project_root = Rails.root
29
+ end
30
+ ```
31
+
32
+ If you are not using Rails, you'll need to set `project_root` differently. Assuming the file you are adding this to is one directory down from your project root:
33
+ ```ruby
34
+ config.project_root = File.absolute_path(File.join(File.basename(__FILE__), '..'))
35
+ ```
36
+
37
+ #### Loading an example document
38
+
39
+ ```ruby
40
+ GovukContentSchemaTestHelpers::Examples.new.get('finder', 'contacts')
41
+ # => '{ "some": "json" }'
42
+ ```
43
+
44
+ #### RSpec matcher
45
+
46
+ To use the built-in RSpec matcher, add this to spec_helper.rb:
47
+
48
+ ```ruby
49
+ require 'govuk-content-schema-test-helpers/rspec_matchers'
50
+ include GovukContentSchemaTestHelpers::RSpecMatchers
51
+ ```
52
+
53
+ Then:
54
+ ```
55
+ expect(presented).to be_valid_against_schema('finder')
56
+ ```
57
+
58
+ or as an argument matcher:
59
+
60
+ ```ruby
61
+ expect(publishing_api).to receive(:put_content_item)
62
+ .with("/first-finder", be_valid_against_schema('finder'))
63
+ ```
64
+
65
+ #### Test::Unit/Minitest
66
+
67
+ Setup:
68
+
69
+ ```ruby
70
+ require 'govuk-content-schema-test-helpers/test_unit'
71
+ include GovukContentSchemaTestHelpers::TestUnit
72
+ ```
73
+
74
+ Then in a test:
75
+ ```ruby
76
+ assert_valid_against_schema(presented_hash, 'case_study')
77
+ ```
78
+
79
+ #### Validating against the schema manually
80
+
81
+ ```ruby
82
+ validator = GovukContentSchemaTestHelpers::Validator.new('finder', '{ "some": "json" }')
83
+ validator.valid?
84
+ # => false
85
+ validator.errors
86
+ # => ["The property '#/' did not contain a required property of 'base_path'", ...]
87
+ ```
88
+
89
+ ### Running the test suite
90
+
91
+ The tests in this project rely upon [govuk-content-schemas](http://github.com/alphagov/govuk-content-schemas) on your file system. By default these should be in a sibling directory to your project. Alternatively, you can specify their location with the `GOVUK_CONTENT_SCHEMAS_PATH` environment variable.
92
+
93
+ Assuming you already have govuk-content-schemas cloned:
94
+
95
+ ```
96
+ bundle exec rake
97
+ ```
98
+
99
+
100
+ ## Licence
101
+
102
+ [MIT License](LICENCE)
103
+
104
+ ## Versioning policy
105
+
106
+ This is versioned according to [Semantic Versioning 2.0](http://semver.org/).
@@ -0,0 +1,27 @@
1
+ module GovukContentSchemaTestHelpers
2
+ class ConfigurationError < StandardError; end
3
+
4
+ class Configuration
5
+ attr_writer :schema_type, :project_root
6
+
7
+ def schema_type
8
+ @schema_type || raise(ConfigurationError, 'GovukContentSchemaTestHelpers.configuration.schema_type must be set (and not nil)')
9
+ end
10
+
11
+ def project_root
12
+ @project_root || raise(ConfigurationError, 'GovukContentSchemaTestHelpers.configuration.project_root must be set (and not nil)')
13
+ end
14
+ end
15
+
16
+ def self.configuration
17
+ @configuration ||= Configuration.new
18
+ end
19
+
20
+ def self.configuration=(config)
21
+ @configuration = config
22
+ end
23
+
24
+ def self.configure
25
+ yield(configuration)
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ module GovukContentSchemaTestHelpers
2
+ class Examples
3
+ def initialize
4
+ Util.check_govuk_content_schemas_path!
5
+ end
6
+
7
+ def get(schema_name, example_name)
8
+ path = example_path(schema_name, example_name)
9
+ check_example_file_exists!(path)
10
+ File.read(path)
11
+ end
12
+
13
+ def check_example_file_exists!(path)
14
+ if !File.exists?(path)
15
+ raise ImproperlyConfiguredError, "Schema file not found: #{path}."
16
+ end
17
+ end
18
+
19
+ private
20
+ def example_path(schema_name, example_name)
21
+ schema_type = GovukContentSchemaTestHelpers.configuration.schema_type
22
+ File.join(Util.govuk_content_schemas_path, "/formats/#{schema_name}/#{schema_type}/examples/#{example_name}.json").to_s
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module GovukContentSchemaTestHelpers
2
+ class ImproperlyConfiguredError < RuntimeError; end
3
+ end
@@ -0,0 +1,22 @@
1
+ module GovukContentSchemaTestHelpers
2
+ module RSpecMatchers
3
+ RSpec::Matchers.define :be_valid_against_schema do |schema_name|
4
+ match do |actual|
5
+ validator = Validator.new(schema_name, actual)
6
+ validator.valid?
7
+ end
8
+
9
+ # Required for a helpful message with RSpec 3
10
+ description do |actual|
11
+ validator = Validator.new(schema_name, actual)
12
+ "to be valid against '#{schema_name}' schema. Errors: #{validator.errors}"
13
+ end
14
+
15
+ # Required for a helpful message with RSpec 2
16
+ failure_message_for_should do |actual|
17
+ validator = Validator.new(schema_name, actual)
18
+ "to be valid against '#{schema_name}' schema. Errors: #{validator.errors}"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ module GovukContentSchemaTestHelpers
2
+ module TestUnit
3
+ def assert_valid_against_schema(content_item_hash, format)
4
+ validator = GovukContentSchemaTestHelpers::Validator.new(format, content_item_hash.to_json)
5
+ assert validator.valid?, "JSON not valid against #{format} schema: #{validator.errors.to_s}"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ module GovukContentSchemaTestHelpers
2
+ module Util
3
+ def self.govuk_content_schemas_path
4
+ relative_path = ENV['GOVUK_CONTENT_SCHEMAS_PATH'] || '../govuk-content-schemas'
5
+ project_root = GovukContentSchemaTestHelpers.configuration.project_root
6
+ File.absolute_path(relative_path, project_root)
7
+ end
8
+
9
+ def self.check_govuk_content_schemas_path!
10
+ if !File.exists?(Util.govuk_content_schemas_path)
11
+ message = "Dependency govuk-content-schemas cannot be found at: #{govuk_content_schemas_path}."
12
+ message += " Clone it to that directory, or set GOVUK_CONTENT_SCHEMAS_PATH (see README.md for details)."
13
+ raise ImproperlyConfiguredError, message
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ require 'json-schema'
2
+
3
+ module GovukContentSchemaTestHelpers
4
+ class Validator
5
+ def self.schema_path(schema_name)
6
+ schema_type = GovukContentSchemaTestHelpers.configuration.schema_type
7
+ File.join(Util.govuk_content_schemas_path, "/formats/#{schema_name}/#{schema_type}/schema.json").to_s
8
+ end
9
+
10
+ # schema_name should be a string, such as 'finder'
11
+ # document should be a JSON string of the document to validate
12
+ def initialize(schema_name, document)
13
+ Util.check_govuk_content_schemas_path!
14
+ @schema_path = GovukContentSchemaTestHelpers::Validator.schema_path(schema_name)
15
+ if !File.exists?(@schema_path)
16
+ raise ImproperlyConfiguredError, "Schema file not found: #{@schema_path}."
17
+ end
18
+ @document = document
19
+ end
20
+
21
+ def valid?
22
+ errors.empty?
23
+ end
24
+
25
+ def errors
26
+ @errors ||= JSON::Validator.fully_validate(@schema_path, @document)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module GovukContentSchemaTestHelpers
2
+ VERSION = '1.0.1'
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk-content-schema-test-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Cobbett
@@ -75,7 +75,17 @@ executables: []
75
75
  extensions: []
76
76
  extra_rdoc_files: []
77
77
  files:
78
+ - LICENSE
79
+ - README.md
78
80
  - lib/govuk-content-schema-test-helpers.rb
81
+ - lib/govuk-content-schema-test-helpers/configuration.rb
82
+ - lib/govuk-content-schema-test-helpers/examples.rb
83
+ - lib/govuk-content-schema-test-helpers/exceptions.rb
84
+ - lib/govuk-content-schema-test-helpers/rspec_matchers.rb
85
+ - lib/govuk-content-schema-test-helpers/test_unit.rb
86
+ - lib/govuk-content-schema-test-helpers/util.rb
87
+ - lib/govuk-content-schema-test-helpers/validator.rb
88
+ - lib/govuk-content-schema-test-helpers/version.rb
79
89
  homepage: https://github.com/alphagov/govuk-content-schema-test-helpers
80
90
  licenses:
81
91
  - MIT