162 lines
5.8 KiB
Python
162 lines
5.8 KiB
Python
import unittest
|
|
import sqlite3
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import json
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from database import get_db_connection, create_tables
|
|
from app import app
|
|
|
|
|
|
class TestProducts(unittest.TestCase):
|
|
"""Тестирование функционала товаров"""
|
|
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
"""Создаем тестовую базу данных"""
|
|
cls.db_fd, cls.db_path = tempfile.mkstemp()
|
|
|
|
import database
|
|
database.DATABASE = cls.db_path
|
|
|
|
create_tables()
|
|
|
|
cls.conn = sqlite3.connect(cls.db_path)
|
|
cls.conn.row_factory = sqlite3.Row
|
|
cursor = cls.conn.cursor()
|
|
|
|
# Добавляем типы товаров
|
|
cursor.execute("INSERT INTO product_types (type_name) VALUES ('Ручки')")
|
|
cursor.execute("INSERT INTO product_types (type_name) VALUES ('Тетради')")
|
|
cursor.execute("INSERT INTO product_types (type_name) VALUES ('Бумага')")
|
|
|
|
# Добавляем товары (БЕЗ quantity_in_stock)
|
|
cursor.execute('''
|
|
INSERT INTO products (product_type_id, product_name, article, min_partner_price)
|
|
VALUES (1, 'Ручка синяя', 'R001', 50.0)
|
|
''')
|
|
cursor.execute('''
|
|
INSERT INTO products (product_type_id, product_name, article, min_partner_price)
|
|
VALUES (1, 'Ручка красная', 'R002', 60.0)
|
|
''')
|
|
cursor.execute('''
|
|
INSERT INTO products (product_type_id, product_name, article, min_partner_price)
|
|
VALUES (2, 'Тетрадь 48л', 'T001', 100.0)
|
|
''')
|
|
|
|
cls.conn.commit()
|
|
|
|
def test_db_connection():
|
|
conn = sqlite3.connect(cls.db_path)
|
|
conn.row_factory = sqlite3.Row
|
|
return conn
|
|
|
|
app.config['DATABASE'] = cls.db_path
|
|
database.get_db_connection = test_db_connection
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
cls.conn.close()
|
|
os.close(cls.db_fd)
|
|
try:
|
|
os.unlink(cls.db_path)
|
|
except:
|
|
pass
|
|
|
|
def setUp(self):
|
|
self.app = app.test_client()
|
|
self.app.testing = True
|
|
|
|
def test_1_products_page(self):
|
|
"""Тест страницы товаров"""
|
|
response = self.app.get('/products')
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertIn('Каталог товаров', response.data.decode('utf-8'))
|
|
self.assertIn('Ручка синяя', response.data.decode('utf-8'))
|
|
|
|
def test_2_api_get_product(self):
|
|
"""Тест API получения товара"""
|
|
response = self.app.get('/api/product/1')
|
|
self.assertEqual(response.status_code, 200)
|
|
data = json.loads(response.data)
|
|
self.assertEqual(data['product_name'], 'Ручка синяя')
|
|
self.assertEqual(data['article'], 'R001')
|
|
|
|
def test_3_api_add_product(self):
|
|
"""Тест API добавления товара"""
|
|
new_product = {
|
|
'product_type_id': 2,
|
|
'product_name': 'Тетрадь новая',
|
|
'article': 'T002',
|
|
'min_partner_price': 150.0
|
|
}
|
|
|
|
response = self.app.post('/api/product',
|
|
data=json.dumps(new_product),
|
|
content_type='application/json')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
data = json.loads(response.data)
|
|
self.assertTrue(data['success'])
|
|
self.assertIsNotNone(data['product_id'])
|
|
|
|
response = self.app.get(f'/api/product/{data["product_id"]}')
|
|
self.assertEqual(response.status_code, 200)
|
|
product_data = json.loads(response.data)
|
|
self.assertEqual(product_data['product_name'], 'Тетрадь новая')
|
|
|
|
def test_4_api_update_product(self):
|
|
"""Тест API обновления товара"""
|
|
updated_product = {
|
|
'product_type_id': 1,
|
|
'product_name': 'Ручка обновленная',
|
|
'article': 'R001',
|
|
'min_partner_price': 75.0
|
|
}
|
|
|
|
response = self.app.put('/api/product/1',
|
|
data=json.dumps(updated_product),
|
|
content_type='application/json')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
data = json.loads(response.data)
|
|
self.assertTrue(data['success'])
|
|
|
|
response = self.app.get('/api/product/1')
|
|
product_data = json.loads(response.data)
|
|
self.assertEqual(product_data['product_name'], 'Ручка обновленная')
|
|
self.assertEqual(product_data['min_partner_price'], 75.0)
|
|
|
|
def test_5_api_add_product_type(self):
|
|
"""Тест API добавления типа товара"""
|
|
new_type = {
|
|
'type_name': 'Ластики',
|
|
'description': 'Для стирания'
|
|
}
|
|
|
|
response = self.app.post('/api/product_type',
|
|
data=json.dumps(new_type),
|
|
content_type='application/json')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
data = json.loads(response.data)
|
|
self.assertTrue(data['success'])
|
|
self.assertIsNotNone(data['type_id'])
|
|
|
|
conn = sqlite3.connect(self.db_path)
|
|
conn.row_factory = sqlite3.Row
|
|
type_row = conn.execute('SELECT * FROM product_types WHERE type_name = ?', ('Ластики',)).fetchone()
|
|
conn.close()
|
|
self.assertIsNotNone(type_row)
|
|
|
|
def test_6_filter_products(self):
|
|
"""Тест фильтрации товаров по типу"""
|
|
response = self.app.get('/products?type=1')
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main() |