def test_job_post_success(self): token = self.get_auth_token_for("*****@*****.**", "test", "test user") job_category = JobCategory(name="Kitchen") job_type = JobType(name="Barista", category_id=job_category.id) job_category.job_types.append(job_type) db.session.add(job_type) db.session.add(job_category) db.session.commit() job_post_response = self.post_job(title="Walk my dog", description="ye", longitude="-63.1315222", latitude="46.2356426", auth_token=token, job_type=job_type.name, category=job_category.name) self.assertEqual(job_post_response.status_code, 200) _json = json.loads(job_post_response.data.decode())['job'] self.assertGreaterEqual(_json['id'], 0) self.assertGreaterEqual(_json['owner_id'], 0) jobs = Job.query.all() self.assertEqual(len(jobs), 1)
def test_job_post_failure(self): # Fails because: title element is null, and category / job_type is also not defined. token = self.get_auth_token_for("*****@*****.**", "test", "test user") job_category = JobCategory(name="Kitchen") job_type = JobType(name="Barista", category_id=job_category.id) db.session.add(job_type) db.session.add(job_category) job_category.job_types.append(job_type) db.session.add(job_category) db.session.commit() job_post_response = self.post_job(title="", description="ye", longitude="-63.1315222", latitude="46.2356426", auth_token=token, category=job_category.name, job_type=job_type.name) self.assertEqual(job_post_response.status_code, 400) _json = json.loads(job_post_response.data.decode()) self.assertEqual(_json['status'], "error")
def test_job_category_route(self): user_1 = self.get_auth_token_for("*****@*****.**", "test", "test user") category = JobCategory(name="Kitchen") category.save(commit=True) job_type = JobType(name="Barista", category=category) job_type.save(commit=True) category_request = self.app.test_client().get( '/api/jobs/categories', content_type="application/json", headers={'Authorization': 'Bearer {0}'.format(user_1)}) _json = json.loads(category_request.data.decode()) self.assertEqual(len(_json['categories']), 1) self.assertEqual(_json['categories'][0]['id'], 1)
def test_job_list(self): token = self.get_auth_token_for("*****@*****.**", "test", "test user") job_list_response = self.app.test_client().get( '/api/jobs/list', headers={'Authorization': "Bearer {0}".format(token)}) self.assertEqual(job_list_response.status_code, 200) _json = json.loads(job_list_response.data.decode()) self.assertEqual(len(_json['jobs']), 0) # Now add a job to the list and see if we can see it. category = JobCategory(name="Test Cat") category.save() job_type = JobType(name="Test Type", category=category) job_type.save(commit=True) job_post_response = self.post_job("Title", "Description", "-1", "1", token, category=category.name, job_type=job_type.name) self.assertEqual(job_post_response.status_code, 200) job_list_response = self.app.test_client().get( '/api/jobs/list/all', headers={'Authorization': "Bearer {0}".format(token)}) _json = json.loads(job_list_response.data.decode()) self.assertGreaterEqual(len(_json['jobs']), 1) job_list_response = self.app.test_client().get( '/api/jobs/list/applied', headers={'Authorization': "Bearer {0}".format(token)}) _json = json.loads(job_list_response.data.decode()) self.assertGreaterEqual(len(_json['jobs']), 0)
def test_job_search(self): user_1_token = self.get_auth_token_for("*****@*****.**", "test", "test user") user_2_token = self.get_auth_token_for("*****@*****.**", "test1", "test user 2") user_2_long = 46.2570767 user_2_lat = -63.1264856 job_category = JobCategory(name="Kitchen") job_type = JobType(name="Barrista", category_id=job_category.id) job_category.job_types.append(job_type) db.session.add(job_category) db.session.add(job_type) job_1 = Job(owner_id=1, title="Babysit my pet", description="I need my dog watched", longitude="46.2588051", latitude="-63.1245543", category=job_category, job_type=job_type) db.session.commit() user_2 = User.query.filter_by(id=2).first() db.session.add(job_1) db.session.add(user_2) db.session.commit() search_request = self.app.test_client().post( '/api/jobs/search/', data=json.dumps( dict(search=dict(radius=5, long=user_2_long, lat=user_2_lat, job_type="Barrista"))), content_type="application/json", headers={'Authorization': 'Bearer {0}'.format(user_2_token)}) self.assertEqual(search_request.status_code, 200) _json = json.loads(search_request.data.decode()) self.assertEqual(_json['jobs'][0]['id'], 1)
def test_get_job_info(self): token = self.get_auth_token_for("*****@*****.**", "test", "test user") job_category = JobCategory(name="Kitchen") db.session.add(job_category) job_type = JobType(name="Barrista", category=job_category) db.session.add(job_type) db.session.commit() job_post_response = self.post_job(title="Walk my dog", description="ye", longitude="-63.1315222", latitude="46.2356426", auth_token=token, category=job_category.name, job_type=job_type.name) self.assertEqual(job_post_response.status_code, 200) jp_resp = job_post_response.data.decode() job_id = json.loads(job_post_response.data.decode())['job']['id'] job_info_response = self.app.test_client().get( '/api/jobs/info/{0}'.format(job_id), headers={'Authorization': "Bearer {0}".format(token)}) self.assertEqual(job_info_response.status_code, 200) _json = json.loads(job_info_response.data.decode()) self.assertEqual(_json['status'], 'success')
try: print("Creating database, please wait.") db.create_all(app=application) except: print("Database exists; Operation resumed") # With so we have the app context. with application.app_context(): # Query database first and see if the items exist # if the database is populated with items, exit script # otherwise insert the default values. for key, value in job_type_categories.items(): category = JobCategory.query.filter_by(name=key).first() if category is None: category = JobCategory(name=key) category.save(commit=True) else: print("! Category {0} ALREADY EXISTS".format(key)) # Iterate value as its a list for job_type in value: _type = JobType.query.filter_by(name=job_type).first() if _type is None: _type = JobType(name=job_type, category=category) _type.save(commit=True) print("+ {0} in {1}".format(_type.name, category.name))
def test_job_apply(self): token = self.get_auth_token_for("*****@*****.**", "test", "test user") category = JobCategory(name="Kitchen") db.session.add(category) job_type = JobType(name="Barrista", category_id=category.id) db.session.add(job_type) category.job_types.append(job_type) db.session.add(category) db.session.commit() post_data = self.post_job("Title", "description", "-1", "1", token, category=category.name, job_type=job_type.name) self.assertEqual(post_data.status_code, 200) apply_response = self.app.test_client().post( '/api/jobs/apply/', data=json.dumps(dict(id=1)), headers={'Authorization': 'Bearer {0}'.format(token)}, content_type="application/json") _json = json.loads(apply_response.data.decode()) self.assertEqual(apply_response.status_code, 200) self.assertEqual(_json['status'], 'success') user = User.query.filter_by(id=decode_auth_token(token)['sub']).first() self.assertEqual(len(user.jobs_applied), 1) job_1 = Job.query.all()[0] self.assertEqual(job_1.id, user.jobs_applied[0].job_id) # Now we apply a second time to the same job # And get an error stating we've already applied apply_response = self.app.test_client().post( '/api/jobs/apply/', data=json.dumps(dict(id=1)), headers={'Authorization': 'Bearer {0}'.format(token)}, content_type="application/json") self.assertEqual(apply_response.status_code, 400) _json = json.loads(apply_response.data.decode()) self.assertEqual(_json['status'], 'error') self.assertEqual( _json['message'], 'An application has already been submitted for this job.')