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

Sync data with Google Drive #930

Open
wants to merge 31 commits into
base: sync
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b237802
Add menu buttons to sync with google drive
Dec 8, 2022
4505718
Add google-api to dependencies
Dec 8, 2022
75c8cbd
use example code to connect to google drive api
Dec 8, 2022
cf52e41
test code to upload data to drive
Dec 8, 2022
a018ac0
export db as JSON to google drive
Dec 9, 2022
09d84da
download json file from google drive
Dec 9, 2022
844b8e3
first draft of importing data from google drive
Dec 12, 2022
1b0bf2d
Refactor export, add confirmation & success window
Dec 12, 2022
3b5e324
confirmation window for db import
Dec 12, 2022
49ca41e
fix formating
Jan 3, 2023
8277abb
Merge branch 'thamara:main' into sync-data-with-google-drive
SarahRemus Jan 3, 2023
84388a8
reload calendar after database import
Jan 3, 2023
ea53ce6
err handling for data export
Jan 4, 2023
a4ec25f
err handling for data import
Jan 4, 2023
9ec0781
simplify export function
Jan 5, 2023
2409a89
cleanup and formatting
Jan 5, 2023
a70d0ab
Added test for import from buffer
Jan 5, 2023
f283351
rename function
Jan 6, 2023
9edb738
Added test for get database as json
Jan 6, 2023
84b8624
refactor code structure
Jan 7, 2023
848f341
refactor code structure
Jan 7, 2023
373e66b
refactor code structure
Jan 7, 2023
e8e6d23
Add tests for import from drive
Jan 7, 2023
80c33d8
Fix format
Jan 8, 2023
f1b53eb
update package lock
Jan 8, 2023
da1ba39
fix requested formatting
Jan 21, 2023
7d3ea75
fix requested formatting
Jan 21, 2023
413d538
log errors to console
Jan 22, 2023
efea2b4
various small requested changes
Jan 22, 2023
2191298
remove regular entry from import fun & test case
Jan 22, 2023
5bc08ea
revert unrelated changes
Jan 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 57 additions & 20 deletions __tests__/__main__/import-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
const {
exportDatabaseToFile,
importDatabaseFromFile,
importDatabaseFromBuffer,
migrateFixedDbToFlexible,
validEntry
validEntry,
getDatabaseAsJSON
} = require('../../js/import-export');

const fs = require('fs');
Expand All @@ -19,13 +21,13 @@ describe('Import export', function()
// TODO: Regular store entries are still here to test import of old dbs. Please remove on the next release.
describe('validEntry(entry)', function()
{
const goodRegularEntry = {'type': 'regular', 'date': '2020-06-03', 'data': 'day-begin', 'hours': '08:00'};
const goodFlexibleEntry = {'type': 'flexible', 'date': '2020-06-03', 'values': ['08:00', '12:00', '13:00', '14:00']};
const goodWaivedEntry = {'type': 'waived', 'date': '2020-06-03', 'data': 'waived', 'hours': '08:00'};
const badRegularEntry = {'type': 'regular', 'date': 'not-a-date', 'data': 'day-begin', 'hours': '08:00'};
const badFlexibleEntry = {'type': 'flexible', 'date': '2020-06-03', 'values': ['not-an-hour']};
const badFlexibleEntry2 = {'type': 'flexible', 'date': '2020-06-03', 'values': 'not-an-array'};
const badWaivedEntry = {'type': 'regular', 'date': '2020-06-03', 'data': 'day-begin', 'hours': 'not-an-hour'};
const goodRegularEntry = { 'type': 'regular', 'date': '2020-06-03', 'data': 'day-begin', 'hours': '08:00' };
const goodFlexibleEntry = { 'type': 'flexible', 'date': '2020-06-03', 'values': ['08:00', '12:00', '13:00', '14:00'] };
const goodWaivedEntry = { 'type': 'waived', 'date': '2020-06-03', 'data': 'waived', 'hours': '08:00' };
const badRegularEntry = { 'type': 'regular', 'date': 'not-a-date', 'data': 'day-begin', 'hours': '08:00' };
const badFlexibleEntry = { 'type': 'flexible', 'date': '2020-06-03', 'values': ['not-an-hour'] };
const badFlexibleEntry2 = { 'type': 'flexible', 'date': '2020-06-03', 'values': 'not-an-array' };
const badWaivedEntry = { 'type': 'regular', 'date': '2020-06-03', 'data': 'day-begin', 'hours': 'not-an-hour' };
test('should be valid', () =>
{
expect(validEntry(goodRegularEntry)).toBeTruthy();
Expand All @@ -43,8 +45,8 @@ describe('Import export', function()
});

const store = new Store();
const flexibleStore = new Store({name: 'flexible-store'});
const waivedWorkdays = new Store({name: 'waived-workdays'});
const flexibleStore = new Store({ name: 'flexible-store' });
const waivedWorkdays = new Store({ name: 'waived-workdays' });

// TODO: Regular store is still here to test migration of dbs. Please remove on the next release.
store.clear();
Expand All @@ -63,16 +65,16 @@ describe('Import export', function()

flexibleStore.clear();
const flexibleEntries = {
'2020-3-1': {'values': ['08:00', '12:00', '13:00', '17:00']},
'2020-3-2': {'values': ['07:00', '11:00', '14:00', '18:00']}
'2020-3-1': { 'values': ['08:00', '12:00', '13:00', '17:00'] },
'2020-3-2': { 'values': ['07:00', '11:00', '14:00', '18:00'] }
};
flexibleStore.set(flexibleEntries);

waivedWorkdays.clear();
const waivedEntries = {
'2019-12-31': {reason: 'New Year\'s eve', hours: '08:00'},
'2020-01-01': {reason: 'New Year\'s Day', hours: '08:00'},
'2020-04-10': {reason: 'Good Friday', hours: '08:00'}
'2019-12-31': { reason: 'New Year\'s eve', hours: '08:00' },
'2020-01-01': { reason: 'New Year\'s Day', hours: '08:00' },
'2020-04-10': { reason: 'Good Friday', hours: '08:00' }
};
waivedWorkdays.set(waivedEntries);

Expand Down Expand Up @@ -110,8 +112,8 @@ describe('Import export', function()
});

const migratedFlexibleEntries = {
'2020-3-1': {'values': ['08:00', '12:00', '13:00', '17:00']},
'2020-3-2': {'values': ['10:00', '18:00']}
'2020-3-1': { 'values': ['08:00', '12:00', '13:00', '17:00'] },
'2020-3-2': { 'values': ['10:00', '18:00'] }
};

describe('migrateFixedDbToFlexible', function()
Expand Down Expand Up @@ -139,8 +141,8 @@ describe('Import export', function()

// Expected values have month-1, due to how the db saves them starting from 0
const expectedMixedEntries = {
'2020-2-1': {'values': ['08:00', '12:00', '13:00', '17:00']},
'2020-5-3': {'values': ['08:00', '10:00']}
'2020-2-1': { 'values': ['08:00', '12:00', '13:00', '17:00'] },
'2020-5-3': { 'values': ['08:00', '10:00'] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

many of those and above are unrelated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted

};

describe('importDatabaseFromFile (mixedContent)', function()
Expand All @@ -156,8 +158,43 @@ describe('Import export', function()
});
});

const regularEntriesJSON =
`[{"type": "flexible","date": "2020-4-1","values": ["08:00","12:00","13:00","17:00"]},
{"type": "flexible","date": "2020-4-2","values": ["07:00","11:00","14:00","18:00"]},
{"type": "waived","date": "2019-12-31","data": "New Year's eve","hours": "08:00"},
{"type": "waived","date": "2020-01-01","data": "New Year's Day","hours": "08:00"},
{"type": "waived","date": "2020-04-10","data": "Good Friday","hours": "08:00"}]`;

const notValidJSON =
'[{"type": "flexible","date": "2022-11-6","values": "08:44","08:45"]}]';

describe('importDatabaseFromBuffer', function()
{
test('Check that import fom buffer works', () =>
{
expect(importDatabaseFromBuffer(regularEntriesJSON)['result']).toBeTruthy();
expect(importDatabaseFromBuffer(notValidJSON)['result']).not.toBeTruthy();
expect(importDatabaseFromBuffer(notValidJSON)['failed']).toBe(0);
expect(importDatabaseFromBuffer(invalidEntriesContent)['result']).not.toBeTruthy();
expect(importDatabaseFromBuffer(invalidEntriesContent)['failed']).toBe(5);
});
});

store.clear();
store.set(regularEntries);

describe('getDatabaseAsJSON', function()
{
const databaseJSONObject = JSON.parse(getDatabaseAsJSON());
const regularEntriesJSONObject = JSON.parse(regularEntriesJSON);
test('Check that returning database as JSON works', () =>
{
expect(databaseJSONObject).toMatchObject(regularEntriesJSONObject);
});
});

afterAll(() =>
{
fs.rmdirSync(folder, {recursive: true});
fs.rmdirSync(folder, { recursive: true });
});
});
51 changes: 51 additions & 0 deletions __tests__/__main__/invalid-entries-import-drive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
const Store = require('electron-store');
const { importDatabaseFromGoogleDrive } = require('../../js/import-export-online');

// first five entries are invalid, second five are valid
const invalidEntriesContent =
`[{"type": "regular", "date": "not-a-date", "data": "day-begin", "hours": "08:00"},
{"type": "waived", "date": "2020-01-01", "data": "example waiver 2", "hours": "not-an-hour"},
{"type": "regular", "date": "not-a-date", "data": "day-end", "hours": "17:00"},
{"type": "flexible", "date": "not-a-date", "values": "not-an-array"},
{"type": "not-a-type", "date": "not-a-date", "data": "day-end", "hours": "17:00"},
{"type": "flexible","date": "2020-4-1","values": ["08:00","12:00","13:00","17:00"]},
{"type": "flexible","date": "2020-4-2","values": ["07:00","11:00","14:00","18:00"]},
{"type": "waived","date": "2019-12-31","data": "New Year's eve","hours": "08:00"},
{"type": "waived","date": "2020-01-01","data": "New Year's Day","hours": "08:00"},
{"type": "waived","date": "2020-04-10","data": "Good Friday","hours": "08:00"}
]`;

jest.mock('../../js/google-drive', () => ({
authorize: jest.fn(),
searchFile: jest.fn(),
downloadFile: jest.fn().mockResolvedValue(invalidEntriesContent),
}));

describe('Faulty import from google drive', function()
{
process.env.NODE_ENV = 'test';
const store = new Store();
const flexibleStore = new Store({ name: 'flexible-store' });
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
flexibleStore.clear();
waivedWorkdays.clear();

test('Check that invalid json is not imported', async() =>
{
expect(store.size).toBe(0);
expect(flexibleStore.size).toBe(0);
expect(waivedWorkdays.size).toBe(0);

const data = await importDatabaseFromGoogleDrive();

expect(data['result']).not.toBeTruthy();
expect(data['failed']).toBe(5);
expect(data['total']).toBe(10);

expect(store.size).toBe(0);
expect(flexibleStore.size).toBe(2);
expect(waivedWorkdays.size).toBe(3);
});
});
41 changes: 41 additions & 0 deletions __tests__/__main__/invalid-json-import-drive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable no-undef */
'use strict';
const Store = require('electron-store');
const { importDatabaseFromGoogleDrive } = require('../../js/import-export-online');

const notValidJSON =
'[{"type": "flexible","date": "2022-11-6","values": "08:44","08:45"]}]';

jest.mock('../../js/google-drive', () => ({
authorize: jest.fn(),
searchFile: jest.fn(),
downloadFile: jest.fn().mockResolvedValue(notValidJSON),
}));

describe('Invalid import from google drive', function()
{
process.env.NODE_ENV = 'test';
const store = new Store();
const flexibleStore = new Store({ name: 'flexible-store' });
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
flexibleStore.clear();
waivedWorkdays.clear();

test('Check that invalid json is not imported', async() =>
{
expect(store.size).toBe(0);
expect(flexibleStore.size).toBe(0);
expect(waivedWorkdays.size).toBe(0);

const data = await importDatabaseFromGoogleDrive();

expect(data['result']).not.toBeTruthy();
expect(data['failed']).toBe(0);
expect(data['total']).toBe(0);

expect(store.size).toBe(0);
expect(flexibleStore.size).toBe(0);
expect(waivedWorkdays.size).toBe(0);
});
});
45 changes: 45 additions & 0 deletions __tests__/__main__/regular-import-drive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-disable no-undef */
'use strict';

const { importDatabaseFromGoogleDrive } = require('../../js/import-export-online');
const Store = require('electron-store');

const regularEntriesJSON =
`[{"type": "flexible","date": "2020-4-1","values": ["08:00","12:00","13:00","17:00"]},
{"type": "flexible","date": "2020-4-2","values": ["07:00","11:00","14:00","18:00"]},
{"type": "waived","date": "2019-12-31","data": "New Year's eve","hours": "08:00"},
{"type": "waived","date": "2020-01-01","data": "New Year's Day","hours": "08:00"},
{"type": "waived","date": "2020-04-10","data": "Good Friday","hours": "08:00"}]`;

jest.mock('../../js/google-drive', () => ({
authorize: jest.fn(),
searchFile: jest.fn(),
downloadFile: jest.fn().mockResolvedValue(regularEntriesJSON),
}));

describe('Regular import from google drive', function()
{
process.env.NODE_ENV = 'test';

const store = new Store();
const flexibleStore = new Store({ name: 'flexible-store' });
const waivedWorkdays = new Store({ name: 'waived-workdays' });
store.clear();
flexibleStore.clear();
waivedWorkdays.clear();

test('Check valid json is imported', async() =>
{
expect(store.size).toBe(0);
expect(flexibleStore.size).toBe(0);
expect(waivedWorkdays.size).toBe(0);

const data = await importDatabaseFromGoogleDrive();

expect(data['result']).toBeTruthy();

expect(store.size).toBe(0);
expect(flexibleStore.size).toBe(2);
expect(waivedWorkdays.size).toBe(3);
});
});
Loading