Skip to content

Commit 754faf8

Browse files
author
Paulina Nguyen
committed
add test for create document
1 parent 60f1644 commit 754faf8

File tree

3 files changed

+159
-1
lines changed

3 files changed

+159
-1
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/** Copyright 2023 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
async function main(
18+
projectNumber = 'YOUR_PROJECT_NUMBER',
19+
location = 'YOUR_PROJECT_LOCATION',
20+
userId = 'user:xxx@example.com',
21+
) {
22+
// [START contentwarehouse_quickstart]
23+
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
* const projectNumber = 'YOUR_PROJECT_NUMBER';
27+
* const location = 'YOUR_PROJECT_LOCATION'; // Format is 'us' or 'eu'
28+
* const userId = 'user:xxx@example.com'; // Format is "user:xxx@example.com"
29+
*/
30+
31+
// Import from google cloud
32+
const {DocumentSchemaServiceClient, DocumentServiceClient} =
33+
require('@google-cloud/contentwarehouse').v1;
34+
35+
// Create service client
36+
const schemaClient = new DocumentSchemaServiceClient();
37+
const serviceClient = new DocumentServiceClient();
38+
39+
// Get Document Schema
40+
async function quickstart() {
41+
// Initialize request argument(s)
42+
const schemaRequest = {};
43+
const documentRequest = {};
44+
45+
// The full resource name of the location, e.g.:
46+
// projects/{project_number}/locations/{location}
47+
const parent = `projects/${projectNumber}/locations/${location}`;
48+
schemaRequest.parent = parent;
49+
documentRequest.parent = parent;
50+
51+
// Property Definition
52+
const propertyDefinition = {};
53+
propertyDefinition.name = 'schema property 1'; // Must be unique within a document schema (case insensitive)
54+
propertyDefinition.displayName = 'searchable text';
55+
propertyDefinition.isSearchable = true;
56+
propertyDefinition.textTypeOptions = {};
57+
58+
// Document Schema
59+
const documentSchemaRequest = {};
60+
documentSchemaRequest.displayName = 'My Test Schema';
61+
documentSchemaRequest.propertyDefinitions = [];
62+
63+
schemaRequest.documentSchema = documentSchemaRequest;
64+
65+
// Create Document Schema
66+
const documentSchema = await schemaClient.createDocumentSchema(schemaRequest);
67+
68+
// Property Value Definition
69+
const documentProperty = {};
70+
documentProperty.name = propertyDefinition.name;
71+
documentProperty.values = {textValues: ["GOOG"]}; //TODO: how to get the right object type for this?
72+
73+
// Document Definition
74+
const document = {};
75+
document.displayName = 'My Test Document';
76+
document.documentSchemaName = documentSchema[0].name;
77+
document.plainText = "This is a sample of a document's text.";
78+
document.properties = [];
79+
80+
documentRequest.document = document;
81+
82+
// Metadata Definition
83+
documentRequest.requestMetadata = {userInfo: {id: userId}};
84+
85+
// Make Request
86+
const response = serviceClient.createDocument(documentRequest);
87+
88+
// Print out response
89+
response.then(
90+
result => console.log(`Document Created: ${JSON.stringify(result)}`),
91+
error => console.log(`${error}`)
92+
);
93+
}
94+
95+
// [END contentwarehouse_quickstart]
96+
await quickstart();
97+
}
98+
99+
main(...process.argv.slice(2)).catch(err => {
100+
console.error(err);
101+
process.exitCode = 1;
102+
});

document-warehouse/test/document-schema.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const confirmationGet = 'Schema Found';
2828

2929
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
3030

31-
describe('Create and delete document schema', () => {
31+
describe('Document schema tests', () => {
3232
let projectNumber;
3333
let documentSchema;
3434
let documentSchemaId;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {describe, it} = require('mocha');
18+
const assert = require('assert');
19+
const cp = require('child_process');
20+
21+
const {PoliciesClient} = require('@google-cloud/iam').v2;
22+
const {ProjectsClient} = require('@google-cloud/resource-manager').v3;
23+
const iamClient = new PoliciesClient();
24+
const projectClient = new ProjectsClient();
25+
26+
const confirmationCreate = 'Document Created';
27+
28+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
29+
30+
describe('Document tests', () => {
31+
let projectNumber;
32+
const location = 'us';
33+
const userId = 'serviceAccount:kokoro-system-test@long-door-651.iam.gserviceaccount.com';
34+
35+
async function getProjectNumber() {
36+
const projectId = await iamClient.getProjectId();
37+
const request = {name: `projects/${projectId}`};
38+
const project = await projectClient.getProject(request);
39+
const resources = project[0].name.toString().split('/');
40+
const projectNumber = resources[resources.length - 1];
41+
return projectNumber;
42+
}
43+
44+
45+
before(async () => {
46+
projectNumber = await getProjectNumber();
47+
});
48+
49+
it('should create a document', async () => {
50+
const output = execSync(
51+
`node create-document.js ${projectNumber} ${location} ${userId}`
52+
);
53+
54+
assert(output.startsWith(confirmationCreate));
55+
});
56+
});

0 commit comments

Comments
 (0)