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,24 +1,30 @@
[
{
[
{
"date": "3/24/2015", "version": "4.4.5",
"changes": [
{ "text": "- Code refactoring, fixes Podcasts retrieval"}
]
},
{
"date": "1/17/2015", "version": "4.4.0",
"changes": [
{ "text": "- Code refactoring from <a href=\"https://github.com/tsquillario/Jamstash/pull/196\" target=\"_blank\">https://github.com/tsquillario/Jamstash/pull/196</a>"}
]
},
{
{
"date": "12/20/2014", "version": "4.3",
"changes": [
{ "text": "- I'm feeling really good about this one!"},
{ "text": "- jPlayer updated to 2.8.4"}
]
},
{
{
"date": "12/7/2014", "version": "4.2.5",
"changes": [
{ "text": "- Better mobile support (CSS Media Queries)"}
]
},
{
{
"date": "12/6/2014", "version": "4.2.4",
"changes": [
{ "text": "- Double click plays album, Single click to select song. Other bug fixes."}
@ -333,4 +339,4 @@
{ "date": "8/17/2011", "version": "", "changes": [{ "text": ".004 https fix, audio player tweaks"}] },
{ "date": "8/15/2011", "version": "", "changes": [{ "text": ".003 Fixed song details on player"}] },
{ "date": "8/15/2011", "version": "", "changes": [{ "text": ".001 Initial Release"}] }
]
]

View file

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

View file

@ -1,7 +1,7 @@
describe("Persistence service", function() {
'use strict';
var persistence, player, notifications, locker,
var persistence, player, notifications, locker, json,
song, fakeStorage, fakeVersionChangesets;
beforeEach(function() {
fakeVersionChangesets = {versions: []};
@ -10,7 +10,6 @@ describe("Persistence service", function() {
$provide.decorator('locker', function () {
return jasmine.createSpyObj("locker", ["get", "put", "forget"]);
});
$provide.constant("jamstashVersion", "1.0.1");
$provide.value("jamstashVersionChangesets", fakeVersionChangesets);
$provide.decorator('notifications', function () {
@ -22,13 +21,18 @@ describe("Persistence service", function() {
fakePlayer.queue = [];
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_;
player = _player_;
notifications = _notifications_;
locker = _locker_;
json = _json_;
});
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';
spyOn(persistence, 'upgradeToVersion');
spyOn(persistence, 'upgradeVersion');
json.getChangeLog.and.callFake(function (callback) {
console.log('callback', callback);
callback([
{version: '1.0.1'}
]);
});
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() {
@ -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() {
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(notifications.updateMessage).toHaveBeenCalledWith('Version 1.0.0 to 1.0.1', true);
@ -257,39 +267,31 @@ describe("Persistence service", function() {
};
});
describe("and that Jamstash version '1.0.0' was previously stored in local storage,", function() {
beforeEach(function() {
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', {
DefaultSearchType: 0,
DefaultAlbumSort: 0,
Username: "Hedrix",
AutoPlay: true
});
});
it("when I upgrade the storage version to '1.0.1', only the '1.0.1' changeset has been applied", function() {
persistence.upgradeToVersion('1.0.1');
expect(locker.put).toHaveBeenCalledWith('Settings', {
DefaultSearchType: 0,
DefaultAlbumSort: {
id: "default",
name: "Default Sort"
},
Username: "Hedrix",
AutoPlay: true
});
it("when I upgrade the storage version from '1.0.0' to '1.0.2', both changesets have been applied", function() {
persistence.upgradeVersion('1.0.0', '1.0.2');
expect(locker.put).toHaveBeenCalledWith('Settings', {
DefaultSearchType: 0,
DefaultAlbumSort: 0,
Username: "Hedrix",
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() {
fakeStorage.JamstashVersion = '1.0.1';
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.upgradeVersion('1.0.0', '1.0.1');
expect(locker.put).toHaveBeenCalledWith('Settings', {
DefaultSearchType: 0,
DefaultAlbumSort: {
id: "default",
name: "Default Sort"
},
Username: "Hedrix",
AutoPlay: true
});
});
persistence.upgradeToVersion('1.0.2');
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() {
persistence.upgradeVersion('1.0.1', '1.0.2');
expect(locker.put).toHaveBeenCalledWith('Settings', {
DefaultSearchType: {
id: "song",

View file

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

View file

@ -80,6 +80,12 @@ describe("utils service", function() {
required = 'uSIwvRDp8QJO';
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() {
@ -139,5 +145,11 @@ describe("utils service", function() {
older = 'uSIwvRDp8QJO';
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();
});
});
});