-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
82 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from copper.chiller import * | ||
from copper.schema import * | ||
from copper.constants import LOGGING_FORMAT | ||
import sys | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
schema.py | ||
==================================== | ||
Validation of the CLI input files. | ||
""" | ||
|
||
import json, jsonschema, logging | ||
from copper.constants import SCHEMA_PATH | ||
from copper.constants import CHILLER_SCHEMA_PATH | ||
from copper.constants import CHILLER_GENE_SCHEMA_PATH | ||
from copper.constants import CHILLER_ACTION_SCHEMA_PATH | ||
|
||
# Load schemas | ||
schema_chiller = json.load(open(CHILLER_SCHEMA_PATH, "r")) | ||
schema_chiller_gene = json.load(open(CHILLER_GENE_SCHEMA_PATH, "r")) | ||
schema_chiller_action = json.load(open(CHILLER_ACTION_SCHEMA_PATH, "r")) | ||
schema = json.load(open(SCHEMA_PATH, "r")) | ||
|
||
# Define schema store for the validator | ||
schema_store = { | ||
"copper.chiller.schema.json": schema_chiller, | ||
"copper.chiller.generate_set_of_curves.schema.json": schema_chiller_gene, | ||
"copper.chiller.action.schema.json": schema_chiller_action, | ||
"copper.schema.json": schema, | ||
} | ||
|
||
|
||
class Schema: | ||
def __init__(self, input): | ||
self.input = input | ||
self.schema = schema | ||
self.schema_store = schema_store | ||
self.resolver = jsonschema.RefResolver.from_schema(schema, store=schema_store) | ||
Validator = jsonschema.validators.validator_for(schema) | ||
self.validator = Validator(self.schema, resolver=self.resolver) | ||
|
||
def validate(self): | ||
try: | ||
self.validator.validate(self.input) | ||
return True | ||
except: | ||
logging.critical("Input file is not valid.") | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,16 @@ | ||
import json, jsonschema, os | ||
import copper as cp | ||
from unittest import TestCase | ||
|
||
|
||
input_file = json.load(open("./tests/data/cli_input_file.json", "r")) | ||
CHILLER_SCHEMA_PATH = os.path.join("./schema/", "copper.chiller.schema.json") | ||
CHILLER_GENE_SCHEMA_PATH = os.path.join( | ||
"./schema/", "copper.chiller.generate_set_of_curves.schema.json" | ||
) | ||
CHILLER_ACTION_SCHEMA_PATH = os.path.join( | ||
"./schema/", "copper.chiller.action.schema.json" | ||
) | ||
SCHEMA_PATH = os.path.join("./schema/", "copper.schema.json") | ||
schema_chiller = json.load(open(CHILLER_SCHEMA_PATH, "r")) | ||
schema_chiller_gene = json.load(open(CHILLER_GENE_SCHEMA_PATH, "r")) | ||
schema_chiller_action = json.load(open(CHILLER_ACTION_SCHEMA_PATH, "r")) | ||
schema = json.load(open(SCHEMA_PATH, "r")) | ||
|
||
schema_store = { | ||
"copper.chiller.schema.json": schema_chiller, | ||
"copper.chiller.generate_set_of_curves.schema.json": schema_chiller_gene, | ||
"copper.chiller.action.schema.json": schema_chiller_action, | ||
"copper.schema.json": schema, | ||
} | ||
|
||
|
||
class TestCurves(TestCase): | ||
def test_chiller_schema(self): | ||
resolver = jsonschema.RefResolver.from_schema( | ||
schema_chiller, store=schema_store | ||
) | ||
Validator = jsonschema.validators.validator_for(schema_chiller) | ||
validator = Validator(schema_chiller, resolver=resolver) | ||
validator.validate(input_file["actions"][0]["equipment"]) | ||
|
||
def test_chiller_generate_set_of_curves(self): | ||
resolver = jsonschema.RefResolver.from_schema( | ||
schema_chiller_gene, store=schema_store | ||
) | ||
Validator = jsonschema.validators.validator_for(schema_chiller_gene) | ||
validator = Validator(schema_chiller_gene, resolver=resolver) | ||
validator.validate(input_file["actions"][0]["function_call"]) | ||
|
||
def test_chiller_action(self): | ||
resolver = jsonschema.RefResolver.from_schema( | ||
schema_chiller_action, store=schema_store | ||
) | ||
Validator = jsonschema.validators.validator_for(schema_chiller_action) | ||
validator = Validator(schema_chiller_action, resolver=resolver) | ||
validator.validate(input_file["actions"][0]) | ||
|
||
def test_copper(self): | ||
resolver = jsonschema.RefResolver.from_schema(schema, store=schema_store) | ||
Validator = jsonschema.validators.validator_for(schema) | ||
validator = Validator(schema, resolver=resolver) | ||
validator.validate(input_file) | ||
def test_good_schema(self): | ||
input_file = json.load(open("./tests/data/cli_input_file.json", "r")) | ||
assert cp.Schema(input=input_file).validate() | ||
|
||
def test_bad_schema(self): | ||
input_file = json.load(open("./tests/data/cli_input_file.json", "r")) | ||
input_file["actions"][0]["function_call"]["vars"] = 42.0 | ||
with self.assertLogs() as captured: | ||
assert cp.Schema(input=input_file).validate() is None | ||
self.assertTrue(captured[0][0].msg == "Input file is not valid.") |