After setting up the test environments for the command line and the web app in the previous post, I now get started with some serious testing. To begin with, the code for this can be found in the testing branch in either of these two repositories:
https://sourceforge.net/p/pythonpowerelec/code/ci/testing/tree/
https://bitbucket.org/shivkiyer/ppe_simulator/branch/testing
I expanded the testing of csv_element_2D method to the testing of csv_element_2D and csv_tuple_2D as these two methods quite often go hand in hand since one converts a tuple to a string cell position and the vice versa. Here is the code:
There are two types of tests. First, the manual tests are tests where I know there could be a problem and these are the borderline cases - when for example 1Z becomes 1AA or 9A becomes 10A etc. Manual tests can be limited unless you have a lot of time or the function is very complicated and only manual tests are possible. In this case, automatic tests are also possible. For automatic tests, I randomly generate tuples of x (row) and y (column) positions and convert them into string cell positions using csv_element_2D and then convert the resultant cell positions back into tuples using csv_tuple_2D. I chose 20 such iterations and made asserts for each case.
The method can be extended to the Django web app with minor modifications - only the assert statement of pytest will be replaced by assertEqual of TestCase.
Next comes the csv_reader method that takes in a file object which is the .csv file and returns a 2D matrix (lists embedded within a list) representation of the circuit. This is the code:
It is important to write the desired circuit contents to a file. Simply passing the string contents to csv_reader as the argument will not work. This is because when Python opens a file, it creates an iterable object and so you can iterate line by line. However, if I pass a string, the iteration will happen character by character which is not what we want.
I didn't bother with circuit components or circuit topology because what matters here is the reading of the .csv file. A few errors are expected:
Will need to spend some thought on this as spreadsheet software in general are a bit messy and the need to generate .csv files in a particular form may not be obvious to the normal user.
https://sourceforge.net/p/pythonpowerelec/code/ci/testing/tree/
https://bitbucket.org/shivkiyer/ppe_simulator/branch/testing
I expanded the testing of csv_element_2D method to the testing of csv_element_2D and csv_tuple_2D as these two methods quite often go hand in hand since one converts a tuple to a string cell position and the vice versa. Here is the code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_network_cell_conversions(): | |
""" | |
Testing csv_element_2D and csv_tuple_2D | |
csv_element_2D - converts a numeric tuple to a string cell cell_position | |
csv_tuple_2D - converts a string cell position to a numeric tuple | |
""" | |
from network_reader import csv_element_2D, csv_tuple_2D | |
import random | |
print() | |
# Testing what can be humanly verified | |
print("Manual tests") | |
assert csv_element_2D([0, 26]) == "1AA" | |
assert csv_tuple_2D("1AA") == [0, 26] | |
print("Testing for co-ordinate {}, {} that comes to {}".format(0, 26, "1AA")) | |
assert csv_element_2D([11, 26]) == "12AA" | |
assert csv_tuple_2D("12AA") == [11, 26] | |
print("Testing for co-ordinate {}, {} that comes to {}".format(11, 26, "12AA")) | |
assert csv_element_2D([33, 52]) == "34BA" | |
assert csv_tuple_2D("34BA") == [33, 52] | |
print("Testing for co-ordinate {}, {} that comes to {}".format(33, 52, "34BA")) | |
print() | |
# Testing random entries and checking if reverse conversion works. | |
print("Random automatic tests") | |
for test_count in range(20): | |
x = random.randint(0, 200) | |
y = random.randint(0, 200) | |
cell_position = csv_element_2D([x, y]) | |
assert csv_tuple_2D(cell_position) == [x, y] | |
print("Testing for co-ordinate {}, {} that comes to {}".format(x, y, cell_position)) | |
print() | |
return |
The method can be extended to the Django web app with minor modifications - only the assert statement of pytest will be replaced by assertEqual of TestCase.
Next comes the csv_reader method that takes in a file object which is the .csv file and returns a 2D matrix (lists embedded within a list) representation of the circuit. This is the code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_csv_reader(): | |
""" | |
csv_reader - converts string eq of a .csv file to a matrix. | |
""" | |
import os | |
from network_reader import csv_reader | |
# Create a dummy test file as .csv file | |
f = open("test_reader.csv", "w") | |
# Write contents that could be a circuit | |
f.write("a,, b,,") | |
f.write("\n") | |
f.write(",c,, e ,") | |
f.close() | |
# Infer the matrix 2D representation of the circuit | |
req_result = [['a','','b','',''], ['','c','','e','']] | |
f = open("test_reader.csv") | |
# Pass the dummy test file to csv_reader | |
ckt_matrix = csv_reader(f) | |
print() | |
print("Test for {}".format(str(req_result))) | |
f.close() | |
assert ckt_matrix == [['a','','b','',''], ['','c','','e','']] | |
# Cleanup - remove the test .csv file | |
os.remove("test_reader.csv") | |
print() | |
return |
I didn't bother with circuit components or circuit topology because what matters here is the reading of the .csv file. A few errors are expected:
- If the separator is anything other than a comma, the reading fails.
- If one line has an extra character, the reading fails. This is because the expected result is a 2D matrix. This might be ok in most cases, but it might be a good idea to put a catch block in case, some weird spreadsheet software passes through a schematic with unequal line lengths. This should result in a user readable error rather than a syntax error.
- The elements are scrubbed internally which means leading and trailing spaces are removed from the elements. This is ok.
Will need to spend some thought on this as spreadsheet software in general are a bit messy and the need to generate .csv files in a particular form may not be obvious to the normal user.