Skip to content

Commit ba245e7

Browse files
author
Brandon Duffany
committed
Add test for cb_compile_tester in bachelor
1 parent 1d3626f commit ba245e7

File tree

4 files changed

+65
-28
lines changed

4 files changed

+65
-28
lines changed

codebender_testing/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def _rel_path(*args):
1212
# URL of the actual Codebender website
1313
LIVE_SITE_URL = "http://codebender.cc"
1414

15-
1615
# User whose projects we'd like to compile in our compile_tester
1716
# test case(s).
1817
COMPILE_TESTER_URL = "/user/cb_compile_tester"
@@ -36,6 +35,9 @@ def _rel_path(*args):
3635
TEST_DATA_BLANK_PROJECT = os.path.join(TEST_DATA_DIR, 'blank_project.ino')
3736
TEST_DATA_BLANK_PROJECT_ZIP = os.path.join(TEST_DATA_DIR, 'blank_project.zip')
3837

38+
# Directory in which the local compile tester files are stored.
39+
COMPILE_TESTER_DIR = os.path.join(TEST_DATA_DIR, 'cb_compile_tester')
40+
3941
# Set up Selenium Webdrivers to be used for selenium tests
4042

4143
def _get_firefox_profile():

codebender_testing/utils.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from time import gmtime
33
from time import strftime
44
import json
5+
import os
56
import re
67
import tempfile
78

@@ -161,6 +162,30 @@ def open_project(self, project_name=None):
161162
project_link = self.driver.find_element_by_link_text(project_name)
162163
project_link.send_keys(Keys.ENTER)
163164

165+
def upload_project(self, test_fname, project_name=None):
166+
"""Tests that we can successfully upload `test_fname`.
167+
`project_name` is the expected name of the project; by
168+
default it is inferred from the file name.
169+
Returns a pair of (the name of the project, the url of the project sketch)
170+
"""
171+
# A tempfile is used here since we want the name to be
172+
# unique; if the file has already been successfully uploaded
173+
# then the test might give a false-positive.
174+
with temp_copy(test_fname) as test_file:
175+
self.dropzone_upload("#dropzoneForm", test_file.name)
176+
if project_name is None:
177+
project_name = os.path.split(test_file.name)[-1].split('.')[0]
178+
179+
# The upload was successful <==> we get a green "check" on its
180+
# Dropzone upload indicator
181+
self.get_element(By.CSS_SELECTOR, '#dropzoneForm .dz-success')
182+
183+
# Make sure the project shows up in the Projects list
184+
last_project = self.get_element(By.CSS_SELECTOR,
185+
'#sidebar-list-main li:last-child .project_link')
186+
187+
return last_project.text, last_project.get_attribute('href')
188+
164189
def login(self):
165190
"""Performs a login."""
166191
try:
@@ -244,20 +269,25 @@ def dropzone_upload(self, selector, fname):
244269
test_input.send_keys(fname)
245270
self.execute_script(_move_file_to_dropzone_script(selector))
246271

247-
def compile_all_sketches(self, url, selector, iframe=False, logfile=None):
248-
"""Compiles all projects on the page at `url`. `selector` is a CSS selector
272+
def compile_all_sketches(self, url, selector, **kwargs):
273+
"""Compiles all sketches on the page at `url`. `selector` is a CSS selector
249274
that should select all relevant <a> tags containing links to sketches.
275+
See `compile_sketches` for the possible keyword arguments that can be specified.
276+
"""
277+
self.open(url)
278+
sketches = self.execute_script(_GET_SKETCHES_SCRIPT.format(selector=selector))
279+
assert len(sketches) > 0
280+
self.compile_sketches(sketches, **kwargs)
281+
282+
def compile_sketches(self, sketches, iframe=False, logfile=None):
283+
"""Compiles the sketches with URLs given by the `sketches` list.
250284
`logfile` specifies a path to a file to which test results will be
251285
logged. If it is not `None`, compile errors will not cause the test
252286
to halt, but rather be logged to the given file. `logfile` may be a time
253287
format string, which will be formatted appropriately.
254288
`iframe` specifies whether the urls pointed to by `selector` are contained
255289
within an iframe.
256290
"""
257-
self.open(url)
258-
sketches = self.execute_script(_GET_SKETCHES_SCRIPT.format(selector=selector))
259-
assert len(sketches) > 0
260-
261291
if logfile is None:
262292
for sketch in sketches:
263293
self.compile_sketch(sketch, iframe=iframe)
@@ -278,7 +308,6 @@ def compile_all_sketches(self, url, selector, iframe=False, logfile=None):
278308
json.dump(log_entry, f)
279309
f.close()
280310

281-
282311
def execute_script(self, script, *deps):
283312
"""Waits for all JavaScript variables in `deps` to be defined, then
284313
executes the given script."""

tests/compile_tester/test_compile_tester_projects.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import os
2+
13
import pytest
24

5+
from codebender_testing.config import BASE_URL
6+
from codebender_testing.config import COMPILE_TESTER_DIR
37
from codebender_testing.config import COMPILE_TESTER_LOGFILE
48
from codebender_testing.config import COMPILE_TESTER_URL
59
from codebender_testing.config import LIVE_SITE_URL
610
from codebender_testing.utils import SeleniumTestCase
11+
from codebender_testing.utils import temp_copy
712

813

914
class TestCompileTester(SeleniumTestCase):
@@ -16,3 +21,19 @@ def test_compile_all_user_projects(self):
1621
self.compile_all_sketches(COMPILE_TESTER_URL, '#user_projects tbody a',
1722
iframe=True, log_file=COMPILE_TESTER_LOGFILE)
1823

24+
# Here we require BASE_URL since cb_compile_tester's projects are already
25+
# uploaded to the live site.
26+
@pytest.mark.requires_url(BASE_URL)
27+
def test_compile_local_files(self, tester_login):
28+
"""Tests that we can upload all of cb_compile_tester's projects
29+
(stored locally in test_data/cb_compile_tester), compile them,
30+
and finally delete them."""
31+
test_files = [os.path.join(COMPILE_TESTER_DIR, name)
32+
for name in next(os.walk(COMPILE_TESTER_DIR))[2]]
33+
projects = [self.upload_project(fname) for fname in test_files]
34+
project_names, project_urls = zip(*projects)
35+
36+
self.compile_sketches(project_urls, logfile=COMPILE_TESTER_LOGFILE)
37+
for name in project_names:
38+
self.delete_project(name)
39+

tests/user_home/test_user_home.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,15 @@ def _upload_test(self, test_fname, project_name=None):
6060
"""Tests that we can successfully upload `test_fname`.
6161
`project_name` is the expected name of the project; by
6262
default it is inferred from the file name.
63+
We delete the project if it is successfully uploaded.
6364
"""
64-
65-
# A tempfile is used here since we want the name to be
66-
# unique; if the file has already been successfully uploaded
67-
# then the test might give a false-positive.
68-
with temp_copy(test_fname) as test_file:
69-
self.dropzone_upload("#dropzoneForm", test_file.name)
70-
if project_name is None:
71-
project_name = os.path.split(test_file.name)[-1].split('.')[0]
72-
73-
# The upload was successful <==> we get a green "check" on its
74-
# Dropzone upload indicator
75-
self.get_element(By.CSS_SELECTOR, '#dropzoneForm .dz-success')
76-
77-
# Make sure the project shows up in the Projects list
78-
last_project = self.get_element(By.CSS_SELECTOR,
79-
'#sidebar-list-main li:last-child .project_link')
80-
81-
assert last_project.text == project_name
82-
65+
name, _ = self.upload_project(test_fname, project_name=project_name)
66+
if project_name is not None:
67+
assert name == project_name
8368
# Cleanup. If the above assertion failed, then we leave
8469
# garbage behind. This is unavoidable for now since we don't
8570
# have proper test fixtures. (TODO?)
86-
self.delete_project(project_name)
71+
self.delete_project(name)
8772

8873
def test_upload_project_ino(self):
8974
"""Tests that we can upload a .ino file."""

0 commit comments

Comments
 (0)