def test_should_merge(session): #Arrange c = conta(titular="moneda", saldo=0, id=str(uuid4())) session.add(c) session.commit() conta_branch = conta(titular="moneda", saldo=5, id=c.id, from_id=c.rid, branch="cenario-01") conta_branch_2 = conta(titular="moneda", saldo=15, id=c.id, branch="cenario-01") session.add(conta_branch_2) session.add(conta_branch) session.commit() with patch.object(MergeBranch, 'get_event', return_value=get_event()) as mock_event: with patch.object(BranchLink, 'get', return_value=get_branch_link()) as mock_branch_link: with patch.object(Branch, 'set_merged', return_value=[]) as mock_branch: merge = MergeBranch(session) #Act merge.run("40393d75-a8ee-41f8-b16e-36c5368d352f") #Assert mock_event.assert_called() mock_branch_link.assert_called() mock_branch.assert_called_with("cenario-01") merged = session.query(conta).filter(text("branch = 'master'")).all() for i in merged: assert i.saldo != 0 assert len(merged) == 2 old_branch = session.query(conta).filter(text("branch = 'cenario-01'")).all() assert len(old_branch) == 0 history_branch = session.query(conta).filter(text("branch = 'merged:cenario-01'")).all() assert len(history_branch) == 2
def test_should_drop(session): #Arrange c = conta(titular="moneda", saldo=0, id=str(uuid4())) session.add(c) session.commit() conta_branch = conta(titular="moneda", saldo=5, id=c.id, from_id=c.rid, branch="cenario-01") conta_branch_2 = conta(titular="moneda", saldo=15, id=c.id, branch="cenario-01") session.add(conta_branch_2) session.add(conta_branch) session.commit() with patch.object(BranchLink, 'get', return_value=get_branch_link()) as mock_branch_link: with patch.object(Branch, 'set_dropped', return_value=[]) as mock_branch: drop = DropBranch(session) #Act drop.drop("cenario-01", "user") #Assert mock_branch_link.assert_called() mock_branch.assert_called_with("cenario-01", "user") old_branch = session.query(conta).filter( text("branch = 'cenario-01'")).all() assert len(old_branch) == 0 history_branch = session.query(conta).filter( text("branch = 'dropped:cenario-01'")).all() assert len(history_branch) == 2
def test_get_entity_history(session, test_client): id = uuid4() c = conta(titular="A", saldo=100, id=id) session.add(c) session.commit() c.saldo = 200 session.commit() c.titular = 'B' session.commit() with patch.object(HttpClient, 'get', return_value=apicore_map()) as mock_method: status_code, data = test_client.get_json( f'/Conta/Conta/history/{c.id}') assert status_code == 200 assert len(data) == 3 assert data[0]["_metadata"]['version'] == 1 assert data[0]['titular'] == 'A' assert data[0]['saldo'] == 100 assert data[1]["_metadata"]['version'] == 2 assert data[1]['titular'] == 'A' assert data[1]['saldo'] == 200 assert data[2]["_metadata"]['version'] == 3 assert data[2]['titular'] == 'B' assert data[2]['saldo'] == 200
def test_get_entity_history_with_version(session, test_client): c = conta(titular="A", saldo=100) objs = [] objs.append(c.dict()) session.add(c) session.commit() c.saldo = 200 objs.append(c.dict()) session.commit() c.titular = 'B' session.commit() objs.append(c.dict()) with patch.object(HttpClient, 'get', return_value=apicore_map()) as mock_method: versions = [1, 2, 3] for v in versions: status_code, data = test_client.get_json( f'/Conta/Conta/history/{c.id}?version={v}') assert status_code == 200 assert len(data) == 1 assert data[0]["_metadata"]['version'] == v assert data[0]['titular'] == objs[v - 1]['titular'] assert data[0]['saldo'] == objs[v - 1]['saldo']
def test_get_data_from_map_paginated(session, test_client): contas = [conta(titular="Foo", saldo=i * 100) for i in range(20)] session.add_all(contas) session.commit() with patch.object(HttpClient, 'get', return_value=apicore_map()) as mock_method: status_code, data = test_client.get_json( f'/Conta/Conta?page=1&page_size=10') assert status_code == 200 assert len(data) == 10
def test_get_data_from_map(session, test_client): from mapper.builder import MapBuilder MapBuilder.loaded = False origem = conta(titular="Fabio", saldo=10000) destino = conta(titular="Moneda", saldo=100) session.add_all([origem, destino]) session.commit() with patch.object(HttpClient, 'get', return_value=apicore_map()) as mock_method: uri = f'/Conta/Conta?filter=transferencia&origem={origem.id}&destino={destino.id}' status_code, resp = test_client.get_json(uri) assert status_code == 200 assert len(resp) == 2 assert resp[0]["titular"] == "Fabio" assert resp[0]["_metadata"]["branch"] == "master" assert resp[0]["saldo"] == 10000 assert resp[1]["titular"] == "Moneda" assert resp[1]["_metadata"]["branch"] == "master" assert resp[1]["saldo"] == 100
def test_destroy_data(session, test_client): origem = conta(titular="Fabio", saldo=10000) destino = conta(titular="Moneda", saldo=100) session.add_all([origem, destino]) session.commit() with patch.object(HttpClient, 'get', return_value=apicore_map()) as mock_method: with patch.object(BranchLink, 'get_links', return_value=[]) as mock_method1: with patch.object(BranchLink, 'save', return_value=[]) as mock_method2: uri = f'/Conta/Conta?filter=transferencia&origem={origem.id}&destino={destino.id}' status_code, resp = test_client.get_json(uri) destroyed_id = resp[0]["id"] resp[0]["_metadata"]["changeTrack"] = "destroy" body = json.dumps([resp[0]]) assert len(resp) == 2 response = test_client.post('/Conta/persist', follow_redirects=True, data=body, content_type='application/json') assert response.status_code == 200 status_code, resp = test_client.get_json(uri) assert status_code == 200 assert len(resp) == 1 assert resp[0]["id"] != destroyed_id uri = f'/Conta/Conta' status_code, resp = test_client.get_json(uri) assert response.status_code == 200 assert status_code == 200 assert len(resp) == 1 assert resp[0]["id"] != destroyed_id
def test_save_conta(session): c = conta(saldo=1, titular="Teste") session.add(c) session.commit() assert session.query(conta).count() == 1