示例#1
0
    def setUp(self):
        self.product = {
            'title':'My Book Title',
            'description':'yyy',
            'image_url':'http://google.com/logo.png',
            'price':1
        }

        f = ProductForm(self.product)
        f.save()
        self.product['title'] = 'My Another Book Title'
示例#2
0
        def test_imgae_url_endwiths(self):
            url_base = 'http://google.com/'
            oks = ('fred.gif', 'fred.jpg', 'fred.png', 'FRED.JPG', 'FRED.Jpg')
            bads = ('fred.doc', 'fred.gif/more', 'fred.gif.more')
            for endwith in oks:
                self.product['image_url'] = url_base+endwith
                f = ProductForm(self.product)
                self.assertTrue(f.is_valid(),msg='error when image_url endwith '+endwith)

            for endwith in bads:
                self.product['image_url'] = url_base+endwith
                f = ProductForm(self.product)
                self.assertFalse(f.is_valid(),msg='error when image_url endwith '+endwith)

            self.product['image_url'] = 'http://google.com/logo.png'
示例#3
0
    def test_attrs_cannot_empty(self):
        f = ProductForm({})
        self.assertFalse(f.is_valid())
        self.assertTrue(f['title'].errors)
        self.assertTrue(f['description'].errors)
        self.assertTrue(f['price'].errors)
        self.assertTrue(f['image_url'].errors)

        ####   price必须大于零
        def test_price_positive(self):
            f = ProductForm(self.product)
            self.assertTrue(f.is_valid())

            self.product['price'] = 0
            f = ProductForm(self.product)
            self.assertFalse(f.is_valid())

            self.product['price'] = -1
            f = ProductForm(self.product)
            self.assertFalse(f.is_valid())

            self.product['price'] = 1

        ####   image_url必须以jpg,png,jpg结尾,并且对大小写不敏感;
        def test_imgae_url_endwiths(self):
            url_base = 'http://google.com/'
            oks = ('fred.gif', 'fred.jpg', 'fred.png', 'FRED.JPG', 'FRED.Jpg')
            bads = ('fred.doc', 'fred.gif/more', 'fred.gif.more')
            for endwith in oks:
                self.product['image_url'] = url_base+endwith
                f = ProductForm(self.product)
                self.assertTrue(f.is_valid(),msg='error when image_url endwith '+endwith)

            for endwith in bads:
                self.product['image_url'] = url_base+endwith
                f = ProductForm(self.product)
                self.assertFalse(f.is_valid(),msg='error when image_url endwith '+endwith)

            self.product['image_url'] = 'http://google.com/logo.png'

        ###   titile必须唯一
        def test_title_unique(self):
            self.product['title'] = 'My Book Title'
            f = ProductForm(self.product)
            self.assertFalse(f.is_valid())
            self.product['title'] = 'My Another Book Title'
示例#4
0
        def test_price_positive(self):
            f = ProductForm(self.product)
            self.assertTrue(f.is_valid())

            self.product['price'] = 0
            f = ProductForm(self.product)
            self.assertFalse(f.is_valid())

            self.product['price'] = -1
            f = ProductForm(self.product)
            self.assertFalse(f.is_valid())

            self.product['price'] = 1
示例#5
0
 def test_title_unique(self):
     self.product['title'] = 'My Book Title'
     f = ProductForm(self.product)
     self.assertFalse(f.is_valid())
     self.product['title'] = 'My Another Book Title'