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

NodeJs samples for Cloud Job discovery api - part1 #685

Closed
wants to merge 14 commits into from
Closed
Prev Previous commit
Next Next commit
Moved tests from mocha to ava
Changed tests to use ava test framework because
it is used by GCP CI.
Fixed lint errors.
  • Loading branch information
Rahul Shelar committed Jul 13, 2018
commit 555f761fe45567f3ac1f4a095b007ea71d93df00
53 changes: 26 additions & 27 deletions jobs/cjd_sample/basicCompanySample.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const getClient = require('./jobsClient.js').getClient;
* Generate data for a company.
* @returns {Object} Object containing fields of 'Company' resource.
*/
function generateCompany() {
function generateCompany () {
return {
displayName: 'Google',
distributorCompanyId: 'company:' + Math.floor(Math.random() * 100000).toString(),
Expand All @@ -41,7 +41,7 @@ exports.generateCompany = generateCompany;
* @param {Object} companyInfo Object containing fields of 'Company' resource.
* @returns {Promise.Object} Promise containing 'data' field of response.
*/
function createCompany(client, companyInfo) {
function createCompany (client, companyInfo) {
assert(companyInfo, '\'companyInfo\' argument is required.');
// Check required fields.
assert(companyInfo.displayName, '\'displayName\' field is required.');
Expand All @@ -68,7 +68,7 @@ exports.createCompany = createCompany;
* @param {string} companyName Name of company (value of 'name' field).
* @returns {Promise.Object} Promise containing 'data' field of response.
*/
function getCompany(client, companyName) {
function getCompany (client, companyName) {
assert(companyName, 'companyName argument is required.');
return new Promise((resolve, reject) => {
client.companies.get({
Expand All @@ -90,7 +90,7 @@ exports.getCompany = getCompany;
* @param {string} companyName Name of company (value of 'name' field).
* @param {*} companyInfo Object containing fields of 'Company' resource.
*/
function updateCompany(client, companyName, companyInfo) {
function updateCompany (client, companyName, companyInfo) {
assert(companyName, '\'companyName\' argument is required.');
assert(companyInfo, '\'companyInfo\' argument is required.');

Expand All @@ -114,7 +114,7 @@ exports.updateCompany = updateCompany;
* @param {Object} client Instance of google.jobs module.
* @param {string} companyName Name of company (value of 'name' field).
*/
function deleteCompany(client, companyName) {
function deleteCompany (client, companyName) {
assert(companyName, 'companyName argument is required.');
return new Promise((resolve, reject) => {
client.companies.delete({
Expand All @@ -132,35 +132,34 @@ exports.deleteCompany = deleteCompany;
/**
* Main entry point function.
*/
function main() {
function main () {
getClient().then((jobsClient) => {
assert(jobsClient, 'jobs instance not found.');
assert(jobsClient, 'jobs instance not found.');

let companyInfo = generateCompany();
// Create a company.
createCompany(jobsClient, companyInfo).then((info) => {
const companyName = info.name;
console.log('Company name:', companyName);
let companyInfo = generateCompany();
// Create a company.
createCompany(jobsClient, companyInfo).then((info) => {
const companyName = info.name;
console.log('Company name:', companyName);

// Get company.
getCompany(jobsClient, companyName).then((info) => {
// Set 'website' field.
companyInfo.website = 'https://www.foobar.com';
// Update company.
updateCompany(jobsClient, companyName, companyInfo).then((info) => {
assert(companyInfo.website === info.website, '\'website\' field did not get added.');
console.log(info);
// Get company.
getCompany(jobsClient, companyName).then((info) => {
// Set 'website' field.
companyInfo.website = 'https://www.foobar.com';
// Update company.
updateCompany(jobsClient, companyName, companyInfo).then((info) => {
assert(companyInfo.website === info.website, '\'website\' field did not get added.');
console.log(info);

// Delete company.
deleteCompany(jobsClient, companyName);
});
// Delete company.
deleteCompany(jobsClient, companyName);
});
});
})
.catch((err) => {
console.error(err);
throw err;
});
}).catch((err) => {
console.error(err);
throw err;
});
}

if (require.main === module) {
Expand Down
12 changes: 6 additions & 6 deletions jobs/cjd_sample/basicJobSample.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const getClient = require('./jobsClient.js').getClient;
* @param {string} companyName Name of company (value of 'name' field).
* @returns {Object} Object containing fields of 'Job' resource.
*/
function generateJob(companyName) {
function generateJob (companyName) {
assert(companyName, '\'companyName\' argument is required.');
return {
requisitionId: 'jobWithRequiredFields:' + Math.floor(Math.random() * 100000).toString(),
Expand All @@ -45,7 +45,7 @@ exports.generateJob = generateJob;
* @param {Object} jobInfo Object containing fields of 'Job' resource.
* @returns {Promise.Object} Promise containing 'data' field of response.
*/
function createJob(client, jobInfo) {
function createJob (client, jobInfo) {
assert(jobInfo, '\'jobInfo\' argument is required.');

return new Promise((resolve, reject) => {
Expand All @@ -71,7 +71,7 @@ exports.createJob = createJob;
* @param {string} jobName Job name.
* @returns {Promise.Object} Promise containing 'data' field of response.
*/
function getJob(client, jobName) {
function getJob (client, jobName) {
assert(jobName, '\'jobName\' argument is required.');

return new Promise((resolve, reject) => {
Expand All @@ -95,7 +95,7 @@ exports.getJob = getJob;
* @param {Object} jobInfo Object containing fields of 'Job' resource.
* @returns {Promise.Object} Promise containing 'data' field of response.
*/
function updateJob(client, jobName, jobInfo) {
function updateJob (client, jobName, jobInfo) {
assert(jobName, '\'jobName\' argument is required.');
assert(jobInfo, '\'jobInfo\' argument is required.');

Expand All @@ -122,7 +122,7 @@ exports.updateJob = updateJob;
* @param {string} jobName Job name.
* @returns {Promise.Object} Promise containing 'data' field of response.
*/
function deleteJob(client, jobName) {
function deleteJob (client, jobName) {
assert(jobName, '\'jobName\' argument is required.');
return new Promise((resolve, reject) => {
client.jobs.delete({
Expand All @@ -140,7 +140,7 @@ exports.deleteJob = deleteJob;
/**
* Main entry point function.
*/
function main() {
function main () {
getClient().then((jobsClient) => {
assert(jobsClient, 'jobs instance not found.');

Expand Down
6 changes: 3 additions & 3 deletions jobs/cjd_sample/generalSearchSample.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const jobSample = require('./basicJobSample.js');
* @param {string} query Search query.
* @returns {Promise.Object} Promise containing 'data' field of response.
*/
function basicKeywordSearch(client, companyNames, query) {
function basicKeywordSearch (client, companyNames, query) {
const jobQuery = {
companyNames: companyNames,
query: query
Expand Down Expand Up @@ -59,7 +59,7 @@ exports.basicKeywordSearch = basicKeywordSearch;
/**
* Main entry point function.
*/
function main() {
function main () {
getClient().then((jobsClient) => {
assert(jobsClient, 'jobs instance not found.');

Expand All @@ -86,4 +86,4 @@ function main() {
if (require.main === module) {
main();
}
// [END generalSearchSample]
// [END generalSearchSample]
10 changes: 4 additions & 6 deletions jobs/cjd_sample/jobsClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
*/
'use strict';

const {
google
} = require('googleapis');
const { google } = require('googleapis');

/**
* Get authorized client od google.jobs module.
* @returns {Promise.Object} Promise containing instance of google.jobs module.
*/
function getClient() {
function getClient () {
return new Promise((resolve, reject) => {
google.auth.getApplicationDefault((err, authClient) => {
if (err) {
Expand All @@ -32,7 +30,7 @@ function getClient() {

if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped([
"https://www.googleapis.com/auth/jobs"
'https://www.googleapis.com/auth/jobs'
]);
}

Expand All @@ -45,4 +43,4 @@ function getClient() {
});
});
}
exports.getClient = getClient;
exports.getClient = getClient;
6 changes: 3 additions & 3 deletions jobs/cjd_sample/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ const {
google.auth.getApplicationDefault((err, authClient) => {
if (err) {
console.error('Failed to acquire credentials');
console.error(err)
console.error(err);
return;
}

if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped([
"https://www.googleapis.com/auth/jobs"
'https://www.googleapis.com/auth/jobs'
]);
}

Expand Down Expand Up @@ -65,4 +65,4 @@ google.auth.getApplicationDefault((err, authClient) => {
}
});
});
// [END quickstart]
// [END quickstart]
88 changes: 41 additions & 47 deletions jobs/cjd_sample/test/basicCompanySampleTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,56 @@
*/
'use strict';

/**
* Unit tests for basicCompanySample.js
* DEPENDENCIES:
* You need to install mocha to run this test.
* npm install mocha
*
* Before running test:
* export GOOGLE_APPLICATION_CREDENTIALS=<path to credentials json file>
*
* Run test:
* <path to node_modules>/mocha/bin/mocha basicCompanySampleTests.js
*/
const assert = require('assert');
const test = require('ava');
const tools = require('@google-cloud/nodejs-repo-tools');
const companySample = require('../basicCompanySample.js');
const getClient = require('../jobsClient.js').getClient;

describe('Company API', () => {
let companyInfo = {
displayName: 'Acme Inc',
distributorCompanyId: 'company:' + Math.floor(Math.random() * 100000).toString(),
hqLocation: '1 Oak Street, Palo Alto, CA 94105'
};
// Client instance.
let client;
let companyName;
it('can get client instance', () => {
return getClient().then((jobs) => {
client = jobs;
});
test.beforeEach(tools.stubConsole);
test.afterEach.always(tools.restoreConsole);

let client;
let companyName;
let companyInfo = {
displayName: 'Acme Inc',
distributorCompanyId: 'company:' + Math.floor(Math.random() * 100000).toString(),
hqLocation: '1 Oak Street, Palo Alto, CA 94020'
};

test.serial('create client', (t) => {
return getClient().then((jobs) => {
client = jobs;
t.truthy(client);
});
});

it('can create a company', () => {
return companySample.createCompany(client, companyInfo).then((info) => {
assert(companyInfo.displayName === info.displayName);
assert(companyInfo.distributorCompanyId === info.distributorCompanyId);
assert(companyInfo.hqLocation === info.hqLocation);
companyName = info.name;
});
test.serial('create a company', (t) => {
return companySample.createCompany(client, companyInfo).then((info) => {
t.is(info.displayName, companyInfo.displayName);
t.is(info.displayName, companyInfo.displayName);
t.is(info.hqLocation, companyInfo.hqLocation);
companyName = info.name;
t.truthy(companyName);
});
});

it('can get a company', () => {
return companySample.getCompany(client, companyName).then((info) => {
assert(companyName === info.name);
});
test.serial('get a company', (t) => {
return companySample.getCompany(client, companyName).then((info) => {
t.is(info.name, companyName);
});
});

it('can update a company', () => {
companyInfo.companySize = 'SMALL';
companyInfo.website = 'https://www.testabc.com';
return companySample.updateCompany(client, companyName, companyInfo).then((info) => {
assert(companyInfo.companySize === info.companySize);
assert(companyInfo.website === info.website);
});
test.serial('update a company', (t) => {
companyInfo.companySize = 'SMALL';
companyInfo.website = 'https://www.testabc.com';
return companySample.updateCompany(client, companyName, companyInfo).then((info) => {
t.is(info.companySize, companyInfo.companySize);
t.is(info.website, companyInfo.website);
});
});

it('can delete a company', () => {
return companySample.deleteCompany(client, companyName);
test.serial('delete a company', (t) => {
return companySample.deleteCompany(client, companyName).then((info) => {
t.truthy(companyName);
});
});
});
Loading