from unittest import TestCase
from lib import web
from mock import Mock

import main
import framework.file_server as file_server

class BaseFileServerTests (TestCase):

    def test_AddShouldReturnNoneIfDbInsertionFails(self):
        fs = file_server.FileServer()
        fs.addDbRecord = Mock(return_value=None)
        fs.saveFile = Mock()
        
        db = main.sessionDB()
        id = fs.add(db, "This is file data", "myapp")
        
        self.assertEqual(fs.addDbRecord.call_count, 1)
        self.assertFalse(fs.saveFile.called)
        self.assertIsNone(id)
    
    def test_AddShouldCallInsertOnDatabase(self):
        """db.insert should be called with fs.add is called.
        
        Assumes that this should be true every time.  If this assumption becomes
        false, the test will have to be changed.
        """
        fs = file_server.FileServer()
        
        db = main.sessionDB()
        db.insert = Mock(return_value=None)
        
        fs.add(db, "This is file data", "myapp")
        
        self.assertEqual(db.insert.call_count, 1)
    
    def test_DefaultFileServerAddRaisesNotImplentedError(self):
        fs = file_server.FileServer()
        
        db = main.sessionDB()
        db.insert = Mock(return_value=1)
        
        self.assertRaises(NotImplementedError, fs.add, db, "This is file data", "myapp")
    
    def test_DbRecordShouldBeRemovedIfFileSaveIsUnsuccessful(self):
        fs = file_server.FileServer()
        fs.addDbRecord = Mock(return_value=7)
        fs.saveFile = Mock(return_value=False)
        
        db = main.sessionDB()
        db.query = Mock()
        
        fs.add(db, "This is file data", "myapp")
        
        self.assertEqual(db.query.call_count, 1)
        self.assertIn("DELETE FROM files", db.query.call_args[0][0])
    
class S3FileServerTests (TestCase):
    """
    Tests that actually interact with the S3 server should go in 
    integrationtests/framework_file_server_tests.py
    
    """
    def test_S3UploaderIsCalledWithCorrectParameters(self):
        file_server.S3Uploader.upload = Mock()
        
        fs = file_server.S3FileServer()
        fs.getLocalPath = Mock(return_value="local/path/to/file7")
        fs.getS3Path = Mock(return_value="path/to/file7/on/s3")
        fs.saveTemporaryLocalFile = Mock(return_value=True)
        
        success = fs.saveFile(7, "This is file data")
        
        self.assertTrue(success, "File save was not successful")
        self.assertEqual(file_server.S3Uploader.upload.call_count, 1)
        self.assertEqual(file_server.S3Uploader.upload.call_args[0], 
            ("local/path/to/file7", "path/to/file7/on/s3"))
        
    def test_LocalPathDeterminedBasedOnId(self):
        fs = file_server.S3FileServer()
        
        path = fs.getLocalPath(7)
        self.assertEqual(path, "data/files/7")
    
    def test_S3PathDeterminedBasedOnId(self):
        fs = file_server.S3FileServer()
        
        path = fs.getS3Path(7)
        self.assertEqual(path, "data/files/7")


@@ -166,924 +166,8 @@
 ):%0A%0A
-    def test_AddShouldReturnNoneIfDbInsertionFails(self):%0A        fs = file_server.FileServer()%0A        fs.addDbRecord = Mock(return_value=None)%0A        fs.saveFile = Mock()%0A        %0A        db = main.sessionDB()%0A        id = fs.add(db, %22This is file data%22, %22myapp%22)%0A        %0A        self.assertEqual(fs.addDbRecord.call_count, 1)%0A        self.assertFalse(fs.saveFile.called)%0A        self.assertIsNone(id)%0A    %0A    def test_AddShouldCallInsertOnDatabase(self):%0A        %22%22%22db.insert should be called with fs.add is called.%0A        %0A        Assumes that this should be true every time.  If this assumption becomes%0A        false, the test will have to be changed.%0A        %22%22%22%0A        fs = file_server.FileServer()%0A        %0A        db = main.sessionDB()%0A        db.insert = Mock(return_value=None)%0A        %0A        fs.add(db, %22This is file data%22, %22myapp%22)%0A        %0A        self.assertEqual(db.insert.call_count, 1)%0A    %0A
     
@@ -411,12 +411,8 @@
 add,
- db,
  %22Th
@@ -447,462 +447,8 @@
    %0A
-    def test_DbRecordShouldBeRemovedIfFileSaveIsUnsuccessful(self):%0A        fs = file_server.FileServer()%0A        fs.addDbRecord = Mock(return_value=7)%0A        fs.saveFile = Mock(return_value=False)%0A        %0A        db = main.sessionDB()%0A        db.query = Mock()%0A        %0A        fs.add(db, %22This is file data%22, %22myapp%22)%0A        %0A        self.assertEqual(db.query.call_count, 1)%0A        self.assertIn(%22DELETE FROM files%22, db.query.call_args%5B0%5D%5B0%5D)%0A    %0A
 clas
@@ -764,32 +764,36 @@
 er.S3FileServer(
+None
 )%0A        fs.get
@@ -1394,32 +1394,36 @@
 er.S3FileServer(
+None
 )%0A        %0A     
@@ -1579,32 +1579,36 @@
 er.S3FileServer(
+None
 )%0A        %0A     

change the file server unit tests to reflect current interface for file servers