Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
refactor: python normal test
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Jul 15, 2024
1 parent a61e450 commit 55c4d0e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 40 deletions.
11 changes: 11 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ Incorrect: `test_new_sub_set`
> [!NOTE]
> Consider following python naming convention and code style.
Each function must have description such as:
```python
"""
Connecting to database and creating a session.
Testing: CON TQL command
Error: null
"""
```

The first line(s) are description of test. The testing part describes what exactly the function is testing. And the error describe which kind of error the tets must return, if it was an OK test, you can use null as its value.

Since in we use a connection a server with once instance, consider that sometimes order of commands can affect the result of test so make sure you do them in correct order and restart database each time. For example you can't clean a set when you dropped it!

> [!IMPORTANT]
Expand Down
136 changes: 96 additions & 40 deletions test/normal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#! GET method is not tested here now.
# TODO: writing test for method GET.
# TODO: adding test bad cases such as invalid set/subset name.
# TODO: popping items from global variables while dropping or cleaning them(?)

st = time.time()

Expand All @@ -20,75 +21,106 @@
sock.connect(server_address)

def test_connect_ok():
#* TQL command to connect to the server.
"""
Connecting to database and creating a session.
Testing: CON TQL command
Error: null
"""
query = "CON root super_secret_password" #! Considers you config is default.
response = utils.make_query(query, sock)

assert response == "OK", f"\033[91mCan't connect to server with correct info, received {response}, expected OK\033[0m"
assert response == "OK", f"\033[91mCan't connect to server with default info, received: {response}, expected: OK\033[0m"

print(f"\033[32mReceived response: {response}, Connection test Passed.\033[32m")

def test_ping_ok():
#* Ping database.
"""
Ping the database to check session health.
Testing: PING TQL command
Error: null
"""
response = utils.make_query("PING", sock)

assert response == "PONG", f"\033[91mCan't ping the database, received {response}, expected PONG\033[0m"
assert response == "PONG", f"\033[91mCan't ping the database, received: {response}, expected: PONG\033[0m"

print(f"\033[32mReceived response: {response}, We are connected!\033[32m")
print(f"\033[32mReceived response: {response}, we are connected!\033[32m")

def test_new_set_ok():
#* Creating some random sets.
"""
Creating random sets.
Testing: SET TQL command
Error: null
"""
for i in range(10):
set_names.append(utils.get_random_string_name(i+2))

query = f"SET {set_names[i]}"
response = utils.make_query(query, sock)

assert response == "OK", f"\033[91mCan't create set {set_names[i]}, received {response}, expected OK\033[0m"
assert response == "OK", f"\033[91mCan't create set: {set_names[i]}, received: {response}, expected: OK\033[0m"

print(f"\033[32mReceived response: {response}, Created {len(set_names)} sets successfully.\033[32m")
print(f"\033[32mReceived response: {response}, {len(set_names)} sets created successfully.\nSets: {set_names}\033[32m")

def test_new_sub_set_ok():
#* Creating some random sub sets.
"""
Creating random subsets for sets.
Testing: SSET command
Error: null
"""
for s in set_names:
for i in range(7):
sub_set_names.append(utils.get_random_string_name(i+2))

query = f"SSET {s} {sub_set_names[i]}"
response = utils.make_query(query, sock)

assert response == "OK", f"\033[91mCan't create sub set {sub_set_names[i]}, received {response}, expected OK\033[0m"
assert response == "OK", f"\033[91mCan't create subset: {sub_set_names[i]} in set {s}, received: {response}, expected: OK\033[0m"

print(f"\033[32mReceived response: {response}, Created {len(sub_set_names)} sub sets successfully.\033[32m")
print(f"\033[32mReceived response: {response}, {len(sub_set_names)} subsets created successfully.\nSubsets: {sub_set_names}\033[32m")

def test_push_element_ok():
#* Pushing some random elements.
"""
Pushing randomly generated elements in all created subsets.
Testing: PUSH TQL command
Error: null
"""
set_index = 0

for s in set_names:
for i in range(7):
for _ in range(1_000):
element_value = utils.get_random_string_name(i+8)
elements_value.append(element_value)

query = f"PUSH {s} {sub_set_names[i]} {element_value} {int(time.mktime(time.gmtime()))}"
element_time = int(time.mktime(time.gmtime()))
query = f"PUSH {s} {sub_set_names[i]} {element_value} {element_time}"
response = utils.make_query(query, sock)

assert response == "OK", f"\033[91mCan't push element {elements_value[i]}, received {response}, expected OK\033[0m"
assert response == "OK", f"\033[91mCan't push element with value of: {elements_value[i]} and time of: {element_time}, received: {response}, expected: OK\033[0m"

set_index += 7

print(f"\033[32mReceived response: {response}, Created {len(elements_value)} elements pushed successfully.\033[32m")
#? Change `elements_value[:10]` to get more or less elements.
print(f"\033[32mReceived response: {response}, {len(elements_value)} elements pushed successfully.\nElements: {elements_value[:10]}\033[32m")

def test_count_sets_ok():
#* Test counting sets
"""
Counting all sets.
Testing: CNTS TQL command
Error: null
"""
response = utils.make_query("CNTS", sock)

assert response == str(len(set_names)), f"\033[91mCan't count sets, received {response}, expected {len(set_names)}\033[0m"
assert response == str(len(set_names)), f"\033[91mCan't count sets, received: {response}, expected: {len(set_names)}\033[0m"

print(f"\033[32mReceived response: {response}, Sets number counted successfully.\033[32m")
print(f"\033[32mReceived response: {response}, sets number counted successfully.\033[32m")

def test_count_sub_sets_ok():
#* Test Counting sub sets
"""
Counting all subsets.
Testing: CNTSS TQL command
Error: null
"""
sub_sets_count = 0

for s in set_names:
Expand All @@ -97,12 +129,16 @@ def test_count_sub_sets_ok():

sub_sets_count += int(response)

assert sub_sets_count == len(sub_set_names), f"\033[91mCan't count sub sets, received {sub_sets_count}, expected {len(sub_set_names)}\033[0m"
assert sub_sets_count == len(sub_set_names), f"\033[91mCan't count subsets, received: {sub_sets_count}, expected: {len(sub_set_names)}\033[0m"

print(f"\033[32mReceived response: {sub_sets_count}, SubSets number counted successfully.\033[32m")
print(f"\033[32mReceived response: {sub_sets_count}, subsets counted successfully.\033[32m")

def test_count_elements_ok():
#* Test counting all elements
"""
Counting all elements in all subsets.
Testing: CNTE TQL command
Error: null
"""
set_index = 0
elements_count = 0

Expand All @@ -115,67 +151,87 @@ def test_count_elements_ok():

set_index += 7

assert elements_count == len(elements_value), f"\033[91mCan't count elements, received {elements_count}, expected {len(elements_value)}\033[0m"
assert elements_count == len(elements_value), f"\033[91mCan't count elements, received: {elements_count}, expected: {len(elements_value)}\033[0m"

print(f"\033[32mReceived response: {elements_count}, Elements number counted successfully.\033[32m")
print(f"\033[32mReceived response: {elements_count}, elements counted successfully.\033[32m")

def test_clean_sub_sets_elements_ok():
#* Test clean sub sets
"""
Cleaning all elements in all subsets.
Testing: CLNSS TQL command
Error: null
"""
set_index = 0

for s in set_names:
for i in range(7):
query = f"CLNSS {s} {sub_set_names[i]}"
response = utils.make_query(query, sock)

assert response == "OK", f"\033[91mCan't clean sub sets, received {response}, expected OK\033[0m"
assert response == "OK", f"\033[91mCan't clean subset: {sub_set_names[i]} of set {s}, received: {response}, expected: OK\033[0m"

set_index += 7

print(f"\033[32mReceived response: {response}, All Subset elements cleaned successfully.\033[32m")
print(f"\033[32mReceived response: {response}, subset elements cleaned successfully.\033[32m")

def test_drop_sub_sets_ok():
#* Test drop subsets
"""
Dropping all subsets.
Testing: DRPSS TQL command
Error: null
"""
set_index = 0

for s in set_names:
for i in range(7):
query = f"DRPSS {s} {sub_set_names[i]}"
response = utils.make_query(query, sock)

assert response == "OK", f"\033[91mCan't drop sub sets, received {response}, expected OK\033[0m"
assert response == "OK", f"\033[91mCan't drop subset: {sub_set_names[i]} from set: {s}, received: {response}, expected: OK\033[0m"

set_index += 7

print(f"\033[32mReceived response: {response}, All Subsets dropped successfully.\033[32m")
print(f"\033[32mReceived response: {response}, subsets dropped successfully.\033[32m")

def test_clean_sub_sets_ok():
#* Test clean sets
"""
Cleaning all subsets in all sets.
Testing: CLNS TQL command
Error: null
"""
for s in set_names:
query = f"CLNS {s}"
response = utils.make_query(query, sock)

assert response == "OK", f"\033[91mCan't clean sets, received {response}, expected OK\033[0m"
assert response == "OK", f"\033[91mCan't clean set: {s}, received: {response}, expected: OK\033[0m"

print(f"\033[32mReceived response: {response}, All Subsets cleaned successfully.\033[32m")
print(f"\033[32mReceived response: {response}, sets cleaned successfully.\033[32m")

def test_drop_sets_ok():
#* Test drop sets
"""
Dropping all sets.
Testing: DRPS TQL command
Error: null
"""
for s in set_names:
query = f"DRPS {s}"
response = utils.make_query(query, sock)

assert response == "OK", f"\033[91mCan't drop sets, received {response}, expected OK\033[0m"
assert response == "OK", f"\033[91mCan't drop set: {s}, received: {response}, expected: OK\033[0m"

print(f"\033[32mReceived response: {response}, All Sets dropped successfully.\033[32m")
print(f"\033[32mReceived response: {response}, sets dropped successfully.\033[32m")

def test_clean_sets_ok():
#* Clean all sets.
"""
Cleaning all sets.
Testing: CLN TQL command
Error: null
"""
response = utils.make_query("CLN", sock)

assert response == "OK", f"\033[91mCan't clean sets, received {response}, expected OK\033[0m"
assert response == "OK", f"\033[91mCan't clean sets, received: {response}, expected: OK\033[0m"

print(f"\033[32mReceived response: {response}, All Sets cleaned successfully.\033[32m")
print(f"\033[32mReceived response: {response},sets cleaned successfully.\033[32m")


def main():
Expand All @@ -196,4 +252,4 @@ def main():
if __name__ == "__main__":
main()
sock.close()
print('\033[34mAll test successfully passed in:\033[34m', time.time() - st, 'seconds')
print('\033[34mAll tests successfully passed in:\033[34m', time.time() - st, 'seconds')

0 comments on commit 55c4d0e

Please sign in to comment.