OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 from datetime import datetime |
| 6 import json |
| 7 import os |
| 8 |
| 9 from appengine.utils import testing |
| 10 |
| 11 from appengine.path_mangler_hack import PathMangler |
| 12 with PathMangler(os.path.dirname(os.path.dirname(__file__))): |
| 13 from appengine.chromium_cq_status import main |
| 14 from appengine.chromium_cq_status.model.record import Record |
| 15 |
| 16 class StatsTest(testing.AppengineTestCase): # pragma: no cover |
| 17 '''Utility class for stats tests that want to load/clear test Record data''' |
| 18 app_module = main.app |
| 19 |
| 20 def load_records(self, json_filename): |
| 21 json_path = os.path.join(os.path.dirname(__file__), 'resources', |
| 22 json_filename) |
| 23 with open(json_path) as json_file: |
| 24 for record in json.load(json_file): |
| 25 self.mock_now(datetime.utcfromtimestamp(record['timestamp'])) |
| 26 Record( |
| 27 id=record['key'], |
| 28 tags=record['tags'], |
| 29 fields=record['fields'], |
| 30 ).put() |
| 31 |
| 32 @staticmethod |
| 33 def clear_records(): |
| 34 for record in Record.query(): |
| 35 record.key.delete() |
| 36 assert Record.query().count() == 0 |
| 37 |
| 38 def test_load_records(self): |
| 39 self.clear_records() |
| 40 self.load_records('test_load_records.json') |
| 41 self.assertEquals([{ |
| 42 'key': None, |
| 43 'timestamp': 100, |
| 44 'tags': ['tag_a', 'tag_b', 'tag_c'], |
| 45 'fields': {} |
| 46 }, { |
| 47 'key': 'keyed_record', |
| 48 'timestamp': 200, |
| 49 'tags': [], |
| 50 'fields': { |
| 51 'field_a': 'value_a', |
| 52 'field_b': 'value_b', |
| 53 'field_c': 'value_c', |
| 54 } |
| 55 }], |
| 56 [record.to_dict() for record in Record.query().order(Record.timestamp)]) |
| 57 |
| 58 def test_clear_records(self): |
| 59 self.clear_records() |
| 60 self.assertEquals(0, Record.query().count()) |
| 61 for _ in range(10): |
| 62 Record().put() |
| 63 self.assertEquals(10, Record.query().count()) |
| 64 self.clear_records() |
| 65 self.assertEquals(0, Record.query().count()) |
OLD | NEW |