Skip to content

Commit

Permalink
#424 handle merging lists, favorites, and annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
loolmeh committed Oct 10, 2014
1 parent 301b8bf commit 0461d8a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
17 changes: 16 additions & 1 deletion docs/tatoeba2-django/tatoeba2/management/commands/deduplicate.py
@@ -1,5 +1,5 @@
from django.core.management.base import BaseCommand
from tatoeba2.models import Sentences, SentenceComments, SentencesTranslations, Contributions, Users, TagsSentences
from tatoeba2.models import Sentences, SentenceComments, SentencesTranslations, Contributions, Users, TagsSentences, SentencesSentencesLists, FavoritesUsers, SentenceAnnotations
from collections import defaultdict
from datetime import datetime
from itertools import chain
Expand Down Expand Up @@ -72,6 +72,21 @@ def merge_comments(main_id, ids):
def merge_tags(main_id, ids):
TagsSentences.objects.filter(sentence_id__in=ids).update(sentence_id=main_id)

@staticmethod
@transaction.atomic
def merge_lists(main_id, ids):
SentencesSentencesLists.objects.filter(sentence_id__in=ids).update(sentence_id=main_id)

@staticmethod
@transaction.atomic
def merge_favorites(main_id, ids):
FavoritesUsers.objects.filter(favorite_id__in=ids).update(favorite_id=main_id)

@staticmethod
@transaction.atomic
def merge_annotations(main_id, ids):
SentenceAnnotations.objects.filter(sentence_id__in=ids).update(sentence_id=main_id)

@classmethod
def log_link(cls, sent_id, tran_id, action):
return Contributions(
Expand Down
14 changes: 13 additions & 1 deletion docs/tatoeba2-django/tatoeba2/tests/conftest.py
@@ -1,4 +1,4 @@
from tatoeba2.models import Sentences, SentenceComments, SentencesTranslations, Users, TagsSentences
from tatoeba2.models import Sentences, SentenceComments, SentencesTranslations, Users, TagsSentences, SentencesSentencesLists, FavoritesUsers, SentenceAnnotations
from datetime import datetime
import pytest

Expand Down Expand Up @@ -42,6 +42,18 @@ def sents(db):
TagsSentences(tag_id=1, sentence_id=6, user_id=1, added_time=datetime.now()).save()
TagsSentences(tag_id=2, sentence_id=7, user_id=1, added_time=datetime.now()).save()
TagsSentences(tag_id=3, sentence_id=8, user_id=1, added_time=datetime.now()).save()

SentencesSentencesLists(sentences_list_id=1, sentence_id=6).save()
SentencesSentencesLists(sentences_list_id=2, sentence_id=7).save()
SentencesSentencesLists(sentences_list_id=3, sentence_id=8).save()

FavoritesUsers(user_id=1, favorite_id=6).save()
FavoritesUsers(user_id=2, favorite_id=7).save()
FavoritesUsers(user_id=3, favorite_id=8).save()

SentenceAnnotations(meaning_id=1, text='', modified=datetime.now(), user_id=1, sentence_id=6).save()
SentenceAnnotations(meaning_id=2, text='', modified=datetime.now(), user_id=1, sentence_id=7).save()
SentenceAnnotations(meaning_id=3, text='', modified=datetime.now(), user_id=1, sentence_id=8).save()

@pytest.fixture()
def bot(db):
Expand Down
20 changes: 19 additions & 1 deletion docs/tatoeba2-django/tatoeba2/tests/test_deduplication.py
@@ -1,5 +1,5 @@
from tatoeba2.management.commands.deduplicate import Dedup, Command
from tatoeba2.models import Sentences, SentenceComments, SentencesTranslations, Contributions, TagsSentences
from tatoeba2.models import Sentences, SentenceComments, SentencesTranslations, Contributions, TagsSentences, SentencesSentencesLists, FavoritesUsers, SentenceAnnotations
from django.db import transaction
import pytest

Expand Down Expand Up @@ -79,6 +79,24 @@ def test_merge_tags(db, sents):
assert TagsSentences.objects.filter(sentence_id=8).count() == 3
for tag in TagsSentences.objects.all(): assert tag.sentence_id == 8

def test_merge_lists(db, sents):
assert SentencesSentencesLists.objects.filter(sentence_id=8).count() == 1
Dedup.merge_lists(8, [6, 7])
assert SentencesSentencesLists.objects.filter(sentence_id=8).count() == 3
for sent_lst in SentencesSentencesLists.objects.all(): assert sent_lst.sentence_id == 8

def test_merge_favorites(db, sents):
assert FavoritesUsers.objects.filter(favorite_id=8).count() == 1
Dedup.merge_favorites(8, [6, 7])
assert FavoritesUsers.objects.filter(favorite_id=8).count() == 3
for fav in FavoritesUsers.objects.all(): assert fav.favorite_id == 8

def test_merge_annotations(db, sents):
assert SentenceAnnotations.objects.filter(sentence_id=8).count() == 1
Dedup.merge_annotations(8, [6, 7])
assert SentenceAnnotations.objects.filter(sentence_id=8).count() == 3
for ann in SentenceAnnotations.objects.all(): assert ann.sentence_id == 8

def test_merge_links(db, sents, bot):
Dedup.bot = bot
assert SentencesTranslations.objects.filter(sentence_id=8).count() == 0
Expand Down

0 comments on commit 0461d8a

Please sign in to comment.