Coverage for test/test_scenario_forest.py: 100%

1"""Test the ScenarioForest class"""

2

3 import os

4 import filecmp

5 import pdb

6

7 import pytest

8

9 import manyworlds as mw

10

11@pytest.fixture(scope='session', autouse=True)

12 def clear_out_directory():

13 """Delete all files in test/out"""

14 folder = os.path.dirname(os.path.realpath(__file__)) + '/out'

15 for filename in os.listdir(folder):

16 if not filename == '.gitignore':

17 file_path = os.path.join(folder, filename)

18 os.unlink(file_path)

19 yield

20

21 def test_parse():

22 """Test the structure of the forest graph after using the 'from_file' method"""

23 forest = mw.ScenarioForest.from_file('test/fixtures/scenarios_forest.feature')

24 assert len(forest.root_scenarios()) == 1

25

26 root_scenario = forest.find('View users')

27 assert root_scenario.__repr__() == '<Scenario: View users (1 prerequisites, 1 actions, 1 assertions)>'

28 assert root_scenario.name == 'View users'

29 assert root_scenario.level() == 1

30 assert len(root_scenario.ancestors()) == 0

31 assert len(root_scenario.prerequisites()) == 1

32 assert len(root_scenario.actions()) == 1

33 assert len(root_scenario.assertions()) == 1

34 data = root_scenario.prerequisites()[0].data

35 assert len(data) == 4

36 assert data[2]['Name'] == 'Connie'

37 assert data[2]['Status'] == 'Active'

38 action = root_scenario.actions()[0]

39 assert action.__repr__() == '<Action: I go to "Users">'

40

41 leaf_scenario = forest.find('View users', 'Bulk operations', 'Select user', 'Select multiple users', 'Bulk deactivate users', 'Confirm bulk deactivation of users')

42 assert leaf_scenario.__repr__() == '<Scenario: Confirm bulk deactivation of users (0 prerequisites, 1 actions, 2 assertions)>'

43 assert leaf_scenario.name == 'Confirm bulk deactivation of users'

44 assert leaf_scenario.level() == 6

45 assert len(leaf_scenario.ancestors()) == 5

46 assert len(leaf_scenario.prerequisites()) == 0

47 assert len(leaf_scenario.actions()) == 1

48 assert len(leaf_scenario.assertions()) == 2

49 assertion = leaf_scenario.assertions()[0]

50 assert assertion.__repr__() == '<Assertion: I see "0 users selected">'

51

52 def test_flatten_strict():

53 """Test the 'flatten' method in 'strict' mode"""

54 forest = mw.ScenarioForest.from_file('test/fixtures/scenarios_forest.feature')

55 forest.flatten('test/out/scenarios_flat_strict.feature')

56 assert filecmp.cmp('test/out/scenarios_flat_strict.feature',

57 'test/fixtures/scenarios_flat_strict.feature')

58

59 def test_flatten_strict_with_comments():

60 """Test the 'flatten' method in 'strict' mode with comments turned on"""

61 forest = mw.ScenarioForest.from_file('test/fixtures/scenarios_forest.feature')

62 forest.flatten('test/out/scenarios_flat_strict_with_comments.feature', comments=True)

63 assert filecmp.cmp('test/out/scenarios_flat_strict_with_comments.feature',

64 'test/fixtures/scenarios_flat_strict_with_comments.feature')

65 def test_flatten_relaxed():

66 """Test the 'flatten' method in 'relaxed' mode"""

67 forest = mw.ScenarioForest.from_file('test/fixtures/scenarios_forest.feature')

68 forest.flatten('test/out/scenarios_flat_relaxed.feature', mode='relaxed')

69 assert filecmp.cmp('test/out/scenarios_flat_relaxed.feature',

70 'test/fixtures/scenarios_flat_relaxed.feature')

71

72 def test_flatten_relaxed_with_comments():

73 """Test the 'flatten' method in 'relaxed' mode with comments turned on"""

74 forest = mw.ScenarioForest.from_file('test/fixtures/scenarios_forest.feature')

75 forest.flatten('test/out/scenarios_flat_relaxed_with_comments.feature', mode='relaxed', comments=True)

76 assert filecmp.cmp('test/out/scenarios_flat_relaxed_with_comments.feature',

77 'test/fixtures/scenarios_flat_relaxed_with_comments.feature')

78

79 def test_invalid_file():

80 """Test that the correct error is raised when attempting to parse invalid files"""

81 with pytest.raises(ValueError) as error_info:

82 forest = mw.ScenarioForest.from_file('test/fixtures/invalid/invalid_line.feature')

83 assert str(error_info.value) == 'Unable to parse line: Givven the following users: # invalid line'