Merge lp:~alecu/unity-scope-snappy/use-testability into lp:~unity-api-team/unity-scope-snappy/trunk

Proposed by Alejandro J. Cura
Status: Work in progress
Proposed branch: lp:~alecu/unity-scope-snappy/use-testability
Merge into: lp:~unity-api-team/unity-scope-snappy/trunk
Diff against target: 167 lines (+132/-2)
3 files modified
snappy-scope/main.go (+2/-2)
snappy-scope/main_test.go (+124/-0)
webdm/client.go (+6/-0)
To merge this branch: bzr merge lp:~alecu/unity-scope-snappy/use-testability
Reviewer Review Type Date Requested Status
Alejandro J. Cura Pending
Review via email: mp+259862@code.launchpad.net

Description of the change

*** NOT READY YET ***
do not merge

To post a comment you must log in.

Unmerged revisions

9. By Alejandro J. Cura

First attempt at adding interfaces for exported types, to ease testing.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'snappy-scope/main.go'
2--- snappy-scope/main.go 2015-05-20 13:02:51 +0000
3+++ snappy-scope/main.go 2015-05-21 22:24:46 +0000
4@@ -9,7 +9,7 @@
5 )
6
7 type SnappyScope struct {
8- webdmClient *webdm.Client
9+ webdmClient webdm.Webdm
10 }
11
12 func (scope *SnappyScope) SetScopeBase(base *scopes.ScopeBase) {
13@@ -31,7 +31,7 @@
14 }
15 }`
16
17-func (scope SnappyScope) Search(query *scopes.CannedQuery, metadata *scopes.SearchMetadata, reply *scopes.SearchReply, cancelled <-chan bool) error {
18+func (scope SnappyScope) Search(query scopes.CannedQueryInterface, metadata scopes.SearchMetadataInterface, reply scopes.SearchReplyInterface, cancelled <-chan bool) error {
19 packages, err := scope.webdmClient.GetStorePackages()
20 if err != nil {
21 errorString := fmt.Sprintf("unity-scope-snappy: Unable to retrieve store packages: %s",
22
23=== added file 'snappy-scope/main_test.go'
24--- snappy-scope/main_test.go 1970-01-01 00:00:00 +0000
25+++ snappy-scope/main_test.go 2015-05-21 22:24:46 +0000
26@@ -0,0 +1,124 @@
27+package main
28+
29+import (
30+ "testing"
31+ "launchpad.net/go-unityscopes/v2"
32+ "launchpad.net/unity-scope-snappy/webdm"
33+)
34+
35+type fakeWebdmClient struct {
36+ storePackages []webdm.Package
37+ installedPackages []webdm.Package
38+}
39+
40+func (client *fakeWebdmClient) GetStorePackages() ([]webdm.Package, error) {
41+ return client.storePackages, nil
42+}
43+
44+func (client *fakeWebdmClient) GetInstalledPackages() ([]webdm.Package, error) {
45+ return client.installedPackages, nil
46+}
47+
48+// Fake cannedQuery
49+type fakeCannedQuery struct {
50+ departmentID string
51+ queryString string
52+}
53+
54+func (query *fakeCannedQuery) ScopeID() string {
55+ return "fake scope id"
56+}
57+
58+func (query *fakeCannedQuery) DepartmentID() string {
59+ return query.departmentID
60+}
61+
62+func (query *fakeCannedQuery) QueryString() string {
63+ return query.queryString
64+}
65+
66+func (query *fakeCannedQuery) FilterState() scopes.FilterState {
67+ return nil
68+}
69+
70+func (query *fakeCannedQuery) SetDepartmentID(departmentID string) {
71+ query.departmentID = departmentID
72+}
73+
74+func (query *fakeCannedQuery) SetQueryString(queryString string) {
75+ query.queryString = queryString
76+}
77+
78+func (query *fakeCannedQuery) ToURI() string {
79+ return "fake://uri/" + query.ScopeID()
80+}
81+
82+// Fake search metadata
83+type fakeSearchMetadata struct {
84+ location *scopes.Location
85+ keywords []string
86+}
87+
88+func (metadata *fakeSearchMetadata) Cardinality() int {
89+ return 0
90+}
91+
92+func (metadata *fakeSearchMetadata) Location() *scopes.Location {
93+ return metadata.location
94+}
95+
96+func (metadata *fakeSearchMetadata) SetLocation(l *scopes.Location) error {
97+ metadata.location = l
98+ return nil
99+}
100+
101+func (metadata *fakeSearchMetadata) SetAggregatedKeywords(keywords []string) error {
102+ metadata.keywords = keywords
103+ return nil
104+}
105+
106+func (metadata *fakeSearchMetadata) AggregatedKeywords() []string {
107+ return metadata.keywords
108+}
109+
110+func (metadata *fakeSearchMetadata) IsAggregated() bool {
111+ return true
112+}
113+
114+
115+// Fake search reply
116+type fakeSearchReply struct {
117+}
118+
119+func (reply *fakeSearchReply) Finished() {
120+}
121+
122+func (reply *fakeSearchReply) Error(err error) {
123+}
124+
125+func (reply *fakeSearchReply) RegisterCategory(id, title, icon, template string) *scopes.Category {
126+ return nil
127+}
128+
129+func (reply *fakeSearchReply) RegisterDepartments(parent *scopes.Department) {
130+}
131+
132+func (reply *fakeSearchReply) Push(result *scopes.CategorisedResult) error {
133+ return nil
134+}
135+
136+func (reply *fakeSearchReply) PushFilters(filters []scopes.Filter, state scopes.FilterState) error {
137+ return nil
138+}
139+
140+
141+func TestSearch(t *testing.T) {
142+ scope := new(SnappyScope)
143+ scope.webdmClient = new(fakeWebdmClient)
144+
145+ query := new(fakeCannedQuery)
146+ metadata := new(fakeSearchMetadata)
147+ reply := new(fakeSearchReply)
148+
149+ scope.Search(query, metadata, reply, nil)
150+}
151
152=== modified file 'webdm/client.go'
153--- webdm/client.go 2015-05-20 13:02:51 +0000
154+++ webdm/client.go 2015-05-21 22:24:46 +0000
155@@ -23,6 +23,12 @@
156 apiDefaultIconPath = "/public/images/default-package-icon.svg"
157 )
158
159+// exported methods of the webdm client
160+type Webdm interface {
161+ GetInstalledPackages() ([]Package, error)
162+ GetStorePackages() ([]Package, error)
163+}
164+
165 // UnmarshallJSON exists to decode the Status field in the json into an
166 // "Installed" boolean
167 func (s *Status) UnmarshalJSON(data []byte) error {

Subscribers

People subscribed via source and target branches

to all changes: