Skip to content

Commit

Permalink
Merge df40335 into 6bddbe3
Browse files Browse the repository at this point in the history
  • Loading branch information
shayaniox committed Jul 12, 2018
2 parents 6bddbe3 + df40335 commit c800756
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
38 changes: 38 additions & 0 deletions bddrest/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import threading
from urllib.parse import parse_qs

Expand Down Expand Up @@ -100,3 +101,40 @@ def normalize_query_string(query):
k: v[0] if len(v) == 1 else v for k, v in parse_qs(query).items()
} if isinstance(query, str) else query



class Status:

def __init__(self, code):
self.code = int(code.split(' ', 1)[0])
self.text = code.lower()

def __eq__(self, other):
if isinstance(other, int):
return self.code == other
return self.text == other.lower()

def raise_value_error(self):
raise ValueError('Cannot compare with string, Use integer instead for\
all comparisons except equality')

def __gt__(self, other):
if isinstance(other, int):
return self.code > other
self.raise_value_error

def __ge__(self, other):
if isinstance(other, int):
return self.code >= other
self.raise_value_error

def __lt__(self, other):
if isinstance(other, int):
return self.code < other
self.raise_value_error

def __le__(self, other):
if isinstance(other, int):
return self.code <= other
self.raise_value_error

56 changes: 56 additions & 0 deletions bddrest/practice/status_practice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import re


class Status:

def __init__(self, code):
self.code = int(code.split(' ', 1)[0])
self.text = code.lower()

def __eq__(self, other):
if isinstance(other, int):
return self.code == other
return self.text == other.lower()

def raise_value_error(self):
raise ValueError('Cannot compare with string, Use integer instead for\
all comparisons except equality')

def __gt__(self, other):
if isinstance(other, int):
return self.code > other
self.raise_value_error

def __ge__(self, other):
if isinstance(other, int):
return self.code >= other
self.raise_value_error

def __lt__(self, other):
if isinstance(other, int):
return self.code < other
self.raise_value_error

def __le__(self, other):
if isinstance(other, int):
return self.code <= other
self.raise_value_error


if __name__ == '__main__':
s = Status('200 OK')
assert s == '200 ok'
assert s == 200
assert s != 201
assert s != '200 OKOK'
assert s >= 100
assert s >= '100 Continue'
assert s > 100
assert s > '100 Continue'
assert s <= 300
assert s <= '300 Continue'
assert s < 300
assert s < '300 Continue'
assert s >= 200
assert s <= 200

0 comments on commit c800756

Please sign in to comment.