def test_does_not_update_hash_if_modification_and_size_unchanged(self): with FilesMock() as files: files.add_file('./path/to/somefile', 1366207797, 1024) self.hashdb.update_path('.', 'path/to/somefile') expected = HashDb.Entry(1366207797, 1024, '6cf9224c0ced0affde6832a101676ff656a7cd6f') self.assertEqual(self.hashdb['path/to/somefile'], expected)
def test_update_uses_relative_path(self): with FilesMock() as files: files.add_file('path/to/somefile', 1366207797, 42) self.hashdb.update_path('path', 'to/somefile') expected = HashDb.Entry( 1366207797, 42, hashlib.sha1(FilesMock.File.content).hexdigest()) self.assertEqual(self.hashdb['to/somefile'], expected)
def test_updates_hash_if_size_changed(self): with FilesMock() as files: files.add_file('./path/to/somefile', 1366207797, 42) self.hashdb.update_path('.', 'path/to/somefile') expected = HashDb.Entry( 1366207797, 42, hashlib.sha1(FilesMock.File.content).hexdigest()) self.assertEqual(self.hashdb['path/to/somefile'], expected)
def test_updates_hash_if_modification_time_changed(self): with FilesMock() as files: files.add_file('./path/to/somefile', 123, 1024) self.hashdb.update_path('.', 'path/to/somefile') expected = HashDb.Entry( 123, 1024, hashlib.sha1(FilesMock.File.content).hexdigest()) self.assertEqual(self.hashdb['path/to/somefile'], expected)
def test_inserts_hash_for_new_file(self): with FilesMock() as files: files.add_file('./newfile', 123, 42) self.hashdb.update_path('.', 'newfile') expected = HashDb.Entry( 123, 42, hashlib.sha1(FilesMock.File.content).hexdigest()) self.assertEqual(self.hashdb['newfile'], expected)
def test_provides_dictionary_interface(self): entry = self.hashdb['path/to/somefile'] self.assertEqual(entry.modification, 1366207797) self.assertEqual(entry.size, 1024) self.assertEqual(entry.sha1, '6cf9224c0ced0affde6832a101676ff656a7cd6f') with self.assertRaises(KeyError): entry = self.hashdb['newpath'] self.hashdb['newpath'] = HashDb.Entry( 12345, 256, '07d307d64e062a0ba2ed725571aecd89f2214232') self.assertEqual(self.data['newpath'], '12345;256;07d307d64e062a0ba2ed725571aecd89f2214232')
def test_reads_complete_file(self): with FilesMock() as files: with patch('__builtin__.open', mock_open()) as open_patch: chunks = ['con', 'tent', ''] def read_chunk(size): return chunks.pop(0) file_mock = MagicMock() file_mock.__enter__.return_value = file_mock file_mock.read.side_effect = read_chunk open_patch.return_value = file_mock files.add_file('./newfile', 123, 42) self.hashdb.update_path('.', 'newfile') expected = HashDb.Entry(123, 42, hashlib.sha1('content').hexdigest()) self.assertEqual(self.hashdb['newfile'], expected)
def test_entries_differing_in_sha1_are_unequal(self): a = HashDb.Entry(123, 456, '6cf9224c0ced0affde6832a101676ff656a7cd6f') b = HashDb.Entry(123, 456, '07d307d64e062a0ba2ed725571aecd89f2214232') self.assertNotEqual(a, b)
def test_entries_differing_in_size_are_unequal(self): a = HashDb.Entry(123, 456, '6cf9224c0ced0affde6832a101676ff656a7cd6f') b = HashDb.Entry(123, 23, '6cf9224c0ced0affde6832a101676ff656a7cd6f') self.assertNotEqual(a, b)
def test_equal_entries_considered_equal(self): a = HashDb.Entry(123, 456, '6cf9224c0ced0affde6832a101676ff656a7cd6f') b = HashDb.Entry(123, 456, '6cf9224c0ced0affde6832a101676ff656a7cd6f') self.assertEqual(a, b)