Skip to content

Commit

Permalink
Merge pull request #267 from AdamWill/filecache-implement-contains
Browse files Browse the repository at this point in the history
pkgset.FileCache: implement __contains__ for efficiency
  • Loading branch information
rohanpm authored Nov 20, 2024
2 parents ff50d23 + d66f05e commit be7b4bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions kobo/pkgset.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ def __getitem__(self, name):
def __setitem__(self, name, value):
self.file_cache[os.path.abspath(name)] = value

def __contains__(self, item):
return item in self.file_cache

def __iter__(self):
return iter(self.file_cache)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_pkgset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import print_function
import unittest
import unittest.mock

import os
import warnings
Expand Down Expand Up @@ -218,6 +219,18 @@ def test_iter(self):
self.assertTrue(self.file1 in items)
self.assertTrue(self.file2 in items)

def test_contains(self):
open(self.file1, "w").write("hello\n")
open(self.file2, "w").write("hello\n")

self.cache = FileCache()
wrap1 = self.cache.add(self.file1)
wrap2 = self.cache.add(self.file2)

with unittest.mock.patch("kobo.pkgset.FileCache.__iter__", side_effect=ValueError):
self.assertTrue(self.file1 in self.cache)
self.assertTrue(self.file2 in self.cache)

def test_remove_by_file_path(self):
self.test_add_two_different_files()
self.cache.remove(self.file1)
Expand Down

0 comments on commit be7b4bd

Please sign in to comment.