~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_knit.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-02-17 02:58:22 UTC
  • mfrom: (2249.5.20 knit_utf8_revision_ids)
  • Revision ID: pqm@pqm.ubuntu.com-20070217025822-306d98c244b53b08
(John Arbash Meinel) Clean up most internals to use utf-8 revision ids instead of Unicode.

Show diffs side-by-side

added added

removed removed

Lines of Context:
156
156
            transport.calls.pop(0))
157
157
 
158
158
    def test_read_utf8_version_id(self):
 
159
        unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
 
160
        utf8_revision_id = unicode_revision_id.encode('utf-8')
159
161
        transport = MockTransport([
160
162
            _KnitIndex.HEADER,
161
 
            u"version-\N{CYRILLIC CAPITAL LETTER A}"
162
 
                u" option 0 1 :".encode("utf-8")
 
163
            '%s option 0 1 :' % (utf8_revision_id,)
163
164
            ])
164
165
        index = _KnitIndex(transport, "filename", "r")
165
 
        self.assertTrue(
166
 
            index.has_version(u"version-\N{CYRILLIC CAPITAL LETTER A}"))
 
166
        # _KnitIndex is a private class, and deals in utf8 revision_ids, not
 
167
        # Unicode revision_ids.
 
168
        self.assertTrue(index.has_version(utf8_revision_id))
 
169
        self.assertFalse(index.has_version(unicode_revision_id))
167
170
 
168
171
    def test_read_utf8_parents(self):
 
172
        unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
 
173
        utf8_revision_id = unicode_revision_id.encode('utf-8')
169
174
        transport = MockTransport([
170
175
            _KnitIndex.HEADER,
171
 
            u"version option 0 1"
172
 
                u" .version-\N{CYRILLIC CAPITAL LETTER A} :".encode("utf-8")
 
176
            "version option 0 1 .%s :" % (utf8_revision_id,)
173
177
            ])
174
178
        index = _KnitIndex(transport, "filename", "r")
175
 
        self.assertEqual([u"version-\N{CYRILLIC CAPITAL LETTER A}"],
 
179
        self.assertEqual([utf8_revision_id],
176
180
            index.get_parents_with_ghosts("version"))
177
181
 
178
182
    def test_read_ignore_corrupted_lines(self):
184
188
            ])
185
189
        index = _KnitIndex(transport, "filename", "r")
186
190
        self.assertEqual(1, index.num_versions())
187
 
        self.assertTrue(index.has_version(u"version"))
 
191
        self.assertTrue(index.has_version("version"))
188
192
 
189
193
    def test_read_corrupted_header(self):
190
194
        transport = MockTransport(['not a bzr knit index header\n'])
201
205
            ])
202
206
        index = _KnitIndex(transport, "filename", "r")
203
207
        self.assertEqual(2, index.num_versions())
204
 
        self.assertEqual(1, index.lookup(u"version"))
205
 
        self.assertEqual((3, 4), index.get_position(u"version"))
206
 
        self.assertEqual(["options3"], index.get_options(u"version"))
207
 
        self.assertEqual([u"parent", u"other"],
208
 
            index.get_parents_with_ghosts(u"version"))
 
208
        self.assertEqual(1, index.lookup("version"))
 
209
        self.assertEqual((3, 4), index.get_position("version"))
 
210
        self.assertEqual(["options3"], index.get_options("version"))
 
211
        self.assertEqual(["parent", "other"],
 
212
            index.get_parents_with_ghosts("version"))
209
213
 
210
214
    def test_read_compressed_parents(self):
211
215
        transport = MockTransport([
215
219
            "c option 0 1 1 0 :",
216
220
            ])
217
221
        index = _KnitIndex(transport, "filename", "r")
218
 
        self.assertEqual([u"a"], index.get_parents(u"b"))
219
 
        self.assertEqual([u"b", u"a"], index.get_parents(u"c"))
 
222
        self.assertEqual(["a"], index.get_parents("b"))
 
223
        self.assertEqual(["b", "a"], index.get_parents("c"))
220
224
 
221
225
    def test_write_utf8_version_id(self):
 
226
        unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
 
227
        utf8_revision_id = unicode_revision_id.encode('utf-8')
222
228
        transport = MockTransport([
223
229
            _KnitIndex.HEADER
224
230
            ])
225
231
        index = _KnitIndex(transport, "filename", "r")
226
 
        index.add_version(u"version-\N{CYRILLIC CAPITAL LETTER A}",
227
 
            ["option"], 0, 1, [])
 
232
        index.add_version(utf8_revision_id, ["option"], 0, 1, [])
228
233
        self.assertEqual(("append_bytes", ("filename",
229
 
            u"\nversion-\N{CYRILLIC CAPITAL LETTER A}"
230
 
                u" option 0 1  :".encode("utf-8")),
 
234
            "\n%s option 0 1  :" % (utf8_revision_id,)),
231
235
            {}),
232
236
            transport.calls.pop(0))
233
237
 
234
238
    def test_write_utf8_parents(self):
 
239
        unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
 
240
        utf8_revision_id = unicode_revision_id.encode('utf-8')
235
241
        transport = MockTransport([
236
242
            _KnitIndex.HEADER
237
243
            ])
238
244
        index = _KnitIndex(transport, "filename", "r")
239
 
        index.add_version(u"version", ["option"], 0, 1,
240
 
            [u"version-\N{CYRILLIC CAPITAL LETTER A}"])
 
245
        index.add_version("version", ["option"], 0, 1, [utf8_revision_id])
241
246
        self.assertEqual(("append_bytes", ("filename",
242
 
            u"\nversion option 0 1"
243
 
                u" .version-\N{CYRILLIC CAPITAL LETTER A} :".encode("utf-8")),
 
247
            "\nversion option 0 1 .%s :" % (utf8_revision_id,)),
244
248
            {}),
245
249
            transport.calls.pop(0))
246
250
 
249
253
        index = _KnitIndex(transport, "filename", "w", create=True)
250
254
        self.assertEqual([], index.get_graph())
251
255
 
252
 
        index.add_version(u"a", ["option"], 0, 1, [u"b"])
253
 
        self.assertEqual([(u"a", [u"b"])], index.get_graph())
 
256
        index.add_version("a", ["option"], 0, 1, ["b"])
 
257
        self.assertEqual([("a", ["b"])], index.get_graph())
254
258
 
255
 
        index.add_version(u"c", ["option"], 0, 1, [u"d"])
256
 
        self.assertEqual([(u"a", [u"b"]), (u"c", [u"d"])],
 
259
        index.add_version("c", ["option"], 0, 1, ["d"])
 
260
        self.assertEqual([("a", ["b"]), ("c", ["d"])],
257
261
            sorted(index.get_graph()))
258
262
 
259
263
    def test_get_ancestry(self):
267
271
        index = _KnitIndex(transport, "filename", "r")
268
272
 
269
273
        self.assertEqual([], index.get_ancestry([]))
270
 
        self.assertEqual([u"a"], index.get_ancestry([u"a"]))
271
 
        self.assertEqual([u"a", u"b"], index.get_ancestry([u"b"]))
272
 
        self.assertEqual([u"a", u"b", u"c"], index.get_ancestry([u"c"]))
273
 
        self.assertEqual([u"a", u"b", u"c", u"d"], index.get_ancestry([u"d"]))
274
 
        self.assertEqual([u"a", u"b"], index.get_ancestry([u"a", u"b"]))
275
 
        self.assertEqual([u"a", u"b", u"c"], index.get_ancestry([u"a", u"c"]))
 
274
        self.assertEqual(["a"], index.get_ancestry(["a"]))
 
275
        self.assertEqual(["a", "b"], index.get_ancestry(["b"]))
 
276
        self.assertEqual(["a", "b", "c"], index.get_ancestry(["c"]))
 
277
        self.assertEqual(["a", "b", "c", "d"], index.get_ancestry(["d"]))
 
278
        self.assertEqual(["a", "b"], index.get_ancestry(["a", "b"]))
 
279
        self.assertEqual(["a", "b", "c"], index.get_ancestry(["a", "c"]))
276
280
 
277
 
        self.assertRaises(RevisionNotPresent, index.get_ancestry, [u"e"])
 
281
        self.assertRaises(RevisionNotPresent, index.get_ancestry, ["e"])
278
282
 
279
283
    def test_get_ancestry_with_ghosts(self):
280
284
        transport = MockTransport([
287
291
        index = _KnitIndex(transport, "filename", "r")
288
292
 
289
293
        self.assertEqual([], index.get_ancestry_with_ghosts([]))
290
 
        self.assertEqual([u"a"], index.get_ancestry_with_ghosts([u"a"]))
291
 
        self.assertEqual([u"a", u"e", u"b"],
292
 
            index.get_ancestry_with_ghosts([u"b"]))
293
 
        self.assertEqual([u"a", u"g", u"f", u"c"],
294
 
            index.get_ancestry_with_ghosts([u"c"]))
295
 
        self.assertEqual([u"a", u"g", u"f", u"c", u"k", u"j", u"h", u"d"],
296
 
            index.get_ancestry_with_ghosts([u"d"]))
297
 
        self.assertEqual([u"a", u"e", u"b"],
298
 
            index.get_ancestry_with_ghosts([u"a", u"b"]))
299
 
        self.assertEqual([u"a", u"g", u"f", u"c"],
300
 
            index.get_ancestry_with_ghosts([u"a", u"c"]))
 
294
        self.assertEqual(["a"], index.get_ancestry_with_ghosts(["a"]))
 
295
        self.assertEqual(["a", "e", "b"],
 
296
            index.get_ancestry_with_ghosts(["b"]))
 
297
        self.assertEqual(["a", "g", "f", "c"],
 
298
            index.get_ancestry_with_ghosts(["c"]))
 
299
        self.assertEqual(["a", "g", "f", "c", "k", "j", "h", "d"],
 
300
            index.get_ancestry_with_ghosts(["d"]))
 
301
        self.assertEqual(["a", "e", "b"],
 
302
            index.get_ancestry_with_ghosts(["a", "b"]))
 
303
        self.assertEqual(["a", "g", "f", "c"],
 
304
            index.get_ancestry_with_ghosts(["a", "c"]))
301
305
        self.assertEqual(
302
 
            [u"a", u"g", u"f", u"c", u"e", u"b", u"k", u"j", u"h", u"d"],
303
 
            index.get_ancestry_with_ghosts([u"b", u"d"]))
 
306
            ["a", "g", "f", "c", "e", "b", "k", "j", "h", "d"],
 
307
            index.get_ancestry_with_ghosts(["b", "d"]))
304
308
 
305
309
        self.assertRaises(RevisionNotPresent,
306
 
            index.get_ancestry_with_ghosts, [u"e"])
 
310
            index.get_ancestry_with_ghosts, ["e"])
307
311
 
308
312
    def test_num_versions(self):
309
313
        transport = MockTransport([
314
318
        self.assertEqual(0, index.num_versions())
315
319
        self.assertEqual(0, len(index))
316
320
 
317
 
        index.add_version(u"a", ["option"], 0, 1, [])
318
 
        self.assertEqual(1, index.num_versions())
319
 
        self.assertEqual(1, len(index))
320
 
 
321
 
        index.add_version(u"a", ["option2"], 1, 2, [])
322
 
        self.assertEqual(1, index.num_versions())
323
 
        self.assertEqual(1, len(index))
324
 
 
325
 
        index.add_version(u"b", ["option"], 0, 1, [])
 
321
        index.add_version("a", ["option"], 0, 1, [])
 
322
        self.assertEqual(1, index.num_versions())
 
323
        self.assertEqual(1, len(index))
 
324
 
 
325
        index.add_version("a", ["option2"], 1, 2, [])
 
326
        self.assertEqual(1, index.num_versions())
 
327
        self.assertEqual(1, len(index))
 
328
 
 
329
        index.add_version("b", ["option"], 0, 1, [])
326
330
        self.assertEqual(2, index.num_versions())
327
331
        self.assertEqual(2, len(index))
328
332
 
334
338
 
335
339
        self.assertEqual([], index.get_versions())
336
340
 
337
 
        index.add_version(u"a", ["option"], 0, 1, [])
338
 
        self.assertEqual([u"a"], index.get_versions())
339
 
 
340
 
        index.add_version(u"a", ["option"], 0, 1, [])
341
 
        self.assertEqual([u"a"], index.get_versions())
342
 
 
343
 
        index.add_version(u"b", ["option"], 0, 1, [])
344
 
        self.assertEqual([u"a", u"b"], index.get_versions())
 
341
        index.add_version("a", ["option"], 0, 1, [])
 
342
        self.assertEqual(["a"], index.get_versions())
 
343
 
 
344
        index.add_version("a", ["option"], 0, 1, [])
 
345
        self.assertEqual(["a"], index.get_versions())
 
346
 
 
347
        index.add_version("b", ["option"], 0, 1, [])
 
348
        self.assertEqual(["a", "b"], index.get_versions())
345
349
 
346
350
    def test_idx_to_name(self):
347
351
        transport = MockTransport([
351
355
            ])
352
356
        index = _KnitIndex(transport, "filename", "r")
353
357
 
354
 
        self.assertEqual(u"a", index.idx_to_name(0))
355
 
        self.assertEqual(u"b", index.idx_to_name(1))
356
 
        self.assertEqual(u"b", index.idx_to_name(-1))
357
 
        self.assertEqual(u"a", index.idx_to_name(-2))
 
358
        self.assertEqual("a", index.idx_to_name(0))
 
359
        self.assertEqual("b", index.idx_to_name(1))
 
360
        self.assertEqual("b", index.idx_to_name(-1))
 
361
        self.assertEqual("a", index.idx_to_name(-2))
358
362
 
359
363
    def test_lookup(self):
360
364
        transport = MockTransport([
364
368
            ])
365
369
        index = _KnitIndex(transport, "filename", "r")
366
370
 
367
 
        self.assertEqual(0, index.lookup(u"a"))
368
 
        self.assertEqual(1, index.lookup(u"b"))
 
371
        self.assertEqual(0, index.lookup("a"))
 
372
        self.assertEqual(1, index.lookup("b"))
369
373
 
370
374
    def test_add_version(self):
371
375
        transport = MockTransport([
373
377
            ])
374
378
        index = _KnitIndex(transport, "filename", "r")
375
379
 
376
 
        index.add_version(u"a", ["option"], 0, 1, [u"b"])
 
380
        index.add_version("a", ["option"], 0, 1, ["b"])
377
381
        self.assertEqual(("append_bytes",
378
382
            ("filename", "\na option 0 1 .b :"),
379
383
            {}), transport.calls.pop(0))
380
 
        self.assertTrue(index.has_version(u"a"))
 
384
        self.assertTrue(index.has_version("a"))
381
385
        self.assertEqual(1, index.num_versions())
382
 
        self.assertEqual((0, 1), index.get_position(u"a"))
383
 
        self.assertEqual(["option"], index.get_options(u"a"))
384
 
        self.assertEqual([u"b"], index.get_parents_with_ghosts(u"a"))
 
386
        self.assertEqual((0, 1), index.get_position("a"))
 
387
        self.assertEqual(["option"], index.get_options("a"))
 
388
        self.assertEqual(["b"], index.get_parents_with_ghosts("a"))
385
389
 
386
 
        index.add_version(u"a", ["opt"], 1, 2, [u"c"])
 
390
        index.add_version("a", ["opt"], 1, 2, ["c"])
387
391
        self.assertEqual(("append_bytes",
388
392
            ("filename", "\na opt 1 2 .c :"),
389
393
            {}), transport.calls.pop(0))
390
 
        self.assertTrue(index.has_version(u"a"))
 
394
        self.assertTrue(index.has_version("a"))
391
395
        self.assertEqual(1, index.num_versions())
392
 
        self.assertEqual((1, 2), index.get_position(u"a"))
393
 
        self.assertEqual(["opt"], index.get_options(u"a"))
394
 
        self.assertEqual([u"c"], index.get_parents_with_ghosts(u"a"))
 
396
        self.assertEqual((1, 2), index.get_position("a"))
 
397
        self.assertEqual(["opt"], index.get_options("a"))
 
398
        self.assertEqual(["c"], index.get_parents_with_ghosts("a"))
395
399
 
396
 
        index.add_version(u"b", ["option"], 2, 3, [u"a"])
 
400
        index.add_version("b", ["option"], 2, 3, ["a"])
397
401
        self.assertEqual(("append_bytes",
398
402
            ("filename", "\nb option 2 3 0 :"),
399
403
            {}), transport.calls.pop(0))
400
 
        self.assertTrue(index.has_version(u"b"))
 
404
        self.assertTrue(index.has_version("b"))
401
405
        self.assertEqual(2, index.num_versions())
402
 
        self.assertEqual((2, 3), index.get_position(u"b"))
403
 
        self.assertEqual(["option"], index.get_options(u"b"))
404
 
        self.assertEqual([u"a"], index.get_parents_with_ghosts(u"b"))
 
406
        self.assertEqual((2, 3), index.get_position("b"))
 
407
        self.assertEqual(["option"], index.get_options("b"))
 
408
        self.assertEqual(["a"], index.get_parents_with_ghosts("b"))
405
409
 
406
410
    def test_add_versions(self):
407
411
        transport = MockTransport([
410
414
        index = _KnitIndex(transport, "filename", "r")
411
415
 
412
416
        index.add_versions([
413
 
            (u"a", ["option"], 0, 1, [u"b"]),
414
 
            (u"a", ["opt"], 1, 2, [u"c"]),
415
 
            (u"b", ["option"], 2, 3, [u"a"])
 
417
            ("a", ["option"], 0, 1, ["b"]),
 
418
            ("a", ["opt"], 1, 2, ["c"]),
 
419
            ("b", ["option"], 2, 3, ["a"])
416
420
            ])
417
421
        self.assertEqual(("append_bytes", ("filename",
418
422
            "\na option 0 1 .b :"
419
423
            "\na opt 1 2 .c :"
420
424
            "\nb option 2 3 0 :"
421
425
            ), {}), transport.calls.pop(0))
422
 
        self.assertTrue(index.has_version(u"a"))
423
 
        self.assertTrue(index.has_version(u"b"))
 
426
        self.assertTrue(index.has_version("a"))
 
427
        self.assertTrue(index.has_version("b"))
424
428
        self.assertEqual(2, index.num_versions())
425
 
        self.assertEqual((1, 2), index.get_position(u"a"))
426
 
        self.assertEqual((2, 3), index.get_position(u"b"))
427
 
        self.assertEqual(["opt"], index.get_options(u"a"))
428
 
        self.assertEqual(["option"], index.get_options(u"b"))
429
 
        self.assertEqual([u"c"], index.get_parents_with_ghosts(u"a"))
430
 
        self.assertEqual([u"a"], index.get_parents_with_ghosts(u"b"))
 
429
        self.assertEqual((1, 2), index.get_position("a"))
 
430
        self.assertEqual((2, 3), index.get_position("b"))
 
431
        self.assertEqual(["opt"], index.get_options("a"))
 
432
        self.assertEqual(["option"], index.get_options("b"))
 
433
        self.assertEqual(["c"], index.get_parents_with_ghosts("a"))
 
434
        self.assertEqual(["a"], index.get_parents_with_ghosts("b"))
431
435
 
432
436
    def test_delay_create_and_add_versions(self):
433
437
        transport = MockTransport()
438
442
        self.assertEqual([], transport.calls)
439
443
 
440
444
        index.add_versions([
441
 
            (u"a", ["option"], 0, 1, [u"b"]),
442
 
            (u"a", ["opt"], 1, 2, [u"c"]),
443
 
            (u"b", ["option"], 2, 3, [u"a"])
 
445
            ("a", ["option"], 0, 1, ["b"]),
 
446
            ("a", ["opt"], 1, 2, ["c"]),
 
447
            ("b", ["option"], 2, 3, ["a"])
444
448
            ])
445
449
        name, (filename, f), kwargs = transport.calls.pop(0)
446
450
        self.assertEqual("put_file_non_atomic", name)
462
466
            ])
463
467
        index = _KnitIndex(transport, "filename", "r")
464
468
 
465
 
        self.assertTrue(index.has_version(u"a"))
466
 
        self.assertFalse(index.has_version(u"b"))
 
469
        self.assertTrue(index.has_version("a"))
 
470
        self.assertFalse(index.has_version("b"))
467
471
 
468
472
    def test_get_position(self):
469
473
        transport = MockTransport([
473
477
            ])
474
478
        index = _KnitIndex(transport, "filename", "r")
475
479
 
476
 
        self.assertEqual((0, 1), index.get_position(u"a"))
477
 
        self.assertEqual((1, 2), index.get_position(u"b"))
 
480
        self.assertEqual((0, 1), index.get_position("a"))
 
481
        self.assertEqual((1, 2), index.get_position("b"))
478
482
 
479
483
    def test_get_method(self):
480
484
        transport = MockTransport([
485
489
            ])
486
490
        index = _KnitIndex(transport, "filename", "r")
487
491
 
488
 
        self.assertEqual("fulltext", index.get_method(u"a"))
489
 
        self.assertEqual("line-delta", index.get_method(u"b"))
490
 
        self.assertRaises(errors.KnitIndexUnknownMethod, index.get_method, u"c")
 
492
        self.assertEqual("fulltext", index.get_method("a"))
 
493
        self.assertEqual("line-delta", index.get_method("b"))
 
494
        self.assertRaises(errors.KnitIndexUnknownMethod, index.get_method, "c")
491
495
 
492
496
    def test_get_options(self):
493
497
        transport = MockTransport([
497
501
            ])
498
502
        index = _KnitIndex(transport, "filename", "r")
499
503
 
500
 
        self.assertEqual(["opt1"], index.get_options(u"a"))
501
 
        self.assertEqual(["opt2", "opt3"], index.get_options(u"b"))
 
504
        self.assertEqual(["opt1"], index.get_options("a"))
 
505
        self.assertEqual(["opt2", "opt3"], index.get_options("b"))
502
506
 
503
507
    def test_get_parents(self):
504
508
        transport = MockTransport([
509
513
            ])
510
514
        index = _KnitIndex(transport, "filename", "r")
511
515
 
512
 
        self.assertEqual([], index.get_parents(u"a"))
513
 
        self.assertEqual([u"a", u"c"], index.get_parents(u"b"))
514
 
        self.assertEqual([u"b", u"a"], index.get_parents(u"c"))
 
516
        self.assertEqual([], index.get_parents("a"))
 
517
        self.assertEqual(["a", "c"], index.get_parents("b"))
 
518
        self.assertEqual(["b", "a"], index.get_parents("c"))
515
519
 
516
520
    def test_get_parents_with_ghosts(self):
517
521
        transport = MockTransport([
522
526
            ])
523
527
        index = _KnitIndex(transport, "filename", "r")
524
528
 
525
 
        self.assertEqual([], index.get_parents_with_ghosts(u"a"))
526
 
        self.assertEqual([u"a", u"c"], index.get_parents_with_ghosts(u"b"))
527
 
        self.assertEqual([u"b", u"a", u"e"],
528
 
            index.get_parents_with_ghosts(u"c"))
 
529
        self.assertEqual([], index.get_parents_with_ghosts("a"))
 
530
        self.assertEqual(["a", "c"], index.get_parents_with_ghosts("b"))
 
531
        self.assertEqual(["b", "a", "e"],
 
532
            index.get_parents_with_ghosts("c"))
529
533
 
530
534
    def test_check_versions_present(self):
531
535
        transport = MockTransport([
538
542
        check = index.check_versions_present
539
543
 
540
544
        check([])
541
 
        check([u"a"])
542
 
        check([u"b"])
543
 
        check([u"a", u"b"])
544
 
        self.assertRaises(RevisionNotPresent, check, [u"c"])
545
 
        self.assertRaises(RevisionNotPresent, check, [u"a", u"b", u"c"])
 
545
        check(["a"])
 
546
        check(["b"])
 
547
        check(["a", "b"])
 
548
        self.assertRaises(RevisionNotPresent, check, ["c"])
 
549
        self.assertRaises(RevisionNotPresent, check, ["a", "b", "c"])
546
550
 
547
551
 
548
552
class KnitTests(TestCaseWithTransport):