Exemple #1
0
class TestCase(unittest.TestCase):
    def setUp(self):
        self.mojology = Mojology(config_object="mojology.tests.TestConfig")
        self.app = self.mojology.test_client()

        self.db = pymongo.Connection(TestConfig.MONGO_HOST,
                                     TestConfig.MONGO_PORT)
        self.coll = self.db[TestConfig.MONGO_DB][TestConfig.MONGO_COLLECTION]
        self.pagesize = TestConfig.MOJOLOGY_PAGESIZE
        self.cache = TestConfig.MOJOLOGY_COLLECTION_PREFIX
        self.layout = TestConfig.MOJOLOGY_LAYOUT

        self.coll.drop()

    def populate(self):
        fp = open(os.path.join(os.path.dirname(__file__), "test_data.json"))
        for line in fp:
            j = json.loads(line, object_hook=bson.json_util.object_hook)
            self.coll.insert(j)

    def _mr(self, map_js, out):
        self.coll.map_reduce(
            map_js,
            "function (k, vals) { var sum = { count: 0 }; for (var i in vals) sum.count += vals[i].count; return sum; }",
            out=self.cache + 'mr.' + out,
            finalize=
            "function (who, res) { res.stamp = new Date(); return res; }")

    def do_mapreduce(self):
        self._mr(
            "function () { emit(this.%s, { count: 1 }); }" %
            self.layout.fields['program'], "programs")
        self._mr(
            "function () { emit(this.%s, { count: 1 }); }" %
            self.layout.fields['host'], "hosts")
        self._mr(
            "function () { d = new Date (this.%s*1000); d.setMinutes(0); d.setSeconds(0); emit(d.valueOf()/1000, { count: 1 }); }"
            % self.layout.fields['date'], "time")

    def tearDown(self):
        self.db[TestConfig.MONGO_DB][self.cache + 'mr.programs'].drop()
        self.db[TestConfig.MONGO_DB][self.cache + 'mr.hosts'].drop()
        self.db[TestConfig.MONGO_DB][self.cache + 'mr.time'].drop()
        self.coll.drop()
        self.db.disconnect()
Exemple #2
0
class TestCase (unittest.TestCase):
    def setUp (self):
        self.mojology = Mojology (config_object = "mojology.tests.TestConfig")
        self.app = self.mojology.test_client ()

        self.db = pymongo.Connection (TestConfig.MONGO_HOST, TestConfig.MONGO_PORT)
        self.coll = self.db[TestConfig.MONGO_DB][TestConfig.MONGO_COLLECTION]
        self.pagesize = TestConfig.MOJOLOGY_PAGESIZE
        self.cache = TestConfig.MOJOLOGY_COLLECTION_PREFIX
        self.layout = TestConfig.MOJOLOGY_LAYOUT

        self.coll.drop ()

    def populate (self):
        fp = open (os.path.join (os.path.dirname (__file__), "test_data.json"))
        for line in fp:
            j = json.loads (line, object_hook = bson.json_util.object_hook)
            self.coll.insert (j)


    def _mr (self, map_js, out):
        self.coll.map_reduce (map_js,
                              "function (k, vals) { var sum = { count: 0 }; for (var i in vals) sum.count += vals[i].count; return sum; }",
                              out = self.cache + 'mr.' + out,
                              finalize = "function (who, res) { res.stamp = new Date(); return res; }")

    def do_mapreduce (self):
        self._mr ("function () { emit(this.%s, { count: 1 }); }" % self.layout.fields['program'], "programs")
        self._mr ("function () { emit(this.%s, { count: 1 }); }" % self.layout.fields['host'], "hosts")
        self._mr ("function () { d = new Date (this.%s*1000); d.setMinutes(0); d.setSeconds(0); emit(d.valueOf()/1000, { count: 1 }); }" % self.layout.fields['date'],
                  "time")

    def tearDown (self):
        self.db[TestConfig.MONGO_DB][self.cache + 'mr.programs'].drop ()
        self.db[TestConfig.MONGO_DB][self.cache + 'mr.hosts'].drop ()
        self.db[TestConfig.MONGO_DB][self.cache + 'mr.time'].drop ()
        self.coll.drop ()
        self.db.disconnect ()