Commit dd7cb810 authored by Erlend Ydse's avatar Erlend Ydse
Browse files

Extract methods from update for readability

parent 3d4355a9
Pipeline #126961 passed with stage
in 1 minute and 9 seconds
...@@ -6,12 +6,13 @@ from rest_framework.test import APIClient, APIRequestFactory, APITestCase ...@@ -6,12 +6,13 @@ from rest_framework.test import APIClient, APIRequestFactory, APITestCase
from workouts.models import Exercise from workouts.models import Exercise
from sys import maxsize from sys import maxsize
from django.db.utils import IntegrityError from django.db.utils import IntegrityError
from PIL import Image
class WorkoutCreationTests(APITestCase): class WorkoutCreationTests(APITestCase):
def setUp(self): def setUp(self):
self.path = "/api/workouts/" self.path = "/api/workouts/"
self.client = APIClient() self.client = APIClient()
now = timezone.now() self.now = timezone.now()
self.registration_data = { self.registration_data = {
"username": "alice", "username": "alice",
...@@ -44,7 +45,7 @@ class WorkoutCreationTests(APITestCase): ...@@ -44,7 +45,7 @@ class WorkoutCreationTests(APITestCase):
self.data = { self.data = {
"name": "wBR", "name": "wBR",
"date": now, "date": self.now,
"notes": "Good workout", "notes": "Good workout",
"visibility": "PR", "visibility": "PR",
"exercise_instances": [self.exerciseA]} "exercise_instances": [self.exerciseA]}
...@@ -59,7 +60,7 @@ class WorkoutCreationTests(APITestCase): ...@@ -59,7 +60,7 @@ class WorkoutCreationTests(APITestCase):
self.data = { self.data = {
"name": "wA", "name": "wA",
"date": now, "date": self.now,
"notes": "Good workout", "notes": "Good workout",
"visibility": "PU", "visibility": "PU",
"exercise_instances": [self.exerciseA]} "exercise_instances": [self.exerciseA]}
...@@ -69,6 +70,12 @@ class WorkoutCreationTests(APITestCase): ...@@ -69,6 +70,12 @@ class WorkoutCreationTests(APITestCase):
self.access_token = response.data["access"] self.access_token = response.data["access"]
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}") self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}")
def pretty_print_response(self, response):
if "status_code" in response.__dict__:
print(response.status_code)
if "data" in response.__dict__:
print(response.data)
### Black box FR5 ### Black box FR5
def test_create_workout_normal_ok(self): def test_create_workout_normal_ok(self):
...@@ -202,3 +209,18 @@ class WorkoutCreationTests(APITestCase): ...@@ -202,3 +209,18 @@ class WorkoutCreationTests(APITestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], self.pu_workouts + 1) self.assertEqual(response.data["count"], self.pu_workouts + 1)
# Regression tests
def test_create_workout_then_update(self):
data = {
"name": "wBF",
"date": self.now,
"notes": "Good workout",
"visibility": "PR",
"exercise_instances": [self.exerciseA],
"files": []}
response = self.client.post(self.path, data=data, format="json")
self.assertEqual(response.status_code, 201)
data["notes"] = "Bad workout"
response = self.client.put(f"{self.path}{response.data['id']}/", data=data, format="json")
self.assertEqual(response.status_code, 200)
\ No newline at end of file
...@@ -131,8 +131,16 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer): ...@@ -131,8 +131,16 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
instance.date = validated_data.get("date", instance.date) instance.date = validated_data.get("date", instance.date)
instance.save() instance.save()
# Handle ExerciseInstances self.handle_exercise_instances(
instance=instance,
exercise_instances=exercise_instances,
exercise_instances_data=exercise_instances_data)
self.handle_workout_files(instance=instance, validated_data=validated_data)
return instance
def handle_exercise_instances(self, instance, exercise_instances, exercise_instances_data):
# This updates existing exercise instances without adding or deleting object. # This updates existing exercise instances without adding or deleting object.
# zip() will yield n 2-tuples, where n is # zip() will yield n 2-tuples, where n is
# min(len(exercise_instance), len(exercise_instance_data)) # min(len(exercise_instance), len(exercise_instance_data))
...@@ -161,9 +169,8 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer): ...@@ -161,9 +169,8 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
elif len(exercise_instances_data) < len(exercise_instances.all()): elif len(exercise_instances_data) < len(exercise_instances.all()):
for i in range(len(exercise_instances_data), len(exercise_instances.all())): for i in range(len(exercise_instances_data), len(exercise_instances.all())):
exercise_instances.all()[i].delete() exercise_instances.all()[i].delete()
# Handle WorkoutFiles def handle_workout_files(self, instance, validated_data):
if "files" in validated_data: if "files" in validated_data:
files_data = validated_data.pop("files") files_data = validated_data.pop("files")
files = instance.files files = instance.files
...@@ -184,8 +191,6 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer): ...@@ -184,8 +191,6 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
for i in range(len(files_data), len(files.all())): for i in range(len(files_data), len(files.all())):
files.all()[i].delete() files.all()[i].delete()
return instance
def get_owner_username(self, obj): def get_owner_username(self, obj):
"""Returns the owning user's username """Returns the owning user's username
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment