DonConfia_Dev/modules/sale_don_confiao/tests/test_api_donconfiao.py
2025-02-14 22:55:17 -05:00

271 lines
8.6 KiB
Python

from trytond.tests.test_tryton import RouteTestCase
from trytond.pool import Pool
from http import HTTPStatus
import uuid
import json
from trytond.tests.test_tryton import DB_NAME
from trytond.transaction import Transaction
from decimal import Decimal
class DonConfiaoApiRouteTestCase(
RouteTestCase):
"Don Confiao API Routes"
module = 'sale_don_confiao'
key = uuid.uuid4().hex
@classmethod
def setUpDatabase(cls):
pool = Pool()
Application = pool.get('res.user.application')
Party = pool.get('party.party')
Application(
key=cls.key,
user=1,
application='sale_don_confiao',
state='validated').save()
customer = Party()
customer.name = 'Dunkan'
customer.save()
def test_get_parties(self):
client = self.client()
response = client.get(
f'/{self.db_name}/sale_don_confiao/parties',
headers={
'Authorization': f'bearer {self.key}',
})
parties = json.loads(
response.get_data().decode())
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(len(parties), 1)
def test_get_party(self):
client = self.client()
response = client.get(
f'/{self.db_name}/sale_don_confiao/party/1',
headers={
'Authorization': f'bearer {self.key}',
})
party, = json.loads(
response.get_data().decode())
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(party['name'], 'Dunkan')
def test_post_parties(self):
client = self.client()
response = client.post(
f'/{self.db_name}/sale_don_confiao/parties',
headers={
'Authorization': f'bearer {self.key}',
}, data=json.dumps({
"name": "Alejandro",
}))
self.assertEqual(response.status_code, HTTPStatus.OK)
response = client.get(
f'/{self.db_name}/sale_don_confiao/parties',
headers={
'Authorization': f'bearer {self.key}',
})
parties = json.loads(
response.get_data().decode())
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(len(parties), 2)
def test_get_categories(self):
client = self.client()
response = client.get(
f'/{self.db_name}/sale_don_confiao/categories',
headers={
'Authorization': f'bearer {self.key}',
})
self.assertEqual(response.status_code, HTTPStatus.OK)
def test_get_products(self):
pool = Pool(DB_NAME)
transaction = Transaction().start(DB_NAME, 0, _lock_tables=[
'product_product'])
with transaction:
# MACHETE: Se bloquea la tabla manualmente
# transaction._locked_tables = ['product_product']
User = pool.get('res.user')
admin, = User.search([('login', '=', 'admin')])
admin.password = 'password'
admin.save()
ProductTemplate = pool.get('product.template')
Product = pool.get('product.product')
Uom = pool.get('product.uom')
template, = ProductTemplate.create([{
'name': 'Product',
'default_uom': Uom.search([('name', '=', 'Unit')])[0].id,
'sale_uom': Uom.search([('name', '=', 'Unit')])[0].id,
'salable': True
}])
Product.create([{'template': template.id}])
client = self.client()
response = client.get(
f'/{self.db_name}/sale_don_confiao/products',
headers={
'Authorization': f'bearer {self.key}',
})
raise Exception(response.text)
products = json.loads(
response)
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(len(products), 1)
def test_search_products(self):
client = self.client()
response = client.get(
f'/{self.db_name}/sale_don_confiao/search_products/product',
headers={
'Authorization': f'bearer {self.key}',
})
products = json.loads(
response.get_data().decode())
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(len(products), 2)
def test_get_sales(self):
pool = Pool(DB_NAME)
transaction = Transaction().start(DB_NAME, user=0, _lock_tables=[
'product_product'])
with transaction:
Company = pool.get(
'company.company')
Currency = pool.get('currency.currency')
Sale = pool.get('sale.sale')
currency, = Currency.create([{
'name': 'COP',
'code': 'COP'
}])
company, = Company.create([{
'party': 1,
'currency': currency.id
}])
ProductTemplate = pool.get('product.template')
Product = pool.get('product.product')
Uom = pool.get('product.uom')
template, = ProductTemplate.create([
{'name': 'Product',
'salable': True,
'default_uom': Uom.search([('name', '=', 'Unit')])[0].id,
'sale_uom': Uom.search([('name', '=', 'Unit')])[0].id
}
])
product, = Product.create([
{'template': template.id}])
sale, = Sale.create([{
'party': 1,
'company': 1,
'currency': 1,
'lines': [['create', [{
'type': 'line',
'product': product.id,
'quantity': 1,
'unit_price': Decimal(10),
'unit': Uom.search([('name', '=', 'Unit')])[0].id
}]]],
'state': 'draft'
}])
self.assertIsInstance(sale, Sale)
self.assertEqual(sale.state, 'draft')
client = self.client()
response = client.get(
f'/{self.db_name}/sale_don_confiao/sales',
headers={
'Authorization': f'bearer {self.key}',
}, data=json.dumps({
'company': company.id
}))
self.assertEqual(response.status_code, HTTPStatus.OK)
# def test_post_sale(self):
# pool = Pool(DB_NAME)
# transaction = Transaction().start(DB_NAME, user=0)
# with transaction:
# party = pool.get('party.party').search([])[0]
# company, = pool.get('company.company').search([])
# currency, = pool.get('currency.currency').search([])
# product = pool.get('product.product').search([
# ('salable', '=', True)])[0]
# uom = pool.get('product.uom').search([])[0]
# client = self.client()
# response = client.post(
# f'/{self.db_name}/sale_don_confiao/post_sale',
# headers={
# 'Authorization': f'bearer {self.key}',
# }, data=json.dumps({
# "party": party.id,
# "company": company.id,
# # "currency": currency.id,
# # "invoice_method": "order",
# # "shipment_method": "order",
# # "lines": [["create", [{
# # "type": "line",
# # "product": product.id,
# # "quantity": "1",
# # "unit_price": "10",
# # "unit": uom.id
# # }]]],
# # "state": "draft"
# }))
# self.assertEqual(response.status_code, HTTPStatus.OK)
#
def test_get_payment_methods(self):
client = self.client()
response = client.get(
f'/{self.db_name}/sale_don_confiao/payment_methods',
headers={
'Authorization': f'bearer {self.key}',
})
expected_payment_methods = [
{
"text": "Efectivo",
"value": "CASH"
},
{
"text": "Confiar",
"value": "CONFIAR"
},
{
"text": "Bancolombia",
"value": "BANCOLOMBIA"
}
]
self.assertEqual(
json.loads(
response.text
)[0]["payment_methods"], expected_payment_methods)