Private
Public Access
1
0

mocking c2import successfully

This commit is contained in:
Sander Roosendaal
2018-07-01 13:23:24 +02:00
parent e8dd12c8b8
commit cbaada7945
11 changed files with 1804 additions and 57 deletions

View File

@@ -1,6 +1,7 @@
from __future__ import print_function
from bs4 import BeautifulSoup
import re
from django.test import TestCase, Client,override_settings
from django.core.management import call_command
from django.utils.six import StringIO
@@ -22,9 +23,11 @@ from rowers.tasks import handle_makeplot
from rowers.utils import serialize_list,deserialize_list
from rowers.utils import NoTokenError
from shutil import copyfile
from nose.tools import assert_true
from mock import Mock, patch
from minimocktest import MockTestCase
import pandas as pd
import rowers.c2stuff as c2stuff
import json
import numpy as np
@@ -51,7 +54,62 @@ class DjangoTestCase(TestCase, MockTestCase):
# Create your tests here.
def mocked_requests_get(*args, **kwargs):
with open('rowers/testdata/c2jsonworkoutdata.txt','r') as infile:
c2workoutdata = json.load(infile)
with open('rowers/testdata/c2jsonstrokedata.txt','r') as infile:
c2strokedata = json.load(infile)
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return self.json_data
if 'log.concept2.com/' in args[0]:
if 'strokes' in args[0]:
return MockResponse(c2strokedata,200)
else:
return MockResponse(c2workoutdata,200)
return MockResponse(None,404)
class C2Objects(DjangoTestCase):
def setUp(self):
self.c = Client()
self.u = User.objects.create_user('john',
'sander@ds.ds',
'koeinsloot')
self.u.first_name = 'John'
self.u.last_name = 'Sander'
self.u.save()
self.r = Rower.objects.create(user=self.u,gdproptin=True,
gdproptindate=timezone.now()
)
self.r.c2token = '12'
self.r.tokenexpirydate = datetime.datetime.now()+datetime.timedelta(days=1)
self.r.save()
@patch('rowers.c2stuff.requests.get', side_effect=mocked_requests_get)
def test_c2_import(self, mock_get):
self.c.login(username='john',password='koeinsloot')
response = self.c.get('/rowers/workout/c2import/12/',follow=True)
self.assertRedirects(response,
expected_url='/rowers/workout/1/edit',
status_code=302,target_status_code=200)
self.assertEqual(response.status_code, 200)
def test_strokedata(self):
with open('rowers/testdata/c2stroketestdata.txt','r') as infile:
res = json.load(infile)
@@ -65,17 +123,7 @@ class C2Objects(DjangoTestCase):
from rowers.views import add_workout_from_strokedata
u = User.objects.create_user('john',
'sander@ds.ds',
'koeinsloot')
u.first_name = 'John'
u.last_name = 'Sander'
u.save()
r = Rower.objects.create(user=u,gdproptin=True,
gdproptindate=timezone.now()
)
res = add_workout_from_strokedata(u,1,data,strokedata,source='c2')
res = add_workout_from_strokedata(self.u,1,data,strokedata,source='c2')
def test_strokedatanohr(self):
with open('rowers/testdata/c2strokedatanohr.txt','r') as infile:
@@ -90,18 +138,8 @@ class C2Objects(DjangoTestCase):
from rowers.views import add_workout_from_strokedata
u = User.objects.create_user('john',
'sander@ds.ds',
'koeinsloot')
u.first_name = 'John'
u.last_name = 'Sander'
u.save()
r = Rower.objects.create(user=u,gdproptin=True,
gdproptindate=timezone.now()
)
res = add_workout_from_strokedata(u,1,data,strokedata,source='c2')
res = add_workout_from_strokedata(self.u,1,data,strokedata,source='c2')
class StravaObjects(DjangoTestCase):