forked from codecov/example-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestsArchive.py
More file actions
114 lines (97 loc) · 4.34 KB
/
testsArchive.py
File metadata and controls
114 lines (97 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import unittest
from mock import patch
import awesome
# import example_pkg
# 12.1.2020 something close to functional programming
# map, reduce and filter operations
# https://stackoverflow.com/questions/31127824/what-are-pythons-equivalent-of-javascripts-reduce-map-and-filter
# for dataframe there is this functionality build in
# DataFrame.apply, DataFrame.applymap
# datasets for cleaning exercises: https://makingnoiseandhearingthings.com/2018/04/19/datasets-for-data-cleaning-practice/
# 11.1.2020 utest and related.
# from app.mocking import test_method
# for mocking to work you need to import the function to be tested to the same namespace with unit tests!!!
# this is wy below from-sentences are made.
# that is my understanding, untested.
from example_pkg import laughingChain
from example_pkg import laughingChainPara
from example_pkg import funcSameNameSpace
# if testing a function in the same namespace, it does not work!!! trying to find solution...
# https://stackoverflow.com/questions/46464947/python-function-not-using-the-mocked-object
# possible solution maybe here https://docs.python.org/3/library/unittest.mock.html#where-to-patch
from example_pkg import foo_func
from example_pkg import bar_func
from dataTransforming import transformationForData
from dataTransforming import transformationTestingWithApply
from functools import reduce
import pandas as pd
import numpy as np
# import pipeLine
url = 'http://api.worldbank.org/v2/country/all/indicator/SP.POP.TOTL?format=json'
class TestMethods(unittest.TestCase):
def test_add(self):
# call to function changing some "global" data structure
# assert function is checking that the change you expected happens properly.
self.assertEqual(awesome.smile(), ":)")
def test_anotherOne(self):
barres = bar_func()
self.assertRegexpMatches(barres,".*bar.*")
#def test_pipeline(self):
# result = pipeLine.getData(url)
# self.assertRegexpMatches(result,".*bar.*")
def test_laugh(self):
reslau = awesome.laughing()
self.assertEqual(reslau, ":D")
# function to be mocked in patch macro
# seems to be so that patched function can not be in the same namespace (package) as the one calling it.
@patch('awesome.laughing')
def test_modulefunction(self,test_patch):
test_patch.return_value = 1
result = laughingChain()
self.assertEqual(result,2)
@patch('awesome.laughingPara')
def test_parameterfunc(self, test_para):
test_para.return_value = "HUI"
result = laughingChainPara()
self.assertEqual(result,"testausta")
@patch('example_pkg.foo_func')
def test_samemodule(self, test_para):
test_para.return_value = "SAME NAMESPACE"
result = funcSameNameSpace()
self.assertEqual(result,"testausta")
def test_playdata(self):
#https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas first version
#
from functools import reduce
import pandas as pd
import numpy as np
df = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]})
for index, row in df.iterrows():
print(row['c1'], row['c2'])
df.applymap
def test_framingApply(self):
#https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas first version
#
df = pd.DataFrame({'c1': [10, 11, 12], 'c2': [100, 110, 120]})
for index, row in df.iterrows():
print(row['c1'], row['c2'])
df.apply(transformationForData, axis=0, raw=False, result_type=None, args=())
#DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwds)
#transformingMain("testing")
bar_func()
def test_sanity1(self):
result = bar_func()
result = transformationForData("testing")
self.assertEqual(result,"testausta")
def test_sanity2(self):
# preparing data for exploring
df = pd.DataFrame([[4, 9]] * 3, columns=['A', 'B'])
# exploring
print(df)
print(df.apply(np.sqrt))
print("after sqrt, apply-function returns changed copy of the dataFrame")
print("testing imported function")
print(df.apply(transformationTestingWithApply))
print("after apply")
if __name__ == '__main__':
unittest.main()