def test_push_known(self, requests_mock): repo = Repository.objects.create( name='mozilla-central', url='file:///mozilla-central/' ) pushjs_objects = PushJS.pushes_for(repo, 0) self.assertListEqual(pushjs_objects, []) self.assertFalse(requests_mock.called) response = mock.MagicMock() response.json = mock.MagicMock(return_value={"pushes": { "2": { "changesets": [ "e37d6de26da76cfaea5c348019598759051a0576" ], "date": 1593558780, "user": "******" }, "1": { "changesets": [ "d875063bd55a38f4f9ddf6216f7007afff07db28", "530026e48a9edcf5fd55ff0b79765e5807752289" ], "date": 1593532760, "user": "******" }, }}) requests_mock.configure_mock(return_value=response) pushjs_objects = PushJS.pushes_for(repo, 28) self.assertListEqual([1, 2], [p.id for p in pushjs_objects]) self.assertListEqual( [2, 1], [len(p.changesets) for p in pushjs_objects] )
def test_handlePushes_twice(self): repo = Repository.objects.create(name='mozilla-central', url='file://' + self.repo) ui = mock_ui() hgcommands.init(ui, self.repo) hgrepo = repository(ui, self.repo) (open(hgrepo.pathto('file.dtd'), 'w').write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> ''')) hgcommands.addremove(ui, hgrepo) hgcommands.commit(ui, hgrepo, user="******", message="initial commit") rev0 = hgrepo[0].hex() timestamp = int(time.time()) pushjs0 = PushJS(100, { 'date': timestamp, 'changesets': [rev0], 'user': '******', }) result = handlePushes(repo.pk, [pushjs0]) (open(hgrepo.pathto('file.dtd'), 'w').write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> <!ENTITY key3 "World"> ''')) hgcommands.commit(ui, hgrepo, user="******", message="Second commit") rev1 = hgrepo[1].hex() # a second time timestamp = int(time.time()) pushjs0 = PushJS(101, { 'date': timestamp, 'changesets': [rev1], 'user': '******', }) # re-fetch repo = Repository.objects.get(pk=repo.pk) self.assertEqual(repo.changesets.all().count(), 2) result = handlePushes(repo.pk, [pushjs0]) self.assertEqual(result, 1) # re-fetch repo = Repository.objects.get(pk=repo.pk) self.assertEqual(repo.changesets.all().count(), 3)
def test_handlePushes_twice(self): repo = Repository.objects.create( name='mozilla-central', url='file://' + self.repo ) with hglib.init(self.repo).open() as hgrepo: with open(hgrepo.pathto('file.dtd'), 'w') as fh: fh.write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> ''') hgrepo.commit(user="******", message="initial commit", addremove=True) rev0 = hgrepo[0].node().decode('ascii') timestamp = int(time.time()) pushjs0 = PushJS(100, { 'date': timestamp, 'changesets': [rev0], 'user': '******', }) result = handlePushes(repo.pk, [pushjs0]) with open(hgrepo.pathto('file.dtd'), 'w') as fh: fh.write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> <!ENTITY key3 "World"> ''') hgrepo.commit(user="******", message="Second commit") rev1 = hgrepo[1].node().decode('ascii') # a second time timestamp = int(time.time()) pushjs0 = PushJS(101, { 'date': timestamp, 'changesets': [rev1], 'user': '******', }) # re-fetch repo = Repository.objects.get(pk=repo.pk) self.assertEqual(repo.changesets.all().count(), 2) result = handlePushes(repo.pk, [pushjs0]) self.assertEqual(result, 1) # re-fetch repo = Repository.objects.get(pk=repo.pk) self.assertEqual(repo.changesets.all().count(), 3)
def test_handlePushes_repeated(self): repo = Repository.objects.create(name='mozilla-central', url='file:///' + self.repo) with hglib.init(self.repo).open() as hgrepo: with open(hgrepo.pathto('file.dtd'), 'w') as fh: fh.write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> ''') hgrepo.commit(user="******", message="initial commit", addremove=True) rev0 = hgrepo[0].node().decode('ascii') timestamp = int(time.time()) pushjs0 = PushJS(100, { 'date': timestamp, 'changesets': [rev0], 'user': '******', }) # first time pushes_initial = Push.objects.all().count() result = handlePushes(repo.pk, [pushjs0]) self.assertEqual(result, 1) pushes_after = Push.objects.all().count() self.assertEqual(pushes_initial, pushes_after - 1) # a second time should be harmless result = handlePushes(repo.pk, [pushjs0]) self.assertEqual(result, 1) pushes_after_after = Push.objects.all().count() self.assertEqual(pushes_after, pushes_after_after)
def test_handlePushes_space_files(self): repo = Repository.objects.create(name='mozilla-central', url='file:///' + self.repo) with hglib.init(self.repo).open() as hgrepo: # deliberate trailing space in file name with open(hgrepo.pathto('file.dtd '), 'w') as fh: fh.write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> ''') hgrepo.commit(user="******", message="initial commit", addremove=True) rev0 = hgrepo[0].node().decode('ascii') timestamp = int(time.time()) pushjs0 = PushJS(100, { 'date': timestamp, 'changesets': [rev0], 'user': '******', }) handlePushes(repo.pk, [pushjs0]) file_, = File.objects.all() self.assertEqual(file_.path, 'file.dtd ')
def test_handlePushes_cause_repoerror(self): repo = Repository.objects.create(name='mozilla-central', url='file:///does/not/exist') self.assertEqual(handlePushes(repo.pk, []), None) ui = mock_ui() hgcommands.init(ui, self.repo) hgrepo = repository(ui, self.repo) (open(hgrepo.pathto('file.dtd'), 'w').write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> ''')) hgcommands.addremove(ui, hgrepo) hgcommands.commit(ui, hgrepo, user="******", message="initial commit") rev0 = hgrepo[0].hex() timestamp = int(time.time()) pushjs0 = PushJS(100, { 'date': timestamp, 'changesets': [rev0], 'user': '******', }) self.assertRaises(RepoError, handlePushes, repo.pk, [pushjs0])
def test_handlePushes_space_files(self): repo = Repository.objects.create(name='mozilla-central', url='file:///' + self.repo) self.assertEqual(handlePushes(repo.pk, []), None) ui = mock_ui() hgcommands.init(ui, self.repo) hgrepo = repository(ui, self.repo) (open(hgrepo.pathto('file.dtd '), 'w') # deliberate trailing space .write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> ''')) hgcommands.addremove(ui, hgrepo) hgcommands.commit(ui, hgrepo, user="******", message="initial commit") rev0 = hgrepo[0].hex() timestamp = int(time.time()) pushjs0 = PushJS(100, { 'date': timestamp, 'changesets': [rev0], 'user': '******', }) handlePushes(repo.pk, [pushjs0]) file_, = File.objects.all() self.assertEqual(file_.path, 'file.dtd ')
def test_handlePushes_cause_repoerror(self): repo = Repository.objects.create( name='mozilla-central', url='file:///does/not/exist' ) with hglib.init(self.repo).open() as hgrepo: with open(hgrepo.pathto('file.dtd'), 'w') as fh: fh.write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> ''') hgrepo.commit(user="******", message="initial commit", addremove=True) rev0 = hgrepo[0].node().decode('ascii') timestamp = int(time.time()) pushjs0 = PushJS(100, { 'date': timestamp, 'changesets': [rev0], 'user': '******', }) self.assertRaises(hglib.error.CommandError, handlePushes, repo.pk, [pushjs0])
def loadJSON(self, page, repo): pushes = json.loads(page) if not pushes: return log.msg("%s got %d pushes" % (repo.name, len(pushes))) # convert pushes to sorted list if repo.id not in self.cache: self.cache[repo.id] = [] self.cache[repo.id] += sorted( (PushJS(k, v) for k, v in pushes.iteritems()), key=lambda p: p.id) # signal to load more data if this push hit the limits if len(pushes) == self.limit: self.moredata[repo.id] = True
def test_handlePushes(self): repo = Repository.objects.create(name='mozilla-central', url='file:///' + self.repo) self.assertEqual(handlePushes(repo.pk, []), None) ui = mock_ui() hgcommands.init(ui, self.repo) hgrepo = repository(ui, self.repo) (open(hgrepo.pathto('file.dtd'), 'w').write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> ''')) hgcommands.addremove(ui, hgrepo) hgcommands.commit(ui, hgrepo, user="******", message="initial commit") rev0 = hgrepo[0].hex() timestamp = int(time.time()) push_id = 100 username = '******' pushjs0 = PushJS(push_id, { 'date': timestamp, 'changesets': [rev0], 'user': username, }) result = handlePushes(repo.pk, [pushjs0]) self.assertEqual(result, 1) # expect all of these to have been created push, = Push.objects.all() branch, = Branch.objects.all() changeset, = push.changesets.all() self.assertEqual(push.repository, repo) self.assertEqual(push.push_id, push_id) self.assertEqual(push.user, username) self.assertEqual(push.push_date.strftime('%Y%m%d%H%M'), datetime.datetime.utcnow().strftime('%Y%m%d%H%M')) self.assertEqual(changeset.description, 'initial commit') self.assertEqual(changeset.user, 'Jane Doe <*****@*****.**>') self.assertEqual(changeset.revision, rev0) self.assertEqual(changeset.branch, branch) self.assertEqual(branch.name, 'default')
def test_handlePushes(self): repo = Repository.objects.create( name='mozilla-central', url='file:///' + self.repo ) with hglib.init(self.repo).open() as hgrepo: with open(hgrepo.pathto('file.dtd'), 'w') as fh: fh.write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> ''') hgrepo.commit(user="******", message="initial commit", addremove=True) rev0 = hgrepo[0].node().decode('ascii') timestamp = int(time.time()) push_id = 100 username = '******' pushjs0 = PushJS(push_id, { 'date': timestamp, 'changesets': [rev0], 'user': username, }) result = handlePushes(repo.pk, [pushjs0]) self.assertEqual(result, 1) # expect all of these to have been created push, = Push.objects.all() branch, = Branch.objects.all() changeset, = push.changesets.all() self.assertEqual(push.repository, repo) self.assertEqual(push.push_id, push_id) self.assertEqual(push.user, username) self.assertEqual( push.push_date, datetime.datetime.utcfromtimestamp(timestamp) ) self.assertEqual(changeset.description, 'initial commit') self.assertEqual(changeset.user, 'Jane Doe <*****@*****.**>') self.assertEqual(changeset.revision, rev0) self.assertEqual(changeset.branch, branch) self.assertEqual(branch.name, 'default')
def test_handlePushes_messedup_revisions(self): repo = Repository.objects.create(name='mozilla-central', url='file:///' + self.repo) hgrepo = hglib.init(self.repo).open() (open(hgrepo.pathto('file.dtd'), 'w').write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> ''')) hgrepo.commit(user="******", message="initial commit", addremove=True) rev0 = hgrepo[0].node() timestamp = int(time.time()) pushjs0 = PushJS(100, { 'date': timestamp, 'changesets': [rev0[::-1]], 'user': '******', }) self.assertRaises(KeyError, handlePushes, repo.pk, [pushjs0])
def test_handlePushes_repeated(self): repo = Repository.objects.create(name='mozilla-central', url='file:///' + self.repo) self.assertEqual(handlePushes(repo.pk, []), None) ui = mock_ui() hgcommands.init(ui, self.repo) hgrepo = repository(ui, self.repo) (open(hgrepo.pathto('file.dtd'), 'w').write(''' <!ENTITY key1 "Hello"> <!ENTITY key2 "Cruel"> ''')) hgcommands.addremove(ui, hgrepo) hgcommands.commit(ui, hgrepo, user="******", message="initial commit") rev0 = hgrepo[0].hex() timestamp = int(time.time()) pushjs0 = PushJS(100, { 'date': timestamp, 'changesets': [rev0], 'user': '******', }) # first time pushes_initial = Push.objects.all().count() result = handlePushes(repo.pk, [pushjs0]) self.assertEqual(result, 1) pushes_after = Push.objects.all().count() self.assertEqual(pushes_initial, pushes_after - 1) # a second time should be harmless result = handlePushes(repo.pk, [pushjs0]) self.assertEqual(result, 1) pushes_after_after = Push.objects.all().count() self.assertEqual(pushes_after, pushes_after_after)