UNPKG

3.67 kBJavaScriptView Raw
1/*jshint -W030 */
2'use strict';
3
4const aws = require('aws-sdk');
5const MigrationTable = require('../lib/MigrationTable');
6const uuid = require('node-uuid');
7const chai = require('chai');
8const expect = require('chai').expect;
9const _deleteDynamoTable = require('./util/deleteDynamoTable');
10const _queryFileByName = require('../lib/util/queryFileByName');
11const config = require('../config');
12
13require('chai').should();
14chai.config.includeStack = true;
15
16const dynamodb = new aws.DynamoDB(config.dynamodb);
17
18describe('Migration table test', function() {
19 describe('Creating migration table', () => {
20 let randomTableName;
21 let migrationTable;
22
23 beforeEach(() => {
24 randomTableName = `dynograte-${uuid.v4()}`;
25 migrationTable = new MigrationTable(dynamodb, randomTableName);
26 });
27
28 afterEach(() => {
29 return _deleteDynamoTable(dynamodb, randomTableName);
30 });
31
32 it('should create migration table with valid params', () => {
33 return new Promise((resolve, reject) => {
34 MigrationTable.create(dynamodb, randomTableName)
35 .then((migrationTable) => {
36 expect(migrationTable.migrationTableName).to.equal(randomTableName);
37
38 dynamodb.describeTable({
39 TableName: randomTableName
40 }, (err, data) => {
41 return err ? reject(err) : resolve(data);
42 });
43 });
44 });
45 });
46
47 it('should put file names into migration table and validate they exist', () => {
48 let fileName = uuid.v4();
49 return MigrationTable.create(dynamodb, randomTableName)
50 .then(() => {
51 return migrationTable.insert(fileName)
52 .then((id) => {
53 return migrationTable.alreadyRan(fileName)
54 .then((alreadyRan) => {
55 expect(alreadyRan).to.equal(true);
56 });
57 });
58 });
59 });
60
61 it('should return false when checking if a file has ran that has not', () => {
62 let fileName = uuid.v4();
63 return MigrationTable.create(dynamodb, randomTableName)
64 .then(() => {
65 return migrationTable.alreadyRan(fileName)
66 .then((alreadyRan) => {
67 expect(alreadyRan).to.equal(false);
68 });
69 });
70 });
71
72 it('should update `pending`', () => {
73 let fileName = uuid.v4();
74 let fileId;
75
76 return MigrationTable.create(dynamodb, randomTableName)
77 .then(() => {
78 return migrationTable.insert(fileName)
79 .then(id => {
80 fileId = id;
81
82 return _queryFileByName(dynamodb, randomTableName, fileName)
83 .then(data => {
84 expect(data.Items[0]).to.deep.equal({
85 filename: {
86 'S': fileName
87 },
88 id: {
89 'S': fileId
90 },
91 pending: {
92 'BOOL': true
93 }
94 });
95 });
96 })
97 .then(() => {
98 return migrationTable.updatePending(fileId, false);
99 })
100 .then(() => {
101 return _queryFileByName(dynamodb, randomTableName, fileName)
102 .then(data => {
103 expect(data.Items[0]).to.deep.equal({
104 filename: {
105 'S': fileName
106 },
107 id: {
108 'S': fileId
109 },
110 pending: {
111 'BOOL': false
112 }
113 });
114 });
115 });
116 });
117 });
118 });
119});