def test_tag_id_creation(self): database = Database(path=TEST_LOG_PATH) existing_ids = set() for _ in range(1000): tag_id = database.generate_tag_id() self.assertNotIn(tag_id, existing_ids) existing_ids.add(tag_id)
def test_missing_and_untracked_files(self): database = Database(TEST_LOG_PATH) documents = [{ 'name': 'first.txt', 'type': 'txt', 'path': 'first.txt' }, { 'name': 'second.png', 'type': 'png', 'path': 'images/second.png' }, { 'name': 'second.png', 'type': 'png', 'path': 'second.png' }] for document in documents: database.create_document(**document) storage = Storage(TEST_ROOT_PATH) storage_paths = {'first.txt', 'second.txt', 'third.txt'} for path in storage_paths: absolute_path = TEST_ROOT_PATH + path touch(absolute_path) repository = Repository(database, storage) untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, {'second.txt', 'third.txt'}) missing_files = repository.collect_missing_file_paths() self.assertEqual(missing_files, {'second.png', 'images/second.png'})
def test_empty_database_and_storage(self): database = Database(TEST_LOG_PATH) storage = Storage(TEST_ROOT_PATH) repository = Repository(database, storage) untracked_files = repository.collect_untracked_file_paths() self.assertEqual(len(untracked_files), 0) missing_files = repository.collect_missing_file_paths() self.assertEqual(len(missing_files), 0)
def test_untracked_files(self): database = Database(TEST_LOG_PATH) storage = Storage(TEST_ROOT_PATH) paths = {'first.txt', 'second.txt', 'third.txt'} for path in paths: absolute_path = TEST_ROOT_PATH + path touch(absolute_path) repository = Repository(database, storage) untracked_file_paths = repository.collect_untracked_file_paths() self.assertEqual(untracked_file_paths, paths)
def create_sample_database(): """ Create a sample database for testing. :return: a `Context` object """ database = Database(path=TEST_LOG_PATH) for document_id in range(1, 9): database.create_document(**sample_documents[document_id]) for tag_id in range(1, 7): database.create_tag(name=sample_tags[tag_id]) for relation in sample_relations: database.create_relation(document_id=relation[0], tag_id=relation[1]) return database
def test_missing_files(self): database = Database(TEST_LOG_PATH) storage = Storage(TEST_ROOT_PATH) documents = [{ 'name': 'first.txt', 'type': 'txt', 'path': 'first.txt' }, { 'name': 'second.png', 'type': 'png', 'path': 'images/second.png' }, { 'name': 'second.png', 'type': 'png', 'path': 'second.png' }] paths = {'first.txt', 'second.png', 'images/second.png'} for document in documents: database.create_document(**document) repository = Repository(database, storage) missing_file_paths = repository.collect_missing_file_paths() self.assertEqual(missing_file_paths, paths)
def test_document_untracking(self): database = Database(TEST_LOG_PATH) documents = [{ 'name': 'first.txt', 'type': 'txt', 'path': 'first.txt' }, { 'name': 'second.png', 'type': 'png', 'path': 'images/second.png' }, { 'name': 'second.png', 'type': 'png', 'path': 'second.png' }] for document in documents: database.create_document(**document) storage = Storage(TEST_ROOT_PATH) storage_paths = {'first.txt', 'images/second.png', 'second.png'} os.mkdir(TEST_ROOT_PATH + 'images') for path in storage_paths: absolute_path = TEST_ROOT_PATH + path touch(absolute_path) repository = Repository(database, storage) untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, set()) repository.untrack_document(3) untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, {'second.png'}) repository.untrack_document(1) untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, {'first.txt', 'second.png'}) repository.untrack_document(2) untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, {'first.txt', 'images/second.png', 'second.png'})
def test_track_untrack_and_track_again(self): database = Database(TEST_LOG_PATH) storage = Storage(TEST_ROOT_PATH) storage_paths = {'alpha.py', 'beta.py'} for path in storage_paths: absolute_path = TEST_ROOT_PATH + path touch(absolute_path) repository = Repository(database, storage) for _ in range(1, 10): untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, {'alpha.py', 'beta.py'}) alpha_id = repository.track_file('alpha.py') untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, {'beta.py'}) beta_id = repository.track_file('beta.py') untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, set()) repository.untrack_document(alpha_id) untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, {'alpha.py'}) repository.untrack_document(beta_id)
def test_file_tracking(self): database = Database(TEST_LOG_PATH) storage = Storage(TEST_ROOT_PATH) paths = {'first.txt', 'second.txt', 'third.txt'} for path in paths: absolute_path = TEST_ROOT_PATH + path touch(absolute_path) repository = Repository(database, storage) untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, {'first.txt', 'second.txt', 'third.txt'}) document_id = repository.track_file('second.txt') self.assertEqual(document_id, 1) untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, {'first.txt', 'third.txt'}) document_id = repository.track_file('third.txt') self.assertEqual(document_id, 2) untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, {'first.txt'}) document_id = repository.track_file('first.txt') self.assertEqual(document_id, 3) untracked_files = repository.collect_untracked_file_paths() self.assertEqual(untracked_files, set())
import tkinter from tkinter import ttk from tkinter import StringVar from tkinter import messagebox from grimoire.database import Database from grimoire.repository import Repository from grimoire.scope import Scope from grimoire.storage import Storage DATABASE_PATH = '/tmp/importer/grimoire.log' STORAGE_PATH = '/tmp/importer/storage/' NOTES_PATH = '/tmp/importer/storage/notes/' database = Database(DATABASE_PATH) storage = Storage(STORAGE_PATH) repository = Repository(database, storage) scope = Scope(database) if os.path.isdir(NOTES_PATH) is False: os.mkdir(NOTES_PATH) def open_path(file_path): absolute_path = os.path.join(storage.path, file_path) if not os.path.isfile(absolute_path): raise ValueError('Invalid file path! {}'.format(absolute_path)) extension = os.path.splitext(file_path)[1] if len(extension) > 1: t = extension[1:].lower()
def test_empty_database(self): database = Database(path=TEST_LOG_PATH) self.assertEqual(database.count_documents(), 0) self.assertEqual(database.count_tags(), 0) self.assertEqual(database.count_relations(), 0)
def test_invalid_untrack(self): database = Database(TEST_LOG_PATH) storage = Storage(TEST_ROOT_PATH) repository = Repository(database, storage) with self.assertRaises(ValueError): _ = repository.untrack_document(1234)
def test_invalid_track(self): database = Database(TEST_LOG_PATH) storage = Storage(TEST_ROOT_PATH) repository = Repository(database, storage) with self.assertRaises(ValueError): _ = repository.track_file('invalid/path.error')