Skip to content

Commit

Permalink
Fixing flaky tests (#3059)
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-owl authored Mar 3, 2021
1 parent ddeeb12 commit 70fc159
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 10 additions & 10 deletions gensim/test/test_fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_fast_text_train_parameters(self):
self.assertRaises(TypeError, model.train, corpus_file=sentences, total_examples=1, epochs=1)

def test_training_fromfile(self):
with temporary_file(get_tmpfile('gensim_fasttext.tst')) as corpus_file:
with temporary_file('gensim_fasttext.tst') as corpus_file:
utils.save_as_line_sentence(sentences, corpus_file)

model = FT_gensim(vector_size=12, min_count=1, hs=1, negative=0, seed=42, workers=1, bucket=BUCKET)
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_persistence(self):
self.assertEqual(len(wv), len(loaded_wv))

def test_persistence_fromfile(self):
with temporary_file(get_tmpfile('gensim_fasttext1.tst')) as corpus_file:
with temporary_file('gensim_fasttext1.tst') as corpus_file:
utils.save_as_line_sentence(sentences, corpus_file)

tmpf = get_tmpfile('gensim_fasttext.tst')
Expand Down Expand Up @@ -430,7 +430,7 @@ def test_cbow_hs_training(self):
"only %i overlap in expected %s & actual %s" % (overlap_count, expected_sims_words, sims_gensim_words))

def test_cbow_hs_training_fromfile(self):
with temporary_file(get_tmpfile('gensim_fasttext.tst')) as corpus_file:
with temporary_file('gensim_fasttext.tst') as corpus_file:
model_gensim = FT_gensim(
vector_size=48, sg=0, cbow_mean=1, alpha=0.05, window=5, hs=1, negative=0,
min_count=5, epochs=10, batch_words=1000, word_ngrams=1, sample=1e-3, min_n=3, max_n=6,
Expand Down Expand Up @@ -498,7 +498,7 @@ def test_sg_hs_training(self):
"only %i overlap in expected %s & actual %s" % (overlap_count, expected_sims_words, sims_gensim_words))

def test_sg_hs_training_fromfile(self):
with temporary_file(get_tmpfile('gensim_fasttext.tst')) as corpus_file:
with temporary_file('gensim_fasttext.tst') as corpus_file:
model_gensim = FT_gensim(
vector_size=48, sg=1, cbow_mean=1, alpha=0.025, window=5, hs=1, negative=0,
min_count=5, epochs=10, batch_words=1000, word_ngrams=1, sample=1e-3, min_n=3, max_n=6,
Expand Down Expand Up @@ -566,7 +566,7 @@ def test_cbow_neg_training(self):
"only %i overlap in expected %s & actual %s" % (overlap_count, expected_sims_words, sims_gensim_words))

def test_cbow_neg_training_fromfile(self):
with temporary_file(get_tmpfile('gensim_fasttext.tst')) as corpus_file:
with temporary_file('gensim_fasttext.tst') as corpus_file:
model_gensim = FT_gensim(
vector_size=48, sg=0, cbow_mean=1, alpha=0.05, window=5, hs=0, negative=5,
min_count=5, epochs=10, batch_words=1000, word_ngrams=1, sample=1e-3, min_n=3, max_n=6,
Expand Down Expand Up @@ -634,7 +634,7 @@ def test_sg_neg_training(self):
"only %i overlap in expected %s & actual %s" % (overlap_count, expected_sims_words, sims_gensim_words))

def test_sg_neg_training_fromfile(self):
with temporary_file(get_tmpfile('gensim_fasttext.tst')) as corpus_file:
with temporary_file('gensim_fasttext.tst') as corpus_file:
model_gensim = FT_gensim(
vector_size=48, sg=1, cbow_mean=1, alpha=0.025, window=5, hs=0, negative=5,
min_count=5, epochs=10, batch_words=1000, word_ngrams=1, sample=1e-3, min_n=3, max_n=6,
Expand Down Expand Up @@ -679,8 +679,8 @@ def test_online_learning(self):
self.assertEqual(model_hs.wv.get_vecattr('artificial', 'count'), 4)

def test_online_learning_fromfile(self):
with temporary_file(get_tmpfile('gensim_fasttext1.tst')) as corpus_file, \
temporary_file(get_tmpfile('gensim_fasttext2.tst')) as new_corpus_file:
with temporary_file('gensim_fasttext1.tst') as corpus_file, \
temporary_file('gensim_fasttext2.tst') as new_corpus_file:
utils.save_as_line_sentence(sentences, corpus_file)
utils.save_as_line_sentence(new_sentences, new_corpus_file)

Expand Down Expand Up @@ -720,8 +720,8 @@ def test_online_learning_through_ft_format_saves(self):
gensim.models.fasttext.save_facebook_model(model_reload, tmpf2)

def test_online_learning_after_save_fromfile(self):
with temporary_file(get_tmpfile('gensim_fasttext1.tst')) as corpus_file, \
temporary_file(get_tmpfile('gensim_fasttext2.tst')) as new_corpus_file:
with temporary_file('gensim_fasttext1.tst') as corpus_file, \
temporary_file('gensim_fasttext2.tst') as new_corpus_file:
utils.save_as_line_sentence(sentences, corpus_file)
utils.save_as_line_sentence(new_sentences, new_corpus_file)

Expand Down
2 changes: 1 addition & 1 deletion gensim/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def get_tmpfile(suffix):
>>> loaded_model = LsiModel.load(tmp_f)
"""
return os.path.join(tempfile.gettempdir(), suffix)
return os.path.join(tempfile.mkdtemp(), suffix)


@contextlib.contextmanager
Expand Down

0 comments on commit 70fc159

Please sign in to comment.