Skip to content

Commit

Permalink
chore(glue): migrate unit tests to Jest (#8862)
Browse files Browse the repository at this point in the history
Migrates unit tests in `@aws-cdk/aws-glue` to Jest, from Nodeunit.
Would help me to start some development of additional L2 constructs for glue.

:warning:  Two tests fail, although I don't know why. To me it seems they also should have failed previously.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
bweigel authored Jul 10, 2020
1 parent e34cb80 commit 50eb8e0
Show file tree
Hide file tree
Showing 11 changed files with 1,941 additions and 2,002 deletions.
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-glue/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ nyc.config.js
.LAST_PACKAGE
*.snk
!.eslintrc.js
!jest.config.js

junit.xml
junit.xml
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-glue/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dist

tsconfig.json
.eslintrc.js
jest.config.js

# exclude cdk artifacts
**/cdk.out
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-glue/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const baseConfig = require('../../../tools/cdk-build-tools/config/jest.config');
module.exports = baseConfig;
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-glue/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ function validateSchema(columns: Column[], partitionKeys?: Column[]): void {
const names = new Set<string>();
(columns.concat(partitionKeys || [])).forEach(column => {
if (names.has(column.name)) {
throw new Error('column names and partition keys must be unique, but \'p1\' is duplicated');
throw new Error(`column names and partition keys must be unique, but \'${column.name}\' is duplicated`);
}
names.add(column.name);
});
Expand Down
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-glue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"compat": "cdk-compat"
},
"cdk-build": {
"cloudformation": "AWS::Glue"
"cloudformation": "AWS::Glue",
"jest": true
},
"keywords": [
"aws",
Expand All @@ -67,7 +68,7 @@
"cdk-build-tools": "0.0.0",
"cdk-integ-tools": "0.0.0",
"cfn2ts": "0.0.0",
"nodeunit": "^0.11.3",
"jest": "^25.5.4",
"pkglint": "0.0.0"
},
"dependencies": {
Expand Down
92 changes: 92 additions & 0 deletions packages/@aws-cdk/aws-glue/test/database.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { expect } from '@aws-cdk/assert';
import '@aws-cdk/assert/jest';
import { Stack } from '@aws-cdk/core';
import { deepEqual, throws } from 'assert';
import * as glue from '../lib';

test('default database does not create a bucket', () => {
const stack = new Stack();

new glue.Database(stack, 'Database', {
databaseName: 'test_database',
});

expect(stack).toMatch({
Resources: {
DatabaseB269D8BB: {
Type: 'AWS::Glue::Database',
Properties: {
CatalogId: {
Ref: 'AWS::AccountId',
},
DatabaseInput: {
Name: 'test_database',
},
},
},
},
});

});

test('explicit locationURI', () => {
const stack = new Stack();

new glue.Database(stack, 'Database', {
databaseName: 'test_database',
locationUri: 's3://my-uri/',
});

expect(stack).toMatch({
Resources: {
DatabaseB269D8BB: {
Type: 'AWS::Glue::Database',
Properties: {
CatalogId: {
Ref: 'AWS::AccountId',
},
DatabaseInput: {
LocationUri: 's3://my-uri/',
Name: 'test_database',
},
},
},
},
});

});

test('fromDatabase', () => {
// GIVEN
const stack = new Stack();

// WHEN
const database = glue.Database.fromDatabaseArn(stack, 'import', 'arn:aws:glue:us-east-1:123456789012:database/db1');

// THEN
deepEqual(database.databaseArn, 'arn:aws:glue:us-east-1:123456789012:database/db1');
deepEqual(database.databaseName, 'db1');
deepEqual(stack.resolve(database.catalogArn), { 'Fn::Join': [ '',
[ 'arn:', { Ref: 'AWS::Partition' }, ':glue:', { Ref: 'AWS::Region' }, ':', { Ref: 'AWS::AccountId' }, ':catalog' ] ] });
deepEqual(stack.resolve(database.catalogId), { Ref: 'AWS::AccountId' });
});

test('locationUri length must be >= 1', () => {
const stack = new Stack();
throws(() =>
new glue.Database(stack, 'Database', {
databaseName: 'test_database',
locationUri: '',
}),
);
});

test('locationUri length must be <= 1024', () => {
const stack = new Stack();
throws(() =>
new glue.Database(stack, 'Database', {
databaseName: 'test_database',
locationUri: 'a'.repeat(1025),
}),
);
});
241 changes: 241 additions & 0 deletions packages/@aws-cdk/aws-glue/test/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
import '@aws-cdk/assert/jest';
import { doesNotThrow, equal, throws } from 'assert';
import { Schema } from '../lib';

test('boolean type', () => {
equal(Schema.BOOLEAN.inputString, 'boolean');
equal(Schema.BOOLEAN.isPrimitive, true);
});

test('binary type', () => {
equal(Schema.BINARY.inputString, 'binary');
equal(Schema.BINARY.isPrimitive, true);
});

test('bigint type', () => {
equal(Schema.BIG_INT.inputString, 'bigint');
equal(Schema.BIG_INT.isPrimitive, true);
});

test('double type', () => {
equal(Schema.DOUBLE.inputString, 'double');
equal(Schema.DOUBLE.isPrimitive, true);
});

test('float type', () => {
equal(Schema.FLOAT.inputString, 'float');
equal(Schema.FLOAT.isPrimitive, true);
});

test('integer type', () => {
equal(Schema.INTEGER.inputString, 'int');
equal(Schema.INTEGER.isPrimitive, true);
});

test('smallint type', () => {
equal(Schema.SMALL_INT.inputString, 'smallint');
equal(Schema.SMALL_INT.isPrimitive, true);
});

test('tinyint type', () => {
equal(Schema.TINY_INT.inputString, 'tinyint');
equal(Schema.TINY_INT.isPrimitive, true);
});

test('decimal type', () => {
equal(Schema.decimal(16).inputString, 'decimal(16)');
equal(Schema.decimal(16, 1).inputString, 'decimal(16,1)');
equal(Schema.decimal(16).isPrimitive, true);
equal(Schema.decimal(16, 1).isPrimitive, true);
});
// TODO: decimal bounds

test('date type', () => {
equal(Schema.DATE.inputString, 'date');
equal(Schema.DATE.isPrimitive, true);
});

test('timestamp type', () => {
equal(Schema.TIMESTAMP.inputString, 'timestamp');
equal(Schema.TIMESTAMP.isPrimitive, true);
});

test('string type', () => {
equal(Schema.STRING.inputString, 'string');
equal(Schema.STRING.isPrimitive, true);
});

test('char type', () => {
equal(Schema.char(1).inputString, 'char(1)');
equal(Schema.char(1).isPrimitive, true);
});

test('char length must be test(at least 1', () => {
doesNotThrow(() => Schema.char(1));
throws(() => Schema.char(0));
throws(() => Schema.char(-1));
});

test('char length must test(be <= 255', () => {
doesNotThrow(() => Schema.char(255));
throws(() => Schema.char(256));
});

test('varchar type', () => {
equal(Schema.varchar(1).inputString, 'varchar(1)');
equal(Schema.varchar(1).isPrimitive, true);
});

test('varchar length must be test(at least 1', () => {
doesNotThrow(() => Schema.varchar(1));
throws(() => Schema.varchar(0));
throws(() => Schema.varchar(-1));
});

test('varchar length must test(be <= 65535', () => {
doesNotThrow(() => Schema.varchar(65535));
throws(() => Schema.varchar(65536));
});

test('test(array<string>', () => {
const type = Schema.array(Schema.STRING);
equal(type.inputString, 'array<string>');
equal(type.isPrimitive, false);
});

test('array<test(char(1)>', () => {
const type = Schema.array(Schema.char(1));
equal(type.inputString, 'array<char(1)>');
equal(type.isPrimitive, false);
});

test('test(array<array>', () => {
const type = Schema.array(
Schema.array(Schema.STRING));
equal(type.inputString, 'array<array<string>>');
equal(type.isPrimitive, false);
});

test('test(array<map>', () => {
const type = Schema.array(
Schema.map(Schema.STRING, Schema.STRING));
equal(type.inputString, 'array<map<string,string>>');
equal(type.isPrimitive, false);
});

test('test(array<struct>', () => {
const type = Schema.array(
Schema.struct([{
name: 'key',
type: Schema.STRING,
}]));
equal(type.inputString, 'array<struct<key:string>>');
equal(type.isPrimitive, false);
});

test('map<test(string,string>', () => {
const type = Schema.map(
Schema.STRING,
Schema.STRING,
);
equal(type.inputString, 'map<string,string>');
equal(type.isPrimitive, false);
});

test('map<test(int,string>', () => {
const type = Schema.map(
Schema.INTEGER,
Schema.STRING,
);
equal(type.inputString, 'map<int,string>');
equal(type.isPrimitive, false);
});

test('map<char(1),test(char(1)>', () => {
const type = Schema.map(
Schema.char(1),
Schema.char(1),
);
equal(type.inputString, 'map<char(1),char(1)>');
equal(type.isPrimitive, false);
});

test('map<test(string,array>', () => {
const type = Schema.map(
Schema.char(1),
Schema.array(Schema.STRING),
);
equal(type.inputString, 'map<char(1),array<string>>');
equal(type.isPrimitive, false);
});

test('map<test(string,map>', () => {
const type = Schema.map(
Schema.char(1),
Schema.map(
Schema.STRING,
Schema.STRING),
);
equal(type.inputString, 'map<char(1),map<string,string>>');
equal(type.isPrimitive, false);
});

test('map<test(string,struct>', () => {
const type = Schema.map(
Schema.char(1),
Schema.struct([{
name: 'key',
type: Schema.STRING,
}]),
);
equal(type.inputString, 'map<char(1),struct<key:string>>');
equal(type.isPrimitive, false);
});

test('map throws if keyType is test(non-primitive', () => {
throws(() => Schema.map(
Schema.array(Schema.STRING),
Schema.STRING,
));
throws(() => Schema.map(
Schema.map(Schema.STRING, Schema.STRING),
Schema.STRING,
));
throws(() => Schema.map(
Schema.struct([{
name: 'key',
type: Schema.STRING,
}]),
Schema.STRING,
));
});

test('struct type', () => {
const type = Schema.struct([{
name: 'primitive',
type: Schema.STRING,
}, {
name: 'with_comment',
type: Schema.STRING,
comment: 'this has a comment',
}, {
name: 'array',
type: Schema.array(Schema.STRING),
}, {
name: 'map',
type: Schema.map(Schema.STRING, Schema.STRING),
}, {
name: 'nested_struct',
type: Schema.struct([{
name: 'nested',
type: Schema.STRING,
comment: 'nested comment',
}]),
}]);

equal(type.isPrimitive, false);
equal(
type.inputString,
// eslint-disable-next-line max-len
'struct<primitive:string,with_comment:string COMMENT \'this has a comment\',array:array<string>,map:map<string,string>,nested_struct:struct<nested:string COMMENT \'nested comment\'>>');
});
Loading

0 comments on commit 50eb8e0

Please sign in to comment.