Skip to content

Commit

Permalink
TST: some actual tests in analogy to unit tests on export_archive
Browse files Browse the repository at this point in the history
  • Loading branch information
jotelha committed Apr 6, 2024
1 parent 798ab96 commit 3375af4
Showing 1 changed file with 80 additions and 1 deletion.
81 changes: 80 additions & 1 deletion datalad_dtool/tests/test_export_dtool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,85 @@
"""Test dtool dataset exporter"""

from datalad.tests.utils_pytest import assert_result_count

import os
import tarfile
import time
from os.path import isabs
from os.path import join as opj

from datalad.api import Dataset

from datalad.tests.utils_pytest import (
assert_equal,
assert_false,
assert_not_equal,
assert_raises,
assert_result_count,
assert_status,
assert_true,
ok_startswith,
with_tree,
)
from datalad.utils import (
chpwd,
md5sum,
)

def test_export_dtool():
_dataset_template = {
'ds': {
'file_up': 'some_content',
'dir': {
'file1_down': 'one',
'file2_down': 'two'}}}


def test_export_dtool_api():
import datalad.api as da
assert hasattr(da, 'export_dtool')


@with_tree(_dataset_template)
def test_failure(path=None):
# non-existing dataset
import datalad.api as da
with assert_raises(ValueError):
da.export_dtool(dataset=Dataset('nowhere'))


@with_tree(_dataset_template)
def test_archive(path=None):
ds = Dataset(opj(path, 'ds')).create(force=True)
ds.save()
committed_date = ds.repo.get_commit_date()
with chpwd(path):
res = list(ds.export_dtool(base_uri=path, name=ds.id))
assert_status('ok', res)
assert_result_count(res, 1)
assert(isabs(res[0]['path']))

# TODO: replace with dtoolcore methods for inspecting content
# def check_contents(outname, prefix):
# with tarfile.open(outname) as tf:
# nfiles = 0
# for ti in tf:
# # any annex links resolved
# assert_false(ti.issym())
# ok_startswith(ti.name, prefix + '/')
# assert_equal(ti.mtime, committed_date)
# if '.datalad' not in ti.name:
# # ignore any files in .datalad for this test to not be
# # susceptible to changes in how much we generate a meta info
# nfiles += 1
# # we have exactly four files (includes .gitattributes for default
# # MD5E backend), and expect no content for any directory
# assert_equal(nfiles, 4)
#
# check_contents(default_outname, 'datalad_%s' % ds.id)
# check_contents(custom_outname, 'myexport')

# now loose some content
ds.drop('file_up', reckless='kill')
assert_raises(IOError, ds.export_dtool, base_uri=path, name='my')
ds.export_dtool(base_uri=path, name='partial', missing_content='ignore')
assert_true(os.path.exists(opj(path, 'partial')))

0 comments on commit 3375af4

Please sign in to comment.