Removes the angular constant jamstashVersion, goes back to using the json changelog as latest version

- Adds tests to checkVersion & checkVersionNewer: if the second version (older or required version) is undefined, it always returns true. This enables us to upgrade the settings even if there never was a Jamstash version stored in local storage.
This commit is contained in:
Hyzual 2015-03-24 11:46:28 +01:00
parent 812fff38cd
commit fb65ae48bf
6 changed files with 91 additions and 62 deletions

View file

@ -48,7 +48,4 @@ angular.module('JamStash', ['ngCookies', 'ngRoute', 'ngSanitize', 'ui.keypress',
} }
}; };
}]); }]);
}]) }]);
//TODO: Hyz: Fill with grunt task
.constant('jamstashVersion', '4.4.5');

View file

@ -1,4 +1,10 @@
[ [
{
"date": "3/24/2015", "version": "4.4.5",
"changes": [
{ "text": "- Code refactoring, fixes Podcasts retrieval"}
]
},
{ {
"date": "1/17/2015", "version": "4.4.0", "date": "1/17/2015", "version": "4.4.0",
"changes": [ "changes": [

View file

@ -14,8 +14,8 @@ angular.module('jamstash.persistence', ['angular-locker',
.setEventsEnabled(false); .setEventsEnabled(false);
}]) }])
.service('persistence', ['globals', 'player', 'notifications', 'locker', 'jamstashVersion', 'jamstashVersionChangesets', 'utils', .service('persistence', ['globals', 'player', 'notifications', 'locker', 'json', 'jamstashVersionChangesets', 'utils',
function (globals, player, notifications, locker, jamstashVersion, jamstashVersionChangesets, utils) { function (globals, player, notifications, locker, json, jamstashVersionChangesets, utils) {
/* Manage current track */ /* Manage current track */
this.loadTrackPosition = function () { this.loadTrackPosition = function () {
// Load Saved Song // Load Saved Song
@ -74,9 +74,16 @@ angular.module('jamstash.persistence', ['angular-locker',
/* Manage user settings */ /* Manage user settings */
this.getSettings = function () { this.getSettings = function () {
if(utils.checkVersionNewer(jamstashVersion, this.getVersion())) { // If the latest version from changelog.json is newer than the version stored in local storage,
this.upgradeToVersion(jamstashVersion); // we upgrade it
var storedVersion = this.getVersion();
var persistenceService = this;
json.getChangeLog(function (changelogs) {
var changelogVersion = changelogs[0].version;
if(utils.checkVersionNewer(changelogVersion, storedVersion)) {
persistenceService.upgradeVersion(storedVersion, changelogVersion);
} }
});
return locker.get('Settings'); return locker.get('Settings');
}; };
@ -93,8 +100,7 @@ angular.module('jamstash.persistence', ['angular-locker',
return locker.get('JamstashVersion'); return locker.get('JamstashVersion');
}; };
this.upgradeToVersion = function (finalVersion) { this.upgradeVersion = function (currentVersion, finalVersion) {
var currentVersion = this.getVersion();
var settings = locker.get('Settings'); var settings = locker.get('Settings');
// Apply all upgrades older than the final version and newer than the current // Apply all upgrades older than the final version and newer than the current
var allUpgrades = _(jamstashVersionChangesets.versions).filter(function (toApply) { var allUpgrades = _(jamstashVersionChangesets.versions).filter(function (toApply) {

View file

@ -1,7 +1,7 @@
describe("Persistence service", function() { describe("Persistence service", function() {
'use strict'; 'use strict';
var persistence, player, notifications, locker, var persistence, player, notifications, locker, json,
song, fakeStorage, fakeVersionChangesets; song, fakeStorage, fakeVersionChangesets;
beforeEach(function() { beforeEach(function() {
fakeVersionChangesets = {versions: []}; fakeVersionChangesets = {versions: []};
@ -10,7 +10,6 @@ describe("Persistence service", function() {
$provide.decorator('locker', function () { $provide.decorator('locker', function () {
return jasmine.createSpyObj("locker", ["get", "put", "forget"]); return jasmine.createSpyObj("locker", ["get", "put", "forget"]);
}); });
$provide.constant("jamstashVersion", "1.0.1");
$provide.value("jamstashVersionChangesets", fakeVersionChangesets); $provide.value("jamstashVersionChangesets", fakeVersionChangesets);
$provide.decorator('notifications', function () { $provide.decorator('notifications', function () {
@ -22,13 +21,18 @@ describe("Persistence service", function() {
fakePlayer.queue = []; fakePlayer.queue = [];
return fakePlayer; return fakePlayer;
}); });
$provide.decorator('json', function () {
return jasmine.createSpyObj("json", ["getChangeLog"]);
});
}); });
inject(function (_persistence_, _player_, _notifications_, _locker_) { inject(function (_persistence_, _player_, _notifications_, _locker_, _json_) {
persistence = _persistence_; persistence = _persistence_;
player = _player_; player = _player_;
notifications = _notifications_; notifications = _notifications_;
locker = _locker_; locker = _locker_;
json = _json_;
}); });
song = { song = {
@ -164,13 +168,19 @@ describe("Persistence service", function() {
}); });
}); });
it("Given that the previously stored Jamstash version was '1.0.0' and given the current constant jamstash.version was '1.0.1', when I get the settings, then upgradeToVersion will be called", function() { it("Given that the previously stored Jamstash version was '1.0.0' and given the latest version in changelog.json was '1.0.1', when I get the settings, then upgradeVersion will be called", function() {
fakeStorage.JamstashVersion = '1.0.0'; fakeStorage.JamstashVersion = '1.0.0';
spyOn(persistence, 'upgradeToVersion'); spyOn(persistence, 'upgradeVersion');
json.getChangeLog.and.callFake(function (callback) {
console.log('callback', callback);
callback([
{version: '1.0.1'}
]);
});
persistence.getSettings(); persistence.getSettings();
expect(persistence.upgradeToVersion).toHaveBeenCalledWith('1.0.1'); expect(persistence.upgradeVersion).toHaveBeenCalledWith('1.0.0', '1.0.1');
}); });
it("Given that no user settings had been saved in local storage, it returns undefined", function() { it("Given that no user settings had been saved in local storage, it returns undefined", function() {
@ -215,11 +225,11 @@ describe("Persistence service", function() {
}); });
}); });
describe("upgradeToVersion() -", function() { describe("upgradeVersion() -", function() {
it("Given that Jamstash version '1.0.0' was previously stored in local storage, when I upgrade the storage version to '1.0.1', Jamstash version '1.0.1' will be in local storage and the user will be notified", function() { it("Given that Jamstash version '1.0.0' was previously stored in local storage, when I upgrade the storage version to '1.0.1', Jamstash version '1.0.1' will be in local storage and the user will be notified", function() {
fakeStorage.JamstashVersion = '1.0.0'; fakeStorage.JamstashVersion = '1.0.0';
persistence.upgradeToVersion('1.0.1'); persistence.upgradeVersion('1.0.0', '1.0.1');
expect(locker.put).toHaveBeenCalledWith('JamstashVersion', '1.0.1'); expect(locker.put).toHaveBeenCalledWith('JamstashVersion', '1.0.1');
expect(notifications.updateMessage).toHaveBeenCalledWith('Version 1.0.0 to 1.0.1', true); expect(notifications.updateMessage).toHaveBeenCalledWith('Version 1.0.0 to 1.0.1', true);
@ -257,13 +267,8 @@ describe("Persistence service", function() {
}; };
}); });
describe("and that Jamstash version '1.0.0' was previously stored in local storage,", function() { it("when I upgrade the storage version from '1.0.0' to '1.0.2', both changesets have been applied", function() {
beforeEach(function() { persistence.upgradeVersion('1.0.0', '1.0.2');
fakeStorage.JamstashVersion = '1.0.0';
});
it("when I upgrade the storage version to '1.0.2', both changesets have been applied", function() {
persistence.upgradeToVersion('1.0.2');
expect(locker.put).toHaveBeenCalledWith('Settings', { expect(locker.put).toHaveBeenCalledWith('Settings', {
DefaultSearchType: 0, DefaultSearchType: 0,
DefaultAlbumSort: 0, DefaultAlbumSort: 0,
@ -272,8 +277,8 @@ describe("Persistence service", function() {
}); });
}); });
it("when I upgrade the storage version to '1.0.1', only the '1.0.1' changeset has been applied", function() { it("when I upgrade the storage version from '1.0.0' to '1.0.1', only the '1.0.1' changeset has been applied", function() {
persistence.upgradeToVersion('1.0.1'); persistence.upgradeVersion('1.0.0', '1.0.1');
expect(locker.put).toHaveBeenCalledWith('Settings', { expect(locker.put).toHaveBeenCalledWith('Settings', {
DefaultSearchType: 0, DefaultSearchType: 0,
DefaultAlbumSort: { DefaultAlbumSort: {
@ -284,12 +289,9 @@ describe("Persistence service", function() {
AutoPlay: true AutoPlay: true
}); });
}); });
});
it("and that Jamstash version '1.0.1' was previously stored in local storage, when I upgrade the storage version to '1.0.2', only the '1.0.2' changeset has been applied", function() { it("when I upgrade the storage version from '1.0.1' to '1.0.2', only the '1.0.2' changeset has been applied", function() {
fakeStorage.JamstashVersion = '1.0.1'; persistence.upgradeVersion('1.0.1', '1.0.2');
persistence.upgradeToVersion('1.0.2');
expect(locker.put).toHaveBeenCalledWith('Settings', { expect(locker.put).toHaveBeenCalledWith('Settings', {
DefaultSearchType: { DefaultSearchType: {
id: "song", id: "song",

View file

@ -250,20 +250,23 @@ angular.module('jamstash.utils', ['jamstash.settings.service'])
}; };
}; };
this.checkVersion = function (newerVersion, olderVersion) { this.checkVersion = function (running, required) {
if (!angular.isObject(newerVersion)) { if (required === undefined) {
newerVersion = this.parseVersionString(newerVersion); return true;
} }
if (!angular.isObject(olderVersion)) { if (!angular.isObject(running)) {
olderVersion = this.parseVersionString(olderVersion); running = this.parseVersionString(running);
} }
if (olderVersion.major !== undefined && newerVersion.major !== undefined && newerVersion.major > olderVersion.major) { if (!angular.isObject(required)) {
required = this.parseVersionString(required);
}
if (required.major !== undefined && running.major !== undefined && running.major > required.major) {
return true; return true;
} else { } else {
if (olderVersion.minor !== undefined && newerVersion.minor !== undefined && newerVersion.minor > olderVersion.minor) { if (required.minor !== undefined && running.minor !== undefined && running.minor > required.minor) {
return true; return true;
} else { } else {
if (olderVersion.patch !== undefined && newerVersion.patch !== undefined && newerVersion.patch >= olderVersion.patch) { if (required.patch !== undefined && running.patch !== undefined && running.patch >= required.patch) {
return true; return true;
} else { } else {
return false; return false;
@ -273,6 +276,9 @@ angular.module('jamstash.utils', ['jamstash.settings.service'])
}; };
this.checkVersionNewer = function (newerVersion, olderVersion) { this.checkVersionNewer = function (newerVersion, olderVersion) {
if (olderVersion === undefined) {
return true;
}
if (!angular.isObject(newerVersion)) { if (!angular.isObject(newerVersion)) {
newerVersion = this.parseVersionString(newerVersion); newerVersion = this.parseVersionString(newerVersion);
} }

View file

@ -80,6 +80,12 @@ describe("utils service", function() {
required = 'uSIwvRDp8QJO'; required = 'uSIwvRDp8QJO';
expect(utils.checkVersion(running, required)).toBeTruthy(); expect(utils.checkVersion(running, required)).toBeTruthy();
}); });
it("Given a version string '1.0.1' and undefined, when I check the version required, the result will be true", function() {
running = '1.0.1';
required = undefined;
expect(utils.checkVersion(running, required)).toBeTruthy();
});
}); });
describe("checkVersionNewer() -", function() { describe("checkVersionNewer() -", function() {
@ -139,5 +145,11 @@ describe("utils service", function() {
older = 'uSIwvRDp8QJO'; older = 'uSIwvRDp8QJO';
expect(utils.checkVersionNewer(newer, older)).toBeFalsy(); expect(utils.checkVersionNewer(newer, older)).toBeFalsy();
}); });
it("Given a version string '1.0.1' and undefined, when I check if the first version is newer, the result will be true", function() {
newer = '1.0.1';
older = undefined;
expect(utils.checkVersionNewer(newer, older)).toBeTruthy();
});
}); });
}); });