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

WIP: Adding testing #5

Merged
merged 7 commits into from
Oct 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ venv/
ENV/
env.bak/
venv.bak/
*Pipfile*

# Spyder project settings
.spyderproject
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Before you start using Upwork API, you need to register your application and obt
After that add required configuration options.
```
# Upwork API
[uwpork]
[upwork]
api_key=
api_secret=
job_skill=
Expand Down
10 changes: 5 additions & 5 deletions upwork/configuration.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[uwpork]
api_key=
api_secret=
job_skill=

[upwork]
api_key =
api_secret =
job_skill =

[email]
smtp_host =
Expand All @@ -12,4 +12,4 @@ smtp_user =
smtp_pass =
smtp_port =
smtp_tls =
smtp_ssl =
smtp_ssl =
45 changes: 36 additions & 9 deletions upwork/notification.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from datetime import datetime, timedelta
import configparser
import smtplib
import upwork


class Job(object):
Expand All @@ -20,15 +22,32 @@ def __str__(self):
return job_info


class Config(object):
def __init__(self, source_path='', content=''):
config = configparser.ConfigParser()
if len(source_path) > 0:
script_dir = os.path.dirname(__file__)
abs_file_path = os.path.join(script_dir, source_path)
config.read_file(open(abs_file_path))
elif len(content) > 0:
config.read_string(content)
else:
raise Exception("Specify a configuration file path, or content.")
self.config = config

class UpworkClient(object):
def __init__(self, public_key, secret_key):
self.public_key = public_key
self.secret_key = secret_key
if (len(public_key) > 0) & (len(secret_key) > 0):
self.public_key = public_key
self.secret_key = secret_key
else:
raise Exception("No Authentication key\n" +\
"Go to https://developers.upwork.com/?lang=python#getting-started")

def __client(self):
'''
Authenticate to Upwork API
:return: uwpork client obj
:return: upwork client obj
'''
try:
upwork_client = upwork.Client(
Expand All @@ -41,7 +60,8 @@ def __client(self):
oauth_access_token=oauth_access_token,
oauth_access_token_secret=oauth_access_token_secret)
except Exception as e:
print("Error: unable to authenticate " + e.message)
print(f"Error: unable to authenticate {e!s}")
raise

return client

Expand Down Expand Up @@ -70,7 +90,8 @@ def search_jobs(self, job_query):
upwork_jobs = \
upwork.provider_v2.search_jobs(job_query, page_size=20)
except Exception as e:
print("Error: unable to connect " + e.message)
print(f"Error: unable to connect {e!s}")
raise

jobs = []
current_time = datetime.now() - timedelta(hours=1)
Expand All @@ -91,10 +112,16 @@ def search_jobs(self, job_query):


if __name__ == "__main__":
config = configparser.ConfigParser()
config.read("configuration.ini")
api_key = config['uwpork']['api_key']
api_secret = config['uwpork']['api_key']
# Import configuration
# script_dir = os.path.dirname(__file__)
# rel_path = "configuration.ini"
# abs_file_path = os.path.join(script_dir, rel_path)
# config = configparser.ConfigParser()
# config.read_file(open(abs_file_path))
config = Config("configuration.ini")
# Define local parameters
api_key = config['upwork']['api_key']
api_secret = config['upwork']['api_key']
job_skill = config['upwork']['job_skill']

upwork = UpworkClient(api_key, api_secret)
Expand Down
14 changes: 14 additions & 0 deletions upwork/test_configuration.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[upwork]
api_key = test
api_secret = test
job_skill = test

[email]
smtp_host = test
mail_from = test
mail_to = [email protected]
smtp_user = test
smtp_pass = test
smtp_port = test
smtp_tls = test
smtp_ssl = test
55 changes: 55 additions & 0 deletions upwork/test_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import datetime as dt
import unittest
from .notification import Job, Config, UpworkClient


config = Config("test_configuration.ini").config
api_key = config['upwork']['api_key']
api_secret = config['upwork']['api_key']
job_skill = config['upwork']['job_skill']
job_query = dict(
skills=[job_skill],
budget='[100 TO 100000]',
duration=['week', 'month', 'ongoing']
)


class TestJob(unittest.TestCase):

def setUp(self):
self.job = Job(
"Expert", dt.date(2001, 1, 1), "developers", "…",
"Lead Android Developer",
"https://www.upwork.com/job/test",
)

def test_init(self):
self.assertEqual(self.job.budget, "Expert")
self.assertEqual(self.job.date, dt.date(2001, 1, 1))

def test_job_info(self):
self.assertEqual(str(self.job),
"New job: Lead Android Developer \nType: developers\n" +\
"Budget : Expert $ \nCreated on: 2001-01-01 " +\
"Informations: … \nLink: https://www.upwork.com/job/test"
)

class TestConfig(unittest.TestCase):

def setUp(self):
self.config = config

def test_init(self):
# self.assertRaises(Exception)
self.assertEqual(self.config['upwork']['api_key'], api_key)
self.assertEqual(self.config['upwork']['api_secret'], api_secret)


class TestUpworkClient(unittest.TestCase):

def setUp(self):
self.upworkclient = UpworkClient(api_key, api_secret)

@unittest.skip("Cannot setup because upwork.Client doesn’t exist")
def test_search_jobs(self):
self.assertEqual(self.upworkclient.search_jobs(job_query), "/Need to fill in here: Does Upwork has a live test job?/")