Skip to content

Commit 9923093

Browse files
author
Andrea Cioni
committed
test and script update
1 parent 5420308 commit 9923093

File tree

3 files changed

+200
-7
lines changed

3 files changed

+200
-7
lines changed

scripts/set_offline_id.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pymongo
55
import bson
66
import uuid
7+
from datetime import date
78

89
parser = argparse.ArgumentParser()
910
parser.add_argument('mongo_url',
@@ -30,7 +31,7 @@
3031

3132
for doc in documents:
3233
print('Update doc', doc['_id'])
33-
db[coll_name].update_one({'_id': doc['_id']}, {'$set': {'offline_id': str(uuid.uuid4())}})
34+
db[coll_name].update_one({'_id': doc['_id']}, {'$set': {'offline_id': str(uuid.uuid4()), 'create_timestamp': int(datetime.utcnow().timestamp()*1000), 'update_timestamp': int(datetime.utcnow().timestamp()*1000)}})
3435
print('Updated document', doc['_id'])
3536

3637

tests/test_recipe.py

+99-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def replace_recipe(client, recipe_id, json, auth_headers):
3030
def get_recipe(client, recipe_id, auth_headers):
3131
return client.get('/api/v1/recipes/{}'.format(recipe_id), headers=auth_headers)
3232

33-
def get_all_recipes(client, auth_headers, page=1, per_page=10):
34-
return client.get('/api/v1/recipes?page={}&per_page={}'.format(page, per_page), headers=auth_headers)
33+
def get_all_recipes(client, auth_headers, page=1, per_page=10, order_by='', desc=False):
34+
return client.get('/api/v1/recipes?page={}&per_page={}&order_by={}&desc={}'.format(page, per_page, order_by, desc), headers=auth_headers)
3535

3636
def test_not_authorized(client: FlaskClient):
3737
response = get_all_recipes(client, {})
@@ -312,4 +312,100 @@ def test_create_update_timestamp(client: FlaskClient, auth_headers):
312312
assert response.status_code == 200 \
313313
and response.json['name'] == 'Tomato' \
314314
and response.json['insert_timestamp'] == insert_timestamp \
315-
and response.json['update_timestamp'] > update_timestamp
315+
and response.json['update_timestamp'] > update_timestamp
316+
317+
def test_get_last_updated(client: FlaskClient, auth_headers):
318+
response = create_recipe(client, {
319+
'name': 'Rice',
320+
}, auth_headers)
321+
322+
assert response.status_code == 201
323+
324+
idx_1 = response.json['_id']
325+
insert_timestamp_1 = response.json['insert_timestamp']
326+
update_timestamp_1 = response.json['update_timestamp']
327+
328+
response = create_recipe(client, {
329+
'name': 'Tomato',
330+
}, auth_headers)
331+
332+
assert response.status_code == 201
333+
334+
idx_2 = response.json['_id']
335+
insert_timestamp_2 = response.json['insert_timestamp']
336+
update_timestamp_2 = response.json['update_timestamp']
337+
338+
response = get_all_recipes(client, auth_headers, order_by='update_timestamp', desc=True, page=1, per_page=1)
339+
340+
assert response.status_code == 200 \
341+
and len(response.json['results']) == 1 \
342+
and response.json['results'][0]['_id'] == idx_2
343+
344+
response = patch_recipe(client, idx_1, {
345+
'name': 'Rice',
346+
}, auth_headers)
347+
348+
assert response.status_code == 200 \
349+
and response.json['insert_timestamp'] == insert_timestamp_1 \
350+
and response.json['update_timestamp'] > update_timestamp_1
351+
352+
update_timestamp_1 = response.json['update_timestamp']
353+
354+
response = get_all_recipes(client, auth_headers, order_by='update_timestamp', desc=True, page=1, per_page=1)
355+
356+
assert response.status_code == 200 \
357+
and len(response.json['results']) == 1 \
358+
and response.json['results'][0]['_id'] == idx_1 \
359+
and response.json['results'][0]['update_timestamp'] == update_timestamp_1
360+
361+
response = put_recipe(client, idx_1, {
362+
'name': 'Rice',
363+
}, auth_headers)
364+
365+
assert response.status_code == 200 \
366+
and response.json['insert_timestamp'] == insert_timestamp_1 \
367+
and response.json['update_timestamp'] > update_timestamp_1
368+
369+
update_timestamp_1 = response.json['update_timestamp']
370+
371+
response = get_all_recipes(client, auth_headers, order_by='update_timestamp', desc=True, page=1, per_page=1)
372+
373+
assert response.status_code == 200 \
374+
and len(response.json['results']) == 1 \
375+
and response.json['results'][0]['_id'] == idx_1 \
376+
and response.json['results'][0]['update_timestamp'] == update_timestamp_1
377+
378+
response = patch_recipe(client, idx_2, {
379+
'name': 'Tomato',
380+
}, auth_headers)
381+
382+
assert response.status_code == 200 \
383+
and response.json['insert_timestamp'] == insert_timestamp_2 \
384+
and response.json['update_timestamp'] > update_timestamp_2
385+
386+
update_timestamp_2 = response.json['update_timestamp']
387+
388+
response = get_all_recipes(client, auth_headers, order_by='update_timestamp', desc=True, page=1, per_page=1)
389+
390+
assert response.status_code == 200 \
391+
and len(response.json['results']) == 1 \
392+
and response.json['results'][0]['_id'] == idx_2 \
393+
and response.json['results'][0]['update_timestamp'] == update_timestamp_2
394+
395+
response = put_recipe(client, idx_2, {
396+
'name': 'Tomato',
397+
}, auth_headers)
398+
399+
assert response.status_code == 200 \
400+
and response.json['insert_timestamp'] == insert_timestamp_2 \
401+
and response.json['update_timestamp'] > update_timestamp_2
402+
403+
update_timestamp_2 = response.json['update_timestamp']
404+
405+
response = get_all_recipes(client, auth_headers, order_by='update_timestamp', desc=True, page=1, per_page=1)
406+
407+
assert response.status_code == 200 \
408+
and len(response.json['results']) == 1 \
409+
and response.json['results'][0]['_id'] == idx_2 \
410+
and response.json['results'][0]['update_timestamp'] == update_timestamp_2
411+

tests/test_shopping_list.py

+99-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def delete_item_in_shopping_list(client, shopping_list_id, shopping_list_item_id
3636
def get_shopping_list(client, shopping_list_id, auth_headers):
3737
return client.get('/api/v1/shopping-lists/{}'.format(shopping_list_id), headers=auth_headers)
3838

39-
def get_all_shopping_list(client, auth_headers, page=1, per_page=10):
40-
return client.get('/api/v1/shopping-lists?page={}&per_page={}'.format(page, per_page), headers=auth_headers)
39+
def get_all_shopping_list(client, auth_headers, page=1, per_page=10, order_by='', desc=False):
40+
return client.get('/api/v1/shopping-lists?page={}&per_page={}&order_by={}&desc={}'.format(page, per_page, order_by, desc), headers=auth_headers)
4141

4242
def patch_shopping_list(client, shopping_list_id, json, auth_headers):
4343
return client.patch('/api/v1/shopping-lists/{}'.format(shopping_list_id), json=json, headers=auth_headers)
@@ -524,4 +524,100 @@ def test_create_update_timestamp(client: FlaskClient, auth_headers):
524524
assert response.status_code == 200 \
525525
and response.json['name'] == 'Tomato' \
526526
and response.json['insert_timestamp'] == insert_timestamp \
527-
and response.json['update_timestamp'] > update_timestamp
527+
and response.json['update_timestamp'] > update_timestamp
528+
529+
def test_get_last_updated(client: FlaskClient, auth_headers):
530+
response = create_shopping_list(client, {
531+
'name': 'Rice',
532+
}, auth_headers)
533+
534+
assert response.status_code == 201
535+
536+
idx_1 = response.json['_id']
537+
insert_timestamp_1 = response.json['insert_timestamp']
538+
update_timestamp_1 = response.json['update_timestamp']
539+
540+
response = create_shopping_list(client, {
541+
'name': 'Tomato',
542+
}, auth_headers)
543+
544+
assert response.status_code == 201
545+
546+
idx_2 = response.json['_id']
547+
insert_timestamp_2 = response.json['insert_timestamp']
548+
update_timestamp_2 = response.json['update_timestamp']
549+
550+
response = get_all_shopping_list(client, auth_headers, order_by='update_timestamp', desc=True, page=1, per_page=1)
551+
552+
assert response.status_code == 200 \
553+
and len(response.json['results']) == 1 \
554+
and response.json['results'][0]['_id'] == idx_2
555+
556+
response = patch_shopping_list(client, idx_1, {
557+
'name': 'Rice',
558+
}, auth_headers)
559+
560+
assert response.status_code == 200 \
561+
and response.json['insert_timestamp'] == insert_timestamp_1 \
562+
and response.json['update_timestamp'] > update_timestamp_1
563+
564+
update_timestamp_1 = response.json['update_timestamp']
565+
566+
response = get_all_shopping_list(client, auth_headers, order_by='update_timestamp', desc=True, page=1, per_page=1)
567+
568+
assert response.status_code == 200 \
569+
and len(response.json['results']) == 1 \
570+
and response.json['results'][0]['_id'] == idx_1 \
571+
and response.json['results'][0]['update_timestamp'] == update_timestamp_1
572+
573+
response = put_shopping_list(client, idx_1, {
574+
'name': 'Rice',
575+
}, auth_headers)
576+
577+
assert response.status_code == 200 \
578+
and response.json['insert_timestamp'] == insert_timestamp_1 \
579+
and response.json['update_timestamp'] > update_timestamp_1
580+
581+
update_timestamp_1 = response.json['update_timestamp']
582+
583+
response = get_all_shopping_list(client, auth_headers, order_by='update_timestamp', desc=True, page=1, per_page=1)
584+
585+
assert response.status_code == 200 \
586+
and len(response.json['results']) == 1 \
587+
and response.json['results'][0]['_id'] == idx_1 \
588+
and response.json['results'][0]['update_timestamp'] == update_timestamp_1
589+
590+
response = patch_shopping_list(client, idx_2, {
591+
'name': 'Tomato',
592+
}, auth_headers)
593+
594+
assert response.status_code == 200 \
595+
and response.json['insert_timestamp'] == insert_timestamp_2 \
596+
and response.json['update_timestamp'] > update_timestamp_2
597+
598+
update_timestamp_2 = response.json['update_timestamp']
599+
600+
response = get_all_shopping_list(client, auth_headers, order_by='update_timestamp', desc=True, page=1, per_page=1)
601+
602+
assert response.status_code == 200 \
603+
and len(response.json['results']) == 1 \
604+
and response.json['results'][0]['_id'] == idx_2 \
605+
and response.json['results'][0]['update_timestamp'] == update_timestamp_2
606+
607+
response = put_shopping_list(client, idx_2, {
608+
'name': 'Tomato',
609+
}, auth_headers)
610+
611+
assert response.status_code == 200 \
612+
and response.json['insert_timestamp'] == insert_timestamp_2 \
613+
and response.json['update_timestamp'] > update_timestamp_2
614+
615+
update_timestamp_2 = response.json['update_timestamp']
616+
617+
response = get_all_shopping_list(client, auth_headers, order_by='update_timestamp', desc=True, page=1, per_page=1)
618+
619+
assert response.status_code == 200 \
620+
and len(response.json['results']) == 1 \
621+
and response.json['results'][0]['_id'] == idx_2 \
622+
and response.json['results'][0]['update_timestamp'] == update_timestamp_2
623+

0 commit comments

Comments
 (0)