Пример #1
0
class ListDBSyncingTests(unittest.TestCase):

    def setUp(self):
        with open('.pdaconfig', 'r') as f:
            cfg = PdaConfig(f)
            self.db = GithubIssues(cfg)
            self.db.sync_local_dbstore()

    def tearDown(self):
        if os.path.exists(self.db.local_dbpath):
            os.remove(self.db.local_dbpath)

        del self.db

    def testSyncLocalDBStore(self):

        # retrieve remote data for testing
        r = requests.get(self.db.url_issues, params={'state': 'open'}, auth=self.db.auth)

        for issue in r.json():
            task_no        = str(issue["number"])
            task_summary   = issue["title"]
            task_milestone = issue["milestone"]["title"] 
            task_prio      = ""
            task_type      = ""

            for label in issue["labels"]:
                if label["color"] == GithubIssues.YELLOW:
                    task_prio = label["name"]
                if label["color"] == GithubIssues.GREEN:
                    task_type = label["name"]

            # after data syncing, the issue should be present in local store
            self.assertTrue(task_no in self.db.shelf)

            # the data stored at local should be the same as remote
            self.assertTrue(self.db.shelf[task_no]["summary"]   == task_summary)
            self.assertTrue(self.db.shelf[task_no]["type"]      == task_type)
            self.assertTrue(self.db.shelf[task_no]["priority"]  == task_prio)
            self.assertTrue(self.db.shelf[task_no]["milestone"] == task_milestone)

        self.assertTrue('CMDS_HISTORY' in self.db.shelf)

    def testSyncRemoteDBStore(self):

        if self.db.shelf:

            task_numbers = [number for number in list(self.db.shelf.keys()) \
                                              if number != 'CMDS_HISTORY']

            # (1) remove tasks
            if task_numbers:
                number = task_numbers.pop()
                self.db.remove_task(int(number))

            if task_numbers:
                number = task_numbers.pop()
                self.db.remove_task(int(number))

            # (2) add a task locally
            self.db.add_task('first added', 'tolearn', 'month', 'low')

            # (3) add another task
            added_2nd = self.db.add_task('secondly added', 'todo', 'year', 'high')

            # (4) edit a locally added task
            self.db.edit_task(int(added_2nd), new_milestone='season', new_tasktype='toread')

            # (5) edit a remotely imported task
            if task_numbers:
                number = task_numbers.pop()
                self.db.edit_task(int(number), new_priority='urgmust')

        # a data structure in memory to hold all the records before data is synced to remote
        records = [self.db.shelf[k] for k in self.db.shelf if k != 'CMDS_HISTORY']

        # syncing data to remote
        self.db.sync_remote_dbstore()

        # retrieving remote data to local memory
        r = requests.get(self.db.url_issues, params={'state': 'open'}, auth=self.db.auth)

        if r.status_code == requests.codes.ok:
            remote_records = r.json()

        # check if data is equivalent by making use of sets!
        local, remote = set([]), set([])

        for rec in records:
            local.add((rec['summary'], rec['type'], rec['milestone'], rec['priority']))

        for rec in remote_records:
            prio, ltype = self.db.get_task_prio_and_type(rec)
            remote.add((rec['title'], ltype, rec['milestone']['title'], prio))

        # finally test if local and remote contents are synced!
        self.assertTrue(local==remote)