def test_adds_pronouns(self): count = Pronoun.objects.count() # update pronouns with the field `comment` set to the PK. old_pronouns = {} for p in self.pdm.pronoun_set.all(): p.comment = str(p.pk) p.save() old_pronouns[p.pk] = p newpdm = copy_paradigm(self.pdm, self.lang2) assert newpdm != self.pdm, 'new paradigm should not be the same as the old one' assert Pronoun.objects.count() == (count * 2), \ "Should have twice as many pronouns now, %r not %r" % (count*2, Pronoun.objects.count()) # loop over new pronouns and check that they match their ancestral object # in all attributes. Remember the ancestral PK is stored in `comment` for new_p in newpdm.pronoun_set.all(): pk = int(new_p.comment) assert pk in old_pronouns old_p = old_pronouns[pk] for attr in ('person', 'number', 'gender', 'alignment'): assert getattr(new_p.pronountype, attr) == getattr(old_p.pronountype, attr) # check paradigm assert new_p.paradigm == newpdm assert old_p.paradigm == self.pdm
def copy(request, paradigm_id): """Copies a Paradigm""" p = get_object_or_404(Paradigm, pk=paradigm_id) paradigm_form = ParadigmForm(request.POST or None, instance=p, prefix='pdm') copy_form = CopyForm(request.POST or None, prefix='copy') # save if valid. if copy_form.is_valid() and paradigm_form.is_valid(): # construct a temporary paradigm to validate, we do NOT # want to update the original paradigm `p` with the # changed form data, so this is just for holding and # validating the incoming form values. temp_p = paradigm_form.save(commit=False) # copy the old paradigm to the temporary paradigm language new_p = copy_paradigm(p, temp_p.language) # update details of new paradigm from temporary paradigm new_p.editor = request.user new_p.comment = temp_p.comment new_p.source = temp_p.source new_p.analect = temp_p.analect new_p.save() return redirect('pronouns:detail', new_p.id) # the initial view and the error view return render(request, 'pronouns/copy.html', { 'paradigm': p, 'paradigm_form': paradigm_form, 'copy_form': copy_form, })
def copy(request, paradigm_id): """Copies a Paradigm""" p = get_object_or_404(Paradigm, pk=paradigm_id) paradigm_form = ParadigmForm(request.POST or None, instance=p, prefix='pdm') copy_form = CopyForm(request.POST or None, prefix='copy') # save if valid. if copy_form.is_valid() and paradigm_form.is_valid(): # construct a temporary paradigm to validate, we do NOT # want to update the original paradigm `p` with the # changed form data, so this is just for holding and # validating the incoming form values. temp_p = paradigm_form.save(commit=False) # copy the old paradigm to the temporary paradigm language new_p = copy_paradigm(p, temp_p.language) # update details of new paradigm from temporary paradigm new_p.editor = request.user new_p.comment = temp_p.comment new_p.source = temp_p.source new_p.analect = temp_p.analect new_p.save() return redirect('pronouns:detail', new_p.id) # the initial view and the error view return render_to_response('pronouns/copy.html', { 'paradigm': p, 'paradigm_form': paradigm_form, 'copy_form': copy_form, }, context_instance=RequestContext(request))
def test_adds_relationships(self): pronouns = self.pdm.pronoun_set.all()[0:3] # set the pronoun comment to something we can check later... for i, p in enumerate(pronouns): p.comment = str(i) p.save() # we need to have a lexical item or two. for i in range(0, 3): lex = Lexicon.objects.create( editor=self.editor, source=self.source, language=self.lang, word=self.word, entry="same" ) pronouns[i].entries.add(lex) pronouns[i].save() # should have no relationships assert len(Relationship.objects.all()) == 0 # create some relationships for paradigm 1 rel1 = Relationship.objects.create( paradigm=self.pdm, pronoun1=pronouns[0], pronoun2=pronouns[1], relationship='TS', editor=self.editor ) rel2 = Relationship.objects.create( paradigm=self.pdm, pronoun1=pronouns[0], pronoun2=pronouns[2], relationship='FO', editor=self.editor ) # Relationships exist on paradigm 1 for 0 & 1 and 0 & 2 # Should have 2 relationships assert len(Relationship.objects.all()) == 2 p = copy_paradigm(self.pdm, self.lang2) assert p != self.pdm, 'new paradigm should not be the same as the old one' # Should have 4 relationships assert len(Relationship.objects.all()) == 4 # test length newrels = p.relationship_set.all().order_by("pronoun1") assert len(newrels) == 2 # have copied the relevant stuff? assert newrels[0].relationship == rel1.relationship == 'TS' assert newrels[1].relationship == rel2.relationship == 'FO' # check the new protoform comments to make sure they're right assert newrels[0].pronoun1.comment == '0', 'Expected 0 got %r' % newrels[0].pronoun1.comment assert newrels[0].pronoun2.comment == '1', 'Expected 1 got %r' % newrels[0].pronoun2.comment assert newrels[1].pronoun1.comment == '0', 'Expected 0 got %r' % newrels[0].pronoun1.comment assert newrels[1].pronoun2.comment == '2', 'Expected 2 got %r' % newrels[0].pronoun2.comment
def test_adds_pronoun_entries(self): def _make_token(p): return "%s-%s-%s-%s" % (p.pronountype.person, p.pronountype.number, p.pronountype.gender, p.pronountype.alignment) def _break_token(p): keys = ['person', 'number', 'gender', 'alignment'] values = [] for v in p.split("-"): if v == u'None': v = None values.append(v) return dict(zip(keys, values)) # go through old paradigm, add some lexical items to each. for pron in self.pdm.pronoun_set.all(): lex = Lexicon.objects.create( editor=self.editor, source=self.source, language=self.lang, word=self.word, entry=_make_token(pron) ) lex.save() pron.entries.add(lex) pron.save() assert pron.entries.count() == 1, "Setup failed!" # copy paradigm... newpdm = copy_paradigm(self.pdm, self.lang2) # check old paradigm for pron in self.pdm.pronoun_set.all(): assert pron.entries.count() == 1, "Should have one pronoun form" lex_obj = pron.entries.all()[0] # check language is correct! assert lex_obj.language == self.pdm.language, \ "Language should still be %s" % self.pdm.language # check other attributes values = _break_token(lex_obj.entry) for attr in values: assert values[attr] == getattr(pron.pronountype, attr) # check new paradigm for pron in newpdm.pronoun_set.all(): assert pron.entries.count() == 1, "Should have one pronoun form" lex_obj = pron.entries.all()[0] # check language is correct! assert lex_obj.language == self.lang2, \ "Language should be changed to %s" % self.lang2 # check other attributes values = _break_token(lex_obj.entry) for attr in values: assert values[attr] == getattr(pron.pronountype, attr)
def setUpTestData(cls): super(TestCopyParadigm, cls).setUpTestData() cls.lang2 = Language.objects.create(language='B', slug='langb', information='', classification='', isocode='bbb', editor=cls.editor) cls.newpdm = copy_paradigm(cls.pdm, cls.lang2)
def test_adds_rules(self): # create a rule on paradigm 1 r1 = Rule.objects.create(paradigm=self.pdm, rule="Obey!", editor=self.editor) # make sure we saved that rule assert len(Rule.objects.filter(paradigm=self.pdm)) == 1, \ "Should only have one rule" # now to test - newpdm = copy_paradigm(self.pdm, self.lang2) assert newpdm != self.pdm, 'new paradigm should not be the same as the old one' # should now have a rule for paradigm 2 r2 = Rule.objects.filter(paradigm=newpdm) assert len(r2) == 1, "Should have one rule for new paradigm" assert r2[0].rule == "Obey!", "Rule value has not been set" # do we still have our original rule? assert len(Rule.objects.filter(paradigm=self.pdm)) == 1 assert Rule.objects.filter(paradigm=self.pdm)[0] == r1
def test_adds_paradigm_doesnt_affect_previous(self): newpdm = copy_paradigm(self.pdm, self.lang2) assert self.pdm.language == self.lang assert self.pdm.pk is not None assert Paradigm.objects.get(pk=self.pdm.pk) == self.pdm
def test_copies_paradigm_analect(self): self.pdm.analect = 'F' self.pdm.save() newpdm = copy_paradigm(self.pdm, self.lang2) assert newpdm != self.pdm assert newpdm.analect == self.pdm.analect
def test_copies_paradigm_label(self): self.pdm.label = 'label' self.pdm.save() newpdm = copy_paradigm(self.pdm, self.lang2) assert newpdm != self.pdm assert newpdm.label == self.pdm.label
def test_copies_paradigm_comment(self): self.pdm.comment = 'banana' self.pdm.save() newpdm = copy_paradigm(self.pdm, self.lang2) assert newpdm != self.pdm assert newpdm.comment == self.pdm.comment
def test_copies_paradigm_source(self): newpdm = copy_paradigm(self.pdm, self.lang2) assert newpdm != self.pdm assert newpdm.source == self.pdm.source
def test_copies_paradigm_language(self): newpdm = copy_paradigm(self.pdm, self.lang2) assert newpdm != self.pdm assert newpdm.language == self.lang2
def handle(self, *args, **options): p = Paradigm.objects.get(pk=args[0]) l = Language.objects.get(pk=args[1]) copy_paradigm(p, l)