def test_nested_model(self): model_dict = { "foo": 1, "bar": [{"foo": 1, "bar": "123", "baz": [1, 2, 3]}, {"foo": 2, "bar": 234, "baz": [5]}], "baz": {"qux": {"foo": 12, "bar": "999", "baz": []}}, } model_json = json_encoder.to_json(model_dict) dict_result = model_loader.load_model(NestedModel, model_dict) json_result = model_loader.load_model_from_json(NestedModel, model_json) self.assertEqual(dict_result, json_result) self.assertEqual(1, dict_result.foo) bar_field = dict_result.bar self.assertEqual(2, len(bar_field)) self.assertEqual(1, bar_field[0].foo) self.assertEqual("123", bar_field[0].bar) self.assertEqual([1, 2, 3], bar_field[0].baz) baz_field = dict_result.baz self.assertEqual(1, len(baz_field)) self.assertEqual(12, baz_field["qux"].foo) self.assertEqual("999", baz_field["qux"].bar) self.assertEqual([], baz_field["qux"].baz)
def _load_status_file(self) -> Tuple[Summary, Analysis, Environment, Network]: path = config.get_data_file(STATUS_FILE_NAME) self.assertTrue(os.path.exists(path)) with open(path, "r", encoding="utf-8") as json_file: status_file = json_file.read() diagnostics_loaded = model_loader.load_model_from_json(Diagnostics, status_file) summary_loaded = diagnostics_loaded.summary analysis_loaded = diagnostics_loaded.analysis network_loaded = analysis_loaded.network environment_loaded = analysis_loaded.environment return summary_loaded, analysis_loaded, environment_loaded, network_loaded
def test_basic_model(self): model_dict = {"foo": 1, "bar": "123", "baz": [1, 2, 3]} model_json = json_encoder.to_json(model_dict) dict_result = model_loader.load_model(BasicModel, model_dict) json_result = model_loader.load_model_from_json(BasicModel, model_json) self.assertEqual(dict_result, json_result) self.assertEqual(1, dict_result.foo) self.assertEqual("123", dict_result.bar) self.assertEqual([1, 2, 3], dict_result.baz)
def test_cloud_api_account_setting(self): new_account_model = BdnAccountModelBase( "fake_id", "fake_name", "fake_cert", tx_paid=BdnQuotaServiceModelConfigBase(), block_paid=BdnQuotaServiceModelConfigBase(), cloud_api=BdnFeedServiceModelConfigBase( expire_date=str(date.today() + timedelta(days=100)), ), new_transaction_streaming=BdnFeedServiceModelConfigBase()) new_account_model_json = json_encoder.to_json(new_account_model) old_account_model = model_loader.load_model_from_json( BdnOldAccountModelBase, new_account_model_json) self.assertTrue(new_account_model.cloud_api.is_service_valid()) self.assertTrue(old_account_model.cloud_api.is_service_valid()) self.assertFalse( old_account_model.new_transaction_streaming.is_service_valid())
def read_config_file(full_path: str) -> GatewayNodeConfigModel: try: file_mod_time = os.stat(full_path).st_mtime except FileNotFoundError: logger.debug("Config file not found {}", full_path) return GatewayNodeConfigModel() if config_file_last_updated.get(full_path) != file_mod_time: try: with open(full_path, "r") as f: raw_input = f.read() config_model = model_loader.load_model_from_json( model_class=GatewayNodeConfigModel, model_params=raw_input) config_file_last_updated[full_path] = file_mod_time last_config_model[full_path] = config_model except (TypeError, JSONDecodeError): logger.warning(log_messages.INVALID_CONFIG_PAYLOAD, full_path) return GatewayNodeConfigModel() except Exception as e: logger.warning(log_messages.READ_CONFIG_FAIL, full_path, e) return GatewayNodeConfigModel() return config_model else: return last_config_model[full_path]
def test_old_account_model_to_new(self): old_account_model = BdnOldAccountModelBase( "fake_id", "fake_name", "fake_cert", tx_paid=BdnServiceModelConfigBase( expire_date=str(date.today() + timedelta(days=100)), msg_quota=BdnServiceModelBase(limit=100)), new_transaction_streaming=BdnServiceModelConfigBase( expire_date=str(date.today() + timedelta(days=100)), permit=BdnServiceModelBase()), cloud_api=BdnServiceModelConfigBase( expire_date=str(date.today() + timedelta(days=100)), permit=BdnServiceModelBase())) old_account_model_json = json_encoder.to_json(old_account_model) new_account_model = model_loader.load_model_from_json( BdnAccountModelBase, old_account_model_json) self.assertEqual( old_account_model.tx_paid.is_service_valid(), new_account_model.tx_paid.is_service_valid(), ) self.assertEqual( old_account_model.tx_paid.msg_quota.limit, new_account_model.tx_paid.msg_quota.limit, ) self.assertEqual( old_account_model.cloud_api.is_service_valid(), new_account_model.cloud_api.is_service_valid(), ) self.assertEqual( old_account_model.new_transaction_streaming.is_service_valid(), new_account_model.new_transaction_streaming.is_service_valid(), ) # new account settings are not being set to default values, the SDN most verify that the payload is valid self.assertIsNone(new_account_model.new_transaction_streaming.feed)