Use version numbers for sorting and querying, not dates

This commit is contained in:
James Allen
2014-03-06 10:45:51 +00:00
parent 2e1307bd82
commit aadce232a1
6 changed files with 33 additions and 30 deletions
@@ -30,11 +30,11 @@ describe "Getting a diff", ->
}, {
op: [{ i: "two ", p: 4 }]
meta: { ts: @from + twoMinutes, user_id: @user_id }
v: 4
v: @fromVersion = 4
}, {
op: [{ i: "three ", p: 8 }]
meta: { ts: @to - twoMinutes, user_id: @user_id }
v: 5
v: @toVersion = 5
}, {
op: [{ i: "four", p: 14 }]
meta: { ts: @to + twoMinutes, user_id: @user_id }
@@ -53,7 +53,7 @@ describe "Getting a diff", ->
TrackChangesClient.pushRawUpdates @doc_id, @updates, (error) =>
throw error if error?
TrackChangesClient.getDiff @project_id, @doc_id, @from, @to, (error, diff) =>
TrackChangesClient.getDiff @project_id, @doc_id, @fromVersion, @toVersion, (error, diff) =>
throw error if error?
@diff = diff.diff
done()
@@ -30,7 +30,7 @@ describe "Getting updates", ->
}, {
op: [{ i: "three ", p: 8 }]
meta: { ts: @to, user_id: @user_id }
v: 5
v: @toVersion = 5
}, {
op: [{ i: "four", p: 14 }]
meta: { ts: @to + 2 * @minutes, user_id: @user_id }
@@ -39,7 +39,7 @@ describe "Getting updates", ->
TrackChangesClient.pushRawUpdates @doc_id, @updates, (error) =>
throw error if error?
TrackChangesClient.getUpdates @project_id, @doc_id, { to: @to, limit: 2 }, (error, body) =>
TrackChangesClient.getUpdates @project_id, @doc_id, { to: @toVersion, limit: 2 }, (error, body) =>
throw error if error?
@updates = body.updates
done()
@@ -53,6 +53,8 @@ describe "DiffManager", ->
{ op: "mock-2", v: 40, meta: { start_ts: new Date(@to.getTime() - 10)} }
{ op: "mock-1", v: 39, meta: { start_ts: new Date(@to.getTime() - 20)} }
]
@fromVersion = 39
@toVersion = 40
@diffed_updates = @updates.slice(2)
@rewound_content = "rewound-content"
@diff = [ u: "mock-diff" ]
@@ -62,11 +64,11 @@ describe "DiffManager", ->
@DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
@DiffGenerator.rewindUpdates = sinon.stub().returns(@rewound_content)
@DiffGenerator.buildDiff = sinon.stub().returns(@diff)
@DiffManager.getDiff @project_id, @doc_id, @from, @to, @callback
@DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
it "should get the latest doc and version with all recent updates", ->
@DiffManager.getLatestDocAndUpdates
.calledWith(@project_id, @doc_id, @from, null)
.calledWith(@project_id, @doc_id, @fromVersion, null)
.should.equal true
it "should rewind the diff", ->
@@ -87,7 +89,7 @@ describe "DiffManager", ->
@version = 50
@updates = [ { op: "mock-1", v: 40 }, { op: "mock-1", v: 39 } ]
@DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
@DiffManager.getDiff @project_id, @doc_id, @from, @to, @callback
@DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
it "should call the callback with an error", ->
@callback
@@ -98,7 +100,7 @@ describe "DiffManager", ->
beforeEach ->
@DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
@DiffGenerator.rewindUpdates = sinon.stub().throws(@error = new Error("inconsistent!"))
@DiffManager.getDiff @project_id, @doc_id, @from, @to, @callback
@DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
it "should call the callback with an error", ->
@callback
@@ -141,25 +141,24 @@ describe "MongoManager", ->
@db.docHistory.limit = sinon.stub().returns @db.docHistory
@db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @updates)
@from = new Date(Date.now())
@to = new Date(Date.now() + 100000)
@from = 42
@to = 55
describe "with a toDate", ->
describe "with a to version", ->
beforeEach ->
@MongoManager.getUpdates @doc_id, from: @from, to: @to, @callback
it "should find the all updates between the to and from date", ->
it "should find the all updates between the to and from versions", ->
@db.docHistory.find
.calledWith({
doc_id: ObjectId(@doc_id)
"meta.end_ts": { $gte: @from }
"meta.start_ts": { $lte: @to }
v: { $gte: @from, $lte: @to }
})
.should.equal true
it "should sort in descending timestamp order", ->
it "should sort in descending version order", ->
@db.docHistory.sort
.calledWith("meta.end_ts": -1)
.calledWith("v": -1)
.should.equal true
it "should not limit the results", ->
@@ -169,15 +168,15 @@ describe "MongoManager", ->
it "should call the call back with the updates", ->
@callback.calledWith(null, @updates).should.equal true
describe "without a todo date", ->
describe "without a to version", ->
beforeEach ->
@MongoManager.getUpdates @doc_id, from: @from, @callback
it "should find the all updates after the from date", ->
it "should find the all updates after the from version", ->
@db.docHistory.find
.calledWith({
doc_id: ObjectId(@doc_id)
"meta.end_ts": { $gte: @from }
v: { $gte: @from }
})
.should.equal true