2
1
mirror of https://github.com/deadc0de6/catcli synced 2024-11-11 13:11:00 +00:00
catcli/tests/test_index.py

83 lines
2.5 KiB
Python
Raw Normal View History

2017-12-14 18:37:31 +00:00
"""
author: deadc0de6 (https://github.com/deadc0de6)
Copyright (c) 2017, deadc0de6
Basic unittest for indexing
"""
import unittest
from catcli.catcli import *
from catcli.noder import Noder
from catcli.walker import Walker
from catcli.catalog import Catalog
from tests.helpers import *
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
d1f1 = create_rnd_file(d1, get_rnd_string(4))
d1f2 = create_rnd_file(d1, get_rnd_string(4))
d2f1 = create_rnd_file(d2, get_rnd_string(6))
noder = Noder()
top = noder.new_top_node()
walker = Walker(noder)
catalog = Catalog(catalogpath, force=True, verbose=False)
# create fake args
tmpdirname = 'tmpdir'
args = {'<path>': dirpath, '<name>': tmpdirname,
'--hash': True, '--meta': 'some meta',
2017-12-14 19:02:24 +00:00
'--subsize': True, '--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()