From f34c6acfb62151382bed985e9faa0bed50b77ac1 Mon Sep 17 00:00:00 2001 From: kehiy Date: Mon, 15 Jul 2024 15:44:40 +0330 Subject: [PATCH] refactor: python normal test --- test/README.md | 19 ++++ test/normal_test.py | 253 ++++++++++++++++++++++++++------------------ 2 files changed, 171 insertions(+), 101 deletions(-) diff --git a/test/README.md b/test/README.md index 7ae0d95..6744acc 100644 --- a/test/README.md +++ b/test/README.md @@ -10,3 +10,22 @@ Here is a list of different tests which must be added here and their status: | -------- | ------- | | Normal test | ✅ | | Concurrent test | ⚒️ | + +## Test writing guide + +All of test must be in form of functions. The functions name must start with keyword `test` and finish with the result of test such as `ok`, `ssnf`, `snf`, `invalid` and so on. + +Example of function name: + +Correct: `test_new_sub_set_ok` +Incorrect: `new_sub_set_test` +Incorrect: `new_subset_test` +Incorrect: `test_new_sub_set` + +> [!NOTE] +> Consider following python naming convention and code style. + +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] +> Codes in this directory are not about benchmark, these are only for testing functionality of project, for benchmark please check benchmark directory. diff --git a/test/normal_test.py b/test/normal_test.py index 96238a8..d9ef148 100644 --- a/test/normal_test.py +++ b/test/normal_test.py @@ -9,140 +9,191 @@ st = time.time() +#? Global variables +sub_set_names = [] +set_names = [] +elements_value = [] + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -server_address = ('localhost', 7070) # You can change port and host for testing. +server_address = ('localhost', 7070) #! You can change port and host for testing. sock.connect(server_address) -#? TQL command to connect to the server. -query = "CON root super_secret_password" #! Considers you config is default. -response = utils.make_query(query, sock) +def test_connect_ok(): + #* TQL command to connect to the server. + query = "CON root super_secret_password" #! Considers you config is default. + response = utils.make_query(query, sock) -assert response == "OK" -print(f"Received response: {response}, Connection test Passed!") + assert response == "OK", f"\033[91mCan't connect to server with correct info, received {response}, expected OK\033[0m" -#? Ping database. -assert utils.make_query("PING", sock) == "PONG" -print(f"Received response: {response}, We are connected!") + print(f"\033[32mReceived response: {response}, Connection test Passed.\033[32m") -#* Creating some random sets. -set_names = [] -for i in range(10): - set_names.append(utils.get_random_string(i+2)) - query = f"SET {set_names[i]}" - - response = utils.make_query(query, sock) - assert response == "OK" +def test_ping_ok(): + #* Ping database. + response = utils.make_query("PING", sock) -print(f"Received response: {response}, Created {len(set_names)} sets successfully.") + assert response == "PONG", f"\033[91mCan't ping the database, received {response}, expected PONG\033[0m" -#* Creating some random sub sets. -sub_set_names = [] -for s in set_names: - for i in range(7): - sub_set_names.append(utils.get_random_string(i+2)) + print(f"\033[32mReceived response: {response}, We are connected!\033[32m") + +def test_new_set_ok(): + #* Creating some random sets. + for i in range(10): + set_names.append(utils.get_random_string(i+2)) - query = f"SSET {s} {sub_set_names[i]}" + query = f"SET {set_names[i]}" response = utils.make_query(query, sock) - assert response == "OK" -print(f"Received response: {response}, Created {len(sub_set_names)} sub sets successfully.") + assert response == "OK", f"\033[91mCan't create set {set_names[i]}, received {response}, expected OK\033[0m" -#* Pushing some random elements. -set_index = 0 -elements_value = [] -for s in set_names: - for i in range(7): - for _ in range(1_000): - element_value = utils.get_random_string(i+8) - elements_value.append(element_value) + print(f"\033[32mReceived response: {response}, Created {len(set_names)} sets successfully.\033[32m") - query = f"PUSH {s} {sub_set_names[i]} {element_value} {int(time.mktime(time.gmtime()))}" - +def test_new_sub_set_ok(): + #* Creating some random sub sets. + for s in set_names: + for i in range(7): + sub_set_names.append(utils.get_random_string(i+2)) + + query = f"SSET {s} {sub_set_names[i]}" response = utils.make_query(query, sock) - assert response == "OK" - - set_index += 7 -print(f"Received response: {response}, Created {len(elements_value)} elements pushed successfully.") + assert response == "OK", f"\033[91mCan't create sub set {sub_set_names[i]}, received {response}, expected OK\033[0m" -#* Test count -response = utils.make_query("CNTS", sock) -assert response == str(len(set_names)) -print(f"Received response: {response}, Sets number counted successfully.") + print(f"\033[32mReceived response: {response}, Created {len(sub_set_names)} sub sets successfully.\033[32m") -sub_sets_count = 0 -for s in set_names: - query = f"CNTSS {s}" - response = utils.make_query(query, sock) - sub_sets_count += int(response) +def test_push_element_ok(): + #* Pushing some random elements. + set_index = 0 + for s in set_names: + for i in range(7): + for _ in range(1_000): + element_value = utils.get_random_string(i+8) + elements_value.append(element_value) -assert sub_sets_count == len(sub_set_names) -print(f"Received response: {sub_sets_count}, SubSets number counted successfully.") + query = f"PUSH {s} {sub_set_names[i]} {element_value} {int(time.mktime(time.gmtime()))}" + response = utils.make_query(query, sock) -set_index = 0 -elements_count = 0 -for s in set_names: - for i in range(7): - query = f"CNTE {s} {sub_set_names[i]}" - + assert response == "OK", f"\033[91mCan't push element {elements_value[i]}, received {response}, expected OK\033[0m" + + set_index += 7 + + print(f"\033[32mReceived response: {response}, Created {len(elements_value)} elements pushed successfully.\033[32m") + +def test_count_sets_ok(): + #* Test counting sets + 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" + + print(f"\033[32mReceived response: {response}, Sets number counted successfully.\033[32m") + +def test_count_sub_sets_ok(): + #* Test Counting sub sets + sub_sets_count = 0 + + for s in set_names: + query = f"CNTSS {s}" response = utils.make_query(query, sock) - elements_count += int(response) - - set_index += 7 - -assert elements_count == len(elements_value) -print(f"Received response: {elements_count}, Elements number counted successfully.") - -#* Test clean sub sets -set_index = 0 -for s in set_names: - for i in range(7): - query = f"CLNSS {s} {sub_set_names[i]}" + + 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" + + print(f"\033[32mReceived response: {sub_sets_count}, SubSets number counted successfully.\033[32m") + +def test_count_elements_ok(): + #* Test counting all elements + set_index = 0 + elements_count = 0 + + for s in set_names: + for i in range(7): + query = f"CNTE {s} {sub_set_names[i]}" + response = utils.make_query(query, sock) + + elements_count += int(response) - response = utils.make_query(query, sock) - assert response == "OK" - - set_index += 7 + set_index += 7 + + 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") + +def test_clean_sub_sets_elements_ok(): + #* Test clean sub sets + 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) -print(f"Received response: {response}, All Subset elements cleaned successfully.") + assert response == "OK", f"\033[91mCan't clean sub sets, received {response}, expected OK\033[0m" + set_index += 7 -#* Test drop subsets -set_index = 0 -for s in set_names: - for i in range(7): - query = f"DRPSS {s} {sub_set_names[i]}" + print(f"\033[32mReceived response: {response}, All Subset elements cleaned successfully.\033[32m") + +def test_drop_sub_sets_ok(): + #* Test drop subsets + 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" + set_index += 7 + + print(f"\033[32mReceived response: {response}, All Subsets dropped successfully.\033[32m") + +def test_clean_sub_sets_ok(): + #* Test clean sets + for s in set_names: + query = f"CLNS {s}" response = utils.make_query(query, sock) - assert response == "OK" - - set_index += 7 -print(f"Received response: {response}, All Subset dropped successfully.") + assert response == "OK", f"\033[91mCan't clean sets, received {response}, expected OK\033[0m" -#* Test clean sets -for s in set_names: - query = f"CLNS {s}" + print(f"\033[32mReceived response: {response}, All Subsets cleaned successfully.\033[32m") - response = utils.make_query(query, sock) - assert response == "OK" +def test_drop_sets_ok(): + #* Test drop sets + for s in set_names: + query = f"DRPS {s}" + response = utils.make_query(query, sock) -print(f"Received response: {response}, All Subsets cleaned successfully.") + assert response == "OK", f"\033[91mCan't drop sets, received {response}, expected OK\033[0m" -#* Test drop sets -for s in set_names: - query = f"DRPS {s}" + print(f"\033[32mReceived response: {response}, All Sets dropped successfully.\033[32m") - response = utils.make_query(query, sock) - assert response == "OK" +def test_clean_sets_ok(): + #* Clean all sets. + response = utils.make_query("CLN", sock) + + 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"Received response: {response}, All Sets dropped successfully.") -#* Clean all sets. -response = utils.make_query("CLN", sock) -assert response == "OK" -print(f"Received response: {response}, All Sets cleaned successfully.") +def main(): + test_connect_ok() + test_ping_ok() + test_new_set_ok() + test_new_sub_set_ok() + test_push_element_ok() + test_count_sets_ok() + test_count_sub_sets_ok() + test_count_elements_ok() + test_clean_sub_sets_elements_ok() + test_drop_sub_sets_ok() + test_clean_sub_sets_ok() + test_drop_sets_ok() + test_clean_sets_ok() -sock.close() -print('All test successfully passed in:', time.time() - st, 'seconds') +if __name__ == "__main__": + main() + sock.close() + print('\033[34mAll test successfully passed in:\033[34m', time.time() - st, 'seconds')