def create_questions(db, count=12):
    some_category = Category(type='Some Category')
    some_category.insert()

    questions = []
    for index in range(count):
        question = Question(question=f'The question? {index}',
                            answer='The answer',
                            category=some_category.id,
                            difficulty=5)
        question.insert()
        questions.append(question)
    return questions, some_category
예제 #2
0
    def setUp(self):
        super().setUp()

        # Add some categories
        self.science = Category('Science')
        self.geography = Category('Geography')
        db.session.add(self.science)
        db.session.add(self.geography)
        db.session.commit()

        # Add some questions
        self.science_question = Question('What is science?', 'Nobody knows', self.science.id, 5)
        self.geog_question = Question('Is geography real?', 'Probably not', self.geography.id, 5)
        self.science_question.insert()
        self.geog_question.insert()
예제 #3
0
def create():
    encrypted_user_id = None

    required = {"name": {'type': 'string', 'required': True}}

    validate = Utilities.validate(required, request.json)
    if validate is not None:
        return HttpResponse.error_response(validate)

    if g.user_id is not None:
        user_id = g.user_id
    else:
        user = userLogic.set_userId()
        user_id = user["user_id"]
        encrypted_user_id = user["encrypted_user_id"]

    name = str(request.json['name']).lower()

    new_category = Category(name, user_id)
    log_data = AuditLog(user_id, auditTypes.CATEGORY_CREATED)
    db.session.add(new_category)
    db.session.add(log_data)
    db.session.commit()

    response = category_schema.jsonify(new_category)
    if encrypted_user_id is not None:
        response.set_cookie("user_id", encrypted_user_id)
    return HttpResponse.success_response(response)
예제 #4
0
    def test_category_creation(self):
        cat = Category(type='Fancy')
        db.session.add(cat)
        db.session.commit()

        self.assertIsNotNone(cat.id)
        self.assertEqual(cat.type, 'Fancy')
예제 #5
0
    def setUp(self):
        """Define test variables and initialize app."""
        self.app = create_app()
        self.client = self.app.test_client
        self.database_name = os.getenv('TEST_DATABASE_NAME')
        self.database_user = os.getenv('DATABASE_USER')
        self.database_password = os.getenv('DATABASE_PASSWORD')
        self.database_path = "postgresql://{}:{}@{}/{}".format(
            self.database_user, self.database_password, 'localhost:5432',
            self.database_name)
        setup_db(self.app, self.database_path)

        # binds the app to the current context
        with self.app.app_context():
            self.db = SQLAlchemy()
            self.db.init_app(self.app)
            # create all tables
            self.db.create_all()
            # add a question to a category in the test database
            self.category = Category(type='Asia')
            self.db.session.add(self.category)
            self.db.session.flush()
            self.question = Question(question='Where is China?',
                                     answer='In Asia',
                                     category=self.category.id,
                                     difficulty=2)
            self.db.session.add(self.question)
            self.db.session.commit()
예제 #6
0
def category_create():
    if request.method == 'POST':
        name = request.form["name"]
        category = Category(name=name)
        db.session.add(category)
        db.session.commit()
        return redirect(url_for('category_list'))
    categories = Category.query.all()
    return render_template('category/edit.html', categories=categories)
예제 #7
0
    def test_category_format(self):
        cat_format = Category(type='Fancy').format()

        self.assertIsNone(cat_format['id'])
        self.assertEqual(cat_format['type'], 'Fancy')

        cat = Category.query.filter_by(type='Art').one().format()

        self.assertEqual(cat['id'], 2)
        self.assertEqual(cat['type'], 'Art')
예제 #8
0
def add_catagory():
    """
    Adds a category
    :return: Id of the question that has been created
    """
    body = request.get_json()

    if not ('type' in body):
        abort(422)

    try:
        category = Category(type=body.get('type')
                            )
        category.insert()
        return jsonify({
            'success': True,
            'created': category.id,
        })
    except:
        abort(422)
예제 #9
0
 def test_category_uniqueness_on_type(self):
     cat_2 = None
     try:
         cat_2 = Category(type='Art')
         db.session.add(cat_2)
         db.session.commit()
     except IntegrityError as e:
         self.assertTrue(True)
     finally:
         if cat_2.id is not None:
             self.assertTrue(False, 'we should not be here!!')
    def test_create_question(self):
        some_category = Category(type='Some Category')
        some_category.insert()

        res = self.client().post('/questions',
                                 json={
                                     'question':
                                     'How long is a mile in kilometers?',
                                     'answer': 'Answer',
                                     'category': some_category.id,
                                     'difficulty': 5
                                 })

        assert res.status_code == 201
        data = json.loads(res.data)
        assert data['id']
        created_question = Question.query.get(data['id'])
        assert created_question.question == data['question']
        assert created_question.answer == data['answer']
        assert created_question.category == data['category']
        assert created_question.difficulty == data['difficulty']
예제 #11
0
    def setUp(self):
        self.app = create_app()
        self.client = self.app.test_client
        setup_db(self.app, 'trivia_test')

        db.drop_all()
        db.create_all()

        self.temp_categories = [
            Category('Science'),
            Category('Art'),
            Category('Geography'),
            Category('History'),
            Category('Entertainment'),
            Category('Sports')
        ]

        self.temp_questions = [
            Question('Whose autobiography is entitled \'I Know Why the Caged Bird Sings\'?', 'Maya Angelou', 2, 4),
            Question('What boxer\'s original name is Cassius Clay?', 'Muhammad Ali', 1, 4),
            Question('The Taj Mahal is located in which Indian city?', 'Agra', 2, 3),
            Question('Which Dutch graphic artist–initials M C was a creator of optical illusions?', 'Escher', 1, 2),
            Question('What is the heaviest organ in the human body?', 'The Liver', 4, 1)
        ]

        db.session.add_all(self.temp_categories)
        db.session.add_all(self.temp_questions)
        db.session.commit()
예제 #12
0
    def test_get_questions_for_category_sadpath_no_questions(self):
        category = Category(type='testing', id=21)
        db.session.add(category)
        db.session.commit()
        response = self.client.get(
            '/categories/{id}/questions'.format(id=category.id))

        self.assertEqual(response.status_code, 200)
        data = json.loads(response.data.decode('utf-8'))

        self.assertIn('total_questions', data)
        self.assertIsInstance(data['total_questions'], int)
        self.assertEqual(0, data['total_questions'])

        self.assertIn('current_category', data)
        self.assertIsNone(data['current_category'])

        self.assertIn('questions', data)
        self.assertIsInstance(data['questions'], list)
        self.assertEqual(0, len(data['questions']))
예제 #13
0
    def test_get_category_questions_fail_no_questions(self):
        db.session.add(Category('Physics'))
        db.session.commit()

        res = self.client().get('/categories/7/questions')

        status = res.status_code
        data = json.loads(res.data)

        # check status and data
        self.assertEqual(status, 404)
        self.assertTrue('success' in data)
        self.assertTrue('error' in data)
        self.assertTrue('description' in data)
        self.assertTrue('message' in data)

        # check success
        self.assertFalse(data['success'])
        self.assertEqual(data['error'], 404)
        self.assertEqual(data['description'], 'no questions found in category 7')
        self.assertEqual(data['message'], 'not found')
예제 #14
0
 def create_new_category(self, category_type):
     category = Category(type=category_type)
     self.db.session.add(category)
     self.db.session.commit()
     return category
예제 #15
0
def seed_data(db):
    cat_1 = Category(id=1, type='Science')
    db.session.add(cat_1)
    cat_2 = Category(id=2, type='Art')
    db.session.add(cat_2)
    cat_3 = Category(id=3, type='Geography')
    db.session.add(cat_3)
    cat_4 = Category(id=4, type='History')
    db.session.add(cat_4)
    cat_5 = Category(id=5, type='Entertainment')
    db.session.add(cat_5)
    cat_6 = Category(id=6, type='Sports')
    db.session.add(cat_6)
    db.session.execute(
        "SELECT pg_catalog.setval('public.categories_id_seq', 6, true);")
    db.session.commit()

    q_2 = Question(
        id=2,
        question=
        "What movie earned Tom Hanks his third straight Oscar nomination, in 1996?",
        answer="Apollo 13",
        difficulty=4,
        category=5)
    db.session.add(q_2)
    q_4 = Question(
        id=4,
        question=
        "What actor did author Anne Rice first denounce, then praise in the role of her beloved Lestat?",
        answer="Tom Cruise",
        difficulty=4,
        category=5)
    db.session.add(q_4)
    q_5 = Question(
        id=5,
        question=
        "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?",
        answer="Maya Angelou",
        difficulty=2,
        category=4)
    db.session.add(q_5)
    q_6 = Question(
        id=6,
        question=
        "What was the title of the 1990 fantasy directed by Tim Burton about a young man with multi-bladed appendages?",
        answer="Edward Scissorhands",
        difficulty=3,
        category=5)
    db.session.add(q_6)
    q_9 = Question(id=9,
                   question="What boxer's original name is Cassius Clay?",
                   answer="Muhammad Ali",
                   difficulty=1,
                   category=4)
    db.session.add(q_9)
    q_10 = Question(
        id=10,
        question=
        "Which is the only team to play in every soccer World Cup tournament?",
        answer="Brazil",
        difficulty=3,
        category=6)
    db.session.add(q_10)
    q_11 = Question(
        id=11,
        question="Which country won the first ever soccer World Cup in 1930?",
        answer="Uruguay",
        difficulty=4,
        category=6)
    db.session.add(q_11)
    q_12 = Question(id=12,
                    question="Who invented Peanut Butter?",
                    answer="George Washington Carver",
                    difficulty=2,
                    category=4)
    db.session.add(q_12)
    q_13 = Question(id=13,
                    question="What is the largest lake in Africa?",
                    answer="Lake Victoria",
                    difficulty=2,
                    category=3)
    db.session.add(q_13)
    q_14 = Question(
        id=14,
        question="In which royal palace would you find the Hall of Mirrors?",
        answer="The Palace of Versailles",
        difficulty=3,
        category=3)
    db.session.add(q_14)
    q_15 = Question(id=15,
                    question="The Taj Mahal is located in which Indian city?",
                    answer="Agra",
                    difficulty=2,
                    category=3)
    db.session.add(q_15)
    q_16 = Question(
        id=16,
        question=
        "Which Dutch graphic artist–initials M C was a creator of optical illusions?",
        answer="Escher",
        difficulty=1,
        category=2)
    db.session.add(q_16)
    q_17 = Question(id=17,
                    question="La Giaconda is better known as what?",
                    answer="Mona Lisa",
                    difficulty=3,
                    category=2)
    db.session.add(q_17)
    q_18 = Question(
        id=18,
        question="How many paintings did Van Gogh sell in his lifetime?",
        answer="One",
        difficulty=4,
        category=2)
    db.session.add(q_18)
    q_19 = Question(
        id=19,
        question=
        "Which American artist was a pioneer of Abstract Expressionism, and a leading exponent of action painting?",
        answer="Jackson Pollock",
        difficulty=2,
        category=2)
    db.session.add(q_19)
    q_20 = Question(id=20,
                    question="What is the heaviest organ in the human body?",
                    answer="The Liver",
                    difficulty=4,
                    category=1)
    db.session.add(q_20)
    q_21 = Question(id=21,
                    question="Who discovered penicillin?",
                    answer="Alexander Fleming",
                    difficulty=3,
                    category=1)
    db.session.add(q_21)
    q_22 = Question(
        id=22,
        question=
        "Hematology is a branch of medicine involving the study of what?",
        answer="Blood",
        difficulty=4,
        category=1)
    db.session.add(q_22)
    q_23 = Question(
        id=23,
        question="Which dung beetle was worshipped by the ancient Egyptians?",
        answer="Scarab",
        difficulty=4,
        category=4)
    db.session.add(q_23)
    db.session.execute(
        "SELECT pg_catalog.setval('public.questions_id_seq', 23, true);")

    db.session.commit()
def create_categories(db):
    animals = Category(type='Animals')
    animals.insert()
    pop_culture = Category(type='Pop Culture')
    pop_culture.insert()
    sports = Category(type='Sports')
    sports.insert()
    return [animals, pop_culture, sports]