govuk-content-schema-test-helpers 1.0.2 → 1.1.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 +36 -0
 - data/lib/govuk-content-schema-test-helpers/examples.rb +18 -5
 - data/lib/govuk-content-schema-test-helpers/version.rb +1 -1
 - metadata +1 -1
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: a67072ac7856ffad37516d5260a262d4df89f4f7
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 154ca00506f3cdf9516904de41f7e2f7fab8bdf1
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 7f5b0a0f56380d258d247d574c6a69333a6f88611df0682010cf5f044cd1ed86e3b596ac57db77c6977219446fdec2e3e744440413c08854f1f19318d2e5c82c
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 1df29e2bbb33772e624b5ecede8a97c80766e17b41956b3384fc6fa8c1c011a2e7a925a619f4f5791f625d5bbeabf32ec3c8307abf82f38c898cb1df42e56627
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -41,6 +41,42 @@ If you are not using Rails, you'll need to set `project_root` differently. Assum 
     | 
|
| 
       41 
41 
     | 
    
         
             
              # => '{ "some": "json" }'
         
     | 
| 
       42 
42 
     | 
    
         
             
            ```
         
     | 
| 
       43 
43 
     | 
    
         | 
| 
      
 44 
     | 
    
         
            +
            #### Loading all examples for a given format
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 48 
     | 
    
         
            +
              GovukContentSchemaTestHelpers::Examples.new.get_all_for_format('case_study')
         
     | 
| 
      
 49 
     | 
    
         
            +
              # => ['{ "first": "example"}', '{ "second": "example" }']
         
     | 
| 
      
 50 
     | 
    
         
            +
            ```
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
            This would be useful for checking your app can handle all examples and any that come into existence later:
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 55 
     | 
    
         
            +
              def supported_formats
         
     | 
| 
      
 56 
     | 
    
         
            +
                %w{
         
     | 
| 
      
 57 
     | 
    
         
            +
                  case_study
         
     | 
| 
      
 58 
     | 
    
         
            +
                  coming_soon
         
     | 
| 
      
 59 
     | 
    
         
            +
                }
         
     | 
| 
      
 60 
     | 
    
         
            +
              end
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
              def all_examples_for_supported_formats
         
     | 
| 
      
 63 
     | 
    
         
            +
                GovukContentSchemaTestHelpers::Examples.new.get_all_for_formats(supported_formats)
         
     | 
| 
      
 64 
     | 
    
         
            +
              end
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
              # In an ActionDispatch::IntegrationTest, for example:
         
     | 
| 
      
 67 
     | 
    
         
            +
              test 'that we can handle every example of the formats we support'
         
     | 
| 
      
 68 
     | 
    
         
            +
                all_examples_for_supported_formats.each do |example|
         
     | 
| 
      
 69 
     | 
    
         
            +
                  content_item = JSON.parse(example)
         
     | 
| 
      
 70 
     | 
    
         
            +
                  content_store_has_item(content_item['base_path'], content_item)
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
                  get content_item['base_path']
         
     | 
| 
      
 73 
     | 
    
         
            +
                  assert_response 200
         
     | 
| 
      
 74 
     | 
    
         
            +
                  assert_select 'title', content_item['title']
         
     | 
| 
      
 75 
     | 
    
         
            +
                end
         
     | 
| 
      
 76 
     | 
    
         
            +
              end
         
     | 
| 
      
 77 
     | 
    
         
            +
            ```
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
      
 79 
     | 
    
         
            +
             
     | 
| 
       44 
80 
     | 
    
         
             
            #### RSpec matcher
         
     | 
| 
       45 
81 
     | 
    
         | 
| 
       46 
82 
     | 
    
         
             
            To use the built-in RSpec matcher, add this to spec_helper.rb:
         
     | 
| 
         @@ -4,22 +4,35 @@ module GovukContentSchemaTestHelpers 
     | 
|
| 
       4 
4 
     | 
    
         
             
                  Util.check_govuk_content_schemas_path!
         
     | 
| 
       5 
5 
     | 
    
         
             
                end
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
       7 
     | 
    
         
            -
                def get( 
     | 
| 
       8 
     | 
    
         
            -
                  path = example_path( 
     | 
| 
      
 7 
     | 
    
         
            +
                def get(format, example_name)
         
     | 
| 
      
 8 
     | 
    
         
            +
                  path = example_path(format, example_name)
         
     | 
| 
       9 
9 
     | 
    
         
             
                  check_example_file_exists!(path)
         
     | 
| 
       10 
10 
     | 
    
         
             
                  File.read(path)
         
     | 
| 
       11 
11 
     | 
    
         
             
                end
         
     | 
| 
       12 
12 
     | 
    
         | 
| 
      
 13 
     | 
    
         
            +
                def get_all_for_format(schema_name)
         
     | 
| 
      
 14 
     | 
    
         
            +
                  glob_path = example_path(schema_name, '*')
         
     | 
| 
      
 15 
     | 
    
         
            +
                  Dir.glob(glob_path).map do |path|
         
     | 
| 
      
 16 
     | 
    
         
            +
                    File.read(path)
         
     | 
| 
      
 17 
     | 
    
         
            +
                  end
         
     | 
| 
      
 18 
     | 
    
         
            +
                end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                def get_all_for_formats(schema_names)
         
     | 
| 
      
 21 
     | 
    
         
            +
                  schema_names.inject([]) do |memo, schema_name|
         
     | 
| 
      
 22 
     | 
    
         
            +
                    memo + get_all_for_format(schema_name)
         
     | 
| 
      
 23 
     | 
    
         
            +
                  end
         
     | 
| 
      
 24 
     | 
    
         
            +
                end
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
       13 
26 
     | 
    
         
             
                def check_example_file_exists!(path)
         
     | 
| 
       14 
27 
     | 
    
         
             
                  if !File.exists?(path)
         
     | 
| 
       15 
     | 
    
         
            -
                    raise ImproperlyConfiguredError, " 
     | 
| 
      
 28 
     | 
    
         
            +
                    raise ImproperlyConfiguredError, "Example file not found: #{path}."
         
     | 
| 
       16 
29 
     | 
    
         
             
                  end
         
     | 
| 
       17 
30 
     | 
    
         
             
                end
         
     | 
| 
       18 
31 
     | 
    
         | 
| 
       19 
32 
     | 
    
         
             
              private
         
     | 
| 
       20 
     | 
    
         
            -
                def example_path( 
     | 
| 
      
 33 
     | 
    
         
            +
                def example_path(format, example_name)
         
     | 
| 
       21 
34 
     | 
    
         
             
                  schema_type = GovukContentSchemaTestHelpers.configuration.schema_type
         
     | 
| 
       22 
     | 
    
         
            -
                  File.join(Util.govuk_content_schemas_path, "/formats/#{ 
     | 
| 
      
 35 
     | 
    
         
            +
                  File.join(Util.govuk_content_schemas_path, "/formats/#{format}/#{schema_type}/examples/#{example_name}.json").to_s
         
     | 
| 
       23 
36 
     | 
    
         
             
                end
         
     | 
| 
       24 
37 
     | 
    
         
             
              end
         
     | 
| 
       25 
38 
     | 
    
         
             
            end
         
     |