Skip to content

Commit

Permalink
Merge pull request #37 from QualiSystems/dev
Browse files Browse the repository at this point in the history
merged - save and restore objects can now serialize using default json
  • Loading branch information
doppleware committed Aug 8, 2016
2 parents edf306f + 66a6c31 commit 9642ba3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cloudshell/shell/core/interfaces/save_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ def __init__(self, resource_name, created_date, restore_rules, saved_artifact):
:param OrchestrationSavedArtifact saved_artifact: The description of the saved artifact itself, the saved artifact can be of different types
"""
self.resource_name = resource_name
self.created_date = created_date
self.created_date = created_date.isoformat()
self.restore_rules = restore_rules
self.saved_artifact = saved_artifact
31 changes: 30 additions & 1 deletion cloudshell/tests/test_session/test_save_and_restore.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
from json import JSONEncoder
from unittest import TestCase
import jsonpickle
from jsonschema import validate
import datetime

import json
from cloudshell.shell.core.interfaces.save_restore import OrchestrationSavedArtifact, \
OrchestrationSavedArtifactInfo, OrchestrationSaveResult, OrchestrationRestoreRules


class SimpleJSONEncoder(JSONEncoder):
def default(self, o):
if isinstance(o, datetime.datetime):
return o.isoformat()
return o.__dict__

class TestSaveAndRestore(TestCase):



def get_schema(self):
return {
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -93,6 +102,26 @@ def test_serializes_to_schema(self):
json_string = jsonpickle.encode(orchestration_save_result, unpicklable=False)
validate(jsonpickle.loads(json_string), schema=self.get_schema())

def test_works_with_standard_json_serializer(self):
created_date = datetime.datetime.now()
identifier = created_date.strftime('%y_%m_%d %H_%M_%S_%f')

orchestration_saved_artifact = OrchestrationSavedArtifact('test_type', identifier)

saved_artifacts_info = OrchestrationSavedArtifactInfo(
resource_name="some_resource",
created_date=created_date,
restore_rules=OrchestrationRestoreRules(requires_same_resource=True),
saved_artifact=orchestration_saved_artifact)

orchestration_save_result = OrchestrationSaveResult(saved_artifacts_info)

result = json.dumps(orchestration_save_result, cls=SimpleJSONEncoder, indent=True)

validate(json.loads(result), schema=self.get_schema())



def test_can_serialize_custom_rules(self):
created_date = datetime.datetime.now()
identifier = created_date.strftime('%y_%m_%d %H_%M_%S_%f')
Expand Down

0 comments on commit 9642ba3

Please sign in to comment.