|
1 | 1 | { |
2 | 2 | "cells": [ |
3 | 3 | { |
4 | | - "attachments": {}, |
5 | 4 | "cell_type": "markdown", |
6 | 5 | "metadata": {}, |
7 | 6 | "source": [ |
|
255 | 254 | " '''\n", |
256 | 255 | " self.storage_driver = storage_driver or self.__class__.DEFAULT_STORAGE_DRIVER\n", |
257 | 256 | " if isinstance(json_schema, (str, dict)):\n", |
258 | | - " self._json_schema = self.__class__.load_json(json_schema)\n", |
259 | | - " elif (default_schema := self.__class__.get_default_json_schema()):\n", |
| 257 | + " self._json_schema = self.__class__.load_json(json_schema, storage_driver=self.storage_driver)\n", |
| 258 | + " elif (default_schema := self.__class__.get_default_json_schema(storage_driver=self.storage_driver)):\n", |
260 | 259 | " self._json_schema = default_schema\n", |
261 | 260 | " else:\n", |
262 | 261 | " raise Exception('did not receive or find a JSON schema')\n", |
|
286 | 285 | " with storage_driver.open(dotenv_path) as ifile:\n", |
287 | 286 | " config = dotenv.dotenv_values(stream=ifile)\n", |
288 | 287 | " return cls.load_validated_config(\n", |
289 | | - " json_schema or cls.get_default_json_schema(), config)" |
| 288 | + " json_schema or cls.get_default_json_schema(storage_driver=storage_driver),\n", |
| 289 | + " config, storage_driver=storage_driver)" |
290 | 290 | ] |
291 | 291 | }, |
292 | 292 | { |
|
507 | 507 | "outputs": [], |
508 | 508 | "source": [ |
509 | 509 | "#| hide\n", |
510 | | - "# FIXME this test clobbers the environment\n", |
511 | | - "# FIXME this is a code smell on the class design\n", |
512 | | - "# here we're testing the ability to override the storage driver (memoryfs here)\n", |
| 510 | + "# test ability to override the storage driver (memoryfs here)\n", |
513 | 511 | "\n", |
514 | 512 | "from fs.memoryfs import MemoryFS\n", |
515 | 513 | "memfs = MemoryFS()\n", |
|
519 | 517 | " ofile.write(json.dumps(example_properties_schema))\n", |
520 | 518 | " os.environ['CONFIG_VALIDATOR_JSON_SCHEMA'] = ofile.name\n", |
521 | 519 | "\n", |
522 | | - "ConfigValidator.DEFAULT_STORAGE_DRIVER = memfs\n", |
523 | | - "validator = ConfigValidator()\n", |
| 520 | + "validator = ConfigValidator(storage_driver=memfs)\n", |
524 | 521 | "validated_config = validator.load_config({\n", |
525 | 522 | " 'string_value_with_enum': 'these',\n", |
526 | 523 | " 'MY_INTEGER_VALUE': '-85',\n", |
|
532 | 529 | " 'MY_INTEGER_VALUE': -85,\n", |
533 | 530 | " 'A_NUMERIC_VALUE': 12300.0,\n", |
534 | 531 | " '_____A_STRING_VALUE____with_default__': 'underscores_and spaces',\n", |
535 | | - "})\n", |
536 | | - "\n", |
| 532 | + "})" |
| 533 | + ] |
| 534 | + }, |
| 535 | + { |
| 536 | + "cell_type": "code", |
| 537 | + "execution_count": null, |
| 538 | + "metadata": {}, |
| 539 | + "outputs": [], |
| 540 | + "source": [ |
| 541 | + "#| hide\n", |
537 | 542 | "# test loading dotenv from an arbitrary file\n", |
| 543 | + "\n", |
538 | 544 | "memfs.makedirs('special-bespoke-location', recreate=True)\n", |
539 | 545 | "with memfs.open('special-bespoke-location/my-own.env', 'w') as ofile:\n", |
540 | 546 | " ofile.write('\\n'.join([\n", |
|
543 | 549 | " 'A_NUMERIC_VALUE=1167.89',\n", |
544 | 550 | " ]))\n", |
545 | 551 | "\n", |
546 | | - "validated_dotenv = validator.load_dotenv(dotenv_path='special-bespoke-location/my-own.env')\n", |
| 552 | + "validated_dotenv = validator.load_dotenv(\n", |
| 553 | + " dotenv_path='special-bespoke-location/my-own.env',\n", |
| 554 | + " storage_driver=memfs,\n", |
| 555 | + ")\n", |
| 556 | + "\n", |
547 | 557 | "test_eq(validated_dotenv, {\n", |
548 | 558 | " 'string_value_with_enum': 'only',\n", |
549 | 559 | " 'MY_INTEGER_VALUE': 9989998,\n", |
550 | 560 | " 'A_NUMERIC_VALUE': 1167.89,\n", |
551 | 561 | " '_____A_STRING_VALUE____with_default__': 'underscores_and spaces',\n", |
| 562 | + "})" |
| 563 | + ] |
| 564 | + }, |
| 565 | + { |
| 566 | + "cell_type": "code", |
| 567 | + "execution_count": null, |
| 568 | + "metadata": {}, |
| 569 | + "outputs": [], |
| 570 | + "source": [ |
| 571 | + "#| hide\n", |
| 572 | + "# test using custom json schema\n", |
| 573 | + "\n", |
| 574 | + "with memfs.open('foo.schema.json', 'w') as ofile:\n", |
| 575 | + " ofile.write(json.dumps({\n", |
| 576 | + " 'type': 'object',\n", |
| 577 | + " 'properties': {\n", |
| 578 | + " 'A_NUMERIC_VALUE': { 'type': 'number' },\n", |
| 579 | + " }\n", |
| 580 | + " }))\n", |
| 581 | + "validated_dotenv = ConfigValidator.load_dotenv(\n", |
| 582 | + " json_schema='foo.schema.json',\n", |
| 583 | + " dotenv_path='special-bespoke-location/my-own.env',\n", |
| 584 | + " storage_driver=memfs,\n", |
| 585 | + ")\n", |
| 586 | + "test_eq(validated_dotenv, {\n", |
| 587 | + " 'A_NUMERIC_VALUE': 1167.89,\n", |
552 | 588 | "})\n", |
553 | 589 | "\n", |
554 | 590 | "test_fail(validator.load_dotenv, kwargs={'dotenv_path': 'non-existent-location-own.env'})" |
|
0 commit comments