Skip to content

Commit

Permalink
Merge branch 'main' into kj/ts-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
hkjpotato authored Nov 8, 2021
2 parents b87e585 + fbe1f2e commit 5cc9a92
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 142 deletions.
2 changes: 1 addition & 1 deletion packages/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@aws-sdk/client-pinpoint": "3.6.1",
"@aws-sdk/util-utf8-browser": "3.6.1",
"lodash": "^4.17.20",
"uuid": "^8.3.2"
"uuid": "^3.2.1"
},
"jest": {
"globals": {
Expand Down
1 change: 1 addition & 0 deletions packages/api-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@aws-amplify/cache": "4.0.24",
"@aws-amplify/core": "4.3.4",
"@aws-amplify/pubsub": "4.1.14",
"uuid": "^3.2.1",
"graphql": "14.5.0",
"zen-observable-ts": "0.8.19"
},
Expand Down
1 change: 0 additions & 1 deletion packages/aws-amplify-angular/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export default {
},
external: [
'aws-sdk',
'uuid',
'@angular/core',
'@angular/common',
'aws-amplify',
Expand Down
11 changes: 11 additions & 0 deletions packages/aws-amplify-react/__tests__/Storage/S3Image-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ jest.mock('../../src/Storage/Common', () => {
return { calcKey };
});

jest.mock('@aws-sdk/client-s3', () => ({
S3Client: jest.fn().mockImplementation(() => ({
send: jest.fn(),
middlewareStack: {
add: jest.fn(),
remove: jest.fn(),
},
config: jest.fn(),
})),
}));

describe('S3Image', () => {
describe('render test', () => {
test('render null if no test and no picker', () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/aws-amplify-react/__tests__/Storage/S3Text-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ jest.mock('../../src/Storage/Common', () => {
return { calcKey };
});

jest.mock('@aws-sdk/client-s3', () => ({
S3Client: jest.fn().mockImplementation(() => ({
send: jest.fn(),
middlewareStack: {
add: jest.fn(),
remove: jest.fn(),
},
config: jest.fn(),
})),
}));

describe('S3Text test', () => {
describe('render test', () => {
test('render null if no test and no picker', () => {
Expand Down
54 changes: 52 additions & 2 deletions packages/datastore-storage-adapter/__tests__/SQLiteUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const INTERNAL_TEST_SCHEMA_STATEMENTS = [
const INTERNAL_TEST_SCHEMA_MANY_TO_MANY_STATEMENT =
'CREATE TABLE IF NOT EXISTS "PostEditor" ("id" PRIMARY KEY NOT NULL, "post" TEXT, "postID" TEXT NOT NULL, "editor" TEXT, "editorID" TEXT NOT NULL, "createdAt" TEXT, "updatedAt" TEXT, "_version" INTEGER, "_lastChangedAt" INTEGER, "_deleted" INTEGER);';

const INTERNAL_TEST_SCHEMA_ONE_TO_MANY_STATEMENT =
'CREATE TABLE IF NOT EXISTS "Post" ("id" PRIMARY KEY NOT NULL, "title" TEXT NOT NULL, "comments" TEXT, "_version" INTEGER, "_lastChangedAt" INTEGER, "_deleted" INTEGER);';

describe('SQLiteUtils tests', () => {
let Model: PersistentModelConstructor<Model>;

Expand All @@ -59,17 +62,23 @@ describe('SQLiteUtils tests', () => {
});

describe('modelCreateTableStatement', () => {
it('should generate valid CREATE TABLE statement from a M:N join table model with implicit FKs', () => {
it('should generate a valid CREATE TABLE statement from a M:N join table model with implicit FKs', () => {
expect(modelCreateTableStatement(postEditorImplicit, true)).toEqual(
INTERNAL_TEST_SCHEMA_MANY_TO_MANY_STATEMENT
);
});

it('should generate valid CREATE TABLE statement from a M:N join table model with explicit FKs', () => {
it('should generate a valid CREATE TABLE statement from a M:N join table model with explicit FKs', () => {
expect(modelCreateTableStatement(postEditorExplicit, true)).toEqual(
INTERNAL_TEST_SCHEMA_MANY_TO_MANY_STATEMENT
);
});

it('should generate a valid CREATE TABLE statement from a 1:M join table model', () => {
expect(modelCreateTableStatement(postWithRequiredComments, true)).toEqual(
INTERNAL_TEST_SCHEMA_ONE_TO_MANY_STATEMENT
);
});
});

describe('implicitAuthFieldsForModel', () => {
Expand Down Expand Up @@ -656,3 +665,44 @@ const groupsAuthExplicit: SchemaModel = {
},
],
};

const postWithRequiredComments: SchemaModel = {
name: 'Post',
pluralName: 'Posts',
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
title: {
name: 'title',
isArray: false,
type: 'String',
isRequired: true,
attributes: [],
},
comments: {
name: 'comments',
isArray: true,
type: {
model: 'Comment',
},
isRequired: true,
attributes: [],
isArrayNullable: true,
association: {
connectionType: 'HAS_MANY',
associatedWith: 'post',
},
},
},
attributes: [
{
type: 'model',
properties: {},
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,25 @@ export function modelCreateTableStatement(
}

if (isModelFieldType(field.type)) {
let columnParam = `"${field.name}" TEXT`;

// add targetName as well as field name for BELONGS_TO relations
if (isTargetNameAssociation(field.association)) {
const required = field.isRequired ? ' NOT NULL' : '';

let columnParam = `"${field.name}" TEXT`;
// check if this field has been explicitly defined in the model
const fkDefinedInModel = Object.values(model.fields).find(
(f: ModelField) => f.name === field.association.targetName
);

// only add auto-generate it if not
// if the FK is not explicitly defined in the model, we have to add it here
if (!fkDefinedInModel) {
const required = field.isRequired ? ' NOT NULL' : '';
columnParam += `, "${field.association.targetName}" TEXT${required}`;
}

return acc + `, ${columnParam}`;
}

// ignore isRequired param for model fields, since they will not contain
// the related data locally
return acc + `, ${columnParam}`;
}

// default to TEXT
Expand Down
2 changes: 1 addition & 1 deletion packages/datastore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"idb": "5.0.6",
"immer": "9.0.6",
"ulid": "2.3.0",
"uuid": "^8.3.2",
"uuid": "3.3.2",
"zen-observable-ts": "0.8.19",
"zen-push": "0.2.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/predictions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@aws-sdk/client-translate": "3.6.1",
"@aws-sdk/eventstream-marshaller": "3.6.1",
"@aws-sdk/util-utf8-node": "3.6.1",
"uuid": "^8.3.2"
"uuid": "^3.2.1"
},
"jest": {
"globals": {
Expand Down
2 changes: 1 addition & 1 deletion packages/pubsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@aws-amplify/core": "4.3.4",
"graphql": "14.0.0",
"paho-mqtt": "^1.1.0",
"uuid": "^8.3.2",
"uuid": "^3.2.1",
"zen-observable-ts": "0.8.19"
},
"jest": {
Expand Down
82 changes: 81 additions & 1 deletion packages/storage/__tests__/common/S3ClientUtils-unit-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import {
getPrefix,
createPrefixMiddleware,
autoAdjustClockskewMiddleware,
createS3Client,
credentialsProvider,
} from '../../src/common/S3ClientUtils';
import { ICredentials, Credentials } from '@aws-amplify/core';
import {
ICredentials,
Credentials,
getAmplifyUserAgent,
} from '@aws-amplify/core';
import { S3ClientConfig } from '@aws-sdk/client-s3';

const credentials: ICredentials = {
accessKeyId: 'accessKeyId',
Expand Down Expand Up @@ -110,4 +118,76 @@ describe('S3ClientUtils tests', () => {
expect(protectedPrefix).toEqual('protected/identityId/key');
expect(privatePrefix).toEqual('private/identityId/key');
});

test('createS3Client test', async () => {
const s3client = createS3Client({
region: 'us-west-2',
useAccelerateEndpoint: true,
});
// ensure customUserAgent is set properly
expect(s3client.config.customUserAgent).toEqual([[getAmplifyUserAgent()]]);
expect(await s3client.config.region()).toEqual('us-west-2');
expect(s3client.config.useAccelerateEndpoint).toBe(true);
});

test('createS3Client test - dangerouslyConnectToHttpEndpointForTesting', async () => {
const s3client = createS3Client({
region: 'us-west-2',
dangerouslyConnectToHttpEndpointForTesting: true,
});
expect(await s3client.config.endpoint()).toStrictEqual({
hostname: 'localhost',
path: '/',
port: 20005,
protocol: 'http:',
query: undefined,
});
expect(s3client.config.tls).toBe(false);
expect(s3client.config.bucketEndpoint).toBe(false);
expect(s3client.config.forcePathStyle).toBe(true);
});

test('credentialsProvider test', async () => {
const mockCredentials: ICredentials = {
accessKeyId: 'accessKeyId',
sessionToken: 'sessionToken',
secretAccessKey: 'secretAccessKey',
identityId: 'identityId',
authenticated: true,
};
jest
.spyOn(Credentials, 'get')
.mockImplementationOnce(() => Promise.resolve(mockCredentials));
const credentials = await credentialsProvider();
expect(credentials).toStrictEqual(mockCredentials);
});

test('credentialsProvider - Credentials.get error', async () => {
jest
.spyOn(Credentials, 'get')
.mockImplementationOnce(() => Promise.reject('err'));
const credentials = await credentialsProvider();
expect(credentials).toStrictEqual({ accessKeyId: '', secretAccessKey: '' });
});

test('autoAdjustClockskewMiddleware tests', async () => {
const dateNow = Date.now();
// keep the Date.now() call inside the middleware consistent
jest.spyOn(Date, 'now').mockImplementation(() => dateNow);
const s3ClientConfig = { systemClockOffset: 0 } as S3ClientConfig;
const middleware = autoAdjustClockskewMiddleware(s3ClientConfig);
const oneHourInMs = 1000 * 60 * 60;
try {
await middleware(
arg =>
Promise.reject({
ServerTime: new Date(dateNow + oneHourInMs),
Code: 'RequestTimeTooSkewed',
}),
null
)({ request: null, input: {} });
} catch (err) {
expect(s3ClientConfig.systemClockOffset).toBe(oneHourInMs);
}
});
});
Loading

0 comments on commit 5cc9a92

Please sign in to comment.