<reponame>JokerQyou/pitools
# coding: utf-8
'''
Test pitools.camera module
'''
from __future__ import unicode_literals
import io
import imghdr
import unittest

from flask import Flask
from pitools import camera
from picamera import PiCamera

app = Flask(__name__)
app.register_blueprint(camera.blueprint)


class CameraTestCase(unittest.TestCase):

    def setUp(self):
        self.app = app.test_client()

    def tearDown(self):
        pass

    def test_01_setup_camera(self):
        '''
        setup_camera() shoud return an opened PiCamera instance
        '''
        with camera.setup_camera() as c:
            assert isinstance(c, PiCamera)
            assert not c.closed

    def test_02_post_shot_api(self):
        '''
        Should fail with 405 with POST requests
        '''
        rv = self.app.post('/camera/shot')
        assert 405 == rv.status_code

    def test_03_get_shot_api(self):
        '''
        Should return an image with MIME of image/*
        '''
        rv = self.app.get('/camera/shot')
        assert rv.content_type.startswith('image/')
        print dir(rv)
        with io.BytesIO() as photo:
            photo.write(rv.get_data())
            photo.flush()
            photo.seek(0)
            assert len(photo.read()) > 0
            photo.seek(0)
            assert 'jpeg' == imghdr.what('', photo.read())

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

# coding: utf-8
'''
Test pitools.camera module
'''
from __future__ import unicode_literals
import io
import imghdr
import unittest

from flask import Flask
from pitools import camera
from picamera import PiCamera

app = Flask(__name__)
app.register_blueprint(camera.blueprint)


class CameraTestCase(unittest.TestCase):

    def setUp(self):
        self.app = app.test_client()

    def tearDown(self):
        pass

    def test_01_setup_camera(self):
        '''
        setup_camera() shoud return an opened PiCamera instance
        '''
        with camera.setup_camera() as c:
            assert isinstance(c, PiCamera)
            assert not c.closed

    def test_02_post_shot_api(self):
        '''
        Should fail with 405 with POST requests
        '''
        rv = self.app.post('/camera/shot')
        assert 405 == rv.status_code

    def test_03_get_shot_api(self):
        '''
        Should return an image with MIME of image/*
        '''
        rv = self.app.get('/camera/shot')
        assert rv.content_type.startswith('image/')
        print dir(rv)
        with io.BytesIO() as photo:
            photo.write(rv.get_data())
            photo.flush()
            photo.seek(0)
            assert len(photo.read()) > 0
            photo.seek(0)
            assert 'jpeg' == imghdr.what('', photo.read())

if __name__ == '__main__':
    unittest.main()
# coding: utf-8 Test pitools.camera module setup_camera() shoud return an opened PiCamera instance Should fail with 405 with POST requests Should return an image with MIME of image/*