diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d35cb3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +*.pyc diff --git a/README.md b/README.md new file mode 100644 index 0000000..c7f4f80 --- /dev/null +++ b/README.md @@ -0,0 +1,126 @@ +# provoj + +Simple library to check the endpoints of an RESTful API. + +### Usage +Import the provoj library, and the requests library +```python +import provoj +import requests +``` + +Define a new test +```python +test = provoj.NewTest("Testing some websites") +``` + +Check if two variables are equal +```python +a = "hello" +b = "world" +test.equal("comparing a and b", a, b)# test.equal(check description, variable1, variable2) +``` + +Check if two variables are not equal +```python +a = "hello" +b = "world" +test.notequal("comparing a and b", a, b)# test.notequal(check description, variable1, variable2) +``` + +Check if first variable is bigger than the second +```python +a = 2 +b = 3 +test.bigger("comparing a and b", a, b)# test.bigger(check description, variable1, variable2) +``` + +Check if first variable is smaller than the second +```python +a = 2 +b = 3 +test.smaller("comparing a and b", a, b)# test.smaller(check description, variable1, variable2) +``` + +Check if the length of a variable +```python +animals = ["cat", "dog", "racoon"] +test.length("checking the length of the array animals", animals, 3)# test.length(check description, variable1, length) +``` + +Check for request status +```python +r = requests.get("https://api.github.com/users/arnaucode/repos") +test.status("get api.github.com/users/arnaucode/repos", r)# test.status(check description, request_response) +``` + +### Full example +```python +import provoj +import requests + + +test1 = provoj.NewTest("testing some function") + +a = "hello" +b = "world" +c = "hello" +test1.equal("comparing a and b", a, b) +test1.notequal("comparing a and b", a, b) +test1.equal("comparing a and c", a, c) + +x = 2 +y = 3 +test1.bigger("comparing x and y", x, y) +test1.smaller("comparing x and y", x, y) + +test1.length("checking length", a, 2) +test1.length("checking length", a, 5) + +animals = ["cat", "dog", "racoon"] +test1.length("checking the length of the array animals", animals, 3) + +test1.printScores() + + +t2 = provoj.NewTest("Testing some websites") + +r = requests.get("https://www.github.com") +t2.rStatus("get github", r) + +r = requests.get("https://arnaucode.com") +t2.rStatus("get arnaucode.com", r) + +r = requests.get("https://arnaucode.com/fake") +t2.rStatus("get arnaucode.com/fake_path", r) + +r = requests.get("https://arnaucode.com/blog") +t2.rStatus("get arnaucode.com/blog", r) + +t2.printScores() + + +tGithub = provoj.NewTest("Github API tests") + +r = requests.get("https://api.github.com") +tGithub.rStatus("get api.github", r) +r = requests.get("https://api.github.com/users/arnaucode") +tGithub.rStatus("get https://api.github.com/users/arnaucode", r) +jsonResp = r.json() +tGithub.equal("checking quantity of followers", jsonResp["followers"], 100) +tGithub.equal("checking quantity of public_repos", jsonResp["public_repos"], 77) + +r = requests.get("https://api.github.com/users/arnaucode/repos") +tGithub.rStatus("get https://api.github.com/users/arnaucode/repos", r) +jsonResp = r.json() +tGithub.length("checking length of repos", jsonResp, 30) +tGithub.equal("checking first repo", jsonResp[0]["name"], "argos") +tGithub.equal("checking second repo", jsonResp[1]["name"], "coffeeminer") +tGithub.equal("checking third repo", jsonResp[2]["name"], "bc") + +tGithub.printScores() +``` + +Output example: + +![provoj](https://raw.githubusercontent.com/arnaucode/provoj/master/provoj-example.gif "provoj") diff --git a/exampleTest.py b/exampleTest.py new file mode 100644 index 0000000..3c7401a --- /dev/null +++ b/exampleTest.py @@ -0,0 +1,65 @@ +import provoj +import requests + + +test1 = provoj.NewTest("testing some function") + +a = "hello" +b = "world" +c = "hello" + +test1.equal("comparing a and b", a, b) +test1.notequal("comparing a and b", a, b) + +test1.equal("comparing a and c", a, c) + +x = 2 +y = 3 +test1.bigger("comparing x and y", x, y) +test1.smaller("comparing x and y", x, y) + +test1.length("checking length", a, 2) +test1.length("checking length", a, 5) + +animals = ["cat", "dog", "racoon"] +test1.length("checking the length of the array animals", animals, 3) + +test1.printScores() + + +t2 = provoj.NewTest("Testing some websites") + +r = requests.get("https://www.github.com") +t2.rStatus("get github", r) + +r = requests.get("https://arnaucode.com") +t2.rStatus("get arnaucode.com", r) + +r = requests.get("https://arnaucode.com/fake") +t2.rStatus("get arnaucode.com/fake_path", r) + +r = requests.get("https://arnaucode.com/blog") +t2.rStatus("get arnaucode.com/blog", r) + +t2.printScores() + + +tGithub = provoj.NewTest("Github API tests") + +r = requests.get("https://api.github.com") +tGithub.rStatus("get api.github", r) +r = requests.get("https://api.github.com/users/arnaucode") +tGithub.rStatus("get https://api.github.com/users/arnaucode", r) +jsonResp = r.json() +tGithub.equal("checking quantity of followers", jsonResp["followers"], 100) +tGithub.equal("checking quantity of public_repos", jsonResp["public_repos"], 77) + +r = requests.get("https://api.github.com/users/arnaucode/repos") +tGithub.rStatus("get https://api.github.com/users/arnaucode/repos", r) +jsonResp = r.json() +tGithub.length("checking length of repos", jsonResp, 30) +tGithub.equal("checking first repo", jsonResp[0]["name"], "argos") +tGithub.equal("checking second repo", jsonResp[1]["name"], "coffeeminer") +tGithub.equal("checking third repo", jsonResp[2]["name"], "bc") + +tGithub.printScores() diff --git a/provoj-example.gif b/provoj-example.gif new file mode 100644 index 0000000..74bfb05 Binary files /dev/null and b/provoj-example.gif differ diff --git a/provoj.py b/provoj.py new file mode 100644 index 0000000..233c585 --- /dev/null +++ b/provoj.py @@ -0,0 +1,112 @@ +import math +import datetime + +class NewTest(object): + def __init__(self, title=None): + # define the scores dictionary, and initialize + self.scores = {} + self.scores["success"] = 0 + self.scores["error"] = 0 + self.time = datetime.datetime.now() + if title != None: + self.title = title + print("") + pc.blue("- " + title) + else: + self.title = "" + print("") + pc.blue("- unnamed test") + def equal(self, action, a, b): + if a != b: + pc.error(action + " - Error, '" + str(a) + "' should be equal to '" + str(b) + "'") + self.scores["error"] += 1 + else: + pc.checked(action) + self.scores["success"] += 1 + def notequal(self, action, a, b): + if a == b: + pc.error(action + " - Error, '" + str(a) + "' should be not equal to '" + str(b) + "'") + self.scores["error"] += 1 + else: + pc.checked(action) + self.scores["success"] += 1 + def bigger(self, action, a, b): + if a < b: + pc.error(action + " - Error, '" + str(a) + "' should be bigger than '" + str(b) + "'") + self.scores["error"] += 1 + else: + pc.checked(action) + self.scores["success"] += 1 + def smaller(self, action, a, b): + if a > b: + pc.error(action + " - Error, '" + str(a) + "' should be smaller than '" + str(b) + "'") + self.scores["error"] += 1 + else: + pc.checked(action) + self.scores["success"] += 1 + + def length(self, action, a, l): + if len(a) != l: + pc.error(action + " - Error, actual length is '" + str(len(a)) + "', expected to be '" + str(l) + "'") + self.scores["error"] += 1 + else: + pc.checked(action) + self.scores["success"] += 1 + + def rStatus(self, action, r, verbose=False): + if r.status_code < 200 or r.status_code > 299: + if verbose: + pc.error(action + " - Error " + str(r.status_code) + ": " + r.text) + else: + pc.error(action + " - Error " + str(r.status_code)) + + self.scores["error"] += 1 + else: + pc.checked(action) + self.scores["success"] += 1 + + def printScores(self): + pc.purple(" Test title: " + self.title) + score = math.ceil(( self.scores["success"]/(self.scores["success"]+self.scores["error"]) )*100) + elapsed = datetime.datetime.now() - self.time + pc.purple(" Score: " + str(score) + + "% (" + str(self.scores["success"]) + "/" + str(self.scores["success"] + self.scores["error"]) + ")" + + " Time benchmark: " + str(elapsed)) + + +''' +code to print the colors in the terminal +''' +checkIcon = "✓ " +errorIcon = "❌ " + +class pc: + RED = '\033[31m' + GREEN = '\033[32m' + BLUE = '\033[94m' + PURPLE = '\033[95m' + END = '\033[0m' + + @classmethod + def red(cls, s, **kwargs): + print(cls.RED + s + cls.END, **kwargs) + + @classmethod + def green(cls, s, **kwargs): + print(cls.GREEN + s + cls.END, **kwargs) + + @classmethod + def blue(cls, s, **kwargs): + print(cls.BLUE + s + cls.END, **kwargs) + + @classmethod + def purple(cls, s, **kwargs): + print(cls.PURPLE + s + cls.END, **kwargs) + + @classmethod + def checked(cls, s, **kwargs): + print(" " + cls.GREEN + checkIcon + s + cls.END, **kwargs) + + @classmethod + def error(cls, s, **kwargs): + print(" " + cls.RED + errorIcon + s + cls.END, **kwargs)