From 64fb7c701dec37b4b62fc4df2f81826e143d8433 Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Mon, 2 Aug 2021 15:11:50 +0100 Subject: [PATCH 1/2] Stop clearing all appointments --- tcsocket/app/views/appointments.py | 6 +++-- tests/test_appointments_set.py | 40 +++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/tcsocket/app/views/appointments.py b/tcsocket/app/views/appointments.py index 324634f..651eb0c 100644 --- a/tcsocket/app/views/appointments.py +++ b/tcsocket/app/views/appointments.py @@ -153,8 +153,10 @@ async def appointment_webhook_delete(request): async def appointment_webhook_clear(request): conn = await request['conn_manager'].get_connection() - v = await conn.execute(sa_appointments.delete().where(ser_c.company == request['company'].id)) - r = await conn.execute(sa_services.delete().where(ser_c.company == request['company'].id)) + services = await conn.execute(select([ser_c.id]).where(ser_c.company == request['company'].id)) + ids = [s[0] async for s in services] + v = await conn.execute(sa_appointments.delete().where(apt_c.service.in_(ids))) + r = await conn.execute(sa_services.delete().where(ser_c.id.in_(ids))) return json_response(request, status='success' if r.rowcount or v.rowcount else 'appointments not found') diff --git a/tests/test_appointments_set.py b/tests/test_appointments_set.py index 371ce98..f071df9 100644 --- a/tests/test_appointments_set.py +++ b/tests/test_appointments_set.py @@ -174,6 +174,25 @@ async def test_delete_old_appointments(db_conn, company, settings): async def test_clear_apts(cli, db_conn, company): + company2 = await create_company(db_conn, 'compan2_public', 'compan2_private', name='company2') + await db_conn.execute( + sa_services.insert().values( + **dict( + id=2, + company=company2.id, + name='testing service', + extra_attributes=[ + { + 'name': 'Foobar', + 'type': 'text_short', + 'machine_name': 'foobar', + 'value': 'this is the value of foobar', + } + ], + colour='#abc', + ) + ) + ) await create_appointment(db_conn, company, appointment_extra={'id': 1}) for i in range(10): await create_appointment( @@ -187,16 +206,29 @@ async def test_clear_apts(cli, db_conn, company): ), ) - assert 11 == await count(db_conn, sa_appointments) - assert 1 == await count(db_conn, sa_services) + for i in range(11, 21): + await create_appointment( + db_conn, + company2, + create_service=False, + appointment_extra=dict( + id=i + 2, + start=datetime(2032, 1, 1, 12, 0, 0) + timedelta(days=i + 1), + finish=datetime(2032, 1, 1, 13, 0, 0) + timedelta(days=i + 1), + ), + service_extra=dict(id=2) + ) + + assert 21 == await count(db_conn, sa_appointments) + assert 2 == await count(db_conn, sa_services) url = cli.server.app.router['webhook-appointment-clear'].url_for(company='thepublickey') r = await signed_request(cli, url, method_='DELETE') assert r.status == 200 assert {'status': 'success'} == await r.json() - assert 0 == await count(db_conn, sa_appointments) - assert 0 == await count(db_conn, sa_services) + assert 10 == await count(db_conn, sa_appointments) + assert 1 == await count(db_conn, sa_services) async def test_mass_apts(cli, db_conn, company): From cc902396992a2229a6e66b250bacdaac6715627a Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Mon, 2 Aug 2021 15:30:09 +0100 Subject: [PATCH 2/2] aioredis 1.3.1 --- tcsocket/requirements.txt | 1 + tests/test_appointments_set.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tcsocket/requirements.txt b/tcsocket/requirements.txt index b7d67ef..3fedd01 100644 --- a/tcsocket/requirements.txt +++ b/tcsocket/requirements.txt @@ -2,6 +2,7 @@ SQLAlchemy==1.3.23 aiodns==2.0.0 aiohttp==3.7.4.post0 aiopg==1.1.0 +aioredis==1.3.1 arq==0.21 boto3==1.17.62 cchardet==2.1.7 diff --git a/tests/test_appointments_set.py b/tests/test_appointments_set.py index f071df9..20f1779 100644 --- a/tests/test_appointments_set.py +++ b/tests/test_appointments_set.py @@ -216,7 +216,7 @@ async def test_clear_apts(cli, db_conn, company): start=datetime(2032, 1, 1, 12, 0, 0) + timedelta(days=i + 1), finish=datetime(2032, 1, 1, 13, 0, 0) + timedelta(days=i + 1), ), - service_extra=dict(id=2) + service_extra=dict(id=2), ) assert 21 == await count(db_conn, sa_appointments)