Commit 8c50ea7d by Shirley He

fix up tests

parent c27687f8
"""
Unittests for generating permutations file
"""
import unittest
from generate_permutations import arg_parse
import sys
class TestArgParsing(unittest.TestCase):
def setUp(self):
self.parser = arg_parse()
"""
If no arguments passed, should fail with SystemExit
"""
def test_no_args(self):
with self.assertRaises(SystemExit):
self.parser.parse_args([])
def test_invalid_num_fields(self):
args = self.parser.parse_args(['start', 'end', 'display_name', 'mobile_available'])
command_error = "Only a max of 3 fields allowed"
self.assertEqual(args, command_error)
def test_invalid_file(self):
sys.argv[1:] = ["--seed_data_file", "permutation.json"]
options = arg_parse()
self.assertEquals("permutation.json", options)
if __name__ == '__main__':
unittest.main()
......@@ -36,6 +36,13 @@ def arg_parse():
def process_field_arguments(parser_args):
"""
Processes field arguments and prepares them for the permutation generator. Prepares
non-field arguments for the generator, as well
Input:
parser_args: the set of command line arguments typed in by the user
"""
try:
file = open(parser_args.seed_data_file)
file_data = json.load(file)
......@@ -78,6 +85,17 @@ def process_field_arguments(parser_args):
def generate_permutations(field_args, index, results, courses_dict, field_values_dict):
"""
Returns a dictionary of course permutation objects. Adds default values to the dict,
and recurses over the array attached to each field to create combinations
Input:
field_args: a dictionary of all the default & permutation fields that will be included in each course
index: iterator for each element in the list of field_args keys
results: the list of all permutation objects
courses_dict: dictionary used to separate "fields" and "enrollment" into different objects
field_values_dict: dictionary to hold each field value iteration result
"""
all_permutations_keys = field_args.keys()
permutation_option = all_permutations_keys[index]
permutations_values = field_args[permutation_option]
......@@ -150,9 +168,11 @@ def calculate_date_value(date_const):
def start_field_recursion(process_field_args):
return generate_permutations(process_field_args, 0, [], {}, {})
if __name__ == "__main__":
def main():
args = arg_parse()
process_field_args = process_field_arguments(args)
json_file = start_field_recursion(process_field_args)
create_courses_json_file(json_file)
if __name__ == "__main__":
main()
"""
Unittests for generating permutations file
"""
import unittest
import generate_permutations
class TestGeneratePermutations(unittest.TestCase):
def test_start_field_recursion(self):
# test display_name permutation
process_field_args = {"audit": [True], "end": ["future"], "mobile_available": [True], "verified": [True],
"start": ["past"],
"display_name": ["International Project Management", "Cybersecurity Fundamentals", "",
None]}
actual_output = generate_permutations.start_field_recursion(process_field_args)
expected_output = {
"courses": [
{
"partner": "edx",
"run": "3T2017",
"user": "edx@example.com",
"organization": "RITX",
"fields": {
"audit": True,
"start": "2017-06-19 14:07:40.835573+00:00",
"verified": True,
"end": "2018-08-18 14:07:40.835529+00:00",
"mobile_available": True,
"display_name": "International Project Management"
},
"enrollment": {
"credit-provider": "test-credit-provider",
"credit": False
},
"number": None
},
{
"partner": "edx",
"run": "3T2017",
"user": "edx@example.com",
"organization": "RITX",
"fields": {
"audit": True,
"start": "2017-06-19 14:07:40.835573+00:00",
"verified": True,
"end": "2018-08-18 14:07:40.835529+00:00",
"mobile_available": True,
"display_name": "Cybersecurity Fundamentals"
},
"enrollment": {
"credit-provider": "test-credit-provider",
"credit": False
},
"number": None
},
{
"partner": "edx",
"run": "3T2017",
"user": "edx@example.com",
"organization": "RITX",
"fields": {
"audit": True,
"start": "2017-06-19 14:07:40.835573+00:00",
"verified": True,
"end": "2018-08-18 14:07:40.835529+00:00",
"mobile_available": True,
"display_name": ""
},
"enrollment": {
"credit-provider": "test-credit-provider",
"credit": False
},
"number": None
},
{
"partner": "edx",
"run": "3T2017",
"user": "edx@example.com",
"organization": "RITX",
"fields": {
"audit": True,
"start": "2017-06-19 14:07:40.835573+00:00",
"verified": True,
"end": "2018-08-18 14:07:40.835529+00:00",
"mobile_available": True,
"display_name": None
},
"enrollment": {
"credit-provider": "test-credit-provider",
"credit": False
},
"number": None
},
{
"partner": "edx",
"run": "3T2017",
"user": "edx@example.com",
"organization": "RITX",
"fields": {
"audit": True,
"start": "2017-06-19 14:07:40.835573+00:00",
"verified": True,
"end": "2018-08-18 14:07:40.835529+00:00",
"mobile_available": True,
"display_name": None
},
"enrollment": {
"credit-provider": "test-credit-provider",
"credit": False
},
"number": None
},
{
"partner": "edx",
"run": "3T2017",
"user": "edx@example.com",
"organization": "RITX",
"fields": {
"audit": True,
"start": "2017-06-19 14:07:40.835573+00:00",
"verified": True,
"end": "2018-08-18 14:07:40.835529+00:00",
"mobile_available": True,
"display_name": None
},
"enrollment": {
"credit-provider": "test-credit-provider",
"credit": False,
"audit": True
},
"number": None
},
{
"partner": "edx",
"run": "3T2017",
"user": "edx@example.com",
"organization": "RITX",
"fields": {
"audit": True,
"start": "2017-06-19 14:07:40.835573+00:00",
"verified": True,
"end": "2018-08-18 14:07:40.835529+00:00",
"mobile_available": True,
"display_name": None
},
"enrollment": {
"credit-provider": "test-credit-provider",
"credit": False,
"audit": True
},
"number": None
},
{
"partner": "edx",
"run": "3T2017",
"user": "edx@example.com",
"organization": "RITX",
"fields": {
"audit": True,
"start": "2017-06-19 14:07:40.835573+00:00",
"verified": True,
"end": "2018-08-18 14:07:40.835529+00:00",
"mobile_available": True,
"display_name": None
},
"enrollment": {
"credit-provider": "test-credit-provider",
"credit": False
},
"number": None
},
{
"partner": "edx",
"run": "3T2017",
"user": "edx@example.com",
"organization": "RITX",
"fields": {
"audit": True,
"start": "2017-06-19 14:07:40.835573+00:00",
"verified": True,
"end": "2018-08-18 14:07:40.835529+00:00",
"mobile_available": True,
"display_name": None
},
"enrollment": {
"credit-provider": "test-credit-provider",
"credit": False,
"audit": True
},
"number": None
}
]
}
self.assertEquals(actual_output, expected_output)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment