Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative weight validation #5564

Merged
merged 3 commits into from
Apr 29, 2020
Merged

Negative weight validation #5564

merged 3 commits into from
Apr 29, 2020

Conversation

fowczarek
Copy link
Member

@fowczarek fowczarek commented Apr 29, 2020

I want to merge this change because adding negative weight validation and fix 0 value in weight scalar.

Impact

  • New migrations
  • New/Updated API fields or mutations
  • Deprecated API fields or mutations
  • Removed API types, fields, or mutations

Pull Request Checklist

  • Privileged queries and mutations are guarded by proper permission checks
  • Database queries are optimized and the number of queries is constant
  • Database migration files are up to date
  • The changes are tested
  • GraphQL schema and type definitions are up to date
  • Changes are mentioned in the changelog

@fowczarek fowczarek self-assigned this Apr 29, 2020
@db-queries
Copy link

db-queries bot commented Apr 29, 2020

Here is the report for 94308f0 (mirumee/saleor @ negative_weight_validation)
Base comparison is c3b771e.

No differences were found. (click me)

# api.benchmark account
  test name                                                  	left count 	right count	duplicate count
  -----------------------------------------------------------	-----------	-----------	---------------
  delete staff members                                       	         32	         32	              0
  query staff user                                           	         21	         21	              4
  staff create                                               	         24	         24	              5
  staff update groups and permissions                        	         36	         36	              6

# api.benchmark category
  test name                                                  	left count 	right count	duplicate count
  -----------------------------------------------------------	-----------	-----------	---------------
  category view                                              	         18	         18	              1

# api.benchmark checkout mutations
  test name                                                  	left count 	right count	duplicate count
  -----------------------------------------------------------	-----------	-----------	---------------
  add billing address to checkout                            	         52	         52	             26
  add shipping to checkout                                   	         40	         40	             12
  checkout email update                                      	         29	         29	             13
  checkout payment charge                                    	         23	         23	              5
  checkout shipping address update                           	         35	         35	              8
  checkout voucher code                                      	         59	         59	             31
  complete checkout                                          	         74	         74	             19
  create checkout                                            	        140	        140	             75
  update checkout lines                                      	        101	        101	             50

# api.benchmark collection
  test name                                                  	left count 	right count	duplicate count
  -----------------------------------------------------------	-----------	-----------	---------------
  collection view                                            	         17	         17	              0

# api.benchmark homepage
  test name                                                  	left count 	right count	duplicate count
  -----------------------------------------------------------	-----------	-----------	---------------
  featured products list                                     	         14	         14	              0
  retrieve main menu                                         	          8	          8	              0
  retrieve product list                                      	          5	          5	              0
  retrieve secondary menu                                    	          8	          8	              0
  retrieve shop                                              	          2	          2	              0
  user checkout details                                      	         52	         52	             26

# api.benchmark order
  test name                                                  	left count 	right count	duplicate count
  -----------------------------------------------------------	-----------	-----------	---------------
  user order details                                         	         17	         17	              2

# api.benchmark permission group
  test name                                                  	left count 	right count	duplicate count
  -----------------------------------------------------------	-----------	-----------	---------------
  permission group create                                    	         21	         21	              3
  permission group delete                                    	         21	         21	              4
  permission group query                                     	          8	          8	              0
  permission group update                                    	         36	         36	              2
  permission group update remove users with manage staff     	         31	         31	              4

# api.benchmark product
  test name                                                  	left count 	right count	duplicate count
  -----------------------------------------------------------	-----------	-----------	---------------
  product details                                            	         19	         19	              2
  retrieve product attributes                                	          7	          7	              0

# api.benchmark variant
  test name                                                  	left count 	right count	duplicate count
  -----------------------------------------------------------	-----------	-----------	---------------
  product variant bulk create                                	         48	         48	              2
  retrieve variant list                                      	         23	         23	              6

# api.benchmark variant stocks
  test name                                                  	left count 	right count	duplicate count
  -----------------------------------------------------------	-----------	-----------	---------------
  product variants stocks create                             	         23	         23	              5
  product variants stocks delete                             	         20	         20	              5
  product variants stocks update                             	         28	         28	              5

# api product sorting attributes
  test name                                                  	left count 	right count	duplicate count
  -----------------------------------------------------------	-----------	-----------	---------------
  sort product not having attribute data                     	         20	         20	              0

@codecov
Copy link

codecov bot commented Apr 29, 2020

Codecov Report

Merging #5564 into master will increase coverage by 0.00%.
The diff coverage is 87.50%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #5564   +/-   ##
=======================================
  Coverage   91.68%   91.68%           
=======================================
  Files         303      303           
  Lines       19776    19789   +13     
  Branches     1835     1840    +5     
=======================================
+ Hits        18131    18144   +13     
  Misses       1209     1209           
  Partials      436      436           
Impacted Files Coverage Δ
saleor/graphql/core/scalars.py 73.52% <33.33%> (ø)
saleor/graphql/product/mutations/products.py 95.73% <100.00%> (+0.03%) ⬆️
saleor/graphql/shipping/mutations.py 97.54% <100.00%> (+0.06%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c3b771e...94308f0. Read the comment docs.

@@ -802,6 +802,18 @@ def clean_attributes(
@classmethod
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)

weight = cleaned_input.get("weight")
if weight and weight.value < 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw. can something have 0? if not that would have to be <=

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We talk about it yesterday, and in some cases, you want to set products (e.g. Post Cards) Weight to 0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, post cards have some not-null weight but I guess there might be someone that sells virtual goods among physical products so that makes sense anyway.

Comment on lines +1173 to +1183
weight = cleaned_input.get("weight")
if weight and weight.value < 0:
raise ValidationError(
{
"weight": ValidationError(
"Product variant can't have negative weight.",
code=ProductErrorCode.INVALID.value,
)
}
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be extracted to something like validate_weight and used here and there as there are already 3 places with the same code

Copy link
Member Author

@fowczarek fowczarek Apr 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But errors are different(Solution with rise errors outside the function do not increase code readability. We also can rise errors in function but we should pass error information in parameters, I think it isn't good solution)
Second, think is that we have more duplicated logic in clean_input in ProductVariantCreate, ProductTypeCreate, and ProcutCreate, We should consider refactor it globally instead of having some logic duplicated and some logic hidden in functions.

@maarcingebala maarcingebala merged commit e0f15fa into master Apr 29, 2020
@maarcingebala maarcingebala deleted the negative_weight_validation branch April 29, 2020 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants