Skip to content
This repository has been archived by the owner on Oct 18, 2018. It is now read-only.

db vs api test #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions commons-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@
<version>1.10.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>2.0.2</version>
</dependency>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Дубль.

<dependency>
<groupId>ru.qatools.school</groupId>
<artifactId>api-tests-module</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ru.qatools.school.webtests;

import org.apache.http.HttpStatus;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import ru.qatools.school.ApiData.retrofits.APITestService;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Этого модуля в коммите вообще нет. В твоем репозитории ApiData и retrofits лежат отдельно, вроде бы это ru.qatools.school.retrofits.APITestService;

import ru.qatools.school.ApiData.retrofits.City;
import ru.qatools.school.DbClient;
import ru.yandex.qatools.allure.annotations.Title;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static ru.qatools.school.ApiData.ApiDataPreconditions.BASE_URL;
import static ru.yandex.qatools.matchers.collection.HasSameItemsAsListMatcher.hasSameItemsAsList;


/**
* @author arrumm 09.05.2016.
*/
public class MyDbTest {

private APITestService service;
private DbClient MyClient;

private static final String SUGGEST_STRING = "bu";

@Before
public void initService() {
MyClient = new DbClient();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(APITestService.class);
}

@Test
@Title("Should get equal lists both by query to database and by api method")
//@TestCaseId("14")
public void shouldGetCitiesWithBuApiDb() throws IOException {

List<String> cities = MyClient.getCitiesNameContains(SUGGEST_STRING);

Call<List<City>> request = service.suggest("bu");
Response<List<City>> response = request.execute();
assertThat("Request status different from expected", response.code(), is(HttpStatus.SC_OK));

List<String> names = response.body().stream().map(City::getName).collect(Collectors.toList());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не очень понятно, почему сначала мы собираем города в список cities, а потом - в список names. По-моему, и там, и там у тебя список городов. А отличаются они тем, откуда ты их получаешь: citiesDb, citiesApi


assertThat("Cities from api don't match cities from db",
cities,
hasSameItemsAsList(names));

}

@After
public void closeConnection() {
MyClient.close();
}

}
5 changes: 5 additions & 0 deletions dbclient-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Но почему? Где ты от него зависишь?

<groupId>ru.qatools.school</groupId>
<artifactId>api-tests-module</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>


Expand Down
23 changes: 18 additions & 5 deletions dbclient-module/src/main/java/ru/qatools/school/DbClient.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package ru.qatools.school;

import org.jooq.*;
import org.jooq.DSLContext;
import org.jooq.Record1;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.table;
import static ru.qatools.school.DbPreconditions.*;

/**
* Created by omaz on 27.04.16.
* edited by arrumm
*/
public class DbClient {
private static final String CONNECTION_STRING =
System.getProperty("db.url", "jdbc:mysql://db.host.ru:3310/db_name");
private static final String USER = System.getProperty("db.user", "user");
private static final String PASSWORD = System.getProperty("db.password", "password");;
System.getProperty("db.url", DB_URL);
private static final String USER = System.getProperty("db.user", DB_USER);
private static final String PASSWORD = System.getProperty("db.password", DB_PASSWORD);

private Connection connection;
private DSLContext create;
Expand All @@ -36,7 +41,15 @@ public String getCityById(Integer id) {
.from(table("table_name"))
.where(field("id").equal(id))
.fetchOne();
return result.getValue(0, String.class);
return result.getValue(0, String.class);
}

public List<String> getCitiesNameContains(String name) {
List<String> cities = create.select()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local variable 'cities' is redundant, подсказывает нам Идея.

.from(table("City"))
.where(field("name").like("%" + name + "%"))
.limit(100).fetch().getValues("name", String.class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я бы назвала метод как-то типа getUpTo100CitiesContaining с учетом вот этого лимит 100. А то это нигде не документировано и может уронить тест, например, если ты будешь запрашивать саджест для городов, которые содержат ovo. Апи тебе всяких Протасово и Домодедово отдаст кучу, а БД - только сотню

return cities;
}

public void close() {
Expand Down
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<modules>
<module>steps-module</module>
<module>commons-module</module>
<module>api-tests-module</module>
<module>dbclient-module</module>
</modules>

Expand Down Expand Up @@ -44,7 +45,17 @@
<artifactId>rest-assured</artifactId>
<version>2.9.0</version>
</dependency>

<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>2.0.2</version>
</dependency>

<dependency>
<groupId>ru.yandex.qatools.allure</groupId>
<artifactId>allure-junit-adaptor</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion steps-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>

</dependencies>

</project>