def test_update_email(self): with self.current_api.app_context(): ClientService.add('admin1', '*****@*****.**') client = ClientService.get_by_email('*****@*****.**') updated_client = ClientService.update_email(client.id, 'admin42') self.assertEqual(updated_client.id, client.id) updated_client = ClientService.update_email(987, 'admin42') self.assertEqual(updated_client, None)
def test_remove(self): with self.current_api.app_context(): ClientService.add('admin1', '*****@*****.**') client = ClientService.get_by_email('*****@*****.**') deleted_client = ClientService.remove(client.id) self.assertEqual(deleted_client.id, client.id) deleted_client = ClientService.remove(987) self.assertEqual(deleted_client, None)
def update_partial_client(client_id): username = request.json['username'] if ClientService.get_by_username(username): return jsonify({'error': 'This username already exists!'}) client = ClientService.update_username(client_id, username) if client == None: abort(404) client_schema = ClientSchema() return client_schema.jsonify(client)
def create_client(): username = request.json['username'] email = request.json['email'] if ClientService.get_by_username(username): return jsonify({'error': 'This username already exists!'}) if ClientService.get_by_email(email): return jsonify({'error': 'This email already exists!'}) client = ClientService.add(username, email) client_schema = ClientSchema() return client_schema.jsonify(client)
def delete_client(client_id): client = ClientService.remove(client_id) if client == None: abort(404) client_schema = ClientSchema() return client_schema.jsonify(client)
def test_get_all(self): with self.current_api.app_context(): ClientService.add('admin1', '*****@*****.**') ClientService.add('admin2', '*****@*****.**') ClientService.add('admin3', '*****@*****.**') clients = ClientService.get_all() self.assertNotEqual(len(clients), 0)
from api import create_api, db from api.clients.models import Client from api.clients.services import ClientService from api.finances.services import RecordService api = create_api('config') if __name__ == "__main__": with api.app_context(): # remove todas as tabelas do banco db.drop_all() # (re)cria o banco de dados db.create_all() # Testes de consultas users = Client.query.all() # Adiciona um usuário for i in range(1000): username, email = f'admin{i}', f'admin{i}@finances.com' u = Client(username, email) db.session.add(u) db.session.commit() data = ClientService.get_all() print(data) data = RecordService.get_all() print(data)
current_api = create_api('config') with current_api.app_context(): # remove todas as tabelas do banco db.drop_all() # (re)cria o banco de dados db.create_all() # obter os registros na base de dados records = RecordService.get_all() print(f'records {list(records)}') # cria um usuário e o adiciona ao banco client_1 = ClientService.add('admin', '*****@*****.**') print(client_1) client_2 = ClientService.add('admin1', '*****@*****.**') print(client_1) client_3 = ClientService.add('admin2', '*****@*****.**') print(client_1) client_4 = ClientService.add('admin3', '*****@*****.**') print(client_1) # cria um registro r = RecordService.add(id_client=client_1.id, descr='any', amount=8.57,
def test_get_by_email(self): with self.current_api.app_context(): ClientService.add('admin1', '*****@*****.**') client = ClientService.get_by_email('*****@*****.**') self.assertNotEqual(client, None)
def test_add(self): with self.current_api.app_context(): client = ClientService.add('admin', '*****@*****.**') self.assertNotEqual(client.id, None)
def pag_clients(page_num): clients = ClientService.paginate(per_page=20, page=page_num).items clients_schema = ClientSchema(many=True) return clients_schema.jsonify(clients)
def get_client(client_id): client = ClientService.get_by_id(client_id) if client == None: abort(404) client_schema = ClientSchema() return client_schema.jsonify(client)
def get_clients(): clients = ClientService.get_all() clients_schema = ClientSchema(many=True) return clients_schema.jsonify(clients)