Skip to content

Commit c81c276

Browse files
Result of dojo 2018-10-06
1 parent 80c31fd commit c81c276

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

2018-10-06/commands.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
DB = {}
3+
4+
5+
def _init_db():
6+
DB.clear()
7+
8+
9+
def command_add(_id, first_name, last_name, birthday, phone_number):
10+
if _id in DB:
11+
return "ID " + str(_id) + " ja cadastrado."
12+
13+
DB[_id] = {
14+
'fn': first_name,
15+
'ln': last_name,
16+
'bd': birthday,
17+
'pn': phone_number
18+
}
19+
return ""
20+
21+
22+
def command_del(_id):
23+
try:
24+
DB.pop(_id)
25+
return ''
26+
except KeyError:
27+
return f'ID {_id} nao existente.'
28+
29+
30+
def command_info(_id):
31+
try:
32+
data = DB[_id]
33+
return ' '.join(data.values())
34+
except KeyError:
35+
return f'ID {_id} nao existente.'
36+
37+
38+
def command_query(query):
39+
# fn:Roberto
40+
result = []
41+
42+
tag, data_value = query.split(':')
43+
for key, value in DB.items():
44+
if value[tag] == data_value:
45+
result.append(key)
46+
47+
# result2 = []
48+
# for number in result:
49+
# result2.append(str(number))
50+
# return ' '.join(result2)
51+
52+
return ' '.join(str(number) for number in sorted(result))

2018-10-06/commands_test.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import pytest
2+
from commands import command_add, command_del, command_info, command_query, DB, _init_db
3+
4+
5+
@pytest.fixture
6+
def roberto():
7+
return [
8+
123,
9+
'Roberto',
10+
'Nascimento',
11+
'01/01/1960',
12+
'+55-21-0190-0190'
13+
]
14+
15+
16+
@pytest.fixture
17+
def rosangela():
18+
return [
19+
456,
20+
'Rosangela',
21+
'Nascimento',
22+
'01/01/1960',
23+
'+55-21-0190-0190'
24+
]
25+
26+
27+
def test_command_add(roberto):
28+
_init_db()
29+
assert command_add(*roberto) == ''
30+
31+
32+
def test_command_add_values(roberto):
33+
_init_db()
34+
roberto[0] = 124
35+
command_add(*roberto)
36+
assert len(DB) == 1
37+
38+
39+
def test_command_add_id(roberto):
40+
_init_db()
41+
roberto[0] = 125
42+
command_add(*roberto)
43+
assert 125 in DB
44+
45+
46+
def test_command_add_same(roberto):
47+
_init_db()
48+
assert command_add(*roberto) == ''
49+
assert command_add(*roberto) == 'ID 123 ja cadastrado.'
50+
51+
52+
def test_command_del(roberto):
53+
_init_db()
54+
command_add(*roberto)
55+
assert command_del(123) == ''
56+
57+
58+
def test_command_del_not_found_id():
59+
_init_db()
60+
assert command_del(123) == 'ID 123 nao existente.'
61+
62+
63+
def test_command_del_2(roberto):
64+
_init_db()
65+
command_add(*roberto)
66+
assert command_del(123) == ''
67+
assert command_del(123) == 'ID 123 nao existente.'
68+
69+
70+
def test_command_info(roberto):
71+
_init_db()
72+
command_add(*roberto)
73+
assert command_info(roberto[0]) == 'Roberto Nascimento 01/01/1960 +55-21-0190-0190'
74+
75+
76+
def test_command_info_not_found():
77+
_init_db()
78+
assert command_info(1) == 'ID 1 nao existente.'
79+
80+
81+
def test_command_query(roberto):
82+
_init_db()
83+
command_add(*roberto)
84+
assert command_query('fn:Roberto') == '123'
85+
86+
87+
def test_command_query_multiple(roberto, rosangela):
88+
_init_db()
89+
command_add(*roberto)
90+
command_add(*rosangela)
91+
assert command_query('ln:Nascimento') == '123 456'
92+
93+
94+
def test_command_query_multiple_unordered(roberto, rosangela):
95+
_init_db()
96+
command_add(*rosangela)
97+
command_add(*roberto)
98+
assert command_query('ln:Nascimento') == '123 456'

0 commit comments

Comments
 (0)