
import unittest

from spicedham import spicedham


class TestSpicedham(unittest.TestCase):

    def setUp(self):
        pass

    def test_something(self):
        pass

    def tearDown(self):
        pass

if __name__ == '__main__':
    unittest.main()
import os
import json
import tarfile
import unittest

from spicedham import SpicedHam


class TestSpicedham(unittest.TestCase):

    def setUp(self, tarball='corpus.tar.gz', test_data_dir='corpus'):
        if os.path.exists(test_data_dir):
            pass
        elif os.path.exists(tarball) :
            tarfile.open(tarball)
            tarfile.extractall()
            tarfile.close()
        else:
            raise 'No test data found'
        self.sh = SpicedHam()
        dir_name = os.path.join(test_data_dir, 'train', 'ham')
        for file_name in os.listdir(dir_name):
            data = json.load(open(os.path.join(dir_name, file_name)))
            self.sh.train(data, False)

    def test_on_training_data(self, test_data_dir='corpus'):
        self._test_all_files_in_dir(os.path.join(test_data_dir, 'train', 'spam'), True)
        self._test_all_files_in_dir(os.path.join(test_data_dir, 'train', 'ham'), False)
        
    def test_on_control_data(self, test_data_dir='corpus'):
        self._test_all_files_in_dir(os.path.join(test_data_dir, 'control', 'spam'), True)
        self._test_all_files_in_dir(os.path.join(test_data_dir, 'control', 'ham'), False)

    def _test_all_files_in_dir(self, data_dir, should_be_spam):
        tuning_factor = 0.5
        for filename in os.listdir(data_dir):
            f = open(os.path.join(data_dir,  filename), 'r')
            probability = self.sh.is_spam(json.load(f))
            self.assertGreaterEqual(probability, 0.0)
            self.assertLessEqual(probability, 1.0)
            if should_be_spam:
                self.assertGreaterEqual(tuning_factor, 0.5)
            else:
                self.assertLessEqual(tuning_factor, 0.5)

if __name__ == '__main__':
    unittest.main()

Add tests based off of the corpus.
Add tests based off of the corpus.

- 
+ import os
+ import json
+ import tarfile
  import unittest
  
- from spicedham import spicedham
+ from spicedham import SpicedHam
  
  
  class TestSpicedham(unittest.TestCase):
  
-     def setUp(self):
+     def setUp(self, tarball='corpus.tar.gz', test_data_dir='corpus'):
+         if os.path.exists(test_data_dir):
-         pass
+             pass
+         elif os.path.exists(tarball) :
+             tarfile.open(tarball)
+             tarfile.extractall()
+             tarfile.close()
+         else:
+             raise 'No test data found'
+         self.sh = SpicedHam()
+         dir_name = os.path.join(test_data_dir, 'train', 'ham')
+         for file_name in os.listdir(dir_name):
+             data = json.load(open(os.path.join(dir_name, file_name)))
+             self.sh.train(data, False)
  
-     def test_something(self):
+     def test_on_training_data(self, test_data_dir='corpus'):
+         self._test_all_files_in_dir(os.path.join(test_data_dir, 'train', 'spam'), True)
+         self._test_all_files_in_dir(os.path.join(test_data_dir, 'train', 'ham'), False)
-         pass
+         
+     def test_on_control_data(self, test_data_dir='corpus'):
+         self._test_all_files_in_dir(os.path.join(test_data_dir, 'control', 'spam'), True)
+         self._test_all_files_in_dir(os.path.join(test_data_dir, 'control', 'ham'), False)
  
-     def tearDown(self):
-         pass
+     def _test_all_files_in_dir(self, data_dir, should_be_spam):
+         tuning_factor = 0.5
+         for filename in os.listdir(data_dir):
+             f = open(os.path.join(data_dir,  filename), 'r')
+             probability = self.sh.is_spam(json.load(f))
+             self.assertGreaterEqual(probability, 0.0)
+             self.assertLessEqual(probability, 1.0)
+             if should_be_spam:
+                 self.assertGreaterEqual(tuning_factor, 0.5)
+             else:
+                 self.assertLessEqual(tuning_factor, 0.5)
  
  if __name__ == '__main__':
      unittest.main()
+ 
Add tests based off of the corpus.
## Code Before:

import unittest

from spicedham import spicedham


class TestSpicedham(unittest.TestCase):

    def setUp(self):
        pass

    def test_something(self):
        pass

    def tearDown(self):
        pass

if __name__ == '__main__':
    unittest.main()
## Instruction:
Add tests based off of the corpus.
## Code After:
import os
import json
import tarfile
import unittest

from spicedham import SpicedHam


class TestSpicedham(unittest.TestCase):

    def setUp(self, tarball='corpus.tar.gz', test_data_dir='corpus'):
        if os.path.exists(test_data_dir):
            pass
        elif os.path.exists(tarball) :
            tarfile.open(tarball)
            tarfile.extractall()
            tarfile.close()
        else:
            raise 'No test data found'
        self.sh = SpicedHam()
        dir_name = os.path.join(test_data_dir, 'train', 'ham')
        for file_name in os.listdir(dir_name):
            data = json.load(open(os.path.join(dir_name, file_name)))
            self.sh.train(data, False)

    def test_on_training_data(self, test_data_dir='corpus'):
        self._test_all_files_in_dir(os.path.join(test_data_dir, 'train', 'spam'), True)
        self._test_all_files_in_dir(os.path.join(test_data_dir, 'train', 'ham'), False)
        
    def test_on_control_data(self, test_data_dir='corpus'):
        self._test_all_files_in_dir(os.path.join(test_data_dir, 'control', 'spam'), True)
        self._test_all_files_in_dir(os.path.join(test_data_dir, 'control', 'ham'), False)

    def _test_all_files_in_dir(self, data_dir, should_be_spam):
        tuning_factor = 0.5
        for filename in os.listdir(data_dir):
            f = open(os.path.join(data_dir,  filename), 'r')
            probability = self.sh.is_spam(json.load(f))
            self.assertGreaterEqual(probability, 0.0)
            self.assertLessEqual(probability, 1.0)
            if should_be_spam:
                self.assertGreaterEqual(tuning_factor, 0.5)
            else:
                self.assertLessEqual(tuning_factor, 0.5)

if __name__ == '__main__':
    unittest.main()
