Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
atdb-ldv
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ASTRON SDC
atdb-ldv
Commits
e0b4dfc8
Commit
e0b4dfc8
authored
10 months ago
by
Nico Vermaas
Browse files
Options
Downloads
Patches
Plain Diff
add unittests
parent
493e9eb4
No related branches found
No related tags found
1 merge request
!367
update unit-tests branch with latest from master
Pipeline
#91896
passed
10 months ago
Stage: test
Stage: build
Stage: deploy_to_test
Stage: deploy_to_production
Changes
1
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
atdb/taskdatabase/tests/test_views.py
+90
-22
90 additions, 22 deletions
atdb/taskdatabase/tests/test_views.py
with
90 additions
and
22 deletions
atdb/taskdatabase/tests/test_views.py
+
90
−
22
View file @
e0b4dfc8
from
django.test
import
TestCase
,
RequestFactory
from
django.test
import
TestCase
,
RequestFactory
from
django.urls
import
reverse
from
django.urls
import
reverse
from
django.http
import
HttpResponse
from
django.shortcuts
import
redirect
from
django.contrib.auth.models
import
User
from
django.contrib.sessions.middleware
import
SessionMiddleware
from
taskdatabase.models
import
Task
,
Workflow
from
taskdatabase.models
import
Task
,
Workflow
from
taskdatabase.views
import
SortTasks
from
taskdatabase.views
import
SortTasks
,
TaskMultiStatus
from
taskdatabase.forms
import
DiscardAnnotationForm
from
unittest.mock
import
patch
,
MagicMock
class
TestViews
(
TestCase
):
class
TestViews
(
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
...
@@ -15,22 +19,86 @@ class TestViews(TestCase):
...
@@ -15,22 +19,86 @@ class TestViews(TestCase):
workflow_requantisation
.
save
()
workflow_requantisation
.
save
()
# Create a test task
# Create a test task
self
.
task1
=
Task
.
objects
.
create
(
filter
=
'
a
'
,
sas_id
=
456
,
status
=
'
stored
'
,
workflow
=
workflow_requantisation
,
priority
=
1
)
self
.
task1
=
Task
.
objects
.
create
(
sas_id
=
456
,
status
=
'
defined
'
,
workflow
=
workflow_requantisation
)
self
.
task2
=
Task
.
objects
.
create
(
filter
=
'
b
'
,
sas_id
=
123
,
status
=
'
stored
'
,
workflow
=
workflow_requantisation
,
priority
=
1
)
self
.
task2
=
Task
.
objects
.
create
(
sas_id
=
123
,
status
=
'
defined
'
,
workflow
=
workflow_requantisation
)
# def test_sort(self):
self
.
user
=
User
.
objects
.
create_user
(
username
=
'
testuser
'
,
password
=
'
testpass
'
)
# # Set up the URL for the view
# #url = reverse('sort-tasks', kwargs={'sort': 'sas_id', 'redirect_to_page': 'atdb'})
self
.
session_data
=
{
#
'
filtered_tasks_as_list
'
:
[
self
.
task1
.
id
,
self
.
task2
.
id
],
# # Create a request object
'
current_query_params
'
:
'
status=defined
'
# request = self.factory.get('/dummy-url')
}
#
# # Call the function with sort='priority' and redirect_to_page='tasks_list'
def
_set_up_session
(
self
,
request
):
# response = SortTasks(request, sort='sas_id', redirect_to_page='atdb')
"""
Helper function to set up session for the request
"""
#
middleware
=
SessionMiddleware
(
get_response
=
lambda
r
:
None
)
# # Check if the sort field is correctly stored in the session
middleware
.
process_request
(
request
)
# self.assertEqual(request.session['sort'], 'name')
request
.
session
.
update
(
self
.
session_data
)
#
request
.
session
.
save
()
# # Check if it redirects to the 'index' page
# self.assertEqual(response.status_code, 302)
def
test_sort
(
self
):
# self.assertEqual(response.url, reverse('atdb'))
# Set up the URL for the view
\ No newline at end of file
# Arrange
# Create a request object
request
=
self
.
factory
.
get
(
'
/dummy-url
'
)
self
.
_set_up_session
(
request
)
# Act
# Call the function with sort='priority' and redirect_to_page='tasks_list'
response
=
SortTasks
(
request
,
sort
=
'
sas_id
'
,
redirect_to_page
=
'
atdb
'
)
# Assert
# Check if the sort field is correctly stored in the session
self
.
assertEqual
(
request
.
session
[
'
sort
'
],
'
sas_id
'
)
# Check if it redirects to the 'index' page
self
.
assertEqual
(
response
.
status_code
,
302
)
self
.
assertEqual
(
response
.
url
,
reverse
(
'
index
'
))
@patch
(
'
taskdatabase.views.Task.objects.get
'
)
@patch
(
'
taskdatabase.views.DiscardAnnotationForm.is_valid
'
)
def
test_task_multi_status_post_discard
(
self
,
mock_is_valid
,
mock_get
):
# Arrange
# Mock the Task.objects.get method to return mock tasks
mock_get
.
side_effect
=
lambda
id
:
self
.
task1
if
id
==
self
.
task1
.
id
else
self
.
task2
mock_is_valid
.
return_value
=
True
request
=
self
.
factory
.
post
(
'
/dummy-url
'
,
data
=
{
'
annotation
'
:
'
test annotation
'
})
self
.
_set_up_session
(
request
)
request
.
user
=
self
.
user
# Act
# Call the function with new_status='discarded'
response
=
TaskMultiStatus
(
request
,
new_status
=
'
discarded
'
,
query_params
=
'
status=defined
'
)
# Assert
# Check that the tasks were updated correctly
self
.
assertEqual
(
self
.
task1
.
status
,
'
discarded
'
)
self
.
assertEqual
(
self
.
task2
.
status
,
'
discarded
'
)
self
.
assertEqual
(
self
.
task1
.
remarks
[
'
discard_reason
'
],
'
test annotation
'
)
self
.
assertEqual
(
self
.
task2
.
remarks
[
'
discard_reason
'
],
'
test annotation
'
)
# Check if it redirects to the correct URL
self
.
assertEqual
(
response
.
status_code
,
302
)
self
.
assertEqual
(
response
.
url
,
reverse
(
'
query
'
)
+
'
?
'
+
'
status=defined
'
)
@patch
(
'
taskdatabase.views.convert_query_params_to_url
'
)
def
test_task_multi_status_get_discard
(
self
,
mock_query_params
):
# Arrange
mock_query_params
.
return_value
=
"
&status=defined
"
request
=
self
.
factory
.
get
(
'
/dummy-url
'
)
self
.
_set_up_session
(
request
)
request
.
user
=
self
.
user
expected_params_on_session
=
"
&status=defined
"
# Act
# Call the function with new_status='discarded'
response
=
TaskMultiStatus
(
request
,
new_status
=
'
discarded
'
,
query_params
=
'
status=defined
'
)
# Assert
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
request
.
session
[
'
current_query_params
'
],
expected_params_on_session
)
\ No newline at end of file
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