Skip to content

Commit

Permalink
support for python 3.5/3.8 and django 2.2 + basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarIthawi committed Jan 4, 2021
1 parent 4989680 commit 0abda4d
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 8 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: feedbackxblock

on:
push:
branches: [master, main]
pull_request:

jobs:
test:
runs-on: ubuntu-18.04
strategy:
matrix:
python-version: [3.5]
tox-env:
- quality
- django22
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install tox
- name: Test with tox
run: tox -e ${{ matrix.tox-env }}
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
*pyc
rate_xblock.egg-info
*~
*~
.coverage
.tox
*.egg-info
__pycache__
6 changes: 3 additions & 3 deletions feedback/feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import cgi
import random

import pkg_resources
import six

from xblock.core import XBlock
from xblock.fields import Scope, Integer, String, List, Float
Expand Down Expand Up @@ -264,9 +264,9 @@ def studio_view(self, context):
prompt = self.get_prompt(0)
for idx in range(len(prompt['scale_text'])):
prompt['likert{i}'.format(i=idx)] = prompt['scale_text'][idx]
frag = Fragment(unicode(html_str).format(**prompt))
frag = Fragment(six.text_type(html_str).format(**prompt))
js_str = self.resource_string("static/js/src/studio.js")
frag.add_javascript(unicode(js_str))
frag.add_javascript(six.text_type(js_str))
frag.initialize_js('FeedbackBlock',
{'icon_set': prompt['icon_set']})
return frag
Expand Down
1 change: 0 additions & 1 deletion feedbacktests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from .test_feedback import TestFeedback
27 changes: 27 additions & 0 deletions feedbacktests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
from mock import Mock

from workbench.runtime import WorkbenchRuntime
from xblock.fields import ScopeIds
from xblock.runtime import DictKeyValueStore, KvsFieldData

from feedback.feedback import FeedbackXBlock


def generate_scope_ids(runtime, block_type):
""" helper to generate scope IDs for an XBlock """
def_id = runtime.id_generator.create_definition(block_type)
usage_id = runtime.id_generator.create_usage(def_id)
return ScopeIds('user', block_type, def_id, usage_id)


@pytest.fixture
def feedback_xblock():
"""Feedback XBlock pytest fixture."""
runtime = WorkbenchRuntime()
key_store = DictKeyValueStore()
db_model = KvsFieldData(key_store)
ids = generate_scope_ids(runtime, 'feedback')
feedback_xblock = FeedbackXBlock(runtime, db_model, scope_ids=ids)
feedback_xblock.usage_id = Mock()
return feedback_xblock
4 changes: 2 additions & 2 deletions feedbacktests/test_feedback.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'''
Tests for the FeedbackXBlock.
Tests for the FeedbackXBlock that needs to run in Open edX.
'''

import json
Expand Down Expand Up @@ -31,7 +31,7 @@ def set_random(self, random_patch_value):


# pylint: disable=abstract-method
class TestFeedback(PatchRandomMixin, XBlockTestCase):
class FeedbackTestCase(PatchRandomMixin, XBlockTestCase):
"""
Basic tests for the FeedbackXBlock. We set up a page with two
of the block, make sure the page renders, toggle a few ratings,
Expand Down
52 changes: 52 additions & 0 deletions feedbacktests/test_feedback_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Tests for the Feedback XBlock with heavy mocking.
"""

from mock import Mock


def test_template_content(feedback_xblock):
""" Test content of FeedbackXBlock's student view """
student_fragment = feedback_xblock.render('student_view', Mock())
assert 'feedback' in student_fragment.content


def test_studio_view(feedback_xblock):
""" Test content of FeedbackXBlock's author view """
student_fragment = feedback_xblock.render('studio_view', Mock())
assert 'feedback' in student_fragment.content


def test_studio_submit(feedback_xblock):
""" Test the FeedbackXBlock's save action """
request_body = b"""{
"display_name": "foo"
}"""
request = Mock(method='POST', body=request_body)
response = feedback_xblock.studio_submit(request)
assert response.status_code == 200 and {'result': 'success'} == response.json, response.json


def test_vote(feedback_xblock):
""" Test content of FeedbackXBlock's vote() method """
feedback_xblock.vote({'vote': 1})


def test_feedback_method(feedback_xblock):
""" Test content of FeedbackXBlock's feedback() method """
request_body = b"""{
"freeform": "yes",
"vote": 1
}"""
request = Mock(method='POST', body=request_body)
response = feedback_xblock.feedback(request)

expected_response_json = {
"aggregate": [0, 1, 0, 0, 0],
"freeform": "yes",
"response": "Thank you for your feedback!",
"success": True,
"vote": 1,
}

assert response.status_code == 200 and response.json == expected_response_json, response.json
Empty file added geckodriver.log
Empty file.
2 changes: 1 addition & 1 deletion makeicons/test_icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import unittest
from bok_choy.web_app_test import WebAppTest
from pages import IconsPage
from .pages import IconsPage


class TestIcons(WebAppTest):
Expand Down
7 changes: 7 additions & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
Test settings for the Feedback XBlock.
"""

from workbench.settings import *

from django.conf.global_settings import LOGGING
37 changes: 37 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[tox]
envlist = py{35,38}-django22,py38-django3
skipsdist = True

[pytest]
# 58, 228-229, 250, 279-288, 307-317, 330-363, 373, 391-396
addopts = --cov=feedback --cov-report=term-missing

[testenv]
usedevelop=True
passenv =
SELENIUM_BROWSER
setenv =
DJANGO_SETTINGS_MODULE = test_settings
deps =
django11: Django>=1.11,<2
django22: Django>=2.2,<2.3
bok-choy
six
django-pyfs
flake8
mock
pytest
pytest-cov
pytest-django
xblock
xblock-sdk
commands =
# TODO: Activate the rest of the tests once they're fixed
{posargs:pytest feedbacktests/test_feedback_unit.py}

[flake8]
max-line-length = 160

[testenv:quality]
commands =
flake8 feedback feedbacktests makeicons setup.py

0 comments on commit 0abda4d

Please sign in to comment.