Advertisement
objarni

New Monitor class in pytddmon

Mar 7th, 2012
36
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # coding: utf-8
  2. import unittest
  3.  
  4. class Monitor:
  5.     def __init__(self, file_finder, get_file_size, get_file_modtime):
  6.         self.file_finder = file_finder
  7.         self.get_file_size = get_file_size
  8.         self.get_file_modtime = get_file_modtime
  9.        
  10.         self.snapshot = self.get_snapshot()
  11.        
  12.     def get_snapshot(self):
  13.         snapshot = {}
  14.         for file in self.file_finder():
  15.             file_size = self.get_file_size(file)
  16.             file_modtime = self.get_file_modtime(file)
  17.             snapshot[file] = (file_size, file_modtime)
  18.         return snapshot
  19.  
  20.     def look_for_changes(self):
  21.         print "\nused to be: " + str(self.snapshot)
  22.         new_snapshot = self.get_snapshot()
  23.         change_detected = new_snapshot != self.snapshot
  24.         self.snapshot = new_snapshot
  25.         print "now is: " + str(new_snapshot)
  26.         return change_detected
  27.  
  28. class test_change_detection(unittest.TestCase):
  29.  
  30.     def test_modification_time_changed(self):
  31.         def file_finder():
  32.             return ['file']
  33.         def get_file_size(file):
  34.             return 1
  35.         modtime = [1]
  36.         def get_file_modification_time(file):
  37.             return modtime[0]
  38.         monitor = Monitor(file_finder, get_file_size, get_file_modification_time)
  39.         modtime[0] = 2
  40.         change_detected = monitor.look_for_changes()
  41.         assert change_detected
  42.        
  43.     def test_nothing_changed(self):
  44.         def file_finder():
  45.             return ['file']
  46.         def get_file_size(file):
  47.             return 1
  48.         def get_file_modification_time(file):
  49.             return 1
  50.         monitor = Monitor(file_finder, get_file_size, get_file_modification_time)
  51.         change_detected = monitor.look_for_changes()
  52.         assert not change_detected
  53.  
  54.     def test_adding_file(self):
  55.         files = ['file']
  56.         def file_finder():
  57.             return files
  58.         def get_file_size(file):
  59.             return 1
  60.         def get_file_modification_time(file):
  61.             return 1
  62.         monitor = Monitor(file_finder, get_file_size, get_file_modification_time)
  63.         files.append('file2')
  64.         change_detected = monitor.look_for_changes()
  65.         assert change_detected
  66.  
  67.     def test_renaming_file(self):
  68.         files = ['file']
  69.         def file_finder():
  70.             return files
  71.         def get_file_size(file):
  72.             return 1
  73.         def get_file_modification_time(file):
  74.             return 1
  75.         monitor = Monitor(file_finder, get_file_size, get_file_modification_time)
  76.         files[0] = 'renamed'
  77.         change_detected = monitor.look_for_changes()
  78.         assert change_detected
  79.  
  80.     def test_change_is_only_detected_once(self):
  81.         files = ['file']
  82.         def file_finder():
  83.             return files
  84.         def get_file_size(file):
  85.             return 1
  86.         def get_file_modification_time(file):
  87.             return 1
  88.         monitor = Monitor(file_finder, get_file_size, get_file_modification_time)
  89.         files[0] = 'changed'
  90.         change_detected = monitor.look_for_changes()
  91.         change_detected = monitor.look_for_changes()
  92.         assert not change_detected
  93.  
  94.     def test_file_size_changed(self):
  95.         files = ['file']
  96.         filesize = [1]
  97.         def file_finder():
  98.             return files
  99.         def get_file_size(file):
  100.             return filesize[0]
  101.         def get_file_modification_time(file):
  102.             return 1
  103.         monitor = Monitor(file_finder, get_file_size, get_file_modification_time)
  104.         filesize[0] = 5
  105.         change_detected = monitor.look_for_changes()
  106.         assert change_detected
  107.  
  108. if __name__ == '__main__':
  109.     unittest.main()
Advertisement
RAW Paste Data Copied
Advertisement