Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

#! /usr/bin/env python3 

# -*- coding: utf-8 -*- 

 

#       Copyright 2012-2014, Marten de Vries 

# 

#       This file is part of OpenTeacher. 

# 

#       OpenTeacher is free software: you can redistribute it and/or modify 

#       it under the terms of the GNU General Public License as published by 

#       the Free Software Foundation, either version 3 of the License, or 

#       (at your option) any later version. 

# 

#       OpenTeacher is distributed in the hope that it will be useful, 

#       but WITHOUT ANY WARRANTY; without even the implied warranty of 

#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

#       GNU General Public License for more details. 

# 

#       You should have received a copy of the GNU General Public License 

#       along with OpenTeacher.  If not, see <http://www.gnu.org/licenses/>. 

 

import unittest 

import tempfile 

import copy 

import os 

import datetime 

import shutil 

import contextlib 

 

MODES = ("all", "save") 

 

class Lesson: 

        def __init__(self, list=None, resources=None): 

                self.list = list or {} 

                self.resources = resources or {} 

 

class TestCase(unittest.TestCase): 

        def _testSavingType(self, type, lesson): 

39                if self.mode not in MODES: 

                        self.skipTest("This tests isn't run in this mode.") 

                for saver in self._mm.mods("active", type="save"): 

                        if not type in saver.saves: 

                                continue 

 

                        lessonCopy = copy.deepcopy(lesson) 

 

                        #save lesson 

                        for ext in saver.saves[type]: 

                                fd, path = tempfile.mkstemp(".%s" % ext) 

                                os.close(fd) 

                                saver.save(type, lesson, path) 

 

                                #file should exist after saving. 

                                self.assertTrue(os.path.isfile(path)) 

                                #and it should not be empty. 

                                self.assertTrue(os.path.getsize(path) > 0) 

 

                                os.remove(path) 

                                with contextlib.suppress(OSError): 

                                        #html saver 

                                        shutil.rmtree(path + ".resources") 

 

                        try: 

                                self.assertEqual(lesson.list, lessonCopy.list) 

                                self.assertEqual(lesson.resources, lessonCopy.resources) 

                        except AssertionError: 

                                print("%s modified the lesson object passed to save(), which isn't allowed. More info:" % saver) 

                                raise 

 

        def testSavingMedia(self): 

                metadata = next(iter(self._mm.mods("active", type="metadata"))).metadata 

                self._testSavingType("media", Lesson({ 

                        "items": [ 

                                { 

                                        "remote": True, 

                                        "name": "http://openteacher.org/", 

                                        "question": "a", 

                                        "filename": "http://openteacher.org/", 

                                        "answer": "b", 

                                        "id": 0 

                                }, 

                                { 

                                        "remote": False, 

                                        "name": "openteacher-icon.png", 

                                        "question": "", 

                                        "filename": metadata["iconPath"], 

                                        "answer": "", 

                                        "id": 1 

                                } 

                        ], 

                        "tests": [] 

                }, {})) 

                #FIXME: see comment on resources in the .otmd saver file. 

 

        def testSavingTopo(self): 

                metadata = next(iter(self._mm.mods("active", type="metadata"))).metadata 

                self._testSavingType("topo", Lesson({ 

                        "items": [ 

                                { 

                                        "y": 364, 

                                        "x": 399, 

                                        "id": 0, 

                                        "name": "Test", 

                                }, 

                        ], 

                        "tests": [ 

                                { 

                                        "finished": True, 

                                        "results": [ 

                                                { 

                                                        "itemId": 0, 

                                                        "active": { 

                                                                "start": datetime.datetime.now(), 

                                                                "end": datetime.datetime.now(), 

                                                        }, 

                                                        "result": "wrong", 

                                                } 

                                        ], 

                                        "pauses": [ 

                                        ], 

                                }, 

                                { 

                                        "finished": True, 

                                        "results": [ 

                                                { 

                                                        "itemId": 0, 

                                                        "active": { 

                                                                "start": datetime.datetime.now(), 

                                                                "end": datetime.datetime.now(), 

                                                        }, 

                                                        "result": "right", 

                                                } 

                                        ], 

                                        "pauses": [ 

                                        ], 

                                }, 

                        ], 

                }, { 

                        "mapPath": metadata["iconPath"], 

                        "mapScreenshot": metadata["iconPath"], 

                })) 

 

        def testSavingWords(self): 

                self._testSavingType("words", Lesson({ 

                        "title": u"My títle", 

                        "questionLanguage": u"Varyíng", 

                        "answerLanguage": u"Va®ying too. Some úñí©óðé.", 

                        "items": [ 

                                { 

                                        "id": 0, 

                                        "questions": [(u"eén",), (u"uno", u"un",)], 

                                        "answers": [(u"twée", u"deux"), ("two", "zwei")], 

                                        "created": datetime.datetime.now(), 

                                }, 

                                { 

                                        "id": 1, 

                                        "questions": [(u"drie",)], 

                                        "answers": [(u"three",)], 

                                } 

                        ], 

                        "tests":[ 

                                { 

                                        "finished": True, 

                                        "results": [ 

                                                { 

                                                        "itemId":0, 

                                                        "active": { 

                                                                "start": datetime.datetime.now(), 

                                                                "end": datetime.datetime.now(), 

                                                        }, 

                                                        "result": "right", 

                                                        "givenAnswer": "one", 

                                                }, 

                                                { 

                                                        "itemId": 1, 

                                                        "active": { 

                                                                "start": datetime.datetime.now(), 

                                                                "end": datetime.datetime.now(), 

                                                        }, 

                                                        "result": "wrong", 

                                                        "givenAnswer": "ttwo", 

                                                }, 

                                        ], 

                                        "pauses": [ 

                                        ], 

                                }, 

                        ], 

                })) 

 

        def testAllEnabled(self): 

                self.assertEqual( 

                        set(self._mm.mods(type="save")), 

                        set(self._mm.mods("active", type="save")) 

                ) 

 

class TestModule: 

        def __init__(self, moduleManager, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

                self._mm = moduleManager 

 

                self.type = "test" 

                self.requires = ( 

                        self._mm.mods(type="save"), 

                        #re-using the icon as test topo map to save a bit of space. 

                        self._mm.mods(type="metadata"), 

                ) 

 

        def enable(self): 

                self.TestCase = TestCase 

                self.TestCase._mm = self._mm 

                self.active = True 

 

        def disable(self): 

                self.active = False 

                del self.TestCase 

 

def init(moduleManager): 

        return TestModule(moduleManager)