~lifeless/python-oops-tools/bug-881400

« back to all changes in this revision

Viewing changes to src/oopstools/oops/test/prefixloader.txt

  • Committer: Robert Collins
  • Date: 2011-10-13 20:18:51 UTC
  • Revision ID: robertc@robertcollins.net-20111013201851-ym8jmdhoeol3p83s
Export of cruft-deleted tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
= PrefixLoader =
 
2
 
 
3
The prefix loader reads a lazr.config file, looks for the oops_prefix entry and
 
4
loads that prefix into the database, if it's not there yet.
 
5
 
 
6
    >>> from django.conf import settings
 
7
    >>> from oopstools.oops.prefixloader import PrefixLoader
 
8
 
 
9
Initialize the PrefixLoader with the path to the lazr.config file or a
 
10
directory containing lazr.config files.
 
11
 
 
12
    >>> prefix_loader = PrefixLoader(settings.LAZR_CONFIG)
 
13
 
 
14
The correct config files are loaded by the PrefixLoader.
 
15
sorted() is used here so a predictable result is always returned.
 
16
 
 
17
    >>> from operator import attrgetter
 
18
    >>> for schema in sorted(prefix_loader._schemas, key=attrgetter('filename')):
 
19
    ...     print schema.filename
 
20
    /.../lazr-configs/edge-lazr.conf
 
21
    /.../lazr-configs/edge/launchpad-lazr.conf
 
22
    /.../lazr-configs/edge1/launchpad-lazr.conf
 
23
    /.../lazr-configs/lpnet-lazr.conf
 
24
    /.../lazr-configs/lpnet2/launchpad-lazr.conf
 
25
    /.../lazr-configs/production/launchpad-lazr.conf
 
26
 
 
27
And a number of prefixes were found in those config files.
 
28
 
 
29
    >>> found_prefixes = prefix_loader.iter_prefixes()
 
30
    >>> print sorted(found_prefixes)
 
31
    ['B', 'BZR', 'EA', 'EBZR', 'SMESSH', 'SMS']
 
32
 
 
33
Now let's make sure those are correctly loaded into the database, but first
 
34
let's grab all prefixes already in the database
 
35
 
 
36
    >>> from oopstools.oops.models import Prefix
 
37
    >>> db_prefixes_before = set()
 
38
    >>> for prefix in Prefix.objects.all():
 
39
    ...     db_prefixes_before.add(prefix.value)
 
40
 
 
41
And check that not all prefixes found by the prefix_loader are in the list of
 
42
db_prefixes.
 
43
 
 
44
    >>> set(found_prefixes).issubset(db_prefixes_before)
 
45
    False
 
46
 
 
47
Load the prefixes found in the config files into the database. The method
 
48
return only those that weren't previously in the database.
 
49
 
 
50
    >>> prefix_loader.load_prefixes_into_database()
 
51
    ['BZR', 'EBZR', 'SMESSH']
 
52
 
 
53
Check all prefixes found by the prefix_loader are now in the list of
 
54
db_prefixes.
 
55
 
 
56
    >>> db_prefixes_after = set()
 
57
    >>> for prefix in Prefix.objects.all():
 
58
    ...     db_prefixes_after.add(prefix.value)
 
59
 
 
60
    >>> set(found_prefixes).issubset(db_prefixes_after)
 
61
    True
 
62
 
 
63
= Bad config data =
 
64
 
 
65
If we try to initialize the PrefixLoader with a non-existent file we get a
 
66
ConfigError exception.
 
67
 
 
68
    >>> PrefixLoader('/does/not/exist.conf')
 
69
    Traceback (most recent call last):
 
70
    ...
 
71
    NoConfigError
 
72
 
 
73
Or initialize with a file that's not a real lazr.config file.
 
74
 
 
75
    >>> import os.path
 
76
    >>> PrefixLoader(os.path.join(settings.ROOT, 'Makefile'))
 
77
    Traceback (most recent call last):
 
78
    ...
 
79
    MissingSectionHeaderError: File contains no section headers.
 
80
    file: .../Makefile...
 
81
    ...
 
82