วันพุธที่ 28 มกราคม พ.ศ. 2558

Chapter 3 Testing a Simple Home Page with Unit Tests

ใช้ คำสั่ง $ python3 manage.py startapp lists
เพื่อสร้าง Folder ขึ้นมาใหม่ชื่อ lists และไฟล์ db.sqlite3

superlists/
     - db.sqlite3
     - functional_tests.py
     - lists
          • admin.py
          • __init__.py
          • migrations
               ->__init__.py
          • models.py
          • tests.py
          • views.py
     - manage.py
     - superlists
          • __init__.py
          • __pycache__
          • settings.py
          • urls.py
          • wsgi.py


Unit Testing in Django

ในไฟล์ lists/tests.py. ให้ใส่ Code

from django.test import TestCase


class SmokeTest(TestCase):


    def test_bad_maths(self):


        self.assertEqual(1 + 1, 3)


คำสั่ง $ python3 manage.py test




จากการทดสอบ code จะฟ้องว่า 2 ไม่เท่ากับ 3 ซึ่งทำงานได้ถูกต้อง


คำสั่ง

$ git status

$ git add lists



$ git diff  --staged



$ git commit -m "Add app for lists, with deliberately failing unit test"






Django’s MVC, URLs, and View Functions


     - ที่ root("/") จะ resolve ไปได้ functions ที่ต้องการ หรือไม่
     - เราทำให้ฟังก์ชั่น view สามารถส่งค่า HTML คืน ได้หรือไม่

ไฟล์ lists/tests.py. 

from django.core.urlresolvers import resolve

from django.test import TestCase
from lists.views import home_page

class HomePageTest(TestCase):


    def test_root_url_resolves_to_home_page_view(self):

        found = resolve('/') 
        self.assertEqual(found.func, home_page) #เช็คว่า found.func เท่ากับ home_page หรือไม่

คำสั่ง $ python3 manage.py test




จากผลลัพธืที่ได้ คือ ไม่สามารถ import ชื่อ home_page ได้

ไฟล์ lists/views.py. 


from django.shortcuts import render



# Create your views here.

home_page = None



คำสั่ง
$ python3 manage.py test


จากผลลัพธ์ที่ได้ โปรแกรมได้ฟ้องว่าหา path ไม่เจอ

ไฟล์ superlists/urls.py.
จาก

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'superlists.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^admin/', include(admin.site.urls)),
)
แก้ไขเป็น

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'superlists.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    # url(r'^admin/', include(admin.site.urls)),
)

หลังจากนั้นให้ทดสอบรันอีกครั้ง


ผลคือ import Error เพราะ module ไม่มีชื่อ superlists.views

ไฟล์ superlists/urls.py. จาก
urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'superlists.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    # url(r'^admin/', include(admin.site.urls)),
)

แก้เป็น
urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'lists.views.home_page', name='home'),
    # url(r'^blog/', include('blog.urls')),
    # url(r'^admin/', include(admin.site.urls)),
)

จากนั้นรันอีกครั้งด้วยคำสั่ง $ python3 manage.py test


จะเห็นว่า import Error ได้หายไปแล้วแต่ตรง view ยังผิดอยู่

ไฟล์ lists/views.py.

from django.shortcuts import render

# Create your views here.
def home_page():
    pass

ใช้คำสั่ง $ python3 manage.py test


คำสั่ง
$ git diff 
$ git commit -am "First unit test and url mapping, dummy view"

Unit Testing a View

ไฟล์ lists/tests.py.

from django.core.urlresolvers import resolve
from django.test import TestCase
from django.http import HttpRequest

from lists.views import home_page


class HomePageTest(TestCase):

    def test_root_url_resolves_to_home_page_view(self):
        found = resolve('/')
        self.assertEqual(found.func, home_page)


    def test_home_page_returns_correct_html(self):
        request = HttpRequest()
        response = home_page(request)
        self.assertTrue(response.content.startswith(b'<html>'))
        self.assertIn(b'<title>To-Do lists</title>', response.content)

        self.assertTrue(response.content.endswith(b'</html>'))

คำสั่ง
$ python3 manage.py test


ผลจากการรันคือ home_page() ไม่มี arguments แต่ต้องการ 1 arguments

ไฟล์ lists/views.py

แก้ไขเป็น
def home_page(request):
    pass


ผลคือ Object ไม่มี attribute content

ไฟล์ lists/views.py
แก้ไขเป็น

from django.http import HttpResponse

# Create your views here.
def home_page(request):
    return HttpResponse()


โปรแกรมฟ้องกลับมาว่า code ไม่ถูกต้อง

ไฟล์ lists/views.py
แก้ไขเป็น

def home_page(request):
    return HttpResponse('<html>')



โปรแกรมฟ้องกลับมาว่าต้องเพิ่ม <title>To-Do lists</title> เข้าไป

ไฟล์ lists/views.py
แก้ไขเป็น

def home_page(request):
    return HttpResponse('<html><title>To-Do lists</title>')


โปรแกรมห้องกลับมาว่าต้องปิดท้ายด้วย </html>

ไฟล์ lists/views.py
แก้ไขเป็น

def home_page(request):
    return HttpResponse('<html><title>To-Do lists</title></html>')


โปรแกรมถูกต้องเสร็จสมบูรณ์

คำสั่ง $ python3 functional_tests.py


$ git diff 
$ git commit -am "Basic view now returns minimal HTML"
$ git log --oneline


วันพฤหัสบดีที่ 22 มกราคม พ.ศ. 2558

การอ่านไฟล์ .CSV และแสดงค่า

     ทำการอ่านไฟล์ .csv ที่ได้ save ไว้บนเครื่อง โดยเขียนภาษา python ให้นำข้อมูลในไฟล์ .csv มาแสดงแบบตาราง บน web ที่เปิดจาก ServerCGI



จากภาพจะมีไฟล์ .csv ที่saveไว้บนเครื่องและลองทดสอบ run code ผ่าน terminal




Code Min to Max by ID





Code Max to Min by ID




http://java.dzone.com/articles/python-101-reading-and-writing เว็บศึกษาการเปิด .csv
http://janphar.lpru.ac.th/wanasiri/htmltag.html สร้างตาราง
http://118.174.14.67/html/basichtml.html แสดงภาษาไทย

วันจันทร์ที่ 19 มกราคม พ.ศ. 2558

การตรวจสอบรายชื่อไฟล์

ทดสอบการเขียนโปรแกรม โดยการเรียกดูไฟล์และขนาด ใน path ที่เรากำหนดไว้




   
     code บรรทัดที่ 1 เนื่องจาก ทำการเปิด Server แบบ CGI โดยใช้คำสั่ง python -m CGIHTTPServer จึงกำหนด ไฟล์ให้เป็น python

     code บรรทัดที่ 2 เป็นการกำหนดให้เป็น HTML

     code บรรทัดที่ 3 เป็นการนำเข้าฟังก์ชัน

     code บรรทัดที่ 5 กำหนด path ที่เราต้องการจะดู

     code บรรทัดที่ 8-9 เป็นคำสั่งในการคนหารายชื่อไฟล์ ใน path

     code บรรทัดที่ 11 เป็นคำสั่งหาขนาดของไฟล์ข้อมูล

     code บรรทัดที่ 12-13 ให้แสดงชื่อไฟล์ + ขนาด
*คำสั่งเรียกดูขนาดของไฟล์ ไม่สามารถดูขนาดไฟล์ของ Folder ได้ ซึ่งจะแดสงเป็น 4096

http://stackoverflow.com/questions/18962166/python-os-statfile-name-st-size-versus-os-path-getsizefile-name เว็บศึกษาวิธีเรียกดูขนาดข้อมูล
http://www.tutorialspoint.com/python/os_listdir.htm เว็บศึกษาคำสั่ง os.listdir()
http://www.thaitux.info/book/export/html/170 ศึกษาของมูลของ CGI

วันพุธที่ 14 มกราคม พ.ศ. 2558

ทดสอบภาษา HTML เบื้องต้น

สร้างไฟล์ .html ขึ้นมาแล้วเขียน code htlm แล้วทดสอบเปิดดูใน Python simple web server โดยใช้คำสั่ง python -m SimpleHTTPServer ผ่าน Terminal ใน Ubuntu และเปิดไฟล์บน web browser ใช้ address http://0.0.0.0:8000/

test<BR>
<FONT COLOR="#FF0000">สีแดง</FONT><BR>
<B>ตัวหนา</b><BR>
<FONT SIZE=6><FONTCOLOR="#FF0000">Header</FONT>



รายละเอียดของคำสั่ง