Skip to content

Mocking inputs #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 50 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
db33738
03-sum_of_three_numbers
kiddopro Feb 17, 2022
a2e65fa
36-net-amount
kiddopro Feb 17, 2022
5c3c087
28-sequence-of-lines
kiddopro Feb 18, 2022
1e56c0c
34-a_aa_aaa_aaaa
kiddopro Feb 18, 2022
761a765
32-numbers-of-letters-and-digits
kiddopro Feb 18, 2022
22300ef
26-two-dimensional-array
kiddopro Feb 21, 2022
d8caa13
04-area_of_right_triangle
kiddopro Feb 22, 2022
13b353e
04-area_of_right_triangle
kiddopro Feb 22, 2022
bc361f3
23-list-and-tuple
kiddopro Feb 22, 2022
0e0ac83
23-list-and-tuple
kiddopro Feb 22, 2022
4ec0767
23-list-and-tuple
kiddopro Feb 22, 2022
9469d42
22-Integral
kiddopro Mar 1, 2022
4550580
22-Integral
kiddopro Mar 1, 2022
ea946f5
27-sequence-of-words
kiddopro Mar 1, 2022
053a129
22-Integral
kiddopro Mar 1, 2022
6f628d3
22-Integral
kiddopro Mar 1, 2022
a508936
29-remove-duplicate-words
kiddopro Mar 2, 2022
64b440b
38-sort-tuples-ascending
kiddopro Mar 2, 2022
67b490d
30-divisable-binary
kiddopro Mar 2, 2022
ae4f049
25-print-formula
kiddopro Mar 2, 2022
4522ebd
41-frequency-of-words
kiddopro Mar 2, 2022
8d6b8f2
40-compute-robot-distance
kiddopro Mar 2, 2022
c779071
37-validity-of-password
kiddopro Mar 2, 2022
27fad6b
03-sum_of_three_numbers
kiddopro Mar 2, 2022
c9e120a
04-area_of_right_triangle
kiddopro Mar 2, 2022
4eee53d
22-Integral
kiddopro Mar 2, 2022
3cfade0
22-Integral
kiddopro Mar 2, 2022
d9ca401
23-list-and-tuple
kiddopro Mar 2, 2022
4d54a6f
25-print-formula
kiddopro Mar 2, 2022
7fe22c2
26-two-dimensional-array
kiddopro Mar 2, 2022
566ca02
27-sequence-of-words
kiddopro Mar 2, 2022
5abacb1
29-remove-duplicate-words
kiddopro Mar 2, 2022
22abad5
30-divisable-binary
kiddopro Mar 2, 2022
02783c9
28-sequence-of-lines
kiddopro Mar 2, 2022
e1c9bae
32-numbers-of-letters-and-digits
kiddopro Mar 2, 2022
47f7952
34-a_aa_aaa_aaaa
kiddopro Mar 2, 2022
df4bdde
36-net-amount
kiddopro Mar 2, 2022
d48fbe5
37-validity-of-password
kiddopro Mar 2, 2022
8a2ca9e
38-sort-tuples-ascending
kiddopro Mar 2, 2022
7e44ea7
40-compute-robot-distance
kiddopro Mar 2, 2022
1619ee4
22-Integral
kiddopro Mar 2, 2022
7474de9
04-area_of_right_triangle
kiddopro Mar 2, 2022
ca7e540
Update app.py
tommygonzaleza Mar 8, 2022
df75263
Modifying the test and exercise in order to work without input
tommygonzaleza Mar 8, 2022
dd62b9a
ex 03, 04, 22, 25, 26, 27 changed
kiddopro Mar 9, 2022
1964c64
ex 28, 29. 30, 32, 34, 36, 37 changed
kiddopro Mar 10, 2022
860e432
ex 38, 40
kiddopro Mar 10, 2022
319bef7
Improved app.py of third exercise
tommygonzaleza Mar 10, 2022
8615164
Added solution.hide.py of exercise 25.
tommygonzaleza Mar 10, 2022
e1b97bd
conflict solved
tommygonzaleza Jul 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions exercises/002-sum_of_three_numbers/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Sum all three input numbers and print on the console the result
first_number = input("First input")
second_number = input("Second input")
third_number = input("Third input")
first_number = int(input("First input"))
second_number = int(input("Second input"))
third_number = int(input("Third input"))
# print here the sum of three inputs

print(first_number+second_number)
25 changes: 17 additions & 8 deletions exercises/002-sum_of_three_numbers/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,32 @@ def test_use_print():
regex = re.compile(r"print(\s)*\(")
assert bool(regex.search(content)) == True

@pytest.mark.it('Almost there! But you need to convert the incoming input from string to integer')
def test_convert_inputs(capsys, app):
@pytest.mark.it('The solution should return the expected output. Tested with 2, 3, 6')
def test_sum_expected_inputs(capsys, app):

fake_input = ["2","3","4"] #fake input
fake_input = ["2","3","6"] #fake input
with mock.patch('builtins.input', lambda x: fake_input.pop()):
app()
captured = capsys.readouterr()
assert captured.out != "432\n"
assert captured.out == "11\n"

@pytest.mark.it('Sum all three input numbers and print on the console the result')
def test_add_variables(capsys, app):
@pytest.mark.it('The solution must sum all entries. Tested with 0, 3, 7')
def test_sum_another_inputs(capsys, app):

fake_input = ["2","3","4"] #fake input
fake_input = ["0","3","7"] #fake input
with mock.patch('builtins.input', lambda x: fake_input.pop()):
app()
captured = capsys.readouterr()
assert captured.out == "9\n"
assert captured.out == "10\n"

@pytest.mark.it('The solution must sum all entries. Tested with 0, 0, 0')
def test_sum_with_zero(capsys, app):

fake_input = ["0","0","0"] #fake input
with mock.patch('builtins.input', lambda x: fake_input.pop()):
app()
captured = capsys.readouterr()
assert captured.out == "0\n"



Expand Down
5 changes: 1 addition & 4 deletions exercises/003-area_of_right_triangle/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#Complete the function to return the area of the triangle.
b = input("Size of B")
h = input("Size of H")

def area_of_triangle(arg1, arg2):
#your code here, please remove the "None"
return None

# Testing your function
print(area_of_triangle(b, h))
print(area_of_triangle(3, 5))
24 changes: 14 additions & 10 deletions exercises/003-area_of_right_triangle/test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import io, sys, os, pytest, json, mock
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'

@pytest.mark.it('Calculate the area of the triangle.')
def test_area_of_triangle(stdin):

_stdin = json.loads(stdin)
with mock.patch('builtins.input', lambda x: _stdin.pop(0)):

sys.stdout = buffer = io.StringIO()
@pytest.mark.it('The function area_of_triangle should exist')
def test_function_exists(capsys):
try:
import app
_stdin = json.loads(stdin)
result = int(_stdin[0]) * int(_stdin[1]) / 2
assert buffer.getvalue() == str(result) + "\n"

app.area_of_triangle
except AttributeError:
raise AttributeError("The function 'area_of_triangle' should exist on app.py")

@pytest.mark.it('The solution should return the expected output. Testing with 3 and 5')
def test_convert_inputs(capsys, app):
result = app.area_of_triangle(3, 5)
assert result == 7.5

@pytest.mark.it('The solution should return the expected output. Testing with 4 and 6')
def test_convert_inputs(capsys, app):
result = app.area_of_triangle(4, 6)
assert result == 12
2 changes: 0 additions & 2 deletions exercises/004-hello_harry/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ def hello_name(name):

return None



#Invoke the function with your name as the function's argument.
print(hello_name("Bob"))
11 changes: 6 additions & 5 deletions exercises/22-Integral/solution.hide.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
n=int(raw_input())
d=dict()
for i in range(1,n+1):
d[i]=i*i
def generate_dict(n):
d=dict()
for i in range(1,n+1):
d[i]=i*i
return d

print d
print(generate_dict(n))
19 changes: 19 additions & 0 deletions exercises/22-Integral/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest,os,re,io,sys, mock, json

@pytest.mark.it('The function generate_dict should exist')
def test_function_existence(capsys, app):
assert app.generate_dict

@pytest.mark.it('The function should return a dictionary')
def test_typeof_return(capsys, app):
assert type(app.generate_dict(7)) == type({})

@pytest.mark.it('The function should return the expected output')
def test_expected_output(capsys, app):
assert app.generate_dict(8) == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}

@pytest.mark.it('The function should work with other entries')
def test_another_entry_5(capsys, app):
assert app.generate_dict(5) == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}


11 changes: 8 additions & 3 deletions exercises/23-list-and-tuple/solution.hide.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
values=raw_input()
values=input("")
l=values.split(",")
t=tuple(l)
print l
print t
print (l)
print (t)

#
values=input("")
t=tuple(values.split(','))
print(t)
20 changes: 20 additions & 0 deletions exercises/23-list-and-tuple/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import io, sys, os, pytest, json, mock
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'


@pytest.mark.it('The output should be the expected for this solution')
def test_output(capsys, app):
fake_input = ["34,67,55,33,12,98"] #fake input
with mock.patch('builtins.input', lambda x: fake_input.pop()):
app()
captured = capsys.readouterr()
assert captured.out == "['34', '67', '55', '33', '12', '98']\n" or "('34', '67', '55', '33', '12', '98')\n"

@pytest.mark.it('The solution should work with other parameters')
def test_output_2(capsys, app):
fake_input = ["11,56,23,12,02"] #fake input
with mock.patch('builtins.input', lambda x: fake_input.pop()):
app()
captured = capsys.readouterr()
assert captured.out == "['11', '56', '23', '12', '02']\n" or "('11', '56', '23', '12', '02')\n"

11 changes: 3 additions & 8 deletions exercises/25-print-formula/solution.hide.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#!/usr/bin/env python
import math
c=50
h=30
value = []
items=[x for x in raw_input().split(',')]
for d in items:
value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))
def print_formula(num):
return round(math.sqrt(2*50*num/30))

print ','.join(value)
print(print_formula(5))
15 changes: 15 additions & 0 deletions exercises/25-print-formula/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest, io, sys, json, mock, re, os
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'

@pytest.mark.it('The function print_formula must exist')
def test_function_existence(capsys, app):
assert app.print_formula

@pytest.mark.it('The solution should work as expected')
def test_expected_output(capsys, app):
assert app.print_formula(100,150,180) == "18,22,24"

@pytest.mark.it('The solution should work as expected')
def test_another_output(capsys, app):
assert app.print_formula(200,90,300) == "26,17,32"

18 changes: 9 additions & 9 deletions exercises/26-two-dimensional-array/solution.hide.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
input_str = raw_input()
dimensions=[int(x) for x in input_str.split(',')]
rowNum=dimensions[0]
colNum=dimensions[1]
multilist = [[0 for col in range(colNum)] for row in range(rowNum)]
def two_dimensional_array(nList, nElements):
dimensions=[int(x) for x in "{},{}".format(nList,nElements).split(',')]
rowNum=dimensions[0]
colNum=dimensions[1]
multilist = [[0 for col in range(colNum)] for row in range(rowNum)]

for row in range(rowNum):
for col in range(colNum):
multilist[row][col]= row*col
for row in range(rowNum):
for col in range(colNum):
multilist[row][col]= row*col

print multilist
return (multilist)
18 changes: 18 additions & 0 deletions exercises/26-two-dimensional-array/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

import pytest,os,re,io,sys, mock, json

@pytest.mark.it('The function two_dimensional_array must exist')
def test_function_existence(capsys, app):
assert app.two_dimensional_array

@pytest.mark.it('The function should return the expected output.')
def test_expected_output(capsys, app):
assert app.two_dimensional_array(3,5) == [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]

@pytest.mark.it('The function should work with other entries. Testing with 2,7')
def test_expected_output(capsys, app):
assert app.two_dimensional_array(2,7) == [[0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6]]

@pytest.mark.it('The function should work with other entries. Testing with 2,7')
def test_expected_output(capsys, app):
assert app.two_dimensional_array(1,10) == [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
7 changes: 4 additions & 3 deletions exercises/27-sequence-of-words/solution.hide.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
items=[x for x in raw_input().split(',')]
items.sort()
print ','.join(items)
def sequence_of_words(words):
items=[x for x in "{}".format(words).split(',')]
items.sort()
return (','.join(items))
14 changes: 14 additions & 0 deletions exercises/27-sequence-of-words/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest, io, sys, json, mock, re, os
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'

@pytest.mark.it('The function sequence_of_words must exist')
def test_function_existence(capsys, app):
assert app.sequence_of_words

@pytest.mark.it('The function should return the expected output')
def test_function_existence(capsys, app):
assert app.sequence_of_words("bag,hello,without,world") == "bag,hello,without,world"

@pytest.mark.it('The function should return the expected output')
def test_function_existence(capsys, app):
assert app.sequence_of_words("No,pain,gain") == "No,gain,pain"
12 changes: 3 additions & 9 deletions exercises/28-sequence-of-lines/solution.hide.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
lines = []
while True:
s = raw_input()
if s:
lines.append(s.upper())
else:
break;
def lines(text):
return text.upper()

for sentence in lines:
print sentence
print(lines('Hello world'))
14 changes: 14 additions & 0 deletions exercises/28-sequence-of-lines/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest, io, sys, json, mock, re, os
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'

@pytest.mark.it('The function lines must exist')
def test_function_existence(capsys, app):
assert app.lines

@pytest.mark.it('The function should return the expected output')
def test_expected_output(capsys, app):
assert app.lines("hello world") == "HELLO WORLD"

@pytest.mark.it('The function should return the expected output')
def test_another_output(capsys, app):
assert app.lines("LeT the WOrld know YoU") == "LET THE WORLD KNOW YOU"
6 changes: 3 additions & 3 deletions exercises/29-remove-duplicate-words/solution.hide.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
s = input()
words = [word for word in s.split(" ")]
print (" ".join(sorted(list(set(words)))))
def remove_duplicate_words(text):
words = text.split()
return (" ".join(sorted(list(set(words)))))
21 changes: 15 additions & 6 deletions exercises/29-remove-duplicate-words/test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import pytest, io, sys, json, mock, re, os

@pytest.mark.it('Your solution should work as expected')
@pytest.mark.it('The function remove_duplicate_words must exist')
def test_function_existence(capsys, app):
app.remove_duplicate_words

@pytest.mark.it('The function should return the expected output')
def test_expected_output(capsys, app):
fake_input=['Hola como Hola']
with mock.patch('builtins.input', lambda x: fake_input.pop()):
app()
captured = capsys.readouterr()
assert captured.out == " como \n"
assert app.remove_duplicate_words("hello world and practice makes perfect and hello world again") == "again and hello makes perfect practice world"

@pytest.mark.it('The function should work with other entries')
def test_expected_output_2(capsys, app):
assert app.remove_duplicate_words("lets try this again with another try") == "again another lets this try with"

@pytest.mark.it('The function should work with other entries')
def test_expected_output_3(capsys, app):
assert app.remove_duplicate_words("Jacke was Named Jacke by his mother") == "Jacke Named by his mother was"

15 changes: 8 additions & 7 deletions exercises/30-divisable-binary/solution.hide.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
value = []
items=[x for x in raw_input().split(',')]
for p in items:
intp = int(p, 2)
if not intp%5:
value.append(p)
def divisable_binary(text):
value=[]
items=[x for x in text.split(',')]
for p in items:
intp = int(p, 2)
if not intp%5:
value.append(p)

print ','.join(value)
return (','.join(value))
17 changes: 17 additions & 0 deletions exercises/30-divisable-binary/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest, io, sys, json, mock, re, os

@pytest.mark.it('The function divisable_binary must exist')
def test_function_existence(capsys, app):
assert app.divisable_binary

@pytest.mark.it('The function should return the expected output')
def test_expected_output(capsys, app):
assert app.divisable_binary("0100,0011,1010,1001") == "1010"

@pytest.mark.it('The function should work with other parameters. testing with 1111,1000,0101,0000')
def test_expected_output_2(capsys, app):
assert app.divisable_binary("1111,1000,0101,0000") == "1111,0101,0000"

@pytest.mark.it("The function should work with other parameters. Testing with 1000")
def test_expected_output_3(capsys, app):
assert app.divisable_binary("1000,1000,1000,1000") == ""
Loading