Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
zesje
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Works on my machine
zesje
Commits
c253773e
Commit
c253773e
authored
5 years ago
by
Ruben Young On
Browse files
Options
Downloads
Patches
Plain Diff
Added more tests, api is now separate test package
parent
4f47dfe1
No related branches found
Branches containing commit
No related tags found
1 merge request
!16
Add API tests, small fix for MultipleChoiceOption
Pipeline
#17799
failed
5 years ago
Stage: build
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tests/api/test_exams.py
+95
-0
95 additions, 0 deletions
tests/api/test_exams.py
tests/api/test_mult_choice.py
+78
-48
78 additions, 48 deletions
tests/api/test_mult_choice.py
with
173 additions
and
48 deletions
tests/api/test_exams.py
0 → 100644
+
95
−
0
View file @
c253773e
import
pytest
from
flask
import
json
from
flask
import
Flask
from
zesje.api
import
api_bp
from
zesje.database
import
db
,
Exam
,
Problem
,
ProblemWidget
@pytest.fixture
(
scope
=
"
module
"
)
def
app
():
app
=
Flask
(
__name__
,
static_folder
=
None
)
app
.
config
.
update
(
SQLALCHEMY_DATABASE_URI
=
'
sqlite:///:memory:
'
,
SQLALCHEMY_TRACK_MODIFICATIONS
=
False
# Suppress future deprecation warning
)
db
.
init_app
(
app
)
with
app
.
app_context
():
db
.
create_all
()
add_test_data
()
app
.
register_blueprint
(
api_bp
,
url_prefix
=
'
/api
'
)
return
app
@pytest.fixture
()
def
test_client
(
app
):
client
=
app
.
test_client
()
yield
client
def
mco_json
():
return
{
'
x
'
:
100
,
'
y
'
:
40
,
'
problem_id
'
:
1
,
'
page
'
:
1
,
'
label
'
:
'
a
'
,
'
name
'
:
'
test
'
}
def
add_test_data
():
exam1
=
Exam
(
id
=
1
,
name
=
'
exam 1
'
,
finalized
=
False
)
exam2
=
Exam
(
id
=
2
,
name
=
'
exam 2
'
,
finalized
=
True
)
db
.
session
.
add
(
exam1
)
db
.
session
.
add
(
exam2
)
db
.
session
.
commit
()
problem1
=
Problem
(
id
=
1
,
name
=
'
Problem 1
'
,
exam_id
=
1
)
problem2
=
Problem
(
id
=
2
,
name
=
'
Problem 2
'
,
exam_id
=
2
)
db
.
session
.
add
(
problem1
)
db
.
session
.
add
(
problem2
)
db
.
session
.
commit
()
problem_widget_1
=
ProblemWidget
(
id
=
1
,
name
=
'
problem widget
'
,
problem_id
=
1
,
page
=
2
,
width
=
100
,
height
=
150
,
x
=
40
,
y
=
200
,
type
=
'
problem_widget
'
)
db
.
session
.
add
(
problem_widget_1
)
db
.
session
.
commit
()
#####################
# Actual test #
#####################
def
test_get_exams
(
test_client
):
mc_option_1
=
{
'
x
'
:
100
,
'
y
'
:
40
,
'
problem_id
'
:
1
,
'
page
'
:
1
,
'
label
'
:
'
a
'
,
'
name
'
:
'
test
'
}
test_client
.
put
(
'
/api/mult-choice/
'
,
data
=
mc_option_1
)
mc_option_2
=
{
'
x
'
:
100
,
'
y
'
:
40
,
'
problem_id
'
:
1
,
'
page
'
:
1
,
'
label
'
:
'
a
'
,
'
name
'
:
'
test
'
}
test_client
.
put
(
'
/api/mult-choice/
'
,
data
=
mc_option_2
)
response
=
test_client
.
get
(
'
/api/exams/1
'
)
data
=
json
.
loads
(
response
.
data
)
assert
len
(
data
[
'
problems
'
][
0
][
'
mc_options
'
])
==
2
This diff is collapsed.
Click to expand it.
tests/test
_api
_mult_choice.py
→
tests/
api/
test_mult_choice.py
+
78
−
48
View file @
c253773e
import
pytest
from
flask
import
Flask
,
json
from
flask
import
json
from
flask
import
Flask
from
zesje.api
import
api_bp
from
zesje.database
import
db
,
Exam
,
Problem
def
mco_json
():
return
{
'
x
'
:
100
,
'
y
'
:
40
,
'
problem_id
'
:
1
,
'
page
'
:
1
,
'
label
'
:
'
a
'
,
'
name
'
:
'
test
'
}
def
add_exam
(
db
,
finalized
=
False
):
# Exam with id 1
exam
=
Exam
(
name
=
'
exam
'
,
finalized
=
finalized
)
db
.
session
.
add
(
exam
)
db
.
session
.
commit
()
return
exam
def
add_problem
(
db
,
exam_id
=
1
):
problem
=
Problem
(
name
=
'
problem
'
,
exam_id
=
exam_id
)
db
.
session
.
add
(
problem
)
db
.
session
.
commit
()
return
problem
@pytest.fixture
(
scope
=
"
module
"
)
def
app
():
app
=
Flask
(
__name__
,
static_folder
=
None
)
...
...
@@ -45,31 +19,67 @@ def app():
with
app
.
app_context
():
db
.
create_all
()
add_problem
(
db
)
add_exam
(
db
)
add_test_data
()
app
.
register_blueprint
(
api_bp
,
url_prefix
=
'
/api
'
)
return
app
@pytest.fixture
def
client
(
app
):
@pytest.fixture
()
def
test_
client
(
app
):
client
=
app
.
test_client
()
yield
client
def
test_get_no_exam
(
client
):
result
=
client
.
get
(
'
/api/mult-choice/1
'
)
def
add_test_data
():
exam1
=
Exam
(
id
=
1
,
name
=
'
exam 1
'
,
finalized
=
False
)
exam2
=
Exam
(
id
=
2
,
name
=
'
exam 2
'
,
finalized
=
True
)
db
.
session
.
add
(
exam1
)
db
.
session
.
add
(
exam2
)
db
.
session
.
commit
()
problem1
=
Problem
(
id
=
1
,
name
=
'
Problem 1
'
,
exam_id
=
1
)
problem2
=
Problem
(
id
=
2
,
name
=
'
Problem 2
'
,
exam_id
=
2
)
db
.
session
.
add
(
problem1
)
db
.
session
.
add
(
problem2
)
db
.
session
.
commit
()
def
mco_json
():
return
{
'
x
'
:
100
,
'
y
'
:
40
,
'
problem_id
'
:
1
,
'
page
'
:
1
,
'
label
'
:
'
a
'
,
'
name
'
:
'
test
'
}
@pytest.fixture
def
mock_exam_finalized
(
monkeypatch
):
monkeypatch
.
setattr
(
db
.
exam
,
'
finalized
'
,
True
)
monkeypatch
.
setattr
(
db
.
mult_choice
.
feedback
.
problem
,
'
exam
'
,
None
)
###################
# Actual tests #
###################
def
test_get_no_exam
(
test_client
):
result
=
test_client
.
get
(
'
/api/mult-choice/1
'
)
data
=
json
.
loads
(
result
.
data
)
assert
data
[
'
message
'
]
==
"
Multiple choice question with id 1 does not exist.
"
def
test_add_exam
(
client
):
def
test_add_exam
(
test_
client
):
req
=
mco_json
()
response
=
client
.
put
(
'
/api/mult-choice/
'
,
data
=
req
)
response
=
test_
client
.
put
(
'
/api/mult-choice/
'
,
data
=
req
)
data
=
json
.
loads
(
response
.
data
)
...
...
@@ -80,15 +90,15 @@ def test_add_exam(client):
assert
data
[
'
status
'
]
==
200
def
test_add_get
(
client
):
def
test_add_get
(
test_
client
):
req
=
mco_json
()
response
=
client
.
put
(
'
/api/mult-choice/
'
,
data
=
req
)
response
=
test_
client
.
put
(
'
/api/mult-choice/
'
,
data
=
req
)
data
=
json
.
loads
(
response
.
data
)
id
=
data
[
'
mult_choice_id
'
]
result
=
client
.
get
(
f
'
/api/mult-choice/
{
id
}
'
)
result
=
test_
client
.
get
(
f
'
/api/mult-choice/
{
id
}
'
)
data
=
json
.
loads
(
result
.
data
)
exp_resp
=
{
...
...
@@ -104,10 +114,10 @@ def test_add_get(client):
assert
exp_resp
==
data
def
test_update_put
(
client
):
def
test_update_put
(
test_
client
):
req
=
mco_json
()
response
=
client
.
put
(
'
/api/mult-choice/
'
,
data
=
req
)
response
=
test_
client
.
put
(
'
/api/mult-choice/
'
,
data
=
req
)
data
=
json
.
loads
(
response
.
data
)
id
=
data
[
'
mult_choice_id
'
]
...
...
@@ -120,29 +130,49 @@ def test_update_put(client):
'
name
'
:
'
test
'
}
result
=
client
.
put
(
f
'
/api/mult-choice/
{
id
}
'
,
data
=
req2
)
result
=
test_
client
.
put
(
f
'
/api/mult-choice/
{
id
}
'
,
data
=
req2
)
data
=
json
.
loads
(
result
.
data
)
assert
data
[
'
message
'
]
==
f
'
Multiple choice question with id
{
id
}
updated
'
def
test_delete
(
client
):
def
test_delete
(
test_
client
):
req
=
mco_json
()
response
=
client
.
put
(
'
/api/mult-choice/
'
,
data
=
req
)
response
=
test_
client
.
put
(
'
/api/mult-choice/
'
,
data
=
req
)
data
=
json
.
loads
(
response
.
data
)
id
=
data
[
'
mult_choice_id
'
]
response
=
client
.
delete
(
f
'
/api/mult-choice/
{
id
}
'
)
response
=
test_
client
.
delete
(
f
'
/api/mult-choice/
{
id
}
'
)
data
=
json
.
loads
(
response
.
data
)
assert
data
[
'
status
'
]
==
200
def
test_delete_not_present
(
client
):
def
test_delete_not_present
(
test_
client
):
id
=
5
response
=
client
.
delete
(
f
'
/api/mult-choice/
{
id
}
'
)
response
=
test_
client
.
delete
(
f
'
/api/mult-choice/
{
id
}
'
)
data
=
json
.
loads
(
response
.
data
)
assert
data
[
'
message
'
]
==
f
'
Multiple choice question with id
{
id
}
does not exist.
'
def
test_delete_finalized_exam
(
test_client
):
mc_option_json
=
{
'
x
'
:
100
,
'
y
'
:
40
,
'
problem_id
'
:
2
,
'
page
'
:
1
,
'
label
'
:
'
a
'
,
'
name
'
:
'
test
'
}
response
=
test_client
.
put
(
'
/api/mult-choice/
'
,
data
=
mc_option_json
)
data
=
json
.
loads
(
response
.
data
)
mc_id
=
data
[
'
mult_choice_id
'
]
response
=
test_client
.
delete
(
f
'
/api/mult-choice/
{
mc_id
}
'
)
data
=
json
.
loads
(
response
.
data
)
assert
data
[
'
status
'
]
==
401
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment