示例#1
0
文件: test.py 项目: liushaochan/cn486
class TestSQLAlchemyFeatures(unittest.TestCase):
    def setUp(self):
        self.app = create_app()
        self.w_without_scoping = TestApp(self.app)
        self.w = TestApp(self.app, db=db, use_session_scopes=True)

        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()

    def tearDown(self):
        db.drop_all()
        self.app_context.pop()

    def test_1(self):
        user = User(name='Anton')
        db.session.add(user)
        db.session.commit()

        r = self.w.get('/user/%i/' % user.id)
        self.assertEqual(r.body, 'Hello, Anton!')

        # Note: we did not commit the change to `user`!
        user.name = 'Petr'

        r = self.w_without_scoping.get('/user/%i/' % user.id)
        self.assertEqual(r.body, 'Hello, Petr!')

        r = self.w.get('/user/%i/' % user.id)
        self.assertEqual(r.body, 'Hello, Anton!')

    def test_2(self):
        user = User(name='Anton')
        db.session.add(user)
        db.session.commit()

        r = self.w.get('/user/%i/' % user.id)
        self.assertEqual(r.body, 'Hello, Anton!')

        r = self.w.post('/user/%i/preview/' % user.id, {
            'greeting': 'Hi, %s.',
        })
        self.assertEqual(r.body, 'Hi, Anton.')
        db.session.refresh(user)

        r = self.w_without_scoping.post('/user/%i/preview/' % user.id, {
            'greeting': 'Hi, %s.',
        })
        self.assertEqual(r.body, 'Hi, Anton.')
        self.assertRaises(
            sqlalchemy.exc.InvalidRequestError,
            lambda: db.session.refresh(user))
示例#2
0
class TestSQLAlchemyFeatures(unittest.TestCase):
    def setUp(self):
        self.app = app2
        self.w_without_scoping = TestApp(self.app)
        self.w = TestApp(self.app, db=db, use_session_scopes=True)

        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()

    def tearDown(self):
        db.drop_all()
        self.app_context.pop()

    def test_1(self):
        user = User(name='Anton')
        db.session.add(user)
        db.session.commit()

        r = self.w.get('/user/%i/' % user.id)
        self.assertEqual(r.body, 'Hello, Anton!')

        # Note: we did not commit the change to `user`!
        user.name = 'Petr'

        r = self.w_without_scoping.get('/user/%i/' % user.id)
        self.assertEqual(r.body, 'Hello, Petr!')

        r = self.w.get('/user/%i/' % user.id)
        self.assertEqual(r.body, 'Hello, Anton!')

    def test_2(self):
        user = User(name='Anton')
        db.session.add(user)
        db.session.commit()

        r = self.w.get('/user/%i/' % user.id)
        self.assertEqual(r.body, 'Hello, Anton!')

        r = self.w.post('/user/%i/preview/' % user.id, {
            'greeting': 'Hi, %s.',
        })
        self.assertEqual(r.body, 'Hi, Anton.')
        db.session.refresh(user)

        r = self.w_without_scoping.post('/user/%i/preview/' % user.id, {
            'greeting': 'Hi, %s.',
        })
        self.assertEqual(r.body, 'Hi, Anton.')
        self.assertRaises(
            sqlalchemy.exc.InvalidRequestError,
            lambda: db.session.refresh(user))
示例#3
0
class TestMainFeatures(unittest.TestCase):
    def setUp(self):
        self.app = create_app()
        self.w = TestApp(self.app)
        self.app.config['TESTING'] = True

    def login(self, username, password):
        return self.w.post('/account/login', {
            'email': username,
            'password': password
        })

    def logout(self):
        return self.w.get('/account/logout')

    def test_home(self):
        r = self.w.get('/entry/')
        self.assertFalse(r.flashes)
        self.assertEqual(r.json, {u'code': ResultCode.SUCCESS})

    def test_curd(self):
        # add

        # 登录
        # r = self.login('*****@*****.**', 'admin888')
        # print r
        # assert 'You were logged in' in r.data

        r = self.w.post('/entry/v1/create?category_id=1', {
            'creater_id': 1,
            'modifier_id': 1,

            'entry_type': EntryType.software,

            'title': '文章测试123',
            'content': '文章测试内容123',
            'summary': '123',
            'tags': 'tag1,tag2,tag3',

            'logo': '/1.gif',
            'feature_image': '/1.png',

            'source_title': '',
            'source_url': '',

            'source_type': EntrySourceType.original,

            'slug': 'ar/1',

            'recommend': 1

        })

        if r.json['code'] != ResultCode.SUCCESS:
            print r.json['message']

        print r

        self.assertFalse(r.flashes)
        self.assertIn(r.json['code'], [ResultCode.SUCCESS, ResultCode.DATA_EXISTED])

        # update
        r = self.w.post('/entry/v1/edit?category_id=1&entry_id=1', {
            'title': '文章测试123, edit',
        })

        print r
        if r.json['code'] != ResultCode.SUCCESS:
            print r.json['message']

        self.assertFalse(r.flashes)
        self.assertIn(r.json['code'], [ResultCode.SUCCESS, ResultCode.DATA_EXISTED])

        return
        # get
        r = self.w.get('/device/%s' % device_id)

        self.assertFalse(r.flashes)
        self.assertEqual(r.json['code'], ResultCode.SUCCESS)
        self.assertTrue(r.json['metadata'])

        # get_list
        r = self.w.get('/device/list?search=%s' % 'len')

        self.assertFalse(r.flashes)
        self.assertEqual(r.json['code'], ResultCode.SUCCESS)
        self.assertTrue(r.json['device_total'] >= 0)

        # del
        r = self.w.post('/device/delete', {
            'device_id': device_id, })

        self.assertFalse(r.flashes)
        self.assertIn(r.json['code'], [ResultCode.SUCCESS, ResultCode.DATA_EXISTED])
示例#4
0
class TestMainFeatures(unittest.TestCase):
    def setUp(self):
        self.app = create_app()
        self.w = TestApp(self.app)
        self.app.config['TESTING'] = True

    def login(self, username, password):
        return self.w.post('/account/login', {
            'email': username,
            'password': password
        })

    def logout(self):
        return self.w.get('/account/logout')

    def test_home(self):
        r = self.w.get('/entry/')
        self.assertFalse(r.flashes)
        self.assertEqual(r.json, {u'code': ResultCode.SUCCESS})

    def test_curd(self):
        # add

        # 登录
        # r = self.login('*****@*****.**', 'admin888')
        # print r
        # assert 'You were logged in' in r.data

        r = self.w.post(
            '/entry/v1/create?category_id=1', {
                'creater_id': 1,
                'modifier_id': 1,
                'entry_type': EntryType.software,
                'title': '文章测试123',
                'content': '文章测试内容123',
                'summary': '123',
                'tags': 'tag1,tag2,tag3',
                'logo': '/1.gif',
                'feature_image': '/1.png',
                'source_title': '',
                'source_url': '',
                'source_type': EntrySourceType.original,
                'slug': 'ar/1',
                'recommend': 1
            })

        if r.json['code'] != ResultCode.SUCCESS:
            print r.json['message']

        print r

        self.assertFalse(r.flashes)
        self.assertIn(r.json['code'],
                      [ResultCode.SUCCESS, ResultCode.DATA_EXISTED])

        # update
        r = self.w.post('/entry/v1/edit?category_id=1&entry_id=1', {
            'title': '文章测试123, edit',
        })

        print r
        if r.json['code'] != ResultCode.SUCCESS:
            print r.json['message']

        self.assertFalse(r.flashes)
        self.assertIn(r.json['code'],
                      [ResultCode.SUCCESS, ResultCode.DATA_EXISTED])

        return
        # get
        r = self.w.get('/device/%s' % device_id)

        self.assertFalse(r.flashes)
        self.assertEqual(r.json['code'], ResultCode.SUCCESS)
        self.assertTrue(r.json['metadata'])

        # get_list
        r = self.w.get('/device/list?search=%s' % 'len')

        self.assertFalse(r.flashes)
        self.assertEqual(r.json['code'], ResultCode.SUCCESS)
        self.assertTrue(r.json['device_total'] >= 0)

        # del
        r = self.w.post('/device/delete', {
            'device_id': device_id,
        })

        self.assertFalse(r.flashes)
        self.assertIn(r.json['code'],
                      [ResultCode.SUCCESS, ResultCode.DATA_EXISTED])