-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP Implement SfTest and sinceVersion #1583
Draft
sfc-gh-pfus
wants to merge
1
commit into
master
Choose a base branch
from
sftest
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+215
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package net.snowflake.client; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD, ElementType.TYPE}) | ||
public @interface SfTest { | ||
String sinceVersion(); | ||
} |
112 changes: 112 additions & 0 deletions
112
src/test/java/net/snowflake/client/TestSinceConditionRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package net.snowflake.client; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import net.snowflake.client.jdbc.SnowflakeDriver; | ||
import org.junit.Assume; | ||
import org.junit.rules.MethodRule; | ||
import org.junit.runners.model.FrameworkMethod; | ||
import org.junit.runners.model.Statement; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public class TestSinceConditionRule implements MethodRule { | ||
private final Version currentVersion; | ||
|
||
public TestSinceConditionRule() { | ||
currentVersion = Version.parse(SnowflakeDriver.implementVersion); | ||
} | ||
|
||
public TestSinceConditionRule(Version currentVersion) { | ||
this.currentVersion = currentVersion; | ||
} | ||
|
||
@Override | ||
public Statement apply(Statement statement, FrameworkMethod frameworkMethod, Object o) { | ||
SfTest sfTest = Optional.ofNullable(frameworkMethod.getAnnotation(SfTest.class)) | ||
.orElseGet(() -> frameworkMethod.getDeclaringClass().getAnnotation(SfTest.class)); | ||
if (sfTest == null) { | ||
return statement; | ||
} | ||
Version testSinceVersion = Version.parse(sfTest.sinceVersion()); | ||
// when we introduce a new feature we have to separate two cases | ||
// running during old driver test - feature is available since version X | ||
// running during development - feature is available since version X-1 | ||
return decideIfShouldBeRun(statement, new IgnoreStatement(sfTest), testSinceVersion, isOldDriverTest()); | ||
} | ||
|
||
@VisibleForTesting | ||
Statement decideIfShouldBeRun(Statement normalRun, Statement ignoreRun, Version testSinceVersion, boolean oldDriverTest) { | ||
if (oldDriverTest && currentVersion.compareTo(testSinceVersion) > 0) { | ||
return normalRun; | ||
} | ||
if (!oldDriverTest && currentVersion.compareTo(testSinceVersion) >= 0) { | ||
return normalRun; | ||
} | ||
return ignoreRun; | ||
} | ||
|
||
private boolean isOldDriverTest() { | ||
return Boolean.TRUE.toString().equals(System.getenv("is_old_driver")); | ||
} | ||
|
||
private class IgnoreStatement extends Statement { | ||
private SfTest sfTest; | ||
|
||
private IgnoreStatement(SfTest sfTest) { | ||
this.sfTest = sfTest; | ||
} | ||
|
||
@Override | ||
public void evaluate() { | ||
Assume.assumeTrue( | ||
"Test ignored, as available since " | ||
+ sfTest.sinceVersion() | ||
+ ", current version " | ||
+ currentVersion, | ||
false); | ||
} | ||
} | ||
|
||
static class Version implements Comparable<Version> { | ||
private final int major; | ||
private final int minor; | ||
private final int patch; | ||
|
||
Version(int major, int minor, int patch) { | ||
this.major = major; | ||
this.minor = minor; | ||
this.patch = patch; | ||
} | ||
|
||
static Version parse(String str) { | ||
if (str == null) { | ||
return null; | ||
} | ||
List<Integer> split = | ||
Arrays.stream(str.split("\\.")).map(Integer::valueOf).collect(Collectors.toList()); | ||
if (split.size() != 3) { | ||
throw new ExceptionInInitializerError(str + " should be in a form of x.y.z"); | ||
} | ||
return new Version(split.get(0), split.get(1), split.get(2)); | ||
} | ||
|
||
@Override | ||
public int compareTo(Version o) { | ||
if (major != o.major) { | ||
return major - o.major; | ||
} else if (minor != o.minor) { | ||
return minor - o.minor; | ||
} else { | ||
return patch - o.patch; | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%d.%d.%d", major, minor, patch); | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/test/java/net/snowflake/client/TestSinceConditionRuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package net.snowflake.client; | ||
|
||
import junit.framework.TestCase; | ||
import org.junit.AfterClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.MethodRule; | ||
import org.junit.rules.TestWatcher; | ||
import org.junit.runner.Description; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.model.FrameworkMethod; | ||
import org.junit.runners.model.Statement; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.ArgumentMatchers.contains; | ||
import static org.mockito.Mockito.mock; | ||
|
||
@RunWith(Parameterized.class) | ||
@SfTest(sinceVersion = "5.6.7") | ||
public class TestSinceConditionRuleTest { | ||
private static final Statement normalRun = mock(Statement.class); | ||
private static final Statement ignoreRun = mock(Statement.class); | ||
|
||
@Rule | ||
public final TestSinceConditionRule testSinceConditionRule = new TestSinceConditionRule(TestSinceConditionRule.Version.parse("2.3.4")); | ||
|
||
@Rule | ||
public final TestWatcher testWatcher = new TestWatcher() { | ||
@Override | ||
protected void succeeded(Description description) { | ||
succeededTests.add(description.getMethodName()); | ||
} | ||
}; | ||
|
||
private static List<String> succeededTests = new ArrayList<>(); | ||
|
||
@AfterClass | ||
public static void afterAll() { | ||
assertThat(succeededTests, hasItem("testDecidingIfTestShouldBeRun[0]")); | ||
} | ||
|
||
@Parameterized.Parameters | ||
public static Object[][] parameters() { | ||
return new Object[][]{ | ||
{TestSinceConditionRule.Version.parse("1.2.3"), true, normalRun}, | ||
{TestSinceConditionRule.Version.parse("2.3.3"), true, normalRun}, | ||
{TestSinceConditionRule.Version.parse("2.3.3"), true, normalRun}, | ||
{TestSinceConditionRule.Version.parse("2.3.4"), false, normalRun}, | ||
{TestSinceConditionRule.Version.parse("2.3.4"), true, ignoreRun}, | ||
{TestSinceConditionRule.Version.parse("2.3.5"), true, ignoreRun}, | ||
{TestSinceConditionRule.Version.parse("2.3.5"), false, ignoreRun}, | ||
{TestSinceConditionRule.Version.parse("3.0.0"), true, ignoreRun}, | ||
{TestSinceConditionRule.Version.parse("3.0.0"), false, ignoreRun}, | ||
}; | ||
} | ||
|
||
private final TestSinceConditionRule.Version version; | ||
private final boolean oldDriverTest; | ||
private final Statement result; | ||
|
||
public TestSinceConditionRuleTest(TestSinceConditionRule.Version version, boolean oldDriverTest, Statement result) { | ||
this.version = version; | ||
this.oldDriverTest = oldDriverTest; | ||
this.result = result; | ||
} | ||
|
||
@Test | ||
@SfTest(sinceVersion = "1.0.0") | ||
public void testDecidingIfTestShouldBeRun() { | ||
assertEquals(result, testSinceConditionRule.decideIfShouldBeRun(normalRun, ignoreRun, version, oldDriverTest)); | ||
} | ||
|
||
@SfTest(sinceVersion = "1000.1000.1000") | ||
@Test | ||
public void testShouldNeverStart() { | ||
throw new AssertionError("should never start this test"); | ||
} | ||
|
||
@Test | ||
public void testShouldNeverStartBecauseClassVersionIsTooHigh() { | ||
throw new AssertionError("should never start this test"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
annotation could be put on class, not only method - is this class aware of it?
are we able to handle different version on both levels? e.g. class declares availability since 3.14.5 whereas method declares availability for version 3.14.6
another question is that during implemntation we are not sure which version will be released so the given version should be last version that does not have the tested feature