1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# Copyright 2012, 2013 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
import logging
from pyelasticsearch import ElasticSearch
from requests import Response
from charmworld.charmstore import CharmStore
from charmworld.jobs.cstat import update_counts
from charmworld.models import CharmSource
from charmworld.search import ElasticSearchClient
from charmworld.testing import (
factory,
logger_buffer,
MongoTestBase,
temp_index_client,
)
class TestUpdateCounts(MongoTestBase):
def test_update_counts(self):
charm_id, charm = factory.makeCharm(self.db)
index_client = self.use_context(temp_index_client())
index_client.index_charm(charm)
log = logging.getLogger("charm.stat")
store = CharmStore()
class FakeSession:
@staticmethod
def send(request):
response = Response()
if ('start' in request.url):
response._content = '[["2010-12-24", 4], ["2010-12-25", 1]]'
else:
response._content = '[[25]]'
return response
store.session = FakeSession
update_counts(CharmSource(self.db, index_client), store, log)
db_charm = self.db.charms.find_one({'_id': charm_id})
self.assertEqual(5, db_charm['downloads_in_past_30_days'])
self.assertEqual(25, db_charm['downloads'])
index_charm = index_client.get(charm_id)
self.assertEqual(5, index_charm['downloads_in_past_30_days'])
self.assertEqual(25, index_charm['downloads'])
def test_update_no_index(self):
charm_id, charm = factory.makeCharm(self.db)
index_client = ElasticSearchClient(
ElasticSearch(['http://localhost:70']), 'foo')
log = logging.getLogger("charm.stat")
store = CharmStore()
class FakeSession:
@staticmethod
def send(request):
response = Response()
response._content = '[["2010-12-24", 4], ["2010-12-25", 1]]'
return response
store.session = FakeSession
with logger_buffer('charm.stat') as handler:
exit_code = update_counts(CharmSource(self.db, index_client),
store, log)
self.assertEqual(40, handler.buffer[1].levelno)
self.assertEqual('Could not connect to search server.',
handler.buffer[1].msg)
self.assertEqual(2, len(handler.buffer))
self.assertEqual(1, exit_code)
|