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 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') Company = pool.get('company.company') Party = pool.get('party.party') # Product = pool.get('product.product') Application( key=cls.key, user=1, application='sale_don_confiao', state='validated').save() customer = Party() customer.name = 'Dunkan' customer.save() company = Company( party=1, currency=1 ) company.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_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_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 } ]) 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}', }) products = json.loads( response.get_data().decode()) self.assertEqual(response.status_code, HTTPStatus.OK) self.assertEqual(len(products), 1)