# Path: fastavro/repository/base.py
# class SchemaRepositoryError(Exception):
#     pass
# 
# Path: fastavro/repository/flat_dict.py
# class FlatDictRepository(AbstractSchemaRepository):
#     def __init__(self, path):
#         self.path = path
#         self.file_ext = "avsc"
# 
#     def load(self, name):
#         file_path = path.join(self.path, f"{name}.{self.file_ext}")
#         try:
#             with open(file_path) as schema_file:
#                 return json.load(schema_file)
#         except IOError as error:
#             raise SchemaRepositoryError(
#                 f"Failed to load '{name}' schema",
#             ) from error
#         except json.decoder.JSONDecodeError as error:
#             raise SchemaRepositoryError(
#                 f"Failed to parse '{name}' schema",
#             ) from error


from os.path import join, abspath, dirname
from fastavro.repository.base import SchemaRepositoryError
from fastavro.repository.flat_dict import FlatDictRepository
import pytest


def test_load_returns_parsed_value():
    repo_dir = join(abspath(dirname(__file__)), "flat_dict_test")
    repo = FlatDictRepository(repo_dir)

    result = repo.load("Valid")
    expected = {
        "name": "Valid",
        "type": "record",
        "fields": [{"name": "foo", "type": "string"}],
    }
    assert expected == result


def test_load_raises_error_if_file_does_not_exist():
    repo_dir = join(abspath(dirname(__file__)), "flat_dict_test")

# Path: fastavro/repository/base.py
# class SchemaRepositoryError(Exception):
#     pass
# 
# Path: fastavro/repository/flat_dict.py
# class FlatDictRepository(AbstractSchemaRepository):
#     def __init__(self, path):
#         self.path = path
#         self.file_ext = "avsc"
# 
#     def load(self, name):
#         file_path = path.join(self.path, f"{name}.{self.file_ext}")
#         try:
#             with open(file_path) as schema_file:
#                 return json.load(schema_file)
#         except IOError as error:
#             raise SchemaRepositoryError(
#                 f"Failed to load '{name}' schema",
#             ) from error
#         except json.decoder.JSONDecodeError as error:
#             raise SchemaRepositoryError(
#                 f"Failed to parse '{name}' schema",
#             ) from error


# Path: tests/repository/test_flat_dict.py
from os.path import join, abspath, dirname
from fastavro.repository.base import SchemaRepositoryError
from fastavro.repository.flat_dict import FlatDictRepository
import pytest



def test_load_returns_parsed_value():
    repo_dir = join(abspath(dirname(__file__)), "flat_dict_test")
    repo = FlatDictRepository(repo_dir)

    result = repo.load("Valid")
    expected = {
        "name": "Valid",
        "type": "record",
        "fields": [{"name": "foo", "type": "string"}],
    }
    assert expected == result


def test_load_raises_error_if_file_does_not_exist():
    repo_dir = join(abspath(dirname(__file__)), "flat_dict_test")

    repo = FlatDictRepository(repo_dir)