Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
db: make name and username unique together in the key collection
Browse files Browse the repository at this point in the history
Closes #194.
  • Loading branch information
Francisco Souza committed Jul 27, 2015
1 parent 2959498 commit 6617ba6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
6 changes: 4 additions & 2 deletions db/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ func (s *Storage) User() *storage.Collection {
}

func (s *Storage) Key() *storage.Collection {
index := mgo.Index{Key: []string{"body"}, Unique: true}
bodyIndex := mgo.Index{Key: []string{"body"}, Unique: true}
nameIndex := mgo.Index{Key: []string{"username", "name"}, Unique: true}
c := s.Collection("key")
c.EnsureIndex(index)
c.EnsureIndex(bodyIndex)
c.EnsureIndex(nameIndex)
return c
}
10 changes: 6 additions & 4 deletions db/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,18 @@ func (s *S) TestSessionKeyShouldReturnKeyCollection(c *check.C) {
c.Assert(key, check.DeepEquals, cKey)
}

func (s *S) TestSessionKeyBodyIsUnique(c *check.C) {
func (s *S) TestSessionKeyIndexes(c *check.C) {
conn, err := Conn()
c.Assert(err, check.IsNil)
defer conn.Close()
key := conn.Key()
indexes, err := key.Indexes()
c.Assert(err, check.IsNil)
c.Assert(indexes, check.HasLen, 2)
c.Assert(indexes[1].Key, check.DeepEquals, []string{"body"})
c.Assert(indexes[1].Unique, check.DeepEquals, true)
c.Check(indexes, check.HasLen, 3)
c.Check(indexes[1].Key, check.DeepEquals, []string{"body"})
c.Check(indexes[1].Unique, check.DeepEquals, true)
c.Check(indexes[2].Key, check.DeepEquals, []string{"username", "name"})
c.Check(indexes[2].Unique, check.DeepEquals, true)
}

func (s *S) TestConnect(c *check.C) {
Expand Down

0 comments on commit 6617ba6

Please sign in to comment.