Terarea  2
The automation project
Loading...
Searching...
No Matches
tests_sql_bind.py
Go to the documentation of this file.
1"""_summary_
2 File in charge of testing the boilerplate Boilerplate non http class.
3"""
4import os
5import sys
6from datetime import datetime
7import constants as TCONST
8
9
10import pytest
11
12sys.path.append(os.getcwd())
13
14try:
15 from src.lib.sql.sql_manager import SQL
16 from src.lib.components import constants as CONST
17except ImportError as e:
18 raise ImportError("Failed to import the src module") from e
19
20ERROR = TCONST.ERROR
21SUCCESS = TCONST.SUCCESS
22DEBUG = TCONST.DEBUG
23
24SI = SQL(
25 url=CONST.DB_HOST,
26 port=CONST.DB_PORT,
27 username=CONST.DB_USER,
28 password=CONST.DB_PASSWORD,
29 db_name=CONST.DB_DATABASE,
30 debug=DEBUG
31)
32
33
34def _datetime_to_date_str(date: datetime) -> str:
35 """_summary_
36 Function in charge of converting a datetime object to a string.
37 """
38 return date.strftime("%Y-%m-%d")
39
40
41def _datetime_to_datetime_str(date: datetime) -> str:
42 """_summary_
43 Function in charge of converting a datetime object to a string.
44 """
45 return date.strftime("%Y-%m-%d %H:%M:%S")
46
47
48def _datetime_to_sql_time_str(date: datetime) -> str:
49 """_summary_
50 Function in charge of converting a datetime object to a string.
51 """
52 microsecond = date.strftime("%f")[:3]
53 converted_time = date.strftime("%Y-%m-%d %H:%M:%S")
54 return f"{converted_time}.{microsecond}"
55
56
57def _str_to_datetime(string: str) -> datetime:
58 """_summary_
59 Function in charge of converting a string to a datetime object.
60 """
61 return datetime.strptime(string, "%Y-%m-%d %H:%M:%S")
62
63
64def _str_to_date(string: str) -> datetime:
65 """_summary_
66 Function in charge of converting a string to a datetime object.
67 """
68 return datetime.strptime(string, "%Y-%m-%d")
69
70
72 """_summary_
73 Function in charge of testing the datetime_to_string function.
74 """
75 test = datetime.now()
76 test_data = SI.datetime_to_string(test, date_only=False, sql_mode=False)
77 correct_data = _datetime_to_datetime_str(test)
78 assert test_data is not None
79 assert test_data == correct_data
80
81
83 """_summary_
84 Function in charge of testing the datetime_to_string function.
85 """
86 test = datetime.now()
87 test_data = SI.datetime_to_string(test, date_only=False, sql_mode=True)
88 correct_data = _datetime_to_sql_time_str(test)
89 assert test_data is not None
90 assert test_data == correct_data
91
92
94 """_summary_
95 Function in charge of testing the datetime_to_string function.
96 """
97 test = datetime.now()
98 test_data = SI.datetime_to_string(test, date_only=True, sql_mode=False)
99 correct_data = _datetime_to_date_str(test)
100 assert test_data is not None
101 assert test_data == correct_data
102
103
105 """_summary_
106 Function in charge of testing the datetime_to_string function.
107 """
108 test = datetime.now()
109 test_data = SI.datetime_to_string(test, date_only=True, sql_mode=True)
110 correct_data = _datetime_to_date_str(test)
111 assert test_data is not None
112 assert test_data == correct_data
113
114
116 """_summary_
117 Function in charge of testing the datetime_to_string function.
118 """
119 test = "test"
120 with pytest.raises(ValueError):
121 SI.datetime_to_string(test, date_only=False, sql_mode=False)
122
123
125 """_summary_
126 Function in charge of testing the datetime_to_string function.
127 """
128 test = "test"
129 with pytest.raises(ValueError):
130 SI.datetime_to_string(test, date_only=False, sql_mode=True)
131
132
134 """_summary_
135 Function in charge of testing the datetime_to_string function.
136 """
137
138 test = "test"
139 with pytest.raises(ValueError):
140 SI.datetime_to_string(test, date_only=True, sql_mode=False)
141
142
144 """_summary_
145 Function in charge of testing the datetime_to_string function.
146 """
147
148 test = "test"
149 with pytest.raises(ValueError):
150 SI.datetime_to_string(test, date_only=True, sql_mode=True)
151
152
154 """_summary_
155 Function in charge of testing the string_to_datetime function.
156 """
157 node = datetime.now()
158 test = _str_to_datetime(node)
159 test_data = SI.string_to_datetime(test, date_only=False)
160 assert test_data is not None
161 assert test_data == node
162
163
165 """_summary_
166 Function in charge of testing the string_to_datetime function.
167 """
168 node = datetime.now()
169 test = _str_to_date(node)
170 test_data = SI.string_to_datetime(test, date_only=True)
171 assert test_data is not None
172 assert test_data == node
173
174
176 """_summary_
177 Function in charge of testing the string_to_datetime function.
178 """
179 test = datetime.now()
180 with pytest.raises(ValueError):
181 SI.string_to_datetime(test, date_only=False)
182
183
185 """_summary_
186 Function in charge of testing the string_to_datetime function.
187 """
188
189 test = datetime.now()
190 with pytest.raises(ValueError):
191 SI.string_to_datetime(test, date_only=True)
192
193
195 """_summary_
196 Function in charge of testing the string_to_datetime function.
197 """
198 test = "test"
199 with pytest.raises(ValueError):
200 SI.string_to_datetime(test, date_only=False)
201
202
204 """_summary_
205 Function in charge of testing the string_to_datetime function.
206 """
207
208 test = "test"
209 with pytest.raises(ValueError):
210 SI.string_to_datetime(test, date_only=True)
datetime _str_to_datetime(str string)
None test_string_to_datetime_no_error()
None test_string_to_date_datetime_not_a_string_instance()
None test_datetime_to_datetime_string_not_a_datetime_instance()
str _datetime_to_date_str(datetime date)
datetime _str_to_date(str string)
None test_string_to_datetime_datetime_not_a_string_instance()
str _datetime_to_sql_time_str(datetime date)
None test_datetime_to_date_sql_string_not_a_datetime_instance()
None test_datetime_to_datetime_string_no_error()
None test_string_to_datetime_datetime_not_a_datetime_string_instance()
None test_datetime_to_date_string_not_a_datetime_instance()
None test_string_to_date_datetime_not_a_datetime_string_instance()
None test_datetime_to_date_sql_string_no_error()
None test_datetime_to_sql_string_not_a_datetime_instance()
None test_datetime_to_sql_string_no_error()
None test_string_to_date_datetime_no_error()
str _datetime_to_datetime_str(datetime date)
None test_datetime_to_date_string_no_error()