def test_home_post4(): """/へアクセス(ファイルを一つ送信)""" url = reverse('app:home') assert url == '/' wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'POST', 'PATH_INFO': url, } files = [ ('files', 'test1.txt', 'Hello Test'), ] environ = add_post_environ(environ, files=files) start_response = lambda x, y: None response = wsgi_app(environ, start_response) response_headers = [ ('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', '914'), ] assert response.headers == response_headers assert '<p>text: None</p>'.encode('utf-8') in response.content assert '<p>text2: None</p>'.encode('utf-8') in response.content assert "<p>select: []</p>".encode('utf-8') in response.content assert '<p>ファイル名: test1.txt</p>'.encode('utf-8') in response.content assert '<p>ファイル名2: </p>'.encode('utf-8') in response.content for _, filename, content in files: path = os.path.join(MEDIA_ROOT, filename) with open(path, 'r') as file: assert file.read() == content shutil.rmtree(MEDIA_ROOT)
def test_home_post3(): """/へアクセス(postとgetパラメータ、views.pyのロジックでpostデータ反映)""" url = reverse('app:home') assert url == '/' wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'POST', 'PATH_INFO': url, 'QUERY_STRING': 'text=auaua&text2=oooo&select=1000&select=42', } data = [('text', 'hello'), ('text2', 'world'), ('select', '3'), ('select', '4')] environ = add_post_environ(environ, data=data) start_response = lambda x, y: None response = wsgi_app(environ, start_response) response_headers = [ ('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', '915'), ] assert response.headers == response_headers assert '<p>text: hello</p>'.encode('utf-8') in response.content assert '<p>text2: world</p>'.encode('utf-8') in response.content assert "<p>select: ['3', '4']</p>".encode('utf-8') in response.content assert '<p>ファイル名: </p>'.encode('utf-8') in response.content assert '<p>ファイル名2: </p>'.encode('utf-8') in response.content
def test_app1_no_template(): """/no/template へアクセス (テンプレートが見つからない)""" url = reverse('app1:no_template') assert url == '/no/template/' wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': url, 'QUERY_STRING': '', } start_response = lambda x, y: None with pytest.raises(TemplateNotFound) as excinfo: response = wsgi_app(environ, start_response) assert 'app1/aaaaa.html' == str(excinfo.value)
def test_app2_hello(): """/app2/hello/ へアクセス (見つからない)""" url = reverse('app2:hello') assert url == '/app2/hello/{name}/' wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/app2/hello/', 'QUERY_STRING': '', } start_response = lambda x, y: None with pytest.raises(Resolver404) as excinfo: response = wsgi_app(environ, start_response) assert 'URL Not Found /app2/hello/' == str(excinfo.value)
def test_media2(): """/media/nofile の配信確認.""" url = reverse('debug:media', file_path='nofile') assert url == '/media/nofile/' wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': url, 'QUERY_STRING': '', } start_response = lambda x, y: None with pytest.raises(Resolver404) as excinfo: response = wsgi_app(environ, start_response) assert 'URL Not Found /media/nofile/' == str(excinfo.value)
def test_app1_aiueo(): """/aiueo/ へアクセス (見つからない)""" with pytest.raises(NoReverseMatch) as excinfo: url = reverse('app1:aiueo') assert 'app1:aiueoが見つかりません' == str(excinfo.value) wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/aiueo/', 'QUERY_STRING': '', } start_response = lambda x, y: None with pytest.raises(Resolver404) as excinfo: response = wsgi_app(environ, start_response) assert 'URL Not Found /aiueo/' == str(excinfo.value)
def test_static2(): """/static/app/b.css/ の配信確認.""" url = reverse('debug:static', file_path='app/b.css') assert url == '/static/app/b.css/' wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': url, 'QUERY_STRING': '', } start_response = lambda x, y: None response = wsgi_app(environ, start_response) response_headers = [ ('Content-Type', 'text/css; charset=UTF-8'), ('Content-Length', '37'), ] assert response.headers == response_headers
def test_app_home(): """/ へアクセス""" url = reverse('app:home') assert url == '/' wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': url, 'QUERY_STRING': '', } start_response = lambda x, y: None response = wsgi_app(environ, start_response) response_headers = [ ('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', '383'), ] assert response.headers == response_headers
def test_media1(): """/media/f.png の配信確認.""" url = reverse('debug:media', file_path='f.png') assert url == '/media/f.png/' wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': url, 'QUERY_STRING': '', } start_response = lambda x, y: None response = wsgi_app(environ, start_response) response_headers = [ ('Content-Type', 'image/png; charset=UTF-8'), ('Content-Length', '473831'), ] assert response.headers == response_headers
def test_app2_hello_narito(): """/app2/hello/narito/ へアクセス""" url = reverse('app2:hello', name='narito') assert url == '/app2/hello/narito/' wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': url, 'QUERY_STRING': '', } start_response = lambda x, y: None response = wsgi_app(environ, start_response) response_headers = [ ('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', '25'), ] assert response.headers == response_headers assert [b'<html>Hello narito</html>'] == list(response)
def test_home_post6(): """/へアクセス(ファイルを複数送信とpostデータ)""" url = reverse('app:home') assert url == '/' wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'POST', 'PATH_INFO': url, } data = [('text', 'hello'), ('text2', 'world'), ('select', '3'), ('select', '4')] files = [ ('files', 'test1.txt', 'Hello Test'), ('files2', 'test2.txt', 'Hello a'), ('files2', 'test3.txt', 'Hello i'), ('files2', 'test4.txt', 'Hello u'), ] environ = add_post_environ(environ, files=files, data=data) start_response = lambda x, y: None response = wsgi_app(environ, start_response) response_headers = [ ('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', '963'), ] assert response.headers == response_headers assert '<p>text: hello</p>'.encode('utf-8') in response.content assert '<p>text2: world</p>'.encode('utf-8') in response.content assert "<p>select: ['3', '4']</p>".encode('utf-8') in response.content assert '<p>ファイル名: test1.txt</p>'.encode('utf-8') in response.content assert "<p>ファイル名2: ['test2.txt', 'test3.txt', 'test4.txt']</p>".encode( 'utf-8') in response.content for _, filename, content in files: path = os.path.join(MEDIA_ROOT, filename) with open(path, 'r') as file: assert file.read() == content shutil.rmtree(MEDIA_ROOT)
def test_home_post1(): """/へアクセス(postデータなし)""" url = reverse('app:home') assert url == '/' wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'POST', 'PATH_INFO': url, } environ = add_post_environ(environ) start_response = lambda x, y: None response = wsgi_app(environ, start_response) response_headers = [ ('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', '905'), ] assert response.headers == response_headers assert '<p>text: None</p>'.encode('utf-8') in response.content assert '<p>text2: None</p>'.encode('utf-8') in response.content assert "<p>select: []</p>".encode('utf-8') in response.content assert '<p>ファイル名: </p>'.encode('utf-8') in response.content assert '<p>ファイル名2: </p>'.encode('utf-8') in response.content
def test_home_get2(): """/?text=1&text2=2&select=3&select=4 へアクセス""" url = reverse('app:home') assert url == '/' wsgi_app = get_wsgi_application() environ = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': url, 'QUERY_STRING': 'text=hello&text2=world&select=3&select=4', } start_response = lambda x, y: None response = wsgi_app(environ, start_response) response_headers = [ ('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', '915'), ] assert response.headers == response_headers assert '<p>text: hello</p>'.encode('utf-8') in response.content assert '<p>text2: world</p>'.encode('utf-8') in response.content assert "<p>select: ['3', '4']</p>".encode('utf-8') in response.content assert '<p>ファイル名: </p>'.encode('utf-8') in response.content assert '<p>ファイル名2: </p>'.encode('utf-8') in response.content