✅ Test app
This commit is contained in:
		
					parent
					
						
							
								e441e541c7
							
						
					
				
			
			
				commit
				
					
						6498f3d56b
					
				
			
		
					 4 changed files with 142 additions and 2 deletions
				
			
		
							
								
								
									
										16
									
								
								app.go
									
										
									
									
									
								
							
							
						
						
									
										16
									
								
								app.go
									
										
									
									
									
								
							|  | @ -26,6 +26,7 @@ func NewApp(ver string, conf config.Config, db proc.Store) (*App, error) { | ||||||
| 	} | 	} | ||||||
| 	app := App{ | 	app := App{ | ||||||
| 		version: ver, | 		version: ver, | ||||||
|  | 		conf:    conf, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	selfIRI := boxap.DefaultServiceIRI(conf.BaseURL) | 	selfIRI := boxap.DefaultServiceIRI(conf.BaseURL) | ||||||
|  | @ -44,6 +45,10 @@ func (l *App) Config() config.Config { | ||||||
| 	return l.conf | 	return l.conf | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func (l *App) Environment() config.Env { | ||||||
|  | 	return l.conf.Environment() | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func (l *App) Storage() proc.Store { | func (l *App) Storage() proc.Store { | ||||||
| 	return l.storage | 	return l.storage | ||||||
| } | } | ||||||
|  | @ -64,6 +69,13 @@ func (l *App) Version() string { | ||||||
| 	return l.version | 	return l.version | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (l *App) String() string { | func (l *App) Name() string { | ||||||
| 	return fmt.Sprintf("Lenore (%s)", l.version) | 	if l.conf.Name == "" { | ||||||
|  | 		return "Lenore" | ||||||
|  | 	} | ||||||
|  | 	return l.conf.Name | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (l *App) String() string { | ||||||
|  | 	return fmt.Sprintf("%s (%s)", l.Name(), l.version) | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										89
									
								
								app_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								app_test.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,89 @@ | ||||||
|  | package lenore | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
|  | 	"codeberg.org/danjones000/lenore/config" | ||||||
|  | 	"codeberg.org/danjones000/lenore/internal/testmocks" | ||||||
|  | 	vocab "github.com/go-ap/activitypub" | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func TestEmptyBaseURL(t *testing.T) { | ||||||
|  | 	c := config.Config{} | ||||||
|  | 	a, er := NewApp("0.0.0", c, testmocks.GetStore()) | ||||||
|  | 	assert.Nil(t, a) | ||||||
|  | 	assert.EqualError(t, er, "Missing BaseURL") | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestDefaultEnvironment(t *testing.T) { | ||||||
|  | 	c := config.Config{BaseURL: "http://localhost:1234/"} | ||||||
|  | 	a, er := NewApp("0.0.0", c, testmocks.GetStore()) | ||||||
|  | 	assert.NoError(t, er) | ||||||
|  | 	if assert.NotNil(t, a) { | ||||||
|  | 		assert.Equal(t, config.DEV, a.Environment()) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestGivenEnvironment(t *testing.T) { | ||||||
|  | 	cases := [...]struct { | ||||||
|  | 		given config.Env | ||||||
|  | 		exp   config.Env | ||||||
|  | 	}{ | ||||||
|  | 		{config.DEV, config.DEV}, | ||||||
|  | 		{config.PROD, config.PROD}, | ||||||
|  | 		{config.QA, config.QA}, | ||||||
|  | 		{config.Env("foo"), config.DEV}, | ||||||
|  | 		{config.Env("✨"), config.DEV}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	for _, c := range cases { | ||||||
|  | 		t.Run(string(c.given), func(t *testing.T) { | ||||||
|  | 			conf := config.Config{BaseURL: "http://localhost:1234/", Env: c.given} | ||||||
|  | 			a, er := NewApp("0.0.0", conf, testmocks.GetStore()) | ||||||
|  | 			assert.NoError(t, er) | ||||||
|  | 			if assert.NotNil(t, a) { | ||||||
|  | 				assert.Equal(t, conf, a.Config()) | ||||||
|  | 				assert.Equal(t, c.exp, a.Environment()) | ||||||
|  | 			} | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestService(t *testing.T) { | ||||||
|  | 	base := "http://localhost:1234/" | ||||||
|  | 	conf := config.Config{BaseURL: base} | ||||||
|  | 	a, er := NewApp("0.0.0.0", conf, testmocks.GetStore()) | ||||||
|  | 	assert.NoError(t, er) | ||||||
|  | 	if assert.NotNil(t, a) { | ||||||
|  | 		assert.Equal(t, vocab.IRI(base), a.ServiceIRI()) | ||||||
|  | 		assert.Equal(t, vocab.IRI(base), a.Service().ID) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestStrings(t *testing.T) { | ||||||
|  | 	cases := [...]struct { | ||||||
|  | 		given string | ||||||
|  | 		exp   string | ||||||
|  | 	}{ | ||||||
|  | 		{"", "Lenore"}, | ||||||
|  | 		{"Lenore", "Lenore"}, | ||||||
|  | 		{"Danny Ray", "Danny Ray"}, | ||||||
|  | 		{"✨👹", "✨👹"}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	for _, c := range cases { | ||||||
|  | 		t.Run(c.given, func(t *testing.T) { | ||||||
|  | 			conf := config.Config{BaseURL: "http://localhost:1234/", Name: c.given} | ||||||
|  | 			expStr := fmt.Sprintf("%s (%s)", c.exp, "0.0.0.0") | ||||||
|  | 			a, er := NewApp("0.0.0.0", conf, testmocks.GetStore()) | ||||||
|  | 			assert.NoError(t, er) | ||||||
|  | 			if assert.NotNil(t, a) { | ||||||
|  | 				assert.Equal(t, c.exp, a.Name()) | ||||||
|  | 				assert.Equal(t, "0.0.0.0", a.Version()) | ||||||
|  | 				assert.Equal(t, expStr, a.String()) | ||||||
|  | 			} | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | @ -1,6 +1,7 @@ | ||||||
| package config | package config | ||||||
| 
 | 
 | ||||||
| type Config struct { | type Config struct { | ||||||
|  | 	Name    string | ||||||
| 	Env     Env | 	Env     Env | ||||||
| 	BaseURL string | 	BaseURL string | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										38
									
								
								internal/testmocks/store.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								internal/testmocks/store.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,38 @@ | ||||||
|  | package testmocks | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	vocab "github.com/go-ap/activitypub" | ||||||
|  | 	"github.com/go-ap/filters" | ||||||
|  | 	proc "github.com/go-ap/processing" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type st struct{} | ||||||
|  | 
 | ||||||
|  | func (s *st) Load(iri vocab.IRI, filters ...filters.Check) (vocab.Item, error) { | ||||||
|  | 	i := vocab.ActorNew(iri, vocab.ActorType) | ||||||
|  | 	return i, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (s *st) Save(v vocab.Item) (vocab.Item, error) { | ||||||
|  | 	return v, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (s *st) Delete(v vocab.Item) error { | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (s *st) Create(col vocab.CollectionInterface) (vocab.CollectionInterface, error) { | ||||||
|  | 	return col, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (s *st) AddTo(col vocab.IRI, it vocab.Item) error { | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (s *st) RemoveFrom(col vocab.IRI, it vocab.Item) error { | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func GetStore() proc.Store { | ||||||
|  | 	return &st{} | ||||||
|  | } | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue