Best Python code snippet using molecule_python
zip_package_test.py
Source:zip_package_test.py  
...232    self.assertEqual(tempfile.gettempdir(), os.path.dirname(actual))233    basename = os.path.basename(actual)234    self.assertTrue(basename.startswith('.zip_pkg-'), actual)235    self.assertTrue(basename.endswith('-cert.pem'), actual)236  def test_extract_resource_temp_dir(self):237    pkg = zip_package.ZipPackage(self.temp_dir)238    pkg.add_directory(os.path.join(ROOT_DIR, 'utils'), 'utils')239    pkg.add_buffer('cert.pem', 'Certificate\n')240    pkg.add_buffer('__main__.py', '\n'.join([241      'import sys',242      'from utils import zip_package',243      'print zip_package.extract_resource(',244      '  sys.modules[__name__], \'cert.pem\', %r)' % self.temp_dir,245    ]))246    zip_file = os.path.join(self.temp_dir, 'out.zip')247    pkg.zip_into_file(zip_file)248    actual = check_output([sys.executable, zip_file]).strip()249    expected = os.path.join(250        self.temp_dir,...zarr_test.py
Source:zarr_test.py  
1# Copyright 2021 Google LLC2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     https://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""Tests for xarray_beam._src.core."""15from absl.testing import absltest16from absl.testing import parameterized17import dask.array as da18import numpy as np19import xarray20import xarray_beam as xbeam21from xarray_beam._src import test_util22# pylint: disable=expression-not-assigned23# pylint: disable=pointless-statement24class DatasetToZarrTest(test_util.TestCase):25  def test_open_zarr(self):26    source_ds = xarray.Dataset(27        {'foo': ('x', da.arange(0, 60, 10, chunks=2))},28    )29    temp_dir = self.create_tempdir().full_path30    source_ds.to_zarr(temp_dir)31    roundtripped_ds, chunks = xbeam.open_zarr(temp_dir)32    xarray.testing.assert_identical(roundtripped_ds, source_ds)33    self.assertEqual(roundtripped_ds.chunks, {})34    self.assertEqual(chunks, {'x': 2})35  def test_open_zarr_inconsistent(self):36    source_ds = xarray.Dataset(37        {38            'foo': ('x', da.arange(0, 60, 10, chunks=2)),39            'bar': ('x', da.arange(0, 60, 10, chunks=3)),40        },41    )42    temp_dir = self.create_tempdir().full_path43    source_ds.to_zarr(temp_dir)44    with self.assertRaisesRegex(45        ValueError,46        "inconsistent chunk sizes on Zarr dataset for dimension 'x': {2, 3}",47    ):48      xbeam.open_zarr(temp_dir)49  def test_make_template(self):50    source = xarray.Dataset(51        {52            'foo': ('x', np.ones(3)),53            'bar': ('x', np.ones(3)),54        },55    )56    actual = xbeam.make_template(source)57    expected = source * 058    xarray.testing.assert_identical(actual, expected)59    self.assertEqual(actual.chunks, {'x': (3,)})60  def test_chunks_to_zarr(self):61    dataset = xarray.Dataset(62        {'foo': ('x', np.arange(0, 60, 10))},63        coords={'x': np.arange(6)},64    )65    chunked = dataset.chunk()66    inputs = [67        (xbeam.Key({'x': 0}), dataset),68    ]69    with self.subTest('no template'):70      temp_dir = self.create_tempdir().full_path71      inputs | xbeam.ChunksToZarr(temp_dir)72      result = xarray.open_zarr(temp_dir, consolidated=True)73      xarray.testing.assert_identical(dataset, result)74    with self.subTest('with template'):75      temp_dir = self.create_tempdir().full_path76      inputs | xbeam.ChunksToZarr(temp_dir, chunked)77      result = xarray.open_zarr(temp_dir, consolidated=True)78      xarray.testing.assert_identical(dataset, result)79    with self.subTest('with zarr_chunks and with template'):80      temp_dir = self.create_tempdir().full_path81      zarr_chunks = {'x': 3}82      inputs | xbeam.ChunksToZarr(temp_dir, chunked, zarr_chunks)83      result = xarray.open_zarr(temp_dir, consolidated=True)84      xarray.testing.assert_identical(dataset, result)85      self.assertEqual(result.chunks, {'x': (3, 3)})86    with self.subTest('with zarr_chunks and no template'):87      temp_dir = self.create_tempdir().full_path88      zarr_chunks = {'x': 3}89      inputs | xbeam.ChunksToZarr(temp_dir, zarr_chunks=zarr_chunks)90      result = xarray.open_zarr(temp_dir, consolidated=True)91      xarray.testing.assert_identical(dataset, result)92      self.assertEqual(result.chunks, {'x': (3, 3)})93    temp_dir = self.create_tempdir().full_path94    with self.assertRaisesRegex(95        ValueError,96        'template does not have any variables chunked with Dask',97    ):98      xbeam.ChunksToZarr(temp_dir, dataset)99    temp_dir = self.create_tempdir().full_path100    template = chunked.assign_coords(x=np.zeros(6))101    with self.assertRaisesRegex(102        ValueError,103        'template and chunk indexes do not match',104    ):105      inputs | xbeam.ChunksToZarr(temp_dir, template)106    inputs2 = [107        (xbeam.Key({'x': 0}), dataset.expand_dims(z=[1, 2])),108    ]109    temp_dir = self.create_tempdir().full_path110    with self.assertRaisesRegex(111        ValueError,112        'unexpected new indexes found in chunk',113    ):114      inputs2 | xbeam.ChunksToZarr(temp_dir, template)115  def test_multiple_vars_chunks_to_zarr(self):116    dataset = xarray.Dataset(117        {118            'foo': ('x', np.arange(0, 60, 10)),119            'bar': ('x', -np.arange(6)),120        },121        coords={'x': np.arange(6)},122    )123    chunked = dataset.chunk()124    inputs = [125        (xbeam.Key({'x': 0}, {'foo'}), dataset[['foo']]),126        (xbeam.Key({'x': 0}, {'bar'}), dataset[['bar']]),127    ]128    with self.subTest('no template'):129      temp_dir = self.create_tempdir().full_path130      inputs | xbeam.ChunksToZarr(temp_dir)131      result = xarray.open_zarr(temp_dir, consolidated=True)132      xarray.testing.assert_identical(dataset, result)133    with self.subTest('with template'):134      temp_dir = self.create_tempdir().full_path135      inputs | xbeam.ChunksToZarr(temp_dir, chunked)136      result = xarray.open_zarr(temp_dir, consolidated=True)137      xarray.testing.assert_identical(dataset, result)138  @parameterized.named_parameters(139      {140          'testcase_name': 'combined_coords',141          'coords': {'bar': (('x', 'y'), -np.arange(6).reshape(3, 2))},142      },143      {144          'testcase_name': 'separate_coords',145          'coords': {'x': np.arange(3), 'y': np.arange(2)},146      },147  )148  def test_2d_chunks_to_zarr(self, coords):149    dataset = xarray.Dataset(150        {'foo': (('x', 'y'), np.arange(0, 60, 10).reshape(3, 2))},151        coords=coords,152    )153    with self.subTest('partial key'):154      inputs = [(xbeam.Key({'x': 0}), dataset)]155      temp_dir = self.create_tempdir().full_path156      inputs | xbeam.ChunksToZarr(temp_dir)157      result = xarray.open_zarr(temp_dir, consolidated=True)158      xarray.testing.assert_identical(dataset, result)159    with self.subTest('split along partial key'):160      inputs = [(xbeam.Key({'x': 0}), dataset)]161      temp_dir = self.create_tempdir().full_path162      inputs | xbeam.SplitChunks({'x': 1}) | xbeam.ChunksToZarr(temp_dir)163      result = xarray.open_zarr(temp_dir, consolidated=True)164      xarray.testing.assert_identical(dataset, result)165    with self.subTest('full key'):166      inputs = [(xbeam.Key({'x': 0, 'y': 0}), dataset)]167      temp_dir = self.create_tempdir().full_path168      inputs | xbeam.ChunksToZarr(temp_dir)169      result = xarray.open_zarr(temp_dir, consolidated=True)170      xarray.testing.assert_identical(dataset, result)171  def test_dataset_to_zarr_simple(self):172    dataset = xarray.Dataset(173        {'foo': ('x', np.arange(0, 60, 10))},174        coords={'x': np.arange(6)},175        attrs={'meta': 'data'},176    )177    chunked = dataset.chunk({'x': 3})178    temp_dir = self.create_tempdir().full_path179    test_util.EagerPipeline() | xbeam.DatasetToZarr(chunked, temp_dir)180    actual = xarray.open_zarr(temp_dir, consolidated=True)181    xarray.testing.assert_identical(actual, dataset)182  def test_dataset_to_zarr_unchunked(self):183    dataset = xarray.Dataset(184        {'foo': ('x', np.arange(0, 60, 10))},185    )186    temp_dir = self.create_tempdir().full_path187    with self.assertRaisesRegex(188        ValueError, 'template does not have any variables chunked with Dask'189    ):190      test_util.EagerPipeline() | xbeam.DatasetToZarr(dataset, temp_dir)191  def test_validate_zarr_chunk_accepts_partial_key(self):192    dataset = xarray.Dataset(193        {'foo': (('x', 'y'), np.zeros((3, 2)))},194        coords={'x': np.arange(3), 'y': np.arange(2)},195    )196    # Should not raise an exception:197    xbeam._src.zarr._validate_zarr_chunk(198        key=xbeam.Key({'x': 0}),199        chunk=dataset,200        template=dataset.chunk(),201        zarr_chunks=None,202    )203  def test_to_zarr_wrong_multiple_error(self):204    inputs = [205        (xbeam.Key({'x': 3}), xarray.Dataset({'foo': ('x', np.arange(3, 6))})),206    ]207    temp_dir = self.create_tempdir().full_path208    with self.assertRaisesRegex(209        ValueError,210        "chunk offset 3 along dimension 'x' is not a multiple of zarr chunks "211        "{'x': 4}",212    ):213      inputs | xbeam.ChunksToZarr(temp_dir, zarr_chunks={'x': 4})214  def test_to_zarr_needs_consolidation_error(self):215    ds = xarray.Dataset({'foo': ('x', np.arange(6))})216    inputs = [217        (xbeam.Key({'x': 0}), ds.head(3)),218        (xbeam.Key({'x': 3}), ds.tail(3)),219    ]220    temp_dir = self.create_tempdir().full_path221    with self.assertRaisesRegex(222        ValueError, 'chunk is smaller than zarr chunks'223    ):224      inputs | xbeam.ChunksToZarr(temp_dir, zarr_chunks={'x': 6})225    with self.assertRaisesRegex(226        ValueError, 'chunk is smaller than zarr chunks'227    ):228      inputs | xbeam.ChunksToZarr(temp_dir, template=ds.chunk())229if __name__ == '__main__':...test_temp_folder.py
Source:test_temp_folder.py  
1# coding=utf-82import pytest3from edlm.convert._temp_folder import Context, TempDir4def test_temp_dir():5    ctx = Context()6    assert ctx.temp_dir is None7    with TempDir(ctx):8        assert ctx.temp_dir.is_dir()9        assert ctx.temp_dir.exists()10        temp_dir = ctx.temp_dir11    assert ctx.temp_dir is None12    assert not temp_dir.exists()13def test_temp_dir_keep():14    ctx = Context()15    ctx.keep_temp_dir = True16    assert ctx.temp_dir is None17    with TempDir(ctx):18        assert ctx.temp_dir.is_dir()...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
