Skip to content

Commit

Permalink
updating code. cleaup. refactoring. updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
podviaznikov committed Nov 27, 2011
1 parent 7a53f23 commit 96625b5
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ test-ci:
$(TESTS)


.PHONY: test
.PHONY: test test-ci
16 changes: 16 additions & 0 deletions Makefile~
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
TESTS = test/*.coffee

test:
@NODE_ENV=test ./node_modules/.bin/mocha \
--require coffee-script \
--reporter list \
$(TESTS)

test-ci:
@NODE_ENV=test ./node_modules/.bin/mocha \
--require coffee-script \
--reporter json \
$(TESTS)


.PHONY: test
2 changes: 0 additions & 2 deletions lib/dao/blobs.dao.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ ObjectsDao = require("./objects.dao").ObjectsDao

class BlobsDao extends ObjectsDao

constructor: (log)-> super log

populateEntity: (meta, attributes) =>
if attributes?
new Blob(attributes, @getRepository(meta.links), meta.key, meta.contentType)
Expand Down
2 changes: 0 additions & 2 deletions lib/dao/commits.dao.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ ObjectsDao = require("./objects.dao").ObjectsDao

class CommitsDao extends ObjectsDao

constructor: (log)-> super log

populateEntity: (meta, attributes) =>
if attributes?
tree = @getLink meta.links, "tree"
Expand Down
9 changes: 4 additions & 5 deletions lib/dao/objects.dao.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
Dao = require("./dao").Dao

class ObjectsDao extends Dao
## ObjectsDao
## -------------
## Base Dao for all Git Objects: Blob, Commit, Tree, Tag.
class exports.ObjectsDao extends Dao

constructor: (log) -> super "objects", log

populateEntity: (meta, attributes) =>
super meta, attributes

# Get link to repository. Can be `null`.
getRepository: (links) => @getLink links, "repository"

exports.ObjectsDao = ObjectsDao

2 changes: 1 addition & 1 deletion lib/dao/repos.dao.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Repo = require("../objects/repo").Repo

class ReposDao extends Dao

constructor: (log)-> super "repositories", log
constructor: (log) -> super "repositories", log

populateEntity: (meta, attributes) =>
if attributes?
Expand Down
7 changes: 2 additions & 5 deletions lib/dao/trees.dao.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ ObjectsDao = require("./objects.dao").ObjectsDao

class TreesDao extends ObjectsDao

constructor: (log)-> super log

populateEntity: (meta, attributes) =>
if attributes?
new Tree(attributes.entries, @getRepository(meta.links), meta.key)
repo = @getRepository(meta.links)
new Tree(attributes.entries, repo, meta.key) if attributes?

getBlobs: (treeId, callback) =>
@walk treeId, [[@bucket, "blob"]], (err, docs) =>
if err
console.log "Cannot get blobs for tree", treeId, err if @log?
callback err
else
blobs = (blobsDao.populateEntity doc.meta, doc.attributes for doc in docs when doc? and doc.attributes?)
Expand Down
7 changes: 2 additions & 5 deletions lib/dao/users.dao.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ User = require("../objects/user").User

class UsersDao extends Dao

constructor: (log) ->
super "users", log
constructor: (log) -> super "users", log

populateEntity: (meta, attributes) ->
if attributes?
new User(meta.key, attributes.email, meta.links)
new User(meta.key, attributes.email, meta.links) if attributes?

# return map:
fetchAllRepos: (user, callback) =>
Expand Down Expand Up @@ -64,4 +62,3 @@ class UsersDao extends Dao
unfollowUser: (user, userToUnfollow, callback) =>

exports.newInstance = (log) -> new UsersDao(log)

43 changes: 26 additions & 17 deletions lib/massive.git.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,7 @@ MassiveGit = exports.MassiveGit = class MassiveGit
if err
callback err
else
@getBlobs tree.id(), (err, blobs) ->
if err
callback err
else
entries = tree.entries
treeEntries = []
console.log "blobs", blobs
for blob in blobs
blobId = blob.id()
name = entry.name for entry in entries when entry.id == blobId
treeEntries.push new TreeEntry name, blob
callback err, treeEntries
@getTreeEntries tree.id(), callback

_prepareEntries: (entries, repoId) =>
plainEntries = []
Expand All @@ -257,22 +246,42 @@ MassiveGit = exports.MassiveGit = class MassiveGit
tasks.push task
{tasks: tasks, treeEntries: plainEntries}

# @TODO write test
getHistoryForRepo: (repoId, callback) =>
@getHead repoId, (err, commitId) =>
if err
callback err
else
@getHistoryForCommit commitId, callback

# @TODO write test
getHistoryForCommit: (commitId, callback) => @commitsDao.getParents commitId, callback

getBlobs: (treeId, callback) =>
@treesDao.getBlobs treeId, (err, blobs) ->

# @TODO write test
getTreeEntries: (treeId, callback) =>
@getBlobs treeId, (err, blobs) ->
if err
err.message = "Cannot retrive blobs"
callback err
else
callback undefined, blobs
entries = tree.entries
treeEntries = []
for blob in blobs
blobId = blob.id()
name = entry.name for entry in entries when entry.id == blobId
treeEntries.push new TreeEntry name, blob
callback err, treeEntries


getBlobs: (id, callback) =>
if !id
callback {statusCode: 422, message: "Invalid parameters"}
else
@treesDao.getBlobs id, (err, blobs) ->
if err
err.message = "Cannot retrive blobs"
callback err
else
callback undefined, blobs

getRepo: (id, callback) =>
if !id
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"validator": ">=0.3.0"
},
"devDependencies": {
"mocha": ">=0.0.6",
"mocha": ">=0.0.8",
"should": ">=0.3.1",
"async": ">=0.1.10"
}
Expand Down
124 changes: 75 additions & 49 deletions test/error.handling.coffee
Original file line number Diff line number Diff line change
@@ -1,96 +1,122 @@
should = require "should"
MassiveGit = new (require("../lib/massive.git").MassiveGit)()

describe "fake", ->
describe "repo", ->
it "should not exists", ->
describe "MassiveGit", ->
describe "#getRepo('fake-repo-id')", ->
it "return 'Repo wasn't found' error", (done) ->
MassiveGit.getRepo "fake-repo-id", (err, repo) ->
err.should.exist
err.should.have.property "message", "Repo wasn't found"
err.should.have.property "statusCode", 400
should.not.exist repo
describe "blob", ->
it "should not exists", ->
MassiveGit.getBlob "fake-blob-id", (err, repo) ->
done()
describe "#getBlob('fake-blob-id')", ->
it "return 'Blob wasn't found' error", (done) ->
MassiveGit.getBlob "fake-blob-id", (err, blob) ->
err.should.exist
err.should.have.property "message", "Blob wasn't found"
err.should.have.property "statusCode", 400
should.not.exist repo
describe "commit", ->
it "should not exists", ->
MassiveGit.getCommit "fake-commit-id", (err, repo) ->
should.not.exist blob
done()
describe "#getCommit('fake-commit-id')", ->
it "return 'Commit wasn't found' error", (done) ->
MassiveGit.getCommit "fake-commit-id", (err, commit) ->
err.should.exist
err.should.have.property "message", "Commit wasn't found"
err.should.have.property "statusCode", 400
should.not.exist repo
describe "tree", ->
it "should not exists", ->
MassiveGit.getTree "fake-tree-id", (err, repo) ->
should.not.exist commit
done()
describe "#getTree('fake-tree-id')", ->
it "return 'Tree wasn't found' error", (done) ->
MassiveGit.getTree "fake-tree-id", (err, tree) ->
err.should.exist
err.should.have.property "message", "Tree wasn't found"
err.should.have.property "statusCode", 400
should.not.exist repo


describe "when id is null", ->
describe "for repo", ->
it "should return 'Invalid parameters' error", ->
should.not.exist tree
done()
describe "#getBlobs('fake-tree-id')", ->
it "return 'Cannot retrive blobs' error", (done) ->
MassiveGit.getBlobs "fake-tree-id", (err, blobs) ->
blobs.length.should.equal 0
done err
describe "#getRepo(null)", ->
it "return 'Invalid parameters' error", (done) ->
MassiveGit.getRepo null, (err, repo) ->
err.should.exist
err.should.have.property "message", "Invalid parameters"
err.should.have.property "statusCode", 422
should.not.exist repo
describe "for blob", ->
it "should return 'Invalid parameters' error", ->
MassiveGit.getBlob null, (err, repo) ->
done()
describe "#getBlob(null)", ->
it "return 'Invalid parameters' error", (done) ->
MassiveGit.getBlob null, (err, blob) ->
err.should.exist
err.should.have.property "message", "Invalid parameters"
err.should.have.property "statusCode", 422
should.not.exist repo
describe "for commit", ->
it "should return 'Invalid parameters' error", ->
MassiveGit.getCommit null, (err, repo) ->
should.not.exist blob
done()
describe "#getCommit(null)", ->
it "return 'Invalid parameters' error", ->
MassiveGit.getCommit null, (err, commit) ->
err.should.exist
err.should.have.property "message", "Invalid parameters"
err.should.have.property "statusCode", 422
should.not.exist repo
describe "for tree", ->
it "should return 'Invalid parameters' error", ->
MassiveGit.getTree null, (err, repo) ->
should.not.exist commit
describe "#getTree(null)", ->
it "return 'Invalid parameters' error", (done) ->
MassiveGit.getTree null, (err, tree) ->
err.should.exist
err.should.have.property "message", "Invalid parameters"
err.should.have.property "statusCode", 422
should.not.exist repo

describe "when id is undefined", ->
describe "for repo", ->
it "should return 'Invalid parameters' error", ->
should.not.exist tree
done()
describe "#getBlobs(null)", ->
it "return 'Invalid parameters' error", (done) ->
MassiveGit.getBlobs null, (err, blobs) ->
err.should.exist
err.should.have.property "message", "Invalid parameters"
err.should.have.property "statusCode", 422
should.not.exist blobs
done()
describe "#getRepo(undefined)", ->
it "return 'Invalid parameters' error", (done) ->
MassiveGit.getRepo undefined, (err, repo) ->
err.should.exist
err.should.have.property "message", "Invalid parameters"
err.should.have.property "statusCode", 422
should.not.exist repo
describe "for blob", ->
it "should return 'Invalid parameters' error", ->
MassiveGit.getBlob undefined, (err, repo) ->
done()
describe "#getBlob(undefined)", ->
it "return 'Invalid parameters' error", (done) ->
MassiveGit.getBlob undefined, (err, blob) ->
err.should.exist
err.should.have.property "message", "Invalid parameters"
err.should.have.property "statusCode", 422
should.not.exist repo
describe "for commit", ->
it "should return 'Invalid parameters' error", ->
MassiveGit.getCommit undefined, (err, repo) ->
should.not.exist blob
done()
describe "#getCommit(undefined)", ->
it "return 'Invalid parameters' error", (done) ->
MassiveGit.getCommit undefined, (err, commit) ->
err.should.exist
err.should.have.property "message", "Invalid parameters"
err.should.have.property "statusCode", 422
should.not.exist repo
describe "for tree", ->
it "should return 'Invalid parameters' error", ->
MassiveGit.getTree undefined, (err, repo) ->
should.not.exist commit
done()
describe "#getTree(undefined)", ->
it "return 'Invalid parameters' error", (done) ->
MassiveGit.getTree undefined, (err, tree) ->
err.should.exist
err.should.have.property "message", "Invalid parameters"
err.should.have.property "statusCode", 422
should.not.exist repo

should.not.exist tree
done()
describe "#getBlobs(undefined)", ->
it "return 'Invalid parameters' error", (done) ->
MassiveGit.getBlobs undefined, (err, blobs) ->
err.should.exist
err.should.have.property "message", "Invalid parameters"
err.should.have.property "statusCode", 422
should.not.exist blobs
done()


0 comments on commit 96625b5

Please sign in to comment.