# Path: xpaw/item.py
# class Item(MutableMapping, BaseItem):
#     def __init__(self, **kwargs):
#         self.fields = {}
#         for k in dir(self):
#             v = getattr(self, k)
#             if isinstance(v, Field):
#                 self.fields[k] = v
#         self.values = {}
#         for k, v in kwargs.items():
#             self[k] = v
# 
#     def __getitem__(self, key):
#         if key not in self:
#             return None
#         v = self.values[key]
#         t = self.fields[key].get('type')
#         if t:
#             if isinstance(t, str):
#                 if t == 'str':
#                     v = str(v)
#                 elif t == 'int':
#                     v = config.getint(v)
#                 elif t == 'float':
#                     v = config.getfloat(v)
#                 elif t == 'bool':
#                     v = config.getbool(v)
#                 else:
#                     raise ValueError('Unsupported item filed type: {}'.format(t))
#             else:
#                 v = t(v)
#         return v
# 
#     def __contains__(self, name):
#         return name in self.values
# 
#     def __setitem__(self, key, value):
#         if key in self.fields:
#             self.values[key] = value
#         else:
#             raise KeyError(key)
# 
#     def __delitem__(self, key):
#         del self.values[key]
# 
#     def __len__(self):
#         return len(self.values)
# 
#     def __iter__(self):
#         return iter(self.values)
# 
#     def keys(self):
#         return self.values.keys()
# 
#     def __repr__(self):
#         return repr(dict(self))
# 
#     def copy(self):
#         return self.__class__(**self.values)
# 
# class Field(dict):
#     """
#     Field consist of metadata
#     """


import pytest
from xpaw.item import Item, Field

class FooItem(Item):
    f1 = Field()
    f2 = Field()


def test_item():
    with pytest.raises(KeyError):
        FooItem(f3='v3')
    item = FooItem(f1='v1')
    assert len(item) == 1 and 'f1' in item
    assert item['f1'] == 'v1' and item['f2'] is None
    item['f2'] = 'v2'
    assert item.keys() == {'f1', 'f2'}
    s = {}
    for k in item:
        s[k] = item[k]
    assert s == {'f1': 'v1', 'f2': 'v2'}
    assert len(item) == 2 and 'f2' in item
    assert item['f2'] == 'v2'
    item_copy = item.copy()
    del item['f2']
    assert len(item) == 1 and 'f2' not in item
    assert len(item_copy) == 2 and item_copy['f1'] == 'v1' and item_copy['f2'] == 'v2'
    item['f2'] = 'new_v2'
    assert item['f2'] == 'new_v2'
    repr_str = repr(item)
    assert repr_str == "{'f1': 'v1', 'f2': 'new_v2'}" or repr_str == "{'f2': 'new_v2', 'f1': 'v1'}"



# Path: xpaw/item.py
# class Item(MutableMapping, BaseItem):
#     def __init__(self, **kwargs):
#         self.fields = {}
#         for k in dir(self):
#             v = getattr(self, k)
#             if isinstance(v, Field):
#                 self.fields[k] = v
#         self.values = {}
#         for k, v in kwargs.items():
#             self[k] = v
# 
#     def __getitem__(self, key):
#         if key not in self:
#             return None
#         v = self.values[key]
#         t = self.fields[key].get('type')
#         if t:
#             if isinstance(t, str):
#                 if t == 'str':
#                     v = str(v)
#                 elif t == 'int':
#                     v = config.getint(v)
#                 elif t == 'float':
#                     v = config.getfloat(v)
#                 elif t == 'bool':
#                     v = config.getbool(v)
#                 else:
#                     raise ValueError('Unsupported item filed type: {}'.format(t))
#             else:
#                 v = t(v)
#         return v
# 
#     def __contains__(self, name):
#         return name in self.values
# 
#     def __setitem__(self, key, value):
#         if key in self.fields:
#             self.values[key] = value
#         else:
#             raise KeyError(key)
# 
#     def __delitem__(self, key):
#         del self.values[key]
# 
#     def __len__(self):
#         return len(self.values)
# 
#     def __iter__(self):
#         return iter(self.values)
# 
#     def keys(self):
#         return self.values.keys()
# 
#     def __repr__(self):
#         return repr(dict(self))
# 
#     def copy(self):
#         return self.__class__(**self.values)
# 
# class Field(dict):
#     """
#     Field consist of metadata
#     """


# Path: tests/test_item.py
import pytest
from xpaw.item import Item, Field


class FooItem(Item):
    f1 = Field()
    f2 = Field()


def test_item():
    with pytest.raises(KeyError):
        FooItem(f3='v3')
    item = FooItem(f1='v1')
    assert len(item) == 1 and 'f1' in item
    assert item['f1'] == 'v1' and item['f2'] is None
    item['f2'] = 'v2'
    assert item.keys() == {'f1', 'f2'}
    s = {}
    for k in item:
        s[k] = item[k]
    assert s == {'f1': 'v1', 'f2': 'v2'}
    assert len(item) == 2 and 'f2' in item
    assert item['f2'] == 'v2'
    item_copy = item.copy()
    del item['f2']
    assert len(item) == 1 and 'f2' not in item
    assert len(item_copy) == 2 and item_copy['f1'] == 'v1' and item_copy['f2'] == 'v2'
    item['f2'] = 'new_v2'
    assert item['f2'] == 'new_v2'
    repr_str = repr(item)
    assert repr_str == "{'f1': 'v1', 'f2': 'new_v2'}" or repr_str == "{'f2': 'new_v2', 'f1': 'v1'}"



class FieldTypeItem(Item):