<commit_before>/*
 * Copyright 1998-2012 Linux.org.ru
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package ru.org.linux.user;

import com.google.common.collect.ImmutableList;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("integration-tests-context.xml")
public class UserTagDaoIntegrationTest {
  private static final String QUERY_COUNT_FAVORITE_BY_USER = "SELECT count(user_id) FROM user_tags WHERE is_favorite=true AND user_id=?";
  private static final String QUERY_COUNT_IGNORE_BY_USER = "SELECT count(user_id) FROM user_tags WHERE is_favorite=false AND user_id=?";
  @Autowired
  UserTagDao userTagDao;

  private JdbcTemplate jdbcTemplate;

  private int user1Id;
  private int user2Id;

  private int tag1Id;
  private int tag2Id;
  private int tag3Id;
  private int tag4Id;
  private int tag5Id;

  @Autowired
  public void setDataSource(DataSource ds) {
    jdbcTemplate = new JdbcTemplate(ds);
  }

  private int createUser(String userName) {
    int userid = jdbcTemplate.queryForInt("SELECT nextval('s_uid') AS userid");

    jdbcTemplate.update(
      "INSERT INTO users (id, name, nick) VALUES (?, ?, ?)",
      userid,
      userName,
      userName
    );
    return userid;
  }

  private int createTag(String tagName) {

    jdbcTemplate.update(
      "INSERT INTO tags_values (value) VALUES (?)",
      tagName
    );
    return jdbcTemplate.queryForInt("SELECT currval('tags_values_id_seq') AS tagid");
  }

  @Before
  public void prepareTestData() {
    cleanupTestData();

    user1Id = createUser("UserTagDaoIntegrationTest_user1");
    user2Id = createUser("UserTagDaoIntegrationTest_user2");

    tag1Id = createTag("UserTagDaoIntegrationTest_tag1");
    tag2Id = createTag("UserTagDaoIntegrationTest_tag2");
    tag3Id = createTag("UserTagDaoIntegrationTest_tag3");
    tag4Id = createTag("UserTagDaoIntegrationTest_tag4");
    tag5Id = createTag("UserTagDaoIntegrationTest_tag5");
  }

  @After
  public void cleanupTestData() {
    jdbcTemplate.update(
      "DELETE FROM user_TAGS WHERE user_id in (" + user1Id + ", " + user2Id + ')'
    );

    jdbcTemplate.update(
      "DELETE FROM users WHERE nick LIKE 'UserTagDaoIntegrationTest_user%'"
    );

    jdbcTemplate.update(
      "DELETE FROM tags_values WHERE value LIKE 'UserTagDaoIntegrationTest_tag%'"
    );

  }

  private void prepareUserTags() {
    userTagDao.addTag(user1Id, tag1Id, true);
    userTagDao.addTag(user2Id, tag1Id, true);
    userTagDao.addTag(user1Id, tag2Id, true);
    userTagDao.addTag(user1Id, tag2Id, false);
    userTagDao.addTag(user2Id, tag2Id, true);
    userTagDao.addTag(user2Id, tag3Id, true);
    userTagDao.addTag(user1Id, tag3Id, true);
    userTagDao.addTag(user2Id, tag4Id, true);
    userTagDao.addTag(user1Id, tag4Id, true);
    userTagDao.addTag(user1Id, tag5Id, false);
    userTagDao.addTag(user2Id, tag5Id, true);
    userTagDao.addTag(user1Id, tag5Id, true);
  }

  @Test
  public void addTest() {
    prepareUserTags();

    try {
      userTagDao.addTag(user1Id, tag1Id, true);
      Assert.fail("Duplicate key check fail!");
    } catch (DuplicateKeyException ignored) {
    }
    userTagDao.addTag(user1Id, tag1Id, false);
    try {
      userTagDao.addTag(user1Id, tag1Id, false);
      Assert.fail("Duplicate key check fail!");
    } catch (DuplicateKeyException ignored) {
    }
<commit_msg>поправил работу UserTagDaoIntegrationTest<commit_after>/*
 * Copyright 1998-2012 Linux.org.ru
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package ru.org.linux.user;

import com.google.common.collect.ImmutableList;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("integration-tests-context.xml")
public class UserTagDaoIntegrationTest {
  private static final String QUERY_COUNT_FAVORITE_BY_USER = "SELECT count(user_id) FROM user_tags WHERE is_favorite=true AND user_id=?";
  private static final String QUERY_COUNT_IGNORE_BY_USER = "SELECT count(user_id) FROM user_tags WHERE is_favorite=false AND user_id=?";
  @Autowired
  UserTagDao userTagDao;

  private JdbcTemplate jdbcTemplate;

  private int user1Id;
  private int user2Id;

  private int tag1Id;
  private int tag2Id;
  private int tag3Id;
  private int tag4Id;
  private int tag5Id;

  @Autowired
  public void setDataSource(DataSource ds) {
    jdbcTemplate = new JdbcTemplate(ds);
  }

  private int createUser(String userName) {
    int userid = jdbcTemplate.queryForInt("SELECT nextval('s_uid') AS userid");

    jdbcTemplate.update(
      "INSERT INTO users (id, name, nick) VALUES (?, ?, ?)",
      userid,
      userName,
      userName
    );
    return userid;
  }

  private int createTag(String tagName) {

    jdbcTemplate.update(
      "INSERT INTO tags_values (value) VALUES (?)",
      tagName
    );
    return jdbcTemplate.queryForInt("SELECT currval('tags_values_id_seq') AS tagid");
  }

  @Before
  public void prepareTestData() {
    cleanupTestData();

    user1Id = createUser("UserTagDaoIntegrationTest_user1");
    user2Id = createUser("UserTagDaoIntegrationTest_user2");

    tag1Id = createTag("UserTagDaoIntegrationTest_tag1");
    tag2Id = createTag("UserTagDaoIntegrationTest_tag2");
    tag3Id = createTag("UserTagDaoIntegrationTest_tag3");
    tag4Id = createTag("UserTagDaoIntegrationTest_tag4");
    tag5Id = createTag("UserTagDaoIntegrationTest_tag5");
  }

  @After
  public void cleanupTestData() {
    jdbcTemplate.update(
      "DELETE FROM user_TAGS WHERE user_id in (" + user1Id + ", " + user2Id + ')'
    );

    jdbcTemplate.update(
      "DELETE FROM users WHERE nick LIKE 'UserTagDaoIntegrationTest_user%'"
    );

    jdbcTemplate.update(
      "DELETE FROM tags_values WHERE value LIKE 'UserTagDaoIntegrationTest_tag%'"
    );

  }

  private void prepareUserTags() {
    userTagDao.addTag(user1Id, tag1Id, true);
    userTagDao.addTag(user2Id, tag1Id, true);
    userTagDao.addTag(user1Id, tag2Id, true);
    userTagDao.addTag(user1Id, tag2Id, false);
    userTagDao.addTag(user2Id, tag2Id, true);
    userTagDao.addTag(user2Id, tag3Id, true);
    userTagDao.addTag(user1Id, tag3Id, true);
    userTagDao.addTag(user2Id, tag4Id, true);
    userTagDao.addTag(user1Id, tag4Id, true);
    userTagDao.addTag(user1Id, tag5Id, false);
    userTagDao.addTag(user2Id, tag5Id, true);
    userTagDao.addTag(user1Id, tag5Id, true);
  }

  @Test
  public void addTest() {
    prepareUserTags();

    userTagDao.addTag(user1Id, tag1Id, false);

{'fraction_non_alphanumeric': 0.09773287912604252, 'fraction_numerical': 0.015505697169035593, 'mean_word_length': 4.207339449541284, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1791251', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2738, 'n_words': 648}