#!/www/python/bin/python
"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/test_survey.py $
$Id: test_survey.py 23466 2004-02-13 19:12:31Z dbinger $
"""

from sancho.unittest import TestScenario, parse_args, run_scenarios
from dulcinea.survey import Question, TextQuestion, SelectQuestion, \
     QuestionDatabase, TextAnswer, SelectAnswer, Survey, Surveyable
from dulcinea.user import DulcineaUser

tested_modules = ["dulcinea.survey"]


class SurveyTest (TestScenario):

    class TestSurveyable(Surveyable):

        def __init__(self, question_db=None):
            Surveyable.__init__(self)
            self.question_db = question_db

        def get_new_survey(self):
            return Survey(self.question_db.get_answers([1,2,3]))

    class BadQuestion(Question):
        pass

    def setup(self):
        self.user = DulcineaUser('u')
        self.questions = [
            TextQuestion("text1 title", sub_title="text sub title"),
            SelectQuestion("select1 title", column_headings=['Yes', 'No']),
            SelectQuestion("select2 title",
                           sub_title="check-all-that-apply question",
                           row_headings=["MEMS using micromachining",
                                         "MEMS using bulk micro",
                                         "MEMS using silicon substrate",
                                         "MEMS using silicon substrate",
                                         "MEMS equipment supplier",
                                         "MEMS material supplier",
                                         "MEMS service supplier",
                                         "Other (please specify):"],
                           last_row_has_text=True),
            SelectQuestion("select3 title",
                           sub_title="This example uses a numbered radio set",
                           column_headings=map(str, range(1,10)),
                           row_headings=["Material type",
                                         "Thickness",
                                         "Thickness uniformity",
                                         "Resistivity",
                                         "Flatness",
                                         "Defect level"]),
            SelectQuestion("select4 title",
                           sub_title="This example uses 'range labels'",
                           range_labels = ["Not Important",
                                           "Very Important"],
                           column_headings=map(str, range(1,10)),
                           row_headings=["Material type",
                                         "Thickness",
                                         "Thickness uniformity",
                                         "Resistivity",
                                         "Flatness",
                                         "Defect level"])]

    def check_question_db(self):
        self.test_stmt("self.question_db = QuestionDatabase()")
        for question in self.questions:
            self.test_stmt("self.question_db.add(question)")

    def check_bad_question(self):
        self.test_stmt("b1 = self.BadQuestion('bad')")
        self.test_exc("b1.get_new_answer()", NotImplementedError)

    def check_text_question(self):
         self.test_stmt("t1 = self.question_db.get(1)")
         self.test_val("t1.get_new_answer().__class__", TextAnswer)
         self.test_val("t1.get_title()", "text1 title")
         self.test_val("t1.get_sub_title()", 'text sub title')

    def check_select_question(self):
         self.test_stmt("s1 = self.question_db.get(2)")
         self.test_val("s1.get_new_answer().__class__", SelectAnswer)
         self.test_val("s1.get_title()", "select1 title")
         self.test_val("s1.get_sub_title()", None)
         self.test_val("s1.get_column_headings()", ['Yes', 'No'])
         self.test_val("s1.get_row_headings()", [])
         self.test_val("s1.get_range_labels()", [])
         self.test_stmt("s2 = self.question_db.get(3)")
         self.test_val("s2.get_sub_title()", "check-all-that-apply question")
         self.test_val("s2.get_row_headings()", ["MEMS using micromachining",
                                                 "MEMS using bulk micro",
                                                 "MEMS using silicon substrate",
                                                 "MEMS using silicon substrate",
                                                 "MEMS equipment supplier",
                                                 "MEMS material supplier",
                                                 "MEMS service supplier",
                                                 "Other (please specify):"])
         self.test_stmt("s4 = self.question_db.get(5)")
         self.test_val("s4.get_range_labels()", ["Not Important",
                                                 "Very Important"])

    def check_text_answer(self):
         self.test_stmt("self.t1 = self.question_db.get(1)")
         self.test_val("self.t1.get_new_answer().__class__", TextAnswer)
         self.test_stmt("a1 = self.t1.get_new_answer()")
         self.test_val("a1.get_question()", self.t1)
         self.test_val("a1.get_answer()", None)
         self.test_val("a1.has_answer()", False)
         self.test_stmt("a1.set_answer('the answer')")
         self.test_val("a1.get_answer()", 'the answer')
         self.test_val("a1.has_answer()", True)

    def check_select_answer(self):
         self.test_stmt("self.s1 = self.question_db.get(2)")
         self.test_val("self.s1.get_new_answer().__class__", SelectAnswer)
         self.test_stmt("a2 = self.s1.get_new_answer()")
         self.test_val("a2.get_question()", self.s1)
         self.test_val("a2.get_answer()", None)
         self.test_val("a2.has_answer()", False)
         self.test_stmt("a2.set_answer([1,2,3])")
         self.test_val("a2.get_answer()", [1,2,3])
         self.test_val("a2.has_answer()", True)
         self.test_val("a2.get_other_text()", None)
         self.test_stmt("a2.set_other_text('other')")
         self.test_val("a2.get_other_text()", 'other')

    def check_survey(self):
        self.test_stmt("s1 = Survey(self.question_db.get_answers([1,2,3,4,5]))")
        self.test_stmt("str(s1)")
        self.test_val("len(s1)", 5)
        self.test_val("len(s1.get_questions())", 5)
        self.test_val("len(s1.get_answers())", 5)
        self.test_val("s1.get_timestamp()", None)
        self.test_stmt("s1.set_timestamp()")
        self.test_true("s1.get_timestamp() is not None")
        self.test_val("s1.get_user()", None)
        self.test_stmt("s1.set_user(self.user)")
        self.test_val("s1.get_user().get_id()", 'u')
        self.test_false("s1.has_answers()")
        self.test_stmt("a1 = s1.get_answers()[0]")
        self.test_stmt("a1.set_answer('the answer')")
        self.test_true("s1.has_answers()")
        self.test_stmt("s1.set_answers(s1.get_answers(), self.user)")

    def check_surveyable(self):
        self.test_stmt("sv = self.TestSurveyable(question_db=self.question_db)")
        self.test_val("sv.get_surveys()", [])
        self.test_stmt("sv.add_survey(Survey(self.question_db.get_answers([1,2,3])))")
        self.test_stmt("sv.get_survey(0)")
        self.test_stmt("sv.get_new_survey()")
        self.test_stmt("sv2 = Surveyable()")
        self.test_exc("sv2.get_new_survey()", NotImplementedError)

if __name__ == "__main__":
    (scenarios, options) = parse_args()
    options.show_coverage = 1
    #options.show_coverage_lines = 1
    run_scenarios(scenarios, options)
