From 6617ba6488b807adc19b42992b2aeb2a7b62cc41 Mon Sep 17 00:00:00 2001 From: Francisco Souza Date: Mon, 27 Jul 2015 19:14:22 -0300 Subject: [PATCH] db: make name and username unique together in the key collection Closes #194. --- db/conn.go | 6 ++++-- db/conn_test.go | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/db/conn.go b/db/conn.go index f529b6f..77328fa 100644 --- a/db/conn.go +++ b/db/conn.go @@ -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 } diff --git a/db/conn_test.go b/db/conn_test.go index dd62f63..e5b8d48 100644 --- a/db/conn_test.go +++ b/db/conn_test.go @@ -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) {