import os
import urllib.parse

import yaml

import numpy as np


def run_schema_example_test(organization, standard, name, version, check_func=None):

    import asdf
    from asdf.tests import helpers
    from asdf.types import format_tag
    from asdf.resolver import default_resolver

    tag = format_tag(organization, standard, version, name)
    schema_path = urllib.parse.urlparse(default_resolver(tag)).path

    with open(schema_path, 'rb') as ff:
        schema = yaml.load(ff)

    examples = []
    for node in asdf.treeutil.iter_tree(schema):
        if (isinstance(node, dict) and
            'examples' in node and
            isinstance(node['examples'], list)):
            for desc, example in node['examples']:
                examples.append(example)

    for example in examples:
        buff = helpers.yaml_to_asdf('example: ' + example.strip())
        ff = asdf.AsdfFile(uri=schema_path)
        # Add some dummy blocks so that the ndarray examples work
        for i in range(3):
            b = asdf.block.Block(np.zeros((1024*1024*8), dtype=np.uint8))
            b._used = True
            ff.blocks.add(b)
        ff._open_impl(ff, buff, mode='r')
        if check_func:
            check_func(ff)

import os
import urllib.parse
import urllib.request

import yaml

import numpy as np


def run_schema_example_test(organization, standard, name, version, check_func=None):

    import asdf
    from asdf.tests import helpers
    from asdf.types import format_tag
    from asdf.resolver import default_tag_to_url_mapping
    from asdf.schema import load_schema

    tag = format_tag(organization, standard, version, name)
    uri = asdf.resolver.default_tag_to_url_mapping(tag)
    r = asdf.AsdfFile().resolver

    examples = []
    schema = load_schema(uri, resolver=r)
    for node in asdf.treeutil.iter_tree(schema):
        if (isinstance(node, dict) and
            'examples' in node and
            isinstance(node['examples'], list)):
            for desc, example in node['examples']:
                examples.append(example)

    for example in examples:
        buff = helpers.yaml_to_asdf('example: ' + example.strip())
        ff = asdf.AsdfFile(uri=uri)
        # Add some dummy blocks so that the ndarray examples work
        for i in range(3):
            b = asdf.block.Block(np.zeros((1024*1024*8), dtype=np.uint8))
            b._used = True
            ff.blocks.add(b)
        ff._open_impl(ff, buff, mode='r')
        if check_func:
            check_func(ff)

Fix ASDF tag test helper to load schemas correctly
Fix ASDF tag test helper to load schemas correctly

  import os
  import urllib.parse
+ import urllib.request
  
  import yaml
  
  import numpy as np
  
  
  def run_schema_example_test(organization, standard, name, version, check_func=None):
  
      import asdf
      from asdf.tests import helpers
      from asdf.types import format_tag
-     from asdf.resolver import default_resolver
+     from asdf.resolver import default_tag_to_url_mapping
+     from asdf.schema import load_schema
  
      tag = format_tag(organization, standard, version, name)
+     uri = asdf.resolver.default_tag_to_url_mapping(tag)
+     r = asdf.AsdfFile().resolver
-     schema_path = urllib.parse.urlparse(default_resolver(tag)).path
- 
-     with open(schema_path, 'rb') as ff:
-         schema = yaml.load(ff)
  
      examples = []
+     schema = load_schema(uri, resolver=r)
      for node in asdf.treeutil.iter_tree(schema):
          if (isinstance(node, dict) and
              'examples' in node and
              isinstance(node['examples'], list)):
              for desc, example in node['examples']:
                  examples.append(example)
  
      for example in examples:
          buff = helpers.yaml_to_asdf('example: ' + example.strip())
-         ff = asdf.AsdfFile(uri=schema_path)
+         ff = asdf.AsdfFile(uri=uri)
          # Add some dummy blocks so that the ndarray examples work
          for i in range(3):
              b = asdf.block.Block(np.zeros((1024*1024*8), dtype=np.uint8))
              b._used = True
              ff.blocks.add(b)
          ff._open_impl(ff, buff, mode='r')
          if check_func:
              check_func(ff)
  
Fix ASDF tag test helper to load schemas correctly
## Code Before:
import os
import urllib.parse

import yaml

import numpy as np


def run_schema_example_test(organization, standard, name, version, check_func=None):

    import asdf
    from asdf.tests import helpers
    from asdf.types import format_tag
    from asdf.resolver import default_resolver

    tag = format_tag(organization, standard, version, name)
    schema_path = urllib.parse.urlparse(default_resolver(tag)).path

    with open(schema_path, 'rb') as ff:
        schema = yaml.load(ff)

    examples = []
    for node in asdf.treeutil.iter_tree(schema):
        if (isinstance(node, dict) and
            'examples' in node and
            isinstance(node['examples'], list)):
            for desc, example in node['examples']:
                examples.append(example)

    for example in examples:
        buff = helpers.yaml_to_asdf('example: ' + example.strip())
        ff = asdf.AsdfFile(uri=schema_path)
        # Add some dummy blocks so that the ndarray examples work
        for i in range(3):
            b = asdf.block.Block(np.zeros((1024*1024*8), dtype=np.uint8))
            b._used = True
            ff.blocks.add(b)
        ff._open_impl(ff, buff, mode='r')
        if check_func:
            check_func(ff)

## Instruction:
Fix ASDF tag test helper to load schemas correctly
## Code After:
import os
import urllib.parse
import urllib.request

import yaml

import numpy as np


def run_schema_example_test(organization, standard, name, version, check_func=None):

    import asdf
    from asdf.tests import helpers
    from asdf.types import format_tag
    from asdf.resolver import default_tag_to_url_mapping
    from asdf.schema import load_schema

    tag = format_tag(organization, standard, version, name)
    uri = asdf.resolver.default_tag_to_url_mapping(tag)
    r = asdf.AsdfFile().resolver

    examples = []
    schema = load_schema(uri, resolver=r)
    for node in asdf.treeutil.iter_tree(schema):
        if (isinstance(node, dict) and
            'examples' in node and
            isinstance(node['examples'], list)):
            for desc, example in node['examples']:
                examples.append(example)

    for example in examples:
        buff = helpers.yaml_to_asdf('example: ' + example.strip())
        ff = asdf.AsdfFile(uri=uri)
        # Add some dummy blocks so that the ndarray examples work
        for i in range(3):
            b = asdf.block.Block(np.zeros((1024*1024*8), dtype=np.uint8))
            b._used = True
            ff.blocks.add(b)
        ff._open_impl(ff, buff, mode='r')
        if check_func:
            check_func(ff)
