import unittest
import main
from pymongo import MongoClient


class test_main(unittest.TestCase):
    def setUp(self):
        with MongoClient('mongodb://travis:test@localhost/testdb') as client:
            db = client.testdb
            db_uri = db.uri
            db_uri.insert_one({'id': 0, 'uri': 'http://example.com/', 'type': 0})
            db_uri.insert_one({'id': 1, 'uri': 'http://example.org/test.html', 'type': 1})
            db_cs = db.counters
            db_cs.insert_one({'id': 'uri_id', 'seq': 2})
        self.app = main.app.test_client()

    def tearDown(self):
        with MongoClient('mongodb://travis:test@localhost/testdb') as client:
            client.drop_database('testdb')

    def test_root(self):
        rv = self.app.get('/')
        self.assertEqual(rv.status_code, 200)
        self.assertRegex(rv.data.decode(), '<h1>短縮URLサービス wq8pw にようこそ</h1>')

    def test_pass(self):
        pass

    def test_get_update(self):
        rv = self.app.get('/accept/post')
        self.assertEqual(rv.status_code, 301)

    def test_update_changeless_type(self):
        rv = self.app.post('/accept/post', data={'uri': 'http://example.com/'})
        self.assertEqual(rv.status_code, 200)
        self.assertRegex(rv.data.decode(), 'https://a/4s5yxhl6exkos')
        self.assertRegex(rv.data.decode(), 'https://a/5LuLnX4l1Ok')

    def test_update_change_type(self):
        rv = self.app.post('/accept/post', data={'uri': 'http://example.com/', 'jamp_flag': 1})
        self.assertEqual(rv.status_code, 200)
        self.assertRegex(rv.data.decode(), 'https://a/hv2zlkml76aj2')
        self.assertRegex(rv.data.decode(), 'https://a/PXWVqYv_gJ0')

    def test_update(self):
        rv = self.app.post('/accept/post', data={'uri': 'http://example.net/'})
        self.assertEqual(rv.status_code, 200)
        self.assertRegex(rv.data.decode(), 'https://a/4s5yxhl6exkos')
        self.assertRegex(rv.data.decode(), 'https://a/5LuLnX4l1Ok')

    def test_update2(self):
        rv = self.app.post('/accept/post', data={'uri': 'http://example.org/test.html', 'jamp_flag': 1})
        self.assertEqual(rv.status_code, 200)
        self.assertRegex(rv.data.decode(), 'https://a/hjgk26lm7v6qg')
        self.assertRegex(rv.data.decode(), 'https://a/OkyteWz9fQM')


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

@@ -1252,33 +1252,41 @@
 ode(), 'https://
-a
+localhost
 /4s5yxhl6exkos')
@@ -1330,33 +1330,41 @@
 ode(), 'https://
-a
+localhost
 /5LuLnX4l1Ok')%0A%0A
@@ -1596,17 +1596,25 @@
 https://
-a
+localhost
 /hv2zlkm
@@ -1674,17 +1674,25 @@
 https://
-a
+localhost
 /PXWVqYv
@@ -1904,17 +1904,25 @@
 https://
-a
+localhost
 /4s5yxhl
@@ -1982,17 +1982,25 @@
 https://
-a
+localhost
 /5LuLnX4
@@ -2238,17 +2238,25 @@
 https://
-a
+localhost
 /hjgk26l
@@ -2316,17 +2316,25 @@
 https://
-a
+localhost
 /OkyteWz

テストコードの uri 修正