# Copyright Hybrid Logic Ltd.  See LICENSE file for details.

"""
Tests for flocker/volume/schemas.
"""

from twisted.trial.unittest import SynchronousTestCase
from jsonschema.exceptions import ValidationError

from flocker.restapi._schema import getValidator
from flocker.volume.httpapi import SCHEMAS


def buildSchemaTest(name, schema, failingInstances, passingInstances):
    """
    Create test case verifying that various instances pass and fail
    verification with a given JSON Schema.

    @param name: Name of test case to create
    @type name: L{str}

    @param schema: Schema to test
    @type schema: L{dict}
    @param failingInstances: List of instances which should fail validation
    @param passingInstances: List of instances which should pass validation

    @return The test case.
    @rtype: A L{SynchronousTestCase} subclass.
    """
    body = {
        'schema': schema,
        'validator': getValidator(schema, SCHEMAS),
        'passingInstances': passingInstances,
        'failingInstances': failingInstances,
        }
    for i, inst in enumerate(failingInstances):
        def test(self, inst=inst):
            self.assertRaises(ValidationError,
                              self.validator.validate, inst)
        test.__name__ = 'test_failsValidation_%d' % (i,)
        body[test.__name__] = test

    for i, inst in enumerate(passingInstances):
        def test(self, inst=inst):
            self.validator.validate(inst)
        test.__name__ = 'test_passesValidation_%d' % (i,)
        body[test.__name__] = test

    return type(name, (SynchronousTestCase, object), body)


VersionsTests = buildSchemaTest(
    name="VersionsTests",
    schema={'$ref': '/v1/endpoints.json#/definitions/versions'},
    failingInstances=[
        # Missing version information
        {},
        # Wrong type for Flocker version
        {'flocker': []},
        # Unexpected version.
        {
            'flocker': '0.3.0-10-dirty',
            'OtherService': '0.3.0-10-dirty',
        },
    ],
    passingInstances=[
        {'flocker': '0.3.0-10-dirty'},
    ],
)

@@ -303,31 +303,33 @@
 %0A%0A%0Adef build
-S
+_s
 chema
-T
+_t
 est(name, sc
@@ -333,33 +333,34 @@
  schema, failing
-I
+_i
 nstances, passin
@@ -352,33 +352,34 @@
 stances, passing
-I
+_i
 nstances):%0A    %22
@@ -497,23 +497,29 @@
 a.%0A%0A    
-@
+:
 param 
+bytes 
 name: Na
@@ -547,44 +547,26 @@
 eate
+.
 %0A    
-@type name: L%7Bstr%7D%0A%0A    @
+:
 param 
+dict 
 sche
@@ -587,54 +587,35 @@
 test
+.
 %0A    
-@type schema: L%7Bdict%7D%0A    @
+:
 param 
+list 
 failing
-I
+_i
 nsta
@@ -655,16 +655,24 @@
 uld fail
+%0A       
  validat
@@ -678,28 +678,35 @@
 tion
+.
 %0A    
-@
+:
 param 
+list 
 passing
-I
+_i
 nsta
@@ -746,16 +746,24 @@
 uld pass
+%0A       
  validat
@@ -769,21 +769,24 @@
 tion
+.
 %0A%0A    
-@
+:
 return
+s:
  The
@@ -799,26 +799,14 @@
 case
-.%0A    @rtype: A L%7B
+; a %60%60
 Sync
@@ -958,33 +958,34 @@
 tances': passing
-I
+_i
 nstances,%0A      
@@ -1005,33 +1005,34 @@
 tances': failing
-I
+_i
 nstances,%0A      
@@ -1067,25 +1067,26 @@
 rate(failing
-I
+_i
 nstances):%0A 
@@ -1352,25 +1352,26 @@
 rate(passing
-I
+_i
 nstances):%0A 
@@ -1626,15 +1626,17 @@
 uild
-S
+_s
 chema
-T
+_t
 est(
@@ -1734,25 +1734,26 @@
 %0A    failing
-I
+_i
 nstances=%5B%0A 
@@ -2019,25 +2019,26 @@
 %0A    passing
-I
+_i
 nstances=%5B%0A 

use pep8 naming conventions