<commit_before>import numpy as np
import sys

from .common import ut, TestCase
from h5py.highlevel import File, Group, SoftLink, HardLink, ExternalLink
from h5py.highlevel import Dataset, Datatype
from h5py import h5t

class BaseGroup(TestCase):

    def setUp(self):
        self.f = File(self.mktemp(), 'w')

    def tearDown(self):
        if self.f:
            self.f.close()

class TestCreate(TestCase):

    """
        Feature: New groups can be created via .create_group method
    """

    def test_create(self):
        """ Simple .create_group call """
        hfile = File(self.mktemp(), 'w')
        grp = hfile.create_group('foo')
        self.assertIsInstance(grp, Group)
        hfile.close()

    def test_create_intermediate(self):
        """ Intermediate groups can be created automatically """
        hfile = File(self.mktemp(), 'w')
        grp = hfile.create_group('foo/bar/baz')
        self.assertEqual(grp.name, '/foo/bar/baz')
        hfile.close()

    def test_create_exception(self):
        """ Name conflict causes group creation to fail with ValueError """
        hfile = File(self.mktemp(), 'w')
        hfile.create_group('foo')
        with self.assertRaises(ValueError):
            hfile.create_group('foo')
        hfile.close()

    def test_unicode(self):
        """ Unicode names are correctly stored """
        name = u"/Name\u4500"
        hfile = File(self.mktemp(), 'w')
        group = hfile.create_group(name)
        self.assertEqual(group.name, name)
        self.assertEqual(group.id.links.get_info(name.encode('utf8')).cset, h5t.CSET_UTF8)
        hfile.close()

class TestDatasetAssignment(BaseGroup):

    """
        Feature: Datasets can be created by direct assignment of data
    """

    def test_ndarray(self):
        """ Dataset auto-creation by direct assignment """
        data = np.ones((4,4),dtype='f')
        self.f['a'] = data
        self.assertIsInstance(self.f['a'], Dataset)
        self.assertArrayEqual(self.f['a'][...], data)

class TestDtypeAssignment(BaseGroup):

    """
        Feature: Named types can be created by direct assignment of dtypes
    """

    def test_dtype(self):
        """ Named type creation """
        dtype = np.dtype('|S10')
        self.f['a'] = dtype
        self.assertIsInstance(self.f['a'], Datatype)
        self.assertEqual(self.f['a'].dtype, dtype)

class TestRequire(TestCase):

    """
        Feature: Groups can be auto-created, or opened via .require_group
    """

    def test_open_existing(self):
        """ Existing group is opened and returned """
        hfile = File(self.mktemp(),'w')
        grp = hfile.create_group('foo')
        grp2 = hfile.require_group('foo')
        self.assertEqual(grp, grp2)
        hfile.close()

    def test_create(self):
        """ Group is created if it doesn't exist """
        hfile = File(self.mktemp(),'w')
        grp = hfile.require_group('foo')
        self.assertIsInstance(grp, Group)
        self.assertEqual(grp.name, '/foo')
        hfile.close()

    def test_require_exception(self):
        """ Opening conflicting object results in TypeError """
        hfile = File(self.mktemp(),'w')
        hfile.create_dataset('foo', (1,), 'f')
        with self.assertRaises(TypeError):
            hfile.require_group('foo')
        hfile.close()

class TestDelete(TestCase):

    """
        Feature: Objects can be unlinked via "del" operator
    """

    def test_delete(self):
        """ Object deletion via "del" """
        hfile = File(self.mktemp(),'w')
        self.addCleanup(hfile.close)
        
        hfile.create_group('foo')
        self.assertIn('foo', hfile)
        del hfile['foo']
        self.assertNotIn('foo', hfile)
        
    def test_nonexisting(self):
        """ Deleting non-existent object raises KeyError """
        hfile = File(self.mktemp(),'w')
        self.addCleanup(hfile.close)
        
        with self.assertRaises(KeyError):
            del hfile['foo']

    # TODO: tweak exception tables
    @ut.expectedFailure
    def test_readonly_delete_exception(self):
        """ Deleting object in readonly file raises ValueError """
        fname = self.mktemp()
        hfile = File(fname,'w')
        try:
            hfile.create_group('foo')
        finally:
            hfile.close()

        hfile = File(fname, 'r')
        self.addCleanup(hfile.close)
        
        with self.assertRaises(ValueError):
            del hfile['foo']

class TestOpen(TestCase):

    """<commit_msg>Change test for readonly files<commit_after>import numpy as np
import sys

from .common import ut, TestCase
from h5py.highlevel import File, Group, SoftLink, HardLink, ExternalLink
from h5py.highlevel import Dataset, Datatype
from h5py import h5t

class BaseGroup(TestCase):

    def setUp(self):
        self.f = File(self.mktemp(), 'w')

    def tearDown(self):
        if self.f:
            self.f.close()

class TestCreate(TestCase):

    """
        Feature: New groups can be created via .create_group method
    """

    def test_create(self):
        """ Simple .create_group call """
        hfile = File(self.mktemp(), 'w')
        grp = hfile.create_group('foo')
        self.assertIsInstance(grp, Group)
        hfile.close()

    def test_create_intermediate(self):
        """ Intermediate groups can be created automatically """
        hfile = File(self.mktemp(), 'w')
        grp = hfile.create_group('foo/bar/baz')
        self.assertEqual(grp.name, '/foo/bar/baz')
        hfile.close()

    def test_create_exception(self):
        """ Name conflict causes group creation to fail with ValueError """
        hfile = File(self.mktemp(), 'w')
        hfile.create_group('foo')
        with self.assertRaises(ValueError):
            hfile.create_group('foo')
        hfile.close()

    def test_unicode(self):
        """ Unicode names are correctly stored """
        name = u"/Name\u4500"
        hfile = File(self.mktemp(), 'w')
        group = hfile.create_group(name)
        self.assertEqual(group.name, name)
        self.assertEqual(group.id.links.get_info(name.encode('utf8')).cset, h5t.CSET_UTF8)
        hfile.close()

class TestDatasetAssignment(BaseGroup):

    """
        Feature: Datasets can be created by direct assignment of data
    """

    def test_ndarray(self):
        """ Dataset auto-creation by direct assignment """
        data = np.ones((4,4),dtype='f')
        self.f['a'] = data
        self.assertIsInstance(self.f['a'], Dataset)
        self.assertArrayEqual(self.f['a'][...], data)

class TestDtypeAssignment(BaseGroup):

    """
        Feature: Named types can be created by direct assignment of dtypes
    """

    def test_dtype(self):
        """ Named type creation """
        dtype = np.dtype('|S10')
        self.f['a'] = dtype
        self.assertIsInstance(self.f['a'], Datatype)
        self.assertEqual(self.f['a'].dtype, dtype)

class TestRequire(TestCase):

    """
        Feature: Groups can be auto-created, or opened via .require_group
    """

    def test_open_existing(self):
        """ Existing group is opened and returned """
        hfile = File(self.mktemp(),'w')
        grp = hfile.create_group('foo')
        grp2 = hfile.require_group('foo')
        self.assertEqual(grp, grp2)
        hfile.close()

    def test_create(self):
        """ Group is created if it doesn't exist """
        hfile = File(self.mktemp(),'w')
        grp = hfile.require_group('foo')
        self.assertIsInstance(grp, Group)
        self.assertEqual(grp.name, '/foo')
        hfile.close()

    def test_require_exception(self):
        """ Opening conflicting object results in TypeError """
        hfile = File(self.mktemp(),'w')
        hfile.create_dataset('foo', (1,), 'f')
        with self.assertRaises(TypeError):
            hfile.require_group('foo')
        hfile.close()

class TestDelete(TestCase):

    """
        Feature: Objects can be unlinked via "del" operator
    """

    def test_delete(self):
        """ Object deletion via "del" """
        hfile = File(self.mktemp(),'w')
        self.addCleanup(hfile.close)
        
        hfile.create_group('foo')
        self.assertIn('foo', hfile)
        del hfile['foo']
        self.assertNotIn('foo', hfile)
        
    def test_nonexisting(self):
        """ Deleting non-existent object raises KeyError """
        hfile = File(self.mktemp(),'w')
        self.addCleanup(hfile.close)
        
        with self.assertRaises(KeyError):
            del hfile['foo']

    def test_readonly_delete_exception(self):
        """ Deleting object in readonly file raises KeyError """
        # Note: it is impossible to restore the old behavior (ValueError)
        # without breaking the above test (non-existing objects)
        fname = self.mktemp()
        hfile = File(fname,'w')
        try:
            hfile.create_group('foo')
        finally:
            hfile.close()

        hfile = File(fname, 'r')
        self.addCleanup(hfile.close)
        
        with self.assertRaises(KeyError):
            del hfile['foo']

class TestOpen(TestCase):

    """
{'fraction_non_alphanumeric': 0.13075486210306558, 'fraction_numerical': 0.003955609273706186, 'mean_word_length': 2.4192336589030803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5602041', 'n_tokens_mistral': 2803, 'n_tokens_neox': 2740, 'n_words': 628}