From 94a99ea7213f08a89a749bdb8c0b41afcffd8822 Mon Sep 17 00:00:00 2001 From: DG266 Date: Mon, 28 Aug 2023 23:11:41 +0200 Subject: [PATCH 1/4] Add tests for getLocalServersList in DDScraper --- src/DiscOrDance-Tests/DDScraperTest.class.st | 91 +++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) diff --git a/src/DiscOrDance-Tests/DDScraperTest.class.st b/src/DiscOrDance-Tests/DDScraperTest.class.st index 2f70c9c..0a1e4d9 100644 --- a/src/DiscOrDance-Tests/DDScraperTest.class.st +++ b/src/DiscOrDance-Tests/DDScraperTest.class.st @@ -2,7 +2,8 @@ Class { #name : #DDScraperTest, #superclass : #DDTestCase, #instVars : [ - 'scraper' + 'scraper', + 'workingDir' ], #category : #'DiscOrDance-Tests-Scraper' } @@ -10,22 +11,108 @@ Class { { #category : #running } DDScraperTest >> setUp [ "Hooks that subclasses may override to define the fixture of test." + | modelsDir tempDir serverFiles | super setUp. scraper := DDScraper new. + workingDir := FileSystem disk workingDirectory. + modelsDir := workingDir / 'models'. + + "Backup files in models folder (if present)" + modelsDir exists + ifTrue: [ + tempDir := workingDir / 'temp'. + tempDir exists ifTrue: [ ] ifFalse: [ FileSystem disk createDirectory: tempDir] . + serverFiles := modelsDir children. + serverFiles do: [ :each | + FileSystem disk copy: each to: (tempDir / (each basename)). + FileSystem disk delete: (modelsDir / (each basename)). + ]. + FileSystem disk delete: modelsDir. + ] + ifFalse: [ + "Do nothing" + ]. ] +{ #category : #running } +DDScraperTest >> tearDown [ + | modelsDir tempDir serverFiles testFiles | + workingDir := FileSystem disk workingDirectory. + modelsDir := workingDir / 'models'. + tempDir := workingDir / 'temp'. + + "Reset models folder" + modelsDir exists + ifTrue: [ + testFiles := modelsDir children. + testFiles do: [ :each | + FileSystem disk delete: (modelsDir / (each basename)). + ]. + FileSystem disk delete: modelsDir. + ] + ifFalse: [ + ]. + + "Restore original files (if present)" + tempDir exists + ifTrue: [ + FileSystem disk createDirectory: modelsDir. + serverFiles := tempDir children. + serverFiles do: [ :each | + FileSystem disk copy: each to: (modelsDir / (each basename)). + FileSystem disk delete: (tempDir / (each basename)). + ]. + FileSystem disk delete: tempDir. + ] + ifFalse: [ + ]. + + super tearDown. +] + { #category : #tests } DDScraperTest >> testGetLocalServersList [ + "Test with non-existing models folder" | list | list := scraper getLocalServersList. + self assert: list size equals: 0. + + + "Original test by wolfenmark" + "| list | + list := scraper getLocalServersList. (list includes: 'Wolfenmark bot development server') ifFalse: [ scraper createPartialModel: 'Wolfenmark bot development server'. list := scraper getLocalServersList. ]. self assert: list isNotEmpty. - self assert: (list includes: 'Wolfenmark bot development server') + self assert: (list includes: 'Wolfenmark bot development server')" +] + +{ #category : #tests } +DDScraperTest >> testGetLocalServersList2 [ + "Test with no files in the models folder" + | list | + FileSystem disk createDirectory: (workingDir / 'models'). + list := scraper getLocalServersList. + self assert: list size equals: 0. + +] + +{ #category : #tests } +DDScraperTest >> testGetLocalServersList3 [ + "Test with one file in the models folder" + | list tempFilePath | + FileSystem disk createDirectory: (workingDir / 'models'). + "tempFilePath := (workingDir / 'models' / 'my-test-server_clean_stats_v1_2.fl')." + tempFilePath := (workingDir / 'models' / 'my-test-server_clean_stats_v1.fl'). + tempFilePath writeStreamDo: [ :stream | ]. + list := scraper getLocalServersList. + self assert: list size equals: 1. + "self assert: (list at: 1) equals: ('my-test-server_clean_stats_v1_')." + self assert: (list at: 1) equals: ('my-test-server_clean_stats'). ] { #category : #tests } From 047269b74008658554c774fd95f1f2156709405e Mon Sep 17 00:00:00 2001 From: DG266 Date: Thu, 31 Aug 2023 16:50:52 +0200 Subject: [PATCH 2/4] Add tests for DDGlobalConfiguration --- .../DDGlobalConfigurationTest.class.st | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/DiscOrDance-Tests/DDGlobalConfigurationTest.class.st diff --git a/src/DiscOrDance-Tests/DDGlobalConfigurationTest.class.st b/src/DiscOrDance-Tests/DDGlobalConfigurationTest.class.st new file mode 100644 index 0000000..0023b18 --- /dev/null +++ b/src/DiscOrDance-Tests/DDGlobalConfigurationTest.class.st @@ -0,0 +1,24 @@ +" +A DDGlobalConfigurationTest is a test class for testing the behavior of DDGlobalConfiguration +" +Class { + #name : #DDGlobalConfigurationTest, + #superclass : #TestCase, + #category : #'DiscOrDance-Tests-Base' +} + +{ #category : #test } +DDGlobalConfigurationTest >> testLimitMessages [ + | aConfiguration | + aConfiguration := DDGlobalConfiguration new. + aConfiguration limitMessages: true. + self assert: aConfiguration limitMessages equals: true. +] + +{ #category : #test } +DDGlobalConfigurationTest >> testMessagesPerChannel [ + | aConfiguration | + aConfiguration := DDGlobalConfiguration new. + aConfiguration messagesPerChannel: 250. + self assert: aConfiguration messagesPerChannel equals: 250. +] From a804b6c5d6a0a2a482f92a3dea50ffe680e9643f Mon Sep 17 00:00:00 2001 From: DG266 Date: Thu, 31 Aug 2023 16:59:57 +0200 Subject: [PATCH 3/4] Add tests for bot in DDScraper --- src/DiscOrDance-Tests/DDScraperTest.class.st | 35 +++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/DiscOrDance-Tests/DDScraperTest.class.st b/src/DiscOrDance-Tests/DDScraperTest.class.st index 0a1e4d9..aac647c 100644 --- a/src/DiscOrDance-Tests/DDScraperTest.class.st +++ b/src/DiscOrDance-Tests/DDScraperTest.class.st @@ -3,7 +3,9 @@ Class { #superclass : #DDTestCase, #instVars : [ 'scraper', - 'workingDir' + 'workingDir', + 'isTokenPresent', + 'tokenValue' ], #category : #'DiscOrDance-Tests-Scraper' } @@ -15,6 +17,8 @@ DDScraperTest >> setUp [ super setUp. scraper := DDScraper new. workingDir := FileSystem disk workingDirectory. + isTokenPresent := false. + tokenValue := nil. modelsDir := workingDir / 'models'. "Backup files in models folder (if present)" @@ -32,6 +36,13 @@ DDScraperTest >> setUp [ ifFalse: [ "Do nothing" ]. + + "Backup Discord token" + Smalltalk os environment at: 'DISCORD_API_TOKEN' ifPresent: [ :value | + isTokenPresent := true. + tokenValue := value. + Smalltalk os environment removeKey: 'DISCORD_API_TOKEN'. + ]. ] @@ -68,9 +79,31 @@ DDScraperTest >> tearDown [ ifFalse: [ ]. + "Restore Discord token" + Smalltalk os environment at: 'DISCORD_API_TOKEN' ifPresent: [ :value | + Smalltalk os environment removeKey: 'DISCORD_API_TOKEN'. + ]. + isTokenPresent + ifTrue: [ + Smalltalk os environment at: 'DISCORD_API_TOKEN' put: tokenValue. + ]. + super tearDown. ] +{ #category : #test } +DDScraperTest >> testBot [ + "Test with no Discord API token" + self should: [ scraper bot ] raise: Error. +] + +{ #category : #tests } +DDScraperTest >> testBot2 [ + "Test with Discord API token" + Smalltalk os environment at: 'DISCORD_API_TOKEN' put: 'test-token'. + self assert: scraper bot token equals: 'test-token'. +] + { #category : #tests } DDScraperTest >> testGetLocalServersList [ "Test with non-existing models folder" From 0541b7fee525bc81531237e0ce8b8b694794273a Mon Sep 17 00:00:00 2001 From: DG266 Date: Fri, 8 Sep 2023 02:12:51 +0200 Subject: [PATCH 4/4] Add test for globalConfiguration in DDScraper --- src/DiscOrDance-Tests/DDScraperTest.class.st | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/DiscOrDance-Tests/DDScraperTest.class.st b/src/DiscOrDance-Tests/DDScraperTest.class.st index aac647c..ac23449 100644 --- a/src/DiscOrDance-Tests/DDScraperTest.class.st +++ b/src/DiscOrDance-Tests/DDScraperTest.class.st @@ -156,3 +156,13 @@ DDScraperTest >> testGetServersList [ self assert: list isNotEmpty. self assert: (list includes: 'Wolfenmark bot development server') ] + +{ #category : #test } +DDScraperTest >> testGlobalConfiguration [ + | aConfiguration | + aConfiguration := DDGlobalConfiguration new limitMessages: true; messagesPerChannel: 1000. + scraper globalConfiguration: aConfiguration. + + self assert: scraper globalConfiguration limitMessages equals: true. + self assert: scraper globalConfiguration messagesPerChannel equals: 1000. +]