Skip to content
This repository was archived by the owner on Nov 11, 2022. It is now read-only.

Commit c416299

Browse files
Fixed bug with not updating price
1 parent 57fa4d8 commit c416299

File tree

7 files changed

+49
-30
lines changed

7 files changed

+49
-30
lines changed

.github/workflows/run-pytest.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test Django-Rest-Framework application via Unit Test
1+
name: Test Django-Rest-Framework application via PyTest
22

33
on: [push]
44

@@ -28,10 +28,12 @@ jobs:
2828
run: |
2929
python -m pip install --upgrade pip
3030
pip install -r requirements.txt
31+
pip install pytest-django
3132
- name: Run migrations
3233
run: |
3334
python manage.py makemigrations
3435
python manage.py migrate
3536
- name: Run tests
3637
run: |
37-
python unit_test.py
38+
python -m coverage run -m pytest
39+
python -m coverage report

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1. `pip install -r requirements.txt`
1616
2. `coverage run -m pytest`
1717
3. `coverage report` or `coverage html` (for generating report in HTML)
18-
4. `coverage-badge -o coverage.svg`
18+
4. Remove `coverage-badge` file and run `coverage-badge -o coverage.svg`
1919
 
2020

2121
#### To run application locally:

core/settings.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,23 @@
7373
}
7474
}
7575
else:
76+
# DATABASES = {
77+
# 'default': {
78+
# 'ENGINE': 'django.db.backends.postgresql',
79+
# 'NAME': 'd2n3ba1ushpvok',
80+
# 'USER': 'jpdfcrppiqgfut',
81+
# 'PASSWORD': '136a7f5787e41773c75aef3b90d612510dc0951fa63a671608089a569a52385c',
82+
# 'HOST': 'ec2-176-34-215-248.eu-west-1.compute.amazonaws.com',
83+
# 'PORT': '5432',
84+
# }
85+
# }
7686
DATABASES = {
7787
'default': {
7888
'ENGINE': 'django.db.backends.postgresql',
79-
'NAME': 'd2n3ba1ushpvok',
80-
'USER': 'jpdfcrppiqgfut',
81-
'PASSWORD': '136a7f5787e41773c75aef3b90d612510dc0951fa63a671608089a569a52385c',
82-
'HOST': 'ec2-176-34-215-248.eu-west-1.compute.amazonaws.com',
89+
'NAME': 'onlineservice',
90+
'USER': 'postgres',
91+
'PASSWORD': 'root',
92+
'HOST': 'localhost',
8393
'PORT': '5432',
8494
}
8595
}

shop/utils.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,33 @@
44

55

66
def update_parent_prices_after_creating(item_to_start) -> None:
7-
#print('UPDATING')
87
price_to_add: int = item_to_start.price
98

109
if item_to_start.parentId is None:
1110
return
1211

13-
current = item_to_start
1412
nexts = ShopUnit.objects.filter(id=item_to_start.parentId)
1513

1614
while len(nexts) != 0:
1715
parent = nexts[0]
18-
print('PARENT: ', parent.name)
1916
parent.totally_inner_goods_count = parent.totally_inner_goods_count + 1
2017
parent.total_inner_sum = parent.total_inner_sum + price_to_add
2118
parent.price = parent.total_inner_sum // parent.totally_inner_goods_count
2219
parent.save()
23-
current = parent
24-
nexts = ShopUnit.objects.filter(id=current.parentId)
20+
nexts = ShopUnit.objects.filter(id=parent.parentId)
21+
22+
23+
def update_parent_date_after_creating(item_to_start, new_date) -> None:
24+
if item_to_start.parentId is None:
25+
return
26+
27+
nexts = ShopUnit.objects.filter(id=item_to_start.parentId)
28+
29+
while len(nexts) != 0:
30+
parent = nexts[0]
31+
parent.date = new_date
32+
parent.save()
33+
nexts = ShopUnit.objects.filter(id=parent.parentId)
2534

2635

2736
def update_parent_prices_after_deleting(nexts_initial, price_to_subtract: int, goods_count_to_subtract: int) -> None:
@@ -50,6 +59,7 @@ def update_parent_prices_after_item_updating(nexts_initial, price: int, count: i
5059

5160
parent.totally_inner_goods_count += count
5261
parent.total_inner_sum += price
62+
5363
if parent.totally_inner_goods_count != 0:
5464
parent.price = parent.total_inner_sum // parent.totally_inner_goods_count
5565
else:
@@ -61,12 +71,11 @@ def update_parent_prices_after_item_updating(nexts_initial, price: int, count: i
6171

6272

6373
def update_parents_date_after_item_updating(nexts_initial, date: str) -> None:
64-
#print('UPDATING PARENTS DATE')
6574
nexts = nexts_initial
6675

6776
while len(nexts) != 0:
6877
parent = nexts[0]
69-
print('parent ', parent.name)
78+
print(f'UPDATING {parent.name} from {parent.date} to {date}')
7079
parent.date = date
7180
parent.save()
7281
nexts = ShopUnit.objects.filter(id=parent.parentId)
@@ -97,21 +106,15 @@ def create_new_item(item: dict, date: str) -> None:
97106
)
98107

99108
created_object.save()
100-
# print('CREATED')
101-
# print('Now own children are: ', created_object.children.all())
102-
# print('ParentId is: ', created_object.parentId)
103109

104110
if created_object.parentId is not None:
105-
# print('Looking parent for ', created_object.name)
106111
parent = ShopUnit.objects.filter(id=created_object.parentId)[0]
107-
# print('So parent is: ', parent.name)
108112
parent.children.add(created_object)
109-
# print('Now parents children are: ', parent.children.all())
110-
# print('And own children are: ', created_object.children.all())
111113
parent.save()
112114

113115
if created_object.type != SHOP_UNIT_TYPES[0][0]:
114116
update_parent_prices_after_creating(created_object)
117+
update_parent_date_after_creating(created_object, date)
115118

116119

117120
def update_existing_item(item: dict, date: str) -> None:
@@ -127,6 +130,7 @@ def update_existing_item(item: dict, date: str) -> None:
127130

128131
existing_item.name = item['name']
129132
existing_item.date = date
133+
existing_item.save()
130134

131135
if item['type'] != SHOP_UNIT_TYPES[0][0]:
132136
existing_item.price = item['price']
@@ -145,8 +149,8 @@ def update_existing_item(item: dict, date: str) -> None:
145149
new_parent.children.add(existing_item)
146150
new_parent.save()
147151

148-
#print('KEK')
149152
update_parent_prices_after_item_updating(ShopUnit.objects.filter(id=existing_item.parentId), existing_item.total_inner_sum, existing_item.totally_inner_goods_count)
153+
# print(f'NOW WILL UPDATE PARENT DATE TO {date}')
150154
update_parents_date_after_item_updating(ShopUnit.objects.filter(id=existing_item.parentId), date)
151155

152156
existing_item.save()

shop/validators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def check_items_for_import(items_to_check: list, update_date: str) -> bool:
3939
# check that all ids are different
4040
if item_to_check['id'] in ids_set:
4141
return False
42+
4243
ids_set.add(item_to_check['id'])
4344

4445
# check parent

shop/views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ def post(self, request: Request) -> Response:
2424
if not check_result:
2525
return Response(Error(400, 'Validation Failed').to_dict(), status=status.HTTP_400_BAD_REQUEST)
2626

27-
existing_units = ShopUnit.objects
28-
2927
for item in received_items:
28+
existing_units = ShopUnit.objects
3029
with_similar_id = existing_units.filter(id=item['id'])
30+
print(existing_units)
3131

3232
if len(with_similar_id) == 0:
3333
# print('CREATING NEW: ', item['name'])
3434
create_new_item(item, received_update_date)
3535
elif len(with_similar_id) == 1:
36+
# print(f'VIEW: GONNA UPDATE {item["name"]}')
3637
update_existing_item(item, received_update_date)
3738

3839
return Response(status=status.HTTP_200_OK)

unit_test.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# encoding=utf8
2-
31
import json
42
import re
53
import subprocess
@@ -8,7 +6,8 @@
86
import urllib.parse
97
import urllib.request
108

11-
API_BASEURL = "http://localhost:80"
9+
API_BASEURL = 'http://127.0.0.1:8000'
10+
1211

1312
ROOT_ID = "069cb8d7-bbdd-47d3-ad8f-82ef4c269df1"
1413

@@ -225,7 +224,7 @@ def test_nodes():
225224
status, response = request(f"/nodes/{ROOT_ID}", json_response=True)
226225
# print(json.dumps(response, indent=2, ensure_ascii=False))
227226

228-
assert status == 200, f"Expected HTTP status code 200, got {status}"
227+
assert status == 200, f'Expected HTTP status code 200, got {status}'
229228

230229
deep_sort_children(response)
231230
deep_sort_children(EXPECTED_TREE)
@@ -235,14 +234,16 @@ def test_nodes():
235234
print("Response tree doesn't match expected tree.")
236235
sys.exit(1)
237236

238-
print("Test nodes passed.")
237+
print('Test nodes passed.')
239238

240239

241240
def test_sales():
242241
params = urllib.parse.urlencode({
243242
"date": "2022-02-04T00:00:00.000Z"
244243
})
245-
status, response = request(f"/sales?{params}", json_response=True)
244+
status, response = request(f'/sales?{params}', json_response=True)
245+
# print(response)
246+
# print(status)
246247
assert status == 200, f"Expected HTTP status code 200, got {status}"
247248
print("Test sales passed.")
248249

0 commit comments

Comments
 (0)