2017-12-14 18:37:31 +00:00
|
|
|
"""
|
|
|
|
author: deadc0de6 (https://github.com/deadc0de6)
|
|
|
|
Copyright (c) 2017, deadc0de6
|
|
|
|
|
|
|
|
Basic unittest for indexing
|
|
|
|
"""
|
|
|
|
|
2019-02-16 12:54:00 +00:00
|
|
|
import os
|
2017-12-14 18:37:31 +00:00
|
|
|
import unittest
|
|
|
|
|
2019-02-16 12:54:00 +00:00
|
|
|
from catcli.catcli import cmd_index
|
2017-12-14 18:37:31 +00:00
|
|
|
from catcli.noder import Noder
|
|
|
|
from catcli.catalog import Catalog
|
2019-02-16 12:54:00 +00:00
|
|
|
from tests.helpers import get_tempdir, create_rnd_file, clean, \
|
|
|
|
get_rnd_string, create_dir
|
2017-12-14 18:37:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestIndexing(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_index(self):
|
|
|
|
# init
|
|
|
|
workingdir = get_tempdir()
|
|
|
|
catalogpath = create_rnd_file(workingdir, 'catalog.json', content='')
|
|
|
|
self.addCleanup(clean, workingdir)
|
|
|
|
|
|
|
|
dirpath = get_tempdir()
|
|
|
|
self.addCleanup(clean, dirpath)
|
|
|
|
|
|
|
|
# create 3 files
|
|
|
|
f1 = create_rnd_file(dirpath, get_rnd_string(5))
|
|
|
|
f2 = create_rnd_file(dirpath, get_rnd_string(5))
|
|
|
|
f3 = create_rnd_file(dirpath, get_rnd_string(5))
|
|
|
|
|
|
|
|
# create 2 directories
|
|
|
|
d1 = create_dir(dirpath, get_rnd_string(3))
|
|
|
|
d2 = create_dir(dirpath, get_rnd_string(3))
|
|
|
|
|
|
|
|
# fill directories with files
|
2019-02-16 12:54:00 +00:00
|
|
|
_ = create_rnd_file(d1, get_rnd_string(4))
|
|
|
|
_ = create_rnd_file(d1, get_rnd_string(4))
|
|
|
|
_ = create_rnd_file(d2, get_rnd_string(6))
|
2017-12-14 18:37:31 +00:00
|
|
|
|
|
|
|
noder = Noder()
|
|
|
|
top = noder.new_top_node()
|
2020-03-27 13:33:06 +00:00
|
|
|
catalog = Catalog(catalogpath, force=True, debug=False)
|
2017-12-14 18:37:31 +00:00
|
|
|
|
|
|
|
# create fake args
|
|
|
|
tmpdirname = 'tmpdir'
|
|
|
|
args = {'<path>': dirpath, '<name>': tmpdirname,
|
2018-09-26 07:00:45 +00:00
|
|
|
'--hash': True, '--meta': ['some meta'],
|
2019-04-13 11:58:43 +00:00
|
|
|
'--no-subsize': False, '--verbose': True}
|
2017-12-14 18:37:31 +00:00
|
|
|
|
|
|
|
# index the directory
|
|
|
|
cmd_index(args, noder, catalog, top)
|
|
|
|
self.assertTrue(os.stat(catalogpath).st_size != 0)
|
|
|
|
|
|
|
|
# explore the top node to find all nodes
|
|
|
|
self.assertTrue(len(top.children) == 1)
|
|
|
|
storage = top.children[0]
|
|
|
|
self.assertTrue(len(storage.children) == 5)
|
|
|
|
|
|
|
|
# ensures files and directories are in
|
|
|
|
names = [x.name for x in storage.children]
|
|
|
|
self.assertTrue(os.path.basename(f1) in names)
|
|
|
|
self.assertTrue(os.path.basename(f2) in names)
|
|
|
|
self.assertTrue(os.path.basename(f3) in names)
|
|
|
|
self.assertTrue(os.path.basename(d1) in names)
|
|
|
|
self.assertTrue(os.path.basename(d2) in names)
|
|
|
|
|
|
|
|
for node in storage.children:
|
|
|
|
if node.name == os.path.basename(d1):
|
|
|
|
self.assertTrue(len(node.children) == 2)
|
|
|
|
elif node.name == os.path.basename(d2):
|
|
|
|
self.assertTrue(len(node.children) == 1)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|